From b7150de7d530d9e70f901b0d3114e183b7624270 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Sun, 15 Dec 2019 17:05:46 -0500 Subject: [PATCH] add tabs component --- assets/js/base/components/tabs/index.js | 108 ++++++++++++++++++++++ assets/js/base/components/tabs/style.scss | 35 +++++++ 2 files changed, 143 insertions(+) create mode 100644 assets/js/base/components/tabs/index.js create mode 100644 assets/js/base/components/tabs/style.scss diff --git a/assets/js/base/components/tabs/index.js b/assets/js/base/components/tabs/index.js new file mode 100644 index 00000000000..330e872aff9 --- /dev/null +++ b/assets/js/base/components/tabs/index.js @@ -0,0 +1,108 @@ +/** + * External dependencies + */ +import { useState, useEffect } from '@wordpress/element'; +import { find, partial } from 'lodash'; +import { withInstanceId } from '@wordpress/compose'; +import classnames from 'classnames'; +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import './style.scss'; + +const TabButton = ( { + tabId, + onClick, + children, + selected, + ariaLabel, + ...rest +} ) => { + return ( + + ); +}; + +const Tabs = ( { + className, + onSelect = () => null, + tabs, + activeClass = 'is-active', + initialTabName, + instanceId, + ariaLabel = __( 'Tabbed Content', 'woo-gutenberg-products-block' ), + children, +} ) => { + const [ selected, setSelected ] = useState( '' ); + useEffect( () => { + if ( ! selected ) { + setSelected( + initialTabName || ( tabs.length > 0 ? tabs[ 0 ].name : null ) + ); + } + }, [ initialTabName, tabs ] ); + if ( ! selected ) { + return null; + } + const handleClick = ( tabKey ) => { + setSelected( tabKey ); + onSelect( tabKey ); + }; + const selectedTab = find( tabs, { name: selected } ); + const selectedId = `${ instanceId }-${ selectedTab.name }`; + return ( +
+
+ { tabs.map( ( tab ) => ( + + { tab.title() } + + ) ) } +
+ { selectedTab && ( +
+ { children( selected ) } +
+ ) } +
+ ); +}; + +export default withInstanceId( Tabs ); diff --git a/assets/js/base/components/tabs/style.scss b/assets/js/base/components/tabs/style.scss new file mode 100644 index 00000000000..42136247d59 --- /dev/null +++ b/assets/js/base/components/tabs/style.scss @@ -0,0 +1,35 @@ +.wc-component__tabs { + .wc-component__tab-list { + display: flex; + flex-direction: row; + flex-wrap: wrap; + width: 100%; + > .wc-component__tab-item { + border: none; + flex: auto; + background: transparent; + padding: $gap-small $gap; + color: $black; + outline-offset: -1px; + transition: box-shadow 0.1s linear; + &.is-active { + box-shadow: inset 0 -3px $black; + font-weight: 600; + position: relative; + } + &:focus { + color: $black; + outline-offset: -1px; + outline: 1px dotted $gray-60; + } + .wc-component__tab-item-content { + width: fit-content; + display: block; + margin: auto; + } + } + } + .wc-component__tab-content { + padding: $gap; + } +}