forked from 10up/block-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (82 loc) · 2.31 KB
/
index.js
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
/**
* External dependencies
*/
import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { BaseControl } from '@wordpress/components';
import { ColorPalette } from '@wordpress/block-editor';
import { useInstanceId } from '@wordpress/compose';
/**
* ColorSetting
*
* @typedef ColorSettingProps
* @property {string} label If this property is added, a label will be generated using label property as the content.
* @property {string} help If this property is added, a help text will be generated using help property as the content.
* @property {string} className If no className is passed only components-base-control is used.
* @property {boolean} hideLabelFromVision If true, the label will only be visible to screen readers
* @property {Array} colors Array with the colors to be shown.
* @property {string} value currently active value.
* @property {Function} onChange Callback called when a color is selected.
* @property {boolean} disableCustomColors Whether to allow custom color or not.
* @property {boolean} clearable Whether the palette should have a clearing button or not.
*
* @param {ColorSettingProps} props ColorSetting Props
* @returns {*} React Element
*/
// eslint-disable-next-line import/prefer-default-export
export const ColorSetting = (props) => {
const {
label,
help,
className,
hideLabelFromVision,
colors,
value,
onChange,
disableCustomColors,
clearable,
...rest
} = props;
const instanceId = useInstanceId(ColorSetting);
const id = `color-settings-${instanceId}`;
return (
<BaseControl
id={id}
label={label}
help={help}
className={className}
hideLabelFromVision={hideLabelFromVision}
{...rest}
>
<ColorPalette
colors={colors}
value={value}
onChange={onChange}
disableCustomColors={disableCustomColors}
clearable={clearable}
/>
</BaseControl>
);
};
ColorSetting.defaultProps = {
label: '',
hideLabelFromVision: false,
help: '',
className: '',
disableCustomColors: false,
value: '',
clearable: true,
};
ColorSetting.propTypes = {
label: PropTypes.string,
hideLabelFromVision: PropTypes.bool,
help: PropTypes.string,
className: PropTypes.string,
disableCustomColors: PropTypes.bool,
value: PropTypes.string,
clearable: PropTypes.bool,
colors: PropTypes.array.isRequired,
onChange: PropTypes.func.isRequired,
};