-
Notifications
You must be signed in to change notification settings - Fork 4.7k
CustomSelectControl: explore ariakit refactor
#55234
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
Closed
Closed
Changes from all commits
Commits
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 hidden or 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,37 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import * as Ariakit from '@ariakit/react'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { | ||
| CustomSelectButton, | ||
| CustomSelectPopover, | ||
| CustomSelectItem as SelectItem, | ||
| } from './styles'; | ||
| import { type CustomSelectProps } from './types'; | ||
|
|
||
| export function CustomSelect( props: CustomSelectProps ) { | ||
| const { label, onChange, options } = props; | ||
|
|
||
| const store = Ariakit.useSelectStore( { | ||
| setValue: ( value ) => onChange?.( value ), | ||
| } ); | ||
|
|
||
| return ( | ||
| <> | ||
| <Ariakit.SelectLabel store={ store }>{ label }</Ariakit.SelectLabel> | ||
| <CustomSelectButton store={ store } /> | ||
| <CustomSelectPopover sameWidth store={ store }> | ||
| { options?.map( ( { name, key, ...rest } ) => { | ||
| return ( | ||
| <SelectItem value={ name } key={ key } { ...rest } /> | ||
| ); | ||
| } ) } | ||
| </CustomSelectPopover> | ||
| </> | ||
| ); | ||
| } | ||
59 changes: 59 additions & 0 deletions
59
packages/components/src/custom-select/stories/index.story.tsx
This file contains hidden or 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,59 @@ | ||
| //@ts-nocheck | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import type { Meta, StoryFn } from '@storybook/react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { CustomSelect, CustomSelectItem } from '..'; | ||
|
|
||
| const meta: Meta< typeof CustomSelect > = { | ||
| title: 'Components/CustomSelect', | ||
| component: CustomSelect, | ||
| argTypes: {}, | ||
| parameters: { | ||
| controls: { expanded: true }, | ||
| docs: { canvas: { sourceState: 'shown' } }, | ||
| }, | ||
| }; | ||
| export default meta; | ||
|
|
||
| const Template: StoryFn = () => { | ||
| const options = [ | ||
| { | ||
| key: 'small', | ||
| name: 'Small', | ||
| style: { fontSize: '50%' }, | ||
| }, | ||
| { | ||
| key: 'normal', | ||
| name: 'Normal', | ||
| style: { fontSize: '100%' }, | ||
| }, | ||
| { | ||
| key: 'large', | ||
| name: 'Large', | ||
| style: { fontSize: '200%' }, | ||
| }, | ||
| { | ||
| key: 'huge', | ||
| name: 'Huge', | ||
| style: { fontSize: '300%' }, | ||
| }, | ||
| ]; | ||
|
|
||
| return ( | ||
| <> | ||
| <CustomSelect label="Font Size" options={ options } /> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export const Default = Template.bind( {} ); |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To note, this is just temporary CSS added from the Ariakit example. |
This file contains hidden or 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,51 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import styled from '@emotion/styled'; | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import * as Ariakit from '@ariakit/react'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { COLORS } from '../utils'; | ||
| import { space } from '../utils/space'; | ||
|
|
||
| export const CustomSelectButton = styled( Ariakit.Select )` | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: space-between; | ||
| gap: ${ space( 1 ) }; | ||
| white-space: nowrap; | ||
| font-size: 1rem; | ||
| border-style: none; | ||
| border-radius: ${ space( 1 ) }; | ||
| min-width: 250px; | ||
| height: auto; | ||
| margin-top: 1rem; | ||
| padding: ${ space( 4 ) }; | ||
| background: ${ COLORS.white }; | ||
| box-shadow: 0 0 0 var( --wp-admin-border-width-focus ) | ||
| ${ COLORS.gray[ 400 ] }; | ||
| &:hover { | ||
| background-color: ${ COLORS.gray[ 100 ] }; | ||
| } | ||
| `; | ||
|
|
||
| export const CustomSelectPopover = styled( Ariakit.SelectPopover )` | ||
| z-index: 50; | ||
| display: flex; | ||
| flex-direction: column; | ||
| border-radius: ${ space( 1 ) }; | ||
| background: ${ COLORS.white }; | ||
| box-shadow: 0 0 0 var( --wp-admin-border-width-focus ) | ||
| ${ COLORS.gray[ 400 ] }; | ||
| `; | ||
| export const CustomSelectItem = styled( Ariakit.SelectItem )` | ||
| cursor: pointer; | ||
| padding: ${ space( 2 ) }; | ||
| &:hover { | ||
| background-color: ${ COLORS.theme.accent }; | ||
| color: ${ COLORS.white }; | ||
| } | ||
| `; |
This file contains hidden or 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,17 @@ | ||
| export type CustomSelectProps = { | ||
| label?: string; | ||
| /** | ||
| * Function called with the control's internal state changes. The `selectedItem` property contains the next selected item. | ||
| */ | ||
| onChange?: Function; | ||
| /** | ||
| * The options that can be chosen from. | ||
| */ | ||
| options: Array< { | ||
| key: string; | ||
| name: string; | ||
| style?: {}; | ||
| className?: string; | ||
| __experimentalHint?: string; | ||
| } >; | ||
| }; |
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.
Our current
CustomSelectControlexpects an object to be returned via theonChangeevent:gutenberg/packages/components/src/custom-select-control/index.js
Lines 100 to 107 in 887cf3d
Which is managed by
useSelect; part of the packagedownshift.In Ariakit,
setValueappears to be what we'd use in lieu ofuseSelect, butvalue/setValueexpect a string (or an array of strings for multi-select).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 appears that the object passed to
onSelectItemChangehas the following structure:I'm not sure if all the elements in this structure are part of the public API. The
typevalue, in particular, seems obscure. I'm also unsure about the meaning ofhighlightedIndex, as it always appears to be-1.Regardless, everything except for
typecan be readily accessed from within thesetValuecallback. You can usestore.getState()in this context as well: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.
Thank you for this! We finally have gotten a chance to try this out. 🙂