GradientPicker is a React component that renders a color gradient picker to define a multi step gradient. There’s either a linear or a radial type available.
import { GradientPicker } from '@wordpress/components';
LinksPermalink to this section
PropsPermalink to this section
| Name | Default | Description |
|---|---|---|
className |
The class name added to the wrapper. | |
onChange Required |
The function called when a new gradient has been defined. It is passed
the | |
value | 'linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)' |
The current value of the gradient. Pass a css gradient string (See default value for example).
Optionally pass in a |
selectedSlug |
The slug of the currently selected predefined gradient. When set to a non-empty string, selection is determined by slug rather
than by gradient value, which correctly handles palettes where two
entries share the same gradient. Entries whose slug does not match will
not appear selected in this mode, even if their gradient value matches
An empty string is treated the same as | |
clearable | true |
Whether the palette should have a clearing button or not. |
headingLevel | 2 |
The heading level. Only applies in cases where gradients are provided
from multiple origins (i.e. when the array passed as the |
presentation | 'listbox' |
How predefined gradient swatches behave and are exposed to assistive technology.
|
loop | true |
Prevents keyboard interaction from wrapping around.
Only used with the |
enableAlpha | true |
Whether to enable alpha transparency options in the picker. |
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. | |
gradients | [] |
An array of objects as predefined gradients displayed above the gradient
selector. Alternatively, if there are multiple sets (or ‘origins’) of
gradients, you can pass an array of objects each with a |
__nextHasNoMargin | false |
Start opting in to the new margin-free styles that will become the default in a future version, currently scheduled to be WordPress 6.4. (The prop can be safely removed once this happens.) |
disableCustomGradients | false |
If true, the gradient picker will not be displayed and only defined
gradients from |
__experimentalIsRenderedInSidebar | false |
Whether this is rendered in the sidebar. |
ExamplesPermalink to this section
DefaultPermalink to this section
const Default = ( {
onChange,
value,
selectedSlug,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] = useState<
React.ComponentProps< typeof GradientPicker >[ 'value' ]
>( value ?? null );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<GradientPicker
{ ...props }
value={ gradient }
selectedSlug={ slug }
onChange={ ( currentGradient, index, newSlug ) => {
setGradient( currentGradient );
setSlug( newSlug );
onChange?.( currentGradient, index, newSlug );
} }
/>
);
};
With No Existing GradientsPermalink to this section
const WithNoExistingGradients = ( {
onChange,
value,
selectedSlug,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] = useState<
React.ComponentProps< typeof GradientPicker >[ 'value' ]
>( value ?? null );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<GradientPicker
{ ...props }
value={ gradient }
selectedSlug={ slug }
onChange={ ( currentGradient, index, newSlug ) => {
setGradient( currentGradient );
setSlug( newSlug );
onChange?.( currentGradient, index, newSlug );
} }
/>
);
};
Duplicate GradientsPermalink to this section
const DuplicateGradients = ( {
onChange,
value,
selectedSlug,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] = useState<
React.ComponentProps< typeof GradientPicker >[ 'value' ]
>( value ?? null );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<GradientPicker
{ ...props }
value={ gradient }
selectedSlug={ slug }
onChange={ ( currentGradient, index, newSlug ) => {
setGradient( currentGradient );
setSlug( newSlug );
onChange?.( currentGradient, index, newSlug );
} }
/>
);
};
Multiple OriginsPermalink to this section
const MultipleOrigins = ( {
onChange,
value,
selectedSlug,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] = useState<
React.ComponentProps< typeof GradientPicker >[ 'value' ]
>( value ?? null );
const [ slug, setSlug ] = useState< string | undefined >( selectedSlug );
return (
<GradientPicker
{ ...props }
value={ gradient }
selectedSlug={ slug }
onChange={ ( currentGradient, index, newSlug ) => {
setGradient( currentGradient );
setSlug( newSlug );
onChange?.( currentGradient, index, newSlug );
} }
/>
);
};
CSS VariablesPermalink to this section
const CSSVariables = () => <div
style={ {
'--red': '#f00',
'--yellow': '#ff0',
'--blue': '#00f',
} }>
<Template
onChange={fn()}
gradients={[
{
name: 'Red to Yellow',
gradient:
'linear-gradient(135deg,var(--red) 0%,var(--yellow) 100%)',
slug: 'red-to-yellow',
},
{
name: 'Yellow to Blue',
gradient:
'linear-gradient(135deg,var(--yellow) 0%,var(--blue) 100%)',
slug: 'yellow-to-blue',
},
{
name: 'Blue to Red',
gradient:
'linear-gradient(135deg,var(--blue) 0%,var(--red) 100%)',
slug: 'blue-to-red',
},
]} />
</div>;