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
import { GradientPicker } from '@wordpress/components';
Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
className | string | The class name added to the wrapper. | ||
onChange | ( currentGradient: string | undefined ) => void | Yes | The function called when a new gradient has been defined. It is passed to
the | |
value | GradientObject[ 'gradient' ] | null | The current value of the gradient. Pass a css gradient string (See default value for example).
Optionally pass in a @default ‘linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)’ | ||
clearable | boolean | true | Whether the palette should have a clearing button or not. @default true | |
headingLevel | | 1
| 2
| 3
| 4
| 5
| 6
| '1'
| '2'
| '3'
| '4'
| '5'
| '6' | 2 | The heading level. Only applies in cases where gradients are provided
from multiple origins (i.e. when the array passed as the @default 2 | |
asButtons | boolean | Whether the control should present as a set of buttons, each with its own tab stop. @default false | ||
loop | boolean | Prevents keyboard interaction from wrapping around.
Only used when @default true | ||
enableAlpha | boolean | true | Whether to enable alpha transparency options in the picker. @default true | |
gradients | GradientObject[] | OriginObject[] | [] | 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 @default [] | |
disableCustomGradients | boolean | false | If true, the gradient picker will not be displayed and only defined
gradients from @default false | |
__experimentalIsRenderedInSidebar | boolean | Whether this is rendered in the sidebar. @default false |
Examples
Default
const Default = ( {
onChange,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] =
useState< ( typeof props )[ 'value' ] >( null );
return (
<GradientPicker
{ ...props }
value={ gradient }
onChange={ ( ...changeArgs ) => {
setGradient( ...changeArgs );
onChange?.( ...changeArgs );
} }
/>
);
};
With No Existing Gradients
const WithNoExistingGradients = ( {
onChange,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] =
useState< ( typeof props )[ 'value' ] >( null );
return (
<GradientPicker
{ ...props }
value={ gradient }
onChange={ ( ...changeArgs ) => {
setGradient( ...changeArgs );
onChange?.( ...changeArgs );
} }
/>
);
};
Multiple Origins
const MultipleOrigins = ( {
onChange,
...props
}: React.ComponentProps< typeof GradientPicker > ) => {
const [ gradient, setGradient ] =
useState< ( typeof props )[ 'value' ] >( null );
return (
<GradientPicker
{ ...props }
value={ gradient }
onChange={ ( ...changeArgs ) => {
setGradient( ...changeArgs );
onChange?.( ...changeArgs );
} }
/>
);
};
CSS Variables
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>;