Allows the user to pick a color from a list of pre-defined color entries.
import { ColorPalette } from '@wordpress/components';
LinksPermalink to this section
PropsPermalink to this section
| Name | Default | Description |
|---|---|---|
onChange Required |
Callback called when a color is selected. The third argument is the slug of the selected palette entry, when available. | |
selectedSlug |
The slug of the currently selected palette entry. When set to a non-empty string, selection is determined by slug rather
than by color value — this correctly handles palettes where two entries
share the same color. Palette entries without a slug will not appear
selected in this mode, even if their color value matches An empty string is treated the same as | |
clearable | true |
Whether the palette should have a clearing button. |
colors | [] |
Array with the colors to be shown. When displaying multiple color palettes to choose from, the format of the array changes from an array of colors objects, to an array of color palettes. |
disableCustomColors | false |
Whether to allow the user to pick a custom color on top of the predefined
choices (defined via the |
enableAlpha | false |
This controls whether the alpha channel will be offered when selecting custom colors. |
headingLevel | 2 |
The heading level. |
value |
Currently active value. | |
presentation | 'listbox' |
How predefined color swatches behave and are exposed to assistive technology.
|
loop | true |
Prevents keyboard interaction from wrapping around.
Only used with the |
__experimentalIsRenderedInSidebar | false |
Whether this is rendered in the sidebar. |
aria-label |
A label to identify the purpose of the control. | |
aria-labelledby |
An ID of an element to provide a label for the control. | |
as |
The HTML element or React component to render the component as. |
ExamplesPermalink to this section
DefaultPermalink to this section
const Default = () => {
const [ color, setColor ] = useState< string | undefined >( value );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<ColorPalette
colors={[
{ name: 'Red', color: '#f00' },
{ name: 'White', color: '#fff' },
{ name: 'Blue', color: '#00f' },
]}
value={ color }
selectedSlug={ slug }
onChange={ ( newColor, index, newSlug ) => {
setColor( newColor );
setSlug( newSlug );
onChange?.( newColor, index, newSlug );
} } />
);
};
Initial ValuePermalink to this section
const InitialValue = () => {
const [ color, setColor ] = useState< string | undefined >( value );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<ColorPalette
colors={[
{ name: 'Red', color: '#f00' },
{ name: 'White', color: '#fff' },
{ name: 'Blue', color: '#00f' },
]}
value={ color }
selectedSlug={ slug }
onChange={ ( newColor, index, newSlug ) => {
setColor( newColor );
setSlug( newSlug );
onChange?.( newColor, index, newSlug );
} } />
);
};
Multiple OriginsPermalink to this section
const MultipleOrigins = () => {
const [ color, setColor ] = useState< string | undefined >( value );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<ColorPalette
colors={[
{
name: 'Primary colors',
colors: [
{ name: 'Red', color: '#f00' },
{ name: 'Yellow', color: '#ff0' },
{ name: 'Blue', color: '#00f' },
],
},
{
name: 'Secondary colors',
colors: [
{ name: 'Orange', color: '#f60' },
{ name: 'Green', color: '#0f0' },
{ name: 'Purple', color: '#60f' },
],
},
]}
value={ color }
selectedSlug={ slug }
onChange={ ( newColor, index, newSlug ) => {
setColor( newColor );
setSlug( newSlug );
onChange?.( newColor, index, newSlug );
} } />
);
};
Duplicate ColorsPermalink to this section
const DuplicateColors = () => {
const [ color, setColor ] = useState< string | undefined >( value );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<ColorPalette
colors={[
{ name: 'Dark Background', slug: 'dark-background', color: '#000' },
{ name: 'Dark Text', slug: 'dark-text', color: '#000' },
{ name: 'Brand', slug: 'brand', color: '#0073aa' },
]}
value={ color }
selectedSlug={ slug }
onChange={ ( newColor, index, newSlug ) => {
setColor( newColor );
setSlug( newSlug );
onChange?.( newColor, index, newSlug );
} } />
);
};
CSS VariablesPermalink to this section
const CSSVariables = () => <div
style={ {
'--red': '#f00',
'--yellow': '#ff0',
'--blue': '#00f',
} }>
<Template
colors={[
{ name: 'Red', color: 'var(--red)' },
{ name: 'Yellow', color: 'var(--yellow)' },
{ name: 'Blue', color: 'var(--blue)' },
]} />
</div>;