-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.tsx
99 lines (86 loc) · 1.84 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* WordPress dependencies
*/
import { BaseControl } from '@wordpress/components';
import { ColorPalette } from '@wordpress/block-editor';
import { useInstanceId } from '@wordpress/compose';
interface ColorSettingProps {
/**
* If this property is added, a label will be generated using label property as the content.
*/
label?: string;
/**
* If true, the label will only be visible to screen readers.
*/
hideLabelFromVision?: boolean;
/**
* If this property is added, a help text will be generated using help property as the content.
*/
help?: string;
/**
* If no className is passed only components-base-control is used.
*/
className?: string;
/**
* Whether to allow custom color or not.
*/
disableCustomColors?: boolean;
/**
* currently active value.
*/
value?: string;
/**
* Whether the palette should have a clearing button or not.
*/
clearable?: boolean;
/**
* Array with the colors to be shown.
*/
colors: Array<Color>;
/**
* Callback called when a color is selected.
*/
onChange: (color: string) => void;
}
interface Color {
/**
* Color name.
*/
name: string;
/**
* Color hex code.
*/
color: string;
}
// eslint-disable-next-line import/prefer-default-export
export const ColorSetting: React.FC<ColorSettingProps> = ({
label = '',
help = '',
className = '',
hideLabelFromVision = false,
colors,
value = '',
onChange,
disableCustomColors = false,
clearable = true,
}) => {
const instanceId = useInstanceId(ColorSetting);
const id = `color-settings-${instanceId}`;
return (
<BaseControl
id={id}
label={label}
help={help}
className={className}
hideLabelFromVision={hideLabelFromVision}
>
<ColorPalette
colors={colors}
value={value}
onChange={onChange}
disableCustomColors={disableCustomColors}
clearable={clearable}
/>
</BaseControl>
);
};