ColorPalette 是一个 WordPress 组件,允许用户从预定义的颜色列表中选择颜色。它支持自定义颜色、透明度控制,并可通过 Props 进行灵活配置。
import { useState } from 'react';
import { ColorPalette } from '@wordpress/components';
const MyColorPalette = () => {
const [ color, setColor ] = useState ( '#f00' )
const colors = [
{ name: 'red', color: '#f00' },
{ name: 'white', color: '#fff' },
{ name: 'blue', color: '#00f' },
];
return (
<ColorPalette
colors={ colors }
value={ color }
onChange={ ( color ) => setColor( color ) }
/>
);
}ColorPalette allows the user to pick a color from a list of pre-defined color entries.
import { useState } from 'react';
import { ColorPalette } from '@wordpress/components';
const MyColorPalette = () => {
const [ color, setColor ] = useState ( '#f00' )
const colors = [
{ name: 'red', color: '#f00' },
{ name: 'white', color: '#fff' },
{ name: 'blue', color: '#00f' },
];
return (
<ColorPalette
colors={ colors }
value={ color }
onChange={ ( color ) => setColor( color ) }
/>
);
} );
If you’re using this component outside the editor, you can
ensure Tooltip positioning
for the ColorPalette‘s color swatches, by rendering your ColorPalette with a
Popover.Slot further up the element tree and within a
SlotFillProvider overall.
The component accepts the following props.
clearable: booleanWhether the palette should have a clearing button.
truecolors: PaletteObject[] | ColorObject[]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: booleanWhether to allow the user to pick a custom color on top of the predefined
choices (defined via the colors prop).
falseenableAlpha: booleanThis controls whether the alpha channel will be offered when selecting custom
colors.
falseheadingLevel: 1 | 2 | 3 | 4 | 5 | 6 | '1' | '2' | '3' | '4' | '5' | '6'The heading level.
2value: stringCurrently active value.
onChange: OnColorChangeCallback called when a color is selected.
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.
true