-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: polish
ToggleGroupControl
(#35600)
- Loading branch information
Showing
16 changed files
with
272 additions
and
213 deletions.
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.