-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Components]: Add SegmentedControl #31937
Merged
Merged
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
dcacf9f
temp commit
ntsekouras 5fceebe
follow up
ntsekouras cac3d00
add/fix types
ntsekouras 26d056a
remove duplicate ref in tsconfig
ntsekouras 5669930
Add the test snapshots
ntsekouras 6db5fa6
fix story
ntsekouras e227c1f
tweak styles - need fixes
ntsekouras 22622d9
demo usage in PostFeaturedImage for styling fixes
ntsekouras bd04f01
remove `size` prop
ntsekouras 76f04aa
Polish.
jasmussen 1ed460a
tweak some types and README
ntsekouras 375e2cd
update tests
ntsekouras 865f405
Fix sliding.
jasmussen f29de11
refactor to not use `important`
ntsekouras bfd4125
rebase and small changes from feedback
ntsekouras d99905d
update snapshots as `CONTROL_HEIGHT` has changed to 36px
ntsekouras 7d6a327
remove control for demo purposes
ntsekouras 2abdf75
Update packages/components/src/segmented-control/README.md
ntsekouras c6e80fb
Fix storybook active icon color
ntsekouras bcfd9cd
add + update tests
ntsekouras 050a08a
try new approach
ntsekouras 80a4063
update tests
ntsekouras 904153e
update storybook and readme
ntsekouras 660e9b5
remove demo code
ntsekouras 5f68a23
add focus style to control
ntsekouras db60768
address feedback part 1
ntsekouras c635735
address feedback part 2
ntsekouras 5a6e6e3
export SegmentedControlOption
ntsekouras 1c157f6
add id to button props
ntsekouras 6046e1a
create string id
ntsekouras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# SegementedControl | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
|
||
`SegementedControl` is a form component that lets users choose options represented in horizontal segments. To render options for this control use `SegmentedControl.Option` component. | ||
|
||
## Usage | ||
|
||
```js | ||
import { __experimentalSegmentedControl as SegmentedControl } from '@wordpress/components'; | ||
|
||
function Example() { | ||
return ( | ||
<SegmentedControl | ||
label="my label" | ||
value="vertical" | ||
isBlock | ||
> | ||
<SegmentedControl.Option value="horizontal" label="horizontal" /> | ||
<SegmentedControl.Option value="vertical" label="vertical" /> | ||
</SegmentedControl> | ||
); | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
### `label` | ||
|
||
- Type: `string` | ||
|
||
Label for the form element. | ||
|
||
### `baseId` | ||
|
||
- Type: `string` | ||
- Required: No | ||
|
||
ID that will serve as a base for all the items IDs. | ||
|
||
### `isAdaptiveWidth` | ||
|
||
- Type: `boolean` | ||
- Required: No | ||
- Default: `false` | ||
|
||
Determines if segments should be rendered with equal widths. | ||
|
||
### `isBlock` | ||
|
||
- Type: `boolean` | ||
- Required: No | ||
- Default: `false` | ||
|
||
Renders `SegmentedControl` as a (CSS) block element. | ||
|
||
### `onChange` | ||
|
||
- Type: `boolean` | ||
- Required: No | ||
- Default: `false` | ||
|
||
Callback when a segment is selected. | ||
|
||
### `value` | ||
|
||
- Type: `string | number` | ||
|
||
The value of the `SegmentedControl`. |
110 changes: 110 additions & 0 deletions
110
packages/components/src/segmented-control/component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// 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, useMemo } from '@wordpress/element'; | ||
import { useMergeRefs } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
contextConnect, | ||
useContextSystem, | ||
PolymorphicComponentProps, | ||
} from '../ui/context'; | ||
import { View } from '../view'; | ||
import * as styles from './styles'; | ||
import { useUpdateEffect, useCx } from '../utils/hooks'; | ||
import Backdrop from './segmented-control-backdrop'; | ||
import Option from './segmented-control-option'; | ||
import type { SegmentedControlProps } from './types'; | ||
import SegmentedControlContext from './segmented-control-context'; | ||
|
||
const noop = () => {}; | ||
|
||
function SegmentedControl( | ||
props: PolymorphicComponentProps< SegmentedControlProps, 'input' >, | ||
forwardedRef: import('react').Ref< any > | ||
) { | ||
const { | ||
className, | ||
baseId, | ||
isAdaptiveWidth = false, | ||
isBlock = false, | ||
id, | ||
label, | ||
onChange = noop, | ||
value, | ||
children, | ||
...otherProps | ||
} = useContextSystem( props, 'SegmentedControl' ); | ||
const cx = useCx(); | ||
const containerRef = useRef(); | ||
const [ resizeListener, sizes ] = useResizeAware(); | ||
|
||
const radio = useRadioState( { | ||
baseId: baseId || id, | ||
state: 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 = useMemo( | ||
() => | ||
cx( | ||
styles.SegmentedControl, | ||
isBlock && styles.block, | ||
'medium', | ||
className | ||
), | ||
[ className ] | ||
); | ||
return ( | ||
<SegmentedControlContext.Provider | ||
value={ { ...radio, isBlock: ! isAdaptiveWidth } } | ||
> | ||
<RadioGroup | ||
{ ...radio } | ||
aria-label={ label } | ||
as={ View } | ||
className={ classes } | ||
{ ...otherProps } | ||
ref={ useMergeRefs( [ containerRef, forwardedRef ] ) } | ||
> | ||
{ resizeListener } | ||
<Backdrop | ||
{ ...radio } | ||
containerRef={ containerRef } | ||
containerWidth={ sizes.width } | ||
/> | ||
{ children } | ||
</RadioGroup> | ||
</SegmentedControlContext.Provider> | ||
); | ||
} | ||
|
||
const ConnectedSegmentedControl: any = contextConnect( | ||
SegmentedControl, | ||
'SegmentedControl' | ||
); | ||
ConnectedSegmentedControl.Option = Option; | ||
|
||
export default ConnectedSegmentedControl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as SegmentedControl } from './component'; |
60 changes: 60 additions & 0 deletions
60
packages/components/src/segmented-control/segmented-control-backdrop.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect, memo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { SegmentedControlBackdropProps } from './types'; | ||
import { BackdropView } from './styles'; | ||
|
||
function SegmentedControlBackdrop( { | ||
containerRef, | ||
containerWidth, | ||
state, | ||
}: SegmentedControlBackdropProps ) { | ||
const [ left, setLeft ] = useState( 0 ); | ||
const [ width, setWidth ] = useState( 0 ); | ||
const [ canAnimate, setCanAnimate ] = useState( false ); | ||
|
||
useEffect( () => { | ||
const containerNode = containerRef?.current; | ||
if ( ! containerNode ) return; | ||
|
||
/** | ||
* Workaround for Reakit | ||
*/ | ||
const targetNode = containerNode.querySelector( | ||
`[data-value="${ state }"]` | ||
); | ||
if ( ! targetNode ) return; | ||
|
||
const { x: parentX } = containerNode.getBoundingClientRect(); | ||
const { width: offsetWidth, x } = targetNode.getBoundingClientRect(); | ||
const borderWidth = 1; | ||
const offsetLeft = x - parentX - borderWidth; | ||
|
||
setLeft( offsetLeft ); | ||
setWidth( offsetWidth ); | ||
|
||
if ( ! canAnimate ) { | ||
window.requestAnimationFrame( () => { | ||
setCanAnimate( true ); | ||
} ); | ||
} | ||
}, [ canAnimate, containerRef, containerWidth, state ] ); | ||
|
||
return ( | ||
<BackdropView | ||
role="presentation" | ||
style={ { | ||
transform: `translateX(${ left }px)`, | ||
transition: canAnimate ? undefined : 'none', | ||
width, | ||
} } | ||
/> | ||
); | ||
} | ||
|
||
export default memo( SegmentedControlBackdrop ); |
72 changes: 72 additions & 0 deletions
72
packages/components/src/segmented-control/segmented-control-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { Radio } from 'reakit'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { memo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import * as styles from './styles'; | ||
import type { SegmentedControlButtonProps } from './types'; | ||
import { useCx } from '../utils/hooks'; | ||
|
||
const { | ||
ButtonContentView, | ||
LabelPlaceholderView, | ||
LabelView, | ||
SeparatorView, | ||
} = styles; | ||
|
||
function SegmentedControlButton( { | ||
className, | ||
forwardedRef, | ||
isBlock = false, | ||
label, | ||
showSeparator, | ||
value, | ||
...props | ||
}: SegmentedControlButtonProps ) { | ||
const isActive = props.state === value; | ||
const cx = useCx(); | ||
const labelViewClasses = cx( isBlock && styles.labelBlock ); | ||
const classes = cx( | ||
styles.buttonView, | ||
className, | ||
isActive && styles.buttonActive | ||
); | ||
return ( | ||
<LabelView className={ labelViewClasses } data-active={ isActive }> | ||
<Radio | ||
{ ...props } | ||
as="button" | ||
aria-label={ label } | ||
className={ classes } | ||
data-value={ value } | ||
ref={ forwardedRef } | ||
value={ value } | ||
> | ||
<ButtonContentView>{ label }</ButtonContentView> | ||
<LabelPlaceholderView aria-hidden> | ||
{ label } | ||
</LabelPlaceholderView> | ||
</Radio> | ||
<SegmentedControlSeparator isActive={ ! showSeparator } /> | ||
</LabelView> | ||
); | ||
} | ||
|
||
const SegmentedControlSeparator = memo( | ||
( { isActive }: { isActive: boolean } ) => { | ||
const cx = useCx(); | ||
const classes = cx( isActive && styles.separatorActive ); | ||
return <SeparatorView aria-hidden className={ classes } />; | ||
} | ||
); | ||
|
||
export default memo( SegmentedControlButton ); |
16 changes: 16 additions & 0 deletions
16
packages/components/src/segmented-control/segmented-control-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createContext, useContext } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { SegmentedControlContextProps } from './types'; | ||
|
||
const SegmentedControlContext = createContext( | ||
{} as SegmentedControlContextProps | ||
); | ||
export const useSegmentedControlContext = () => | ||
useContext( SegmentedControlContext ); | ||
export default SegmentedControlContext; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add some details here for future reference? @diegohaz do you know any additional information about this workaround?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear to me what this workaround is solving either. 🤔