From 51dd5505e68590b0f5400ae798e458c81e3a9e5a Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Thu, 10 Jun 2021 17:12:16 +0300 Subject: [PATCH] follow up --- packages/components/src/index.js | 2 +- .../src/segmented-control/component.tsx | 44 ++++++++------ .../segmented-control-backdrop.tsx | 8 +-- .../segmented-control-button.tsx | 59 +++++++------------ .../src/segmented-control/styles.ts | 4 +- .../components/src/segmented-control/types.ts | 55 ++++++++++++++++- packages/components/tsconfig.json | 3 +- 7 files changed, 106 insertions(+), 69 deletions(-) diff --git a/packages/components/src/index.js b/packages/components/src/index.js index cae43cceb2bceb..48d237aefaaa23 100644 --- a/packages/components/src/index.js +++ b/packages/components/src/index.js @@ -107,7 +107,7 @@ export { default as RangeControl } from './range-control'; export { default as ResizableBox } from './resizable-box'; export { default as ResponsiveWrapper } from './responsive-wrapper'; export { default as SandBox } from './sandbox'; -export { default as __experimentalSegmentedControl } from './segmented-control'; +export { SegmentedControl 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/component.tsx b/packages/components/src/segmented-control/component.tsx index d10cfd13a9cec6..a78e20043f93fb 100644 --- a/packages/components/src/segmented-control/component.tsx +++ b/packages/components/src/segmented-control/component.tsx @@ -4,17 +4,15 @@ import { cx } from 'emotion'; // eslint-disable-next-line no-restricted-imports import { RadioGroup, useRadioState } from 'reakit'; +import useResizeAware from 'react-resize-aware'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { useRef } from '@wordpress/element'; -import { useMergeRefs, useResizeObserver } from '@wordpress/compose'; +import { useRef, useMemo } from '@wordpress/element'; +import { useMergeRefs } from '@wordpress/compose'; -/** - * Internal dependencies - */ /** * Internal dependencies */ @@ -24,25 +22,30 @@ 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 noop = () => {}; + +/** + * @param {import('./types').SegmentedControlProps} props + * @param {import('react').Ref} forwardedRef + */ +function SegmentControl( props, forwardedRef ) { const { className, baseId, isAdaptiveWidth = false, isBlock = false, id, - label = __( 'SegmentControl' ), + label, options = [], - onChange = () => {}, + onChange = noop, size = 'medium', value, ...otherProps } = useContextSystem( props, 'SegmentedControl' ); const containerRef = useRef(); - const [ resizeListener, sizes ] = useResizeObserver(); + const [ resizeListener, sizes ] = useResizeAware(); const radio = useRadioState( { baseId: baseId || id, @@ -62,13 +65,16 @@ function SegmentControl( props: any, forwardedRef: any ) { } }, [ value ] ); - const classes = cx( - styles.SegmentedControl, - isBlock && styles.block, - styles[ size ], - className + const classes = useMemo( + () => + cx( + styles.SegmentedControl, + isBlock && styles.block, + styles[ size ], + className + ), + [ className, size ] ); - const mergedRefs = useMergeRefs( [ containerRef, forwardedRef ] ); return ( { resizeListener } - { options.map( ( option, index: number ) => { + { options.map( ( option: any, index: number ) => { const showSeparator = getShowSeparator( radio, index ); return (