From dcacf9f18cd6d146f5828d8bc3c7f8016b74f340 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Tue, 18 May 2021 17:31:23 +0300 Subject: [PATCH 01/30] temp commit --- docs/manifest.json | 6 + packages/components/src/index.js | 1 + .../src/segmented-control/README.md | 3 + .../src/segmented-control/component.tsx | 122 +++++ .../components/src/segmented-control/index.ts | 1 + .../segmented-control-backdrop.tsx | 57 +++ .../segmented-control-button.tsx | 84 +++ .../src/segmented-control/stories/index.js | 93 ++++ .../src/segmented-control/styles.ts | 137 +++++ .../src/segmented-control/test/index.js | 30 ++ .../test/snapshots/index.js.snap | 480 ++++++++++++++++++ .../components/src/segmented-control/types.ts | 66 +++ .../components/src/utils/colors-values.js | 1 + .../components/src/utils/config-values.js | 43 +- packages/components/tsconfig.json | 1 + 15 files changed, 1111 insertions(+), 14 deletions(-) create mode 100644 packages/components/src/segmented-control/README.md create mode 100644 packages/components/src/segmented-control/component.tsx create mode 100644 packages/components/src/segmented-control/index.ts create mode 100644 packages/components/src/segmented-control/segmented-control-backdrop.tsx create mode 100644 packages/components/src/segmented-control/segmented-control-button.tsx create mode 100644 packages/components/src/segmented-control/stories/index.js create mode 100644 packages/components/src/segmented-control/styles.ts create mode 100644 packages/components/src/segmented-control/test/index.js create mode 100644 packages/components/src/segmented-control/test/snapshots/index.js.snap create mode 100644 packages/components/src/segmented-control/types.ts diff --git a/docs/manifest.json b/docs/manifest.json index 42f2c0ae80a8ed..8459d00cb5c710 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -1151,6 +1151,12 @@ "markdown_source": "../packages/components/src/search-control/README.md", "parent": "components" }, + { + "title": "SegmentedControl", + "slug": "segmented-control", + "markdown_source": "../packages/components/src/segmented-control/README.md", + "parent": "components" + }, { "title": "SelectControl", "slug": "select-control", diff --git a/packages/components/src/index.js b/packages/components/src/index.js index a97f0743c58d23..bca08d810b13bf 100644 --- a/packages/components/src/index.js +++ b/packages/components/src/index.js @@ -109,6 +109,7 @@ export { default as ResizableBox } from './resizable-box'; export { default as ResponsiveWrapper } from './responsive-wrapper'; export { default as SandBox } from './sandbox'; export { default as SearchControl } from './search-control'; +export { default as __experimentalSegmentedControl } from './segmented-control'; export { default as SelectControl } from './select-control'; export { default as Snackbar } from './snackbar'; export { default as SnackbarList } from './snackbar/list'; diff --git a/packages/components/src/segmented-control/README.md b/packages/components/src/segmented-control/README.md new file mode 100644 index 00000000000000..c1b34c838ff320 --- /dev/null +++ b/packages/components/src/segmented-control/README.md @@ -0,0 +1,3 @@ +# SegementedControl + +> **Experimental!** diff --git a/packages/components/src/segmented-control/component.tsx b/packages/components/src/segmented-control/component.tsx new file mode 100644 index 00000000000000..d10cfd13a9cec6 --- /dev/null +++ b/packages/components/src/segmented-control/component.tsx @@ -0,0 +1,122 @@ +/** + * External dependencies + */ +import { cx } from 'emotion'; +// eslint-disable-next-line no-restricted-imports +import { RadioGroup, useRadioState } from 'reakit'; + +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { useRef } from '@wordpress/element'; +import { useMergeRefs, useResizeObserver } from '@wordpress/compose'; + +/** + * Internal dependencies + */ +/** + * Internal dependencies + */ +import { contextConnect, useContextSystem } from '../ui/context'; +import { View } from '../view'; +import * as styles from './styles'; +import { useUpdateEffect } from '../utils/hooks'; +import Backdrop from './segmented-control-backdrop'; +import Button from './segmented-control-button'; +import type { SegmentedControlProps } from './types'; + +function SegmentControl( props: any, forwardedRef: any ) { + const { + className, + baseId, + isAdaptiveWidth = false, + isBlock = false, + id, + label = __( 'SegmentControl' ), + options = [], + onChange = () => {}, + size = 'medium', + value, + ...otherProps + } = useContextSystem( props, 'SegmentedControl' ); + + const containerRef = useRef(); + const [ resizeListener, sizes ] = useResizeObserver(); + + const radio = useRadioState( { + baseId: baseId || id, + unstable_virtual: true, + state: value || options[ 0 ]?.value, + } ); + + // Propagate radio.state change + useUpdateEffect( () => { + onChange( radio.state ); + }, [ radio.state ] ); + + // Sync incoming value with radio.state + useUpdateEffect( () => { + if ( value !== radio.state ) { + radio.setState( value ); + } + }, [ value ] ); + + const classes = cx( + styles.SegmentedControl, + isBlock && styles.block, + styles[ size ], + className + ); + const mergedRefs = useMergeRefs( [ containerRef, forwardedRef ] ); + return ( + + { resizeListener } + + { options.map( ( option, index: number ) => { + const showSeparator = getShowSeparator( radio, index ); + return ( +