CircularOptionPicker 是一个 WordPress 组件,用于以圆形按钮形式展示选项集,主要用于颜色选择等场景。该组件未导出,仅限在 @wordpress/components 包内部使用。
import { useState } from 'react';
import { CircularOptionPicker } from '../circular-option-picker';
const Example = () => {
const [ currentColor, setCurrentColor ] = useState();
const colors = [
{ color: '#f00', name: 'Red' },
{ color: '#0f0', name: 'Green' },
{ color: '#00f', name: 'Blue' },
];
const colorOptions = (
<>
{ colors.map( ( { color, name }, index ) => {
return (
<CircularOptionPicker.Option
key={ `${ color }-${ index }` }
tooltipText={ name }
style={ { backgroundColor: color, color } }
isSelected={ index === currentColor }
onClick={ () => setCurrentColor( index ) }
/>
);
} ) }
</>
);
return (
<CircularOptionPicker
options={ colorOptions }
actions={
<CircularOptionPicker.ButtonAction
onClick={ () => setCurrentColor( undefined ) }
>
{ 'Clear' }
</CircularOptionPicker.ButtonAction>
}
/>
);
};CircularOptionPicker is a component that displays a set of options as circular buttons.
import { useState } from 'react';
import { CircularOptionPicker } from '../circular-option-picker';
const Example = () => {
const [ currentColor, setCurrentColor ] = useState();
const colors = [
{ color: '#f00', name: 'Red' },
{ color: '#0f0', name: 'Green' },
{ color: '#00f', name: 'Blue' },
];
const colorOptions = (
<>
{ colors.map( ( { color, name }, index ) => {
return (
<CircularOptionPicker.Option
key={ `${ color }-${ index }` }
tooltipText={ name }
style={ { backgroundColor: color, color } }
isSelected={ index === currentColor }
onClick={ () => setCurrentColor( index ) }
/>
);
} ) }
</>
);
return (
<CircularOptionPicker
options={ colorOptions }
actions={
<CircularOptionPicker.ButtonAction
onClick={ () => setCurrentColor( undefined ) }
>
{ 'Clear' }
</CircularOptionPicker.ButtonAction>
}
/>
);
};
className: stringA CSS class to apply to the wrapper element.
actions: ReactNodeThe action(s) to be rendered after the options, such as a ‘clear’ button as seen in ColorPalette.
Usually a CircularOptionPicker.ButtonAction or CircularOptionPicker.DropdownLinkAction component.
options: ReactNodeThe options to be rendered, such as color swatches.
Usually a CircularOptionPicker.Option component.
children: ReactNodeThe child elements.
asButtons: booleanWhether the control should present as a set of buttons, each with its own tab stop.
falseloop: booleanPrevents keyboard interaction from wrapping around. Only used when asButtons is not true.
truearia-labelledby: stringThe ID reference list of one or more elements that label the wrapper element.
aria-label: stringThe label for the wrapper element. Not used if an ‘aria-labelledby’ is provided.
Custom color pickerCircularOptionPicker.ButtonActionA ButtonAction is an action that is rendered as a button alongside the options themselves.
A common use case is a ‘clear’ button to deselect the currently selected option.
className: stringA CSS class to apply to the underlying Button component.
children: ReactNodeThe button’s children.
CircularOptionPicker.ButtonAction also inherits all of the Button props, except for href and target.
CircularOptionPicker.DropdownLinkActionCircularOptionPicker.DropdownLinkAction is an action that’s hidden behind a dropdown toggle. The button is formatted as a link and rendered as an anchor element.
className: stringA CSS class to apply to the underlying Dropdown component.
linkText: stringThe text to be displayed on the button.
dropdownProps: objectThe props for the underlying Dropdown component.
Inherits all of the Dropdown props, except for className and renderToggle.
buttonProps: objectProps for the underlying Button component.
Inherits all of the Button props, except for href, target, and children.