-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: polish ToggleGroupControl
#35600
Merged
ciampo
merged 7 commits into
trunk
from
refactor/components-toggle-group-control-option-button
Oct 14, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
582a672
Refactor `ToggleGroupControl` folder structure
ciampo fbd132c
Update docs manifest
ciampo 5f2bb68
Embed `ToggleGroupControlButton` into `ToggleGroupControlOption`
ciampo f86cf35
Add JSDocs (including example) to exported components
ciampo c2e6d25
Remove `ToggleGroupControlButtonProps` type
ciampo 746a108
Do not import types inline
ciampo 648c4b4
CHANGELOG
ciampo 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
File renamed without changes.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { default as ToggleGroupControl } from './component'; | ||
export { default as ToggleGroupControlOption } from './toggle-group-control-option'; | ||
export { ToggleGroupControl } from './toggle-group-control'; | ||
export { ToggleGroupControlOption } from './toggle-group-control-option'; |
57 changes: 0 additions & 57 deletions
57
packages/components/src/toggle-group-control/toggle-group-control-button.tsx
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
packages/components/src/toggle-group-control/toggle-group-control-option.tsx
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
packages/components/src/toggle-group-control/toggle-group-control-option/README.md
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,40 @@ | ||
# `ToggleGroupControlOption` | ||
|
||
<div class="callout callout-alert"> | ||
This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. | ||
</div> | ||
|
||
`ToggleGroupControlOption` is a form component and is meant to be used as a child of [`ToggleGroupControl`]((/packages/components/src/toggle-group-control/toggle-group-control/README.md)). | ||
|
||
|
||
## Usage | ||
|
||
```js | ||
import { | ||
__experimentalToggleGroupControl as ToggleGroupControl, | ||
__experimentalToggleGroupControlOption as ToggleGroupControlOption, | ||
} from '@wordpress/components'; | ||
|
||
function Example() { | ||
return ( | ||
<ToggleGroupControl label="my label" value="vertical" isBlock> | ||
<ToggleGroupControlOption value="horizontal" label="Horizontal" /> | ||
<ToggleGroupControlOption value="vertical" label="Vertical" /> | ||
</ToggleGroupControl> | ||
); | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
### `label`: `string` | ||
|
||
Label for the option. If needed, the `aria-label` prop can be used in addition to specify a different label for assistive technologies. | ||
|
||
- Required: Yes | ||
|
||
### `value`: `string | number` | ||
|
||
The value of the `ToggleGroupControlOption`. | ||
|
||
- Required: Yes |
104 changes: 104 additions & 0 deletions
104
packages/components/src/toggle-group-control/toggle-group-control-option/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,104 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
// eslint-disable-next-line no-restricted-imports | ||
import type { Ref } from 'react'; | ||
// eslint-disable-next-line no-restricted-imports | ||
import { Radio } from 'reakit'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
contextConnect, | ||
useContextSystem, | ||
WordPressComponentProps, | ||
} from '../../ui/context'; | ||
import type { ToggleGroupControlOptionProps } from '../types'; | ||
import { useToggleGroupControlContext } from '../context'; | ||
import * as styles from './styles'; | ||
import { useCx } from '../../utils/hooks'; | ||
|
||
const { ButtonContentView, LabelPlaceholderView, LabelView } = styles; | ||
|
||
function ToggleGroupControlOption( | ||
props: WordPressComponentProps< ToggleGroupControlOptionProps, 'button' >, | ||
forwardedRef: Ref< any > | ||
) { | ||
const toggleGroupControlContext = useToggleGroupControlContext(); | ||
const id = useInstanceId( | ||
ToggleGroupControlOption, | ||
toggleGroupControlContext.baseId || 'toggle-group-control-option' | ||
) as string; | ||
const buttonProps = useContextSystem( | ||
{ ...props, id }, | ||
'ToggleGroupControlOption' | ||
); | ||
|
||
const { className, isBlock = false, label, value, ...radioProps } = { | ||
...toggleGroupControlContext, | ||
...buttonProps, | ||
}; | ||
|
||
const isActive = radioProps.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 | ||
{ ...radioProps } | ||
as="button" | ||
aria-label={ radioProps[ 'aria-label' ] ?? label } | ||
className={ classes } | ||
data-value={ value } | ||
ref={ forwardedRef } | ||
value={ value } | ||
> | ||
<ButtonContentView>{ label }</ButtonContentView> | ||
<LabelPlaceholderView aria-hidden> | ||
{ label } | ||
</LabelPlaceholderView> | ||
</Radio> | ||
</LabelView> | ||
); | ||
} | ||
|
||
/** | ||
* `ToggleGroupControlOption` is a form component and is meant to be used as a | ||
* child of `ToggleGroupControl`. | ||
* | ||
* @example | ||
* ```jsx | ||
* import { | ||
* __experimentalToggleGroupControl as ToggleGroupControl, | ||
* __experimentalToggleGroupControlOption as ToggleGroupControlOption, | ||
* } from '@wordpress/components'; | ||
* | ||
* function Example() { | ||
* return ( | ||
* <ToggleGroupControl label="my label" value="vertical" isBlock> | ||
* <ToggleGroupControlOption value="horizontal" label="Horizontal" /> | ||
* <ToggleGroupControlOption value="vertical" label="Vertical" /> | ||
* </ToggleGroupControl> | ||
* ); | ||
* } | ||
* ``` | ||
*/ | ||
const ConnectedToggleGroupControlOption = contextConnect( | ||
ToggleGroupControlOption, | ||
'ToggleGroupControlOption' | ||
); | ||
|
||
export default ConnectedToggleGroupControlOption; |
1 change: 1 addition & 0 deletions
1
packages/components/src/toggle-group-control/toggle-group-control-option/index.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 @@ | ||
export { default as ToggleGroupControlOption } from './component'; |
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
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.
I had to update this type from
to
because otherwise TypeScript would throw an error on the
Radio
component, since it has theas="button"
prop.