@wordpress/components 21.0.5 → 21.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/build/autocomplete/index.js +11 -9
- package/build/autocomplete/index.js.map +1 -1
- package/build/circular-option-picker/index.js +14 -14
- package/build/circular-option-picker/index.js.map +1 -1
- package/build/color-palette/index.js +83 -30
- package/build/color-palette/index.js.map +1 -1
- package/build/color-palette/styles.js +3 -3
- package/build/color-palette/styles.js.map +1 -1
- package/build/color-palette/types.js +6 -0
- package/build/color-palette/types.js.map +1 -0
- package/build/font-size-picker/index.js +1 -1
- package/build/font-size-picker/index.js.map +1 -1
- package/build/form-token-field/index.js +12 -10
- package/build/form-token-field/index.js.map +1 -1
- package/build/navigator/navigator-provider/component.js +5 -3
- package/build/navigator/navigator-provider/component.js.map +1 -1
- package/build/navigator/navigator-screen/component.js +5 -3
- package/build/navigator/navigator-screen/component.js.map +1 -1
- package/build/popover/index.js +1 -26
- package/build/popover/index.js.map +1 -1
- package/build-module/autocomplete/index.js +10 -9
- package/build-module/autocomplete/index.js.map +1 -1
- package/build-module/circular-option-picker/index.js +14 -14
- package/build-module/circular-option-picker/index.js.map +1 -1
- package/build-module/color-palette/index.js +81 -28
- package/build-module/color-palette/index.js.map +1 -1
- package/build-module/color-palette/styles.js +3 -3
- package/build-module/color-palette/styles.js.map +1 -1
- package/build-module/color-palette/types.js +2 -0
- package/build-module/color-palette/types.js.map +1 -0
- package/build-module/font-size-picker/index.js +1 -1
- package/build-module/font-size-picker/index.js.map +1 -1
- package/build-module/form-token-field/index.js +11 -10
- package/build-module/form-token-field/index.js.map +1 -1
- package/build-module/navigator/navigator-provider/component.js +5 -3
- package/build-module/navigator/navigator-provider/component.js.map +1 -1
- package/build-module/navigator/navigator-screen/component.js +5 -3
- package/build-module/navigator/navigator-screen/component.js.map +1 -1
- package/build-module/popover/index.js +1 -26
- package/build-module/popover/index.js.map +1 -1
- package/build-types/border-control/types.d.ts +1 -1
- package/build-types/border-control/types.d.ts.map +1 -1
- package/build-types/circular-option-picker/index.d.ts +4 -24
- package/build-types/circular-option-picker/index.d.ts.map +1 -1
- package/build-types/color-palette/index.d.ts +33 -18
- package/build-types/color-palette/index.d.ts.map +1 -1
- package/build-types/color-palette/stories/index.d.ts +21 -0
- package/build-types/color-palette/stories/index.d.ts.map +1 -0
- package/build-types/color-palette/styles.d.ts +2 -1
- package/build-types/color-palette/styles.d.ts.map +1 -1
- package/build-types/color-palette/test/index.d.ts +2 -0
- package/build-types/color-palette/test/index.d.ts.map +1 -0
- package/build-types/color-palette/types.d.ts +86 -0
- package/build-types/color-palette/types.d.ts.map +1 -0
- package/build-types/confirm-dialog/component.d.ts +4 -4
- package/build-types/form-token-field/index.d.ts.map +1 -1
- package/build-types/navigator/navigator-provider/component.d.ts.map +1 -1
- package/build-types/navigator/navigator-screen/component.d.ts.map +1 -1
- package/build-types/navigator/types.d.ts +1 -0
- package/build-types/navigator/types.d.ts.map +1 -1
- package/build-types/popover/index.d.ts.map +1 -1
- package/build-types/popover/types.d.ts +0 -14
- package/build-types/popover/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/autocomplete/index.js +18 -9
- package/src/border-control/types.ts +1 -1
- package/src/circular-option-picker/index.js +14 -20
- package/src/color-palette/README.md +51 -49
- package/src/color-palette/{index.js → index.tsx} +132 -51
- package/src/color-palette/stories/{index.js → index.tsx} +38 -27
- package/src/color-palette/{styles.js → styles.ts} +0 -0
- package/src/color-palette/test/__snapshots__/index.tsx.snap +270 -0
- package/src/color-palette/test/index.tsx +164 -0
- package/src/color-palette/types.ts +93 -0
- package/src/font-size-picker/index.js +1 -1
- package/src/form-token-field/index.tsx +21 -10
- package/src/form-token-field/test/index.tsx +167 -73
- package/src/navigator/navigator-provider/component.tsx +2 -0
- package/src/navigator/navigator-screen/component.tsx +9 -1
- package/src/navigator/types.ts +1 -0
- package/src/popover/README.md +3 -9
- package/src/popover/index.tsx +1 -25
- package/src/popover/types.ts +0 -14
- package/tsconfig.tsbuildinfo +1 -1
- package/src/color-palette/test/__snapshots__/index.js.snap +0 -1207
- package/src/color-palette/test/index.js +0 -118
|
@@ -14,6 +14,15 @@ import {
|
|
|
14
14
|
useRef,
|
|
15
15
|
useMemo,
|
|
16
16
|
} from '@wordpress/element';
|
|
17
|
+
import {
|
|
18
|
+
ENTER,
|
|
19
|
+
ESCAPE,
|
|
20
|
+
UP,
|
|
21
|
+
DOWN,
|
|
22
|
+
LEFT,
|
|
23
|
+
RIGHT,
|
|
24
|
+
BACKSPACE,
|
|
25
|
+
} from '@wordpress/keycodes';
|
|
17
26
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
|
18
27
|
import {
|
|
19
28
|
useInstanceId,
|
|
@@ -212,7 +221,7 @@ function useAutocomplete( {
|
|
|
212
221
|
}
|
|
213
222
|
|
|
214
223
|
function handleKeyDown( event ) {
|
|
215
|
-
backspacing.current = event.
|
|
224
|
+
backspacing.current = event.keyCode === BACKSPACE;
|
|
216
225
|
|
|
217
226
|
if ( ! autocompleter ) {
|
|
218
227
|
return;
|
|
@@ -223,8 +232,8 @@ function useAutocomplete( {
|
|
|
223
232
|
if ( event.defaultPrevented ) {
|
|
224
233
|
return;
|
|
225
234
|
}
|
|
226
|
-
switch ( event.
|
|
227
|
-
case
|
|
235
|
+
switch ( event.keyCode ) {
|
|
236
|
+
case UP:
|
|
228
237
|
setSelectedIndex(
|
|
229
238
|
( selectedIndex === 0
|
|
230
239
|
? filteredOptions.length
|
|
@@ -232,24 +241,24 @@ function useAutocomplete( {
|
|
|
232
241
|
);
|
|
233
242
|
break;
|
|
234
243
|
|
|
235
|
-
case
|
|
244
|
+
case DOWN:
|
|
236
245
|
setSelectedIndex(
|
|
237
246
|
( selectedIndex + 1 ) % filteredOptions.length
|
|
238
247
|
);
|
|
239
248
|
break;
|
|
240
249
|
|
|
241
|
-
case
|
|
250
|
+
case ESCAPE:
|
|
242
251
|
setAutocompleter( null );
|
|
243
252
|
setAutocompleterUI( null );
|
|
244
253
|
event.preventDefault();
|
|
245
254
|
break;
|
|
246
255
|
|
|
247
|
-
case
|
|
256
|
+
case ENTER:
|
|
248
257
|
select( filteredOptions[ selectedIndex ] );
|
|
249
258
|
break;
|
|
250
259
|
|
|
251
|
-
case
|
|
252
|
-
case
|
|
260
|
+
case LEFT:
|
|
261
|
+
case RIGHT:
|
|
253
262
|
reset();
|
|
254
263
|
return;
|
|
255
264
|
|
|
@@ -257,7 +266,7 @@ function useAutocomplete( {
|
|
|
257
266
|
return;
|
|
258
267
|
}
|
|
259
268
|
|
|
260
|
-
// Any handled
|
|
269
|
+
// Any handled keycode should prevent original behavior. This relies on
|
|
261
270
|
// the early return in the default case.
|
|
262
271
|
event.preventDefault();
|
|
263
272
|
}
|
|
@@ -16,13 +16,14 @@ import Button from '../button';
|
|
|
16
16
|
import Dropdown from '../dropdown';
|
|
17
17
|
import Tooltip from '../tooltip';
|
|
18
18
|
|
|
19
|
-
function Option( {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
function Option( props ) {
|
|
20
|
+
const {
|
|
21
|
+
className,
|
|
22
|
+
isSelected,
|
|
23
|
+
selectedIconProps,
|
|
24
|
+
tooltipText,
|
|
25
|
+
...additionalProps
|
|
26
|
+
} = props;
|
|
26
27
|
const optionButton = (
|
|
27
28
|
<Button
|
|
28
29
|
isPressed={ isSelected }
|
|
@@ -52,12 +53,8 @@ function Option( {
|
|
|
52
53
|
);
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
function DropdownLinkAction( {
|
|
56
|
-
buttonProps,
|
|
57
|
-
className,
|
|
58
|
-
dropdownProps,
|
|
59
|
-
linkText,
|
|
60
|
-
} ) {
|
|
56
|
+
function DropdownLinkAction( props ) {
|
|
57
|
+
const { buttonProps, className, dropdownProps, linkText } = props;
|
|
61
58
|
return (
|
|
62
59
|
<Dropdown
|
|
63
60
|
className={ classnames(
|
|
@@ -80,7 +77,8 @@ function DropdownLinkAction( {
|
|
|
80
77
|
);
|
|
81
78
|
}
|
|
82
79
|
|
|
83
|
-
function ButtonAction(
|
|
80
|
+
function ButtonAction( props ) {
|
|
81
|
+
const { className, children, ...additionalProps } = props;
|
|
84
82
|
return (
|
|
85
83
|
<Button
|
|
86
84
|
className={ classnames(
|
|
@@ -95,12 +93,8 @@ function ButtonAction( { className, children, ...additionalProps } ) {
|
|
|
95
93
|
);
|
|
96
94
|
}
|
|
97
95
|
|
|
98
|
-
export default function CircularOptionPicker( {
|
|
99
|
-
actions,
|
|
100
|
-
className,
|
|
101
|
-
options,
|
|
102
|
-
children,
|
|
103
|
-
} ) {
|
|
96
|
+
export default function CircularOptionPicker( props ) {
|
|
97
|
+
const { actions, className, options, children } = props;
|
|
104
98
|
return (
|
|
105
99
|
<div
|
|
106
100
|
className={ classnames(
|
|
@@ -1,54 +1,6 @@
|
|
|
1
1
|
# ColorPalette
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
The component accepts the following props.
|
|
6
|
-
|
|
7
|
-
{ colors, disableCustomColors = false, value, onChange, className, clearable = true }
|
|
8
|
-
|
|
9
|
-
### colors
|
|
10
|
-
|
|
11
|
-
Array with the colors to be shown.
|
|
12
|
-
|
|
13
|
-
- Type: `Array`
|
|
14
|
-
- Required: Yes
|
|
15
|
-
|
|
16
|
-
### disableCustomColors
|
|
17
|
-
|
|
18
|
-
Whether to allow custom color or not.
|
|
19
|
-
|
|
20
|
-
- Type: `Boolean`
|
|
21
|
-
- Required: No
|
|
22
|
-
- Default: false
|
|
23
|
-
|
|
24
|
-
### value
|
|
25
|
-
|
|
26
|
-
currently active value
|
|
27
|
-
|
|
28
|
-
- Type: `String`
|
|
29
|
-
- Required: No
|
|
30
|
-
|
|
31
|
-
### onChange
|
|
32
|
-
|
|
33
|
-
Callback called when a color is selected.
|
|
34
|
-
|
|
35
|
-
- Type: `Function`
|
|
36
|
-
- Required: Yes
|
|
37
|
-
|
|
38
|
-
### className
|
|
39
|
-
|
|
40
|
-
classes to be applied to the container.
|
|
41
|
-
|
|
42
|
-
- Type: `String`
|
|
43
|
-
- Required: No
|
|
44
|
-
|
|
45
|
-
### clearable
|
|
46
|
-
|
|
47
|
-
Whether the palette should have a clearing button or not.
|
|
48
|
-
|
|
49
|
-
- Type: `Boolean`
|
|
50
|
-
- Required: No
|
|
51
|
-
- Default: true
|
|
3
|
+
`ColorPalette` allows the user to pick a color from a list of pre-defined color entries.
|
|
52
4
|
|
|
53
5
|
## Usage
|
|
54
6
|
|
|
@@ -79,3 +31,53 @@ If you're using this component outside the editor, you can
|
|
|
79
31
|
for the `ColorPalette`'s color swatches, by rendering your `ColorPalette` with a
|
|
80
32
|
`Popover.Slot` further up the element tree and within a
|
|
81
33
|
`SlotFillProvider` overall.
|
|
34
|
+
|
|
35
|
+
## Props
|
|
36
|
+
|
|
37
|
+
The component accepts the following props.
|
|
38
|
+
|
|
39
|
+
### `colors`: `( PaletteObject | ColorObject )[]`
|
|
40
|
+
|
|
41
|
+
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.
|
|
42
|
+
|
|
43
|
+
- Required: No
|
|
44
|
+
- Default: `[]`
|
|
45
|
+
|
|
46
|
+
### `disableCustomColors`: `boolean`
|
|
47
|
+
|
|
48
|
+
Whether to allow the user to pick a custom color on top of the predefined choices (defined via the `colors` prop).
|
|
49
|
+
|
|
50
|
+
- Required: No
|
|
51
|
+
- Default: `false`
|
|
52
|
+
|
|
53
|
+
### `enableAlpha`: `boolean`
|
|
54
|
+
|
|
55
|
+
Whether the color picker should display the alpha channel both in the bottom inputs as well as in the color picker itself.
|
|
56
|
+
|
|
57
|
+
- Required: No
|
|
58
|
+
- Default: `false`
|
|
59
|
+
|
|
60
|
+
### `value`: `string`
|
|
61
|
+
|
|
62
|
+
currently active value
|
|
63
|
+
|
|
64
|
+
- Required: No
|
|
65
|
+
|
|
66
|
+
### `onChange`: `OnColorChange`
|
|
67
|
+
|
|
68
|
+
Callback called when a color is selected.
|
|
69
|
+
|
|
70
|
+
- Required: Yes
|
|
71
|
+
|
|
72
|
+
### `className`: `string`
|
|
73
|
+
|
|
74
|
+
classes to be applied to the container.
|
|
75
|
+
|
|
76
|
+
- Required: No
|
|
77
|
+
|
|
78
|
+
### `clearable`: `boolean`
|
|
79
|
+
|
|
80
|
+
Whether the palette should have a clearing button.
|
|
81
|
+
|
|
82
|
+
- Required: No
|
|
83
|
+
- Default: `true`
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
1
|
/**
|
|
3
2
|
* External dependencies
|
|
4
3
|
*/
|
|
4
|
+
import type { ForwardedRef } from 'react';
|
|
5
5
|
import { colord, extend } from 'colord';
|
|
6
6
|
import namesPlugin from 'colord/plugins/names';
|
|
7
7
|
import a11yPlugin from 'colord/plugins/a11y';
|
|
@@ -10,7 +10,7 @@ import a11yPlugin from 'colord/plugins/a11y';
|
|
|
10
10
|
* WordPress dependencies
|
|
11
11
|
*/
|
|
12
12
|
import { __, sprintf } from '@wordpress/i18n';
|
|
13
|
-
import { useCallback, useMemo } from '@wordpress/element';
|
|
13
|
+
import { useCallback, useMemo, forwardRef } from '@wordpress/element';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Internal dependencies
|
|
@@ -23,6 +23,15 @@ import { Flex, FlexItem } from '../flex';
|
|
|
23
23
|
import { Truncate } from '../truncate';
|
|
24
24
|
import { ColorHeading } from './styles';
|
|
25
25
|
import DropdownContentWrapper from '../dropdown/dropdown-content-wrapper';
|
|
26
|
+
import type {
|
|
27
|
+
ColorObject,
|
|
28
|
+
ColorPaletteProps,
|
|
29
|
+
CustomColorPickerDropdownProps,
|
|
30
|
+
MultiplePalettesProps,
|
|
31
|
+
PaletteObject,
|
|
32
|
+
SinglePaletteProps,
|
|
33
|
+
} from './types';
|
|
34
|
+
import type { WordPressComponentProps } from '../ui/context';
|
|
26
35
|
|
|
27
36
|
extend( [ namesPlugin, a11yPlugin ] );
|
|
28
37
|
|
|
@@ -33,7 +42,7 @@ function SinglePalette( {
|
|
|
33
42
|
onChange,
|
|
34
43
|
value,
|
|
35
44
|
actions,
|
|
36
|
-
} ) {
|
|
45
|
+
}: SinglePaletteProps ) {
|
|
37
46
|
const colorOptions = useMemo( () => {
|
|
38
47
|
return colors.map( ( { color, name }, index ) => {
|
|
39
48
|
const colordColor = colord( color );
|
|
@@ -74,6 +83,11 @@ function SinglePalette( {
|
|
|
74
83
|
);
|
|
75
84
|
} );
|
|
76
85
|
}, [ colors, value, onChange, clearColor ] );
|
|
86
|
+
|
|
87
|
+
if ( colors.length === 0 ) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
77
91
|
return (
|
|
78
92
|
<CircularOptionPicker
|
|
79
93
|
className={ className }
|
|
@@ -90,7 +104,11 @@ function MultiplePalettes( {
|
|
|
90
104
|
onChange,
|
|
91
105
|
value,
|
|
92
106
|
actions,
|
|
93
|
-
} ) {
|
|
107
|
+
}: MultiplePalettesProps ) {
|
|
108
|
+
if ( colors.length === 0 ) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
94
112
|
return (
|
|
95
113
|
<VStack spacing={ 3 } className={ className }>
|
|
96
114
|
{ colors.map( ( { name, colors: colorPalette }, index ) => {
|
|
@@ -117,7 +135,7 @@ export function CustomColorPickerDropdown( {
|
|
|
117
135
|
isRenderedInSidebar,
|
|
118
136
|
popoverProps: receivedPopoverProps,
|
|
119
137
|
...props
|
|
120
|
-
} ) {
|
|
138
|
+
}: CustomColorPickerDropdownProps ) {
|
|
121
139
|
const popoverProps = useMemo(
|
|
122
140
|
() => ( {
|
|
123
141
|
shift: true,
|
|
@@ -148,9 +166,9 @@ export function CustomColorPickerDropdown( {
|
|
|
148
166
|
}
|
|
149
167
|
|
|
150
168
|
export const extractColorNameFromCurrentValue = (
|
|
151
|
-
currentValue,
|
|
152
|
-
colors = [],
|
|
153
|
-
showMultiplePalettes = false
|
|
169
|
+
currentValue?: ColorPaletteProps[ 'value' ],
|
|
170
|
+
colors: ColorPaletteProps[ 'colors' ] = [],
|
|
171
|
+
showMultiplePalettes: ColorPaletteProps[ '__experimentalHasMultipleOrigins' ] = false
|
|
154
172
|
) => {
|
|
155
173
|
if ( ! currentValue ) {
|
|
156
174
|
return '';
|
|
@@ -162,7 +180,10 @@ export const extractColorNameFromCurrentValue = (
|
|
|
162
180
|
: colord( currentValue ).toHex();
|
|
163
181
|
|
|
164
182
|
// Normalize format of `colors` to simplify the following loop
|
|
165
|
-
|
|
183
|
+
type normalizedPaletteObject = { colors: ColorObject[] };
|
|
184
|
+
const colorPalettes: normalizedPaletteObject[] = showMultiplePalettes
|
|
185
|
+
? ( colors as PaletteObject[] )
|
|
186
|
+
: [ { colors: colors as ColorObject[] } ];
|
|
166
187
|
for ( const { colors: paletteColors } of colorPalettes ) {
|
|
167
188
|
for ( const { name: colorName, color: colorValue } of paletteColors ) {
|
|
168
189
|
const normalizedColorValue = currentValueIsCssVariable
|
|
@@ -179,28 +200,62 @@ export const extractColorNameFromCurrentValue = (
|
|
|
179
200
|
return __( 'Custom' );
|
|
180
201
|
};
|
|
181
202
|
|
|
182
|
-
export const showTransparentBackground = ( currentValue ) => {
|
|
203
|
+
export const showTransparentBackground = ( currentValue?: string ) => {
|
|
183
204
|
if ( typeof currentValue === 'undefined' ) {
|
|
184
205
|
return true;
|
|
185
206
|
}
|
|
186
207
|
return colord( currentValue ).alpha() === 0;
|
|
187
208
|
};
|
|
188
209
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
colors
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
210
|
+
const areColorsMultiplePalette = (
|
|
211
|
+
colors: NonNullable< ColorPaletteProps[ 'colors' ] >
|
|
212
|
+
): colors is PaletteObject[] => {
|
|
213
|
+
return colors.every( ( colorObj ) =>
|
|
214
|
+
Array.isArray( ( colorObj as PaletteObject ).colors )
|
|
215
|
+
);
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
function UnforwardedColorPalette(
|
|
219
|
+
props: WordPressComponentProps< ColorPaletteProps, 'div' >,
|
|
220
|
+
forwardedRef: ForwardedRef< any >
|
|
221
|
+
) {
|
|
222
|
+
const {
|
|
223
|
+
clearable = true,
|
|
224
|
+
colors = [],
|
|
225
|
+
disableCustomColors = false,
|
|
226
|
+
enableAlpha,
|
|
227
|
+
onChange,
|
|
228
|
+
value,
|
|
229
|
+
__experimentalHasMultipleOrigins = false,
|
|
230
|
+
__experimentalIsRenderedInSidebar = false,
|
|
231
|
+
...otherProps
|
|
232
|
+
} = props;
|
|
200
233
|
const clearColor = useCallback( () => onChange( undefined ), [ onChange ] );
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
234
|
+
|
|
235
|
+
const buttonLabelName = useMemo(
|
|
236
|
+
() =>
|
|
237
|
+
extractColorNameFromCurrentValue(
|
|
238
|
+
value,
|
|
239
|
+
colors,
|
|
240
|
+
__experimentalHasMultipleOrigins
|
|
241
|
+
),
|
|
242
|
+
[ value, colors, __experimentalHasMultipleOrigins ]
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
// Make sure that the `colors` array has a format (single/multiple) that is
|
|
246
|
+
// compatible with the `__experimentalHasMultipleOrigins` flag. This is true
|
|
247
|
+
// when __experimentalHasMultipleOrigins and areColorsMultiplePalette() are
|
|
248
|
+
// either both `true` or both `false`.
|
|
249
|
+
if (
|
|
250
|
+
colors.length > 0 &&
|
|
251
|
+
__experimentalHasMultipleOrigins !== areColorsMultiplePalette( colors )
|
|
252
|
+
) {
|
|
253
|
+
// eslint-disable-next-line no-console
|
|
254
|
+
console.warn(
|
|
255
|
+
'wp.components.ColorPalette: please specify a format for the `colors` prop that is compatible with the `__experimentalHasMultipleOrigins` prop.'
|
|
256
|
+
);
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
204
259
|
|
|
205
260
|
const renderCustomColorPicker = () => (
|
|
206
261
|
<DropdownContentWrapper paddingSize="none">
|
|
@@ -212,20 +267,11 @@ export default function ColorPalette( {
|
|
|
212
267
|
</DropdownContentWrapper>
|
|
213
268
|
);
|
|
214
269
|
|
|
215
|
-
const colordColor = colord( value );
|
|
270
|
+
const colordColor = colord( value ?? '' );
|
|
216
271
|
|
|
217
272
|
const valueWithoutLeadingHash = value?.startsWith( '#' )
|
|
218
273
|
? value.substring( 1 )
|
|
219
274
|
: value ?? '';
|
|
220
|
-
const buttonLabelName = useMemo(
|
|
221
|
-
() =>
|
|
222
|
-
extractColorNameFromCurrentValue(
|
|
223
|
-
value,
|
|
224
|
-
colors,
|
|
225
|
-
showMultiplePalettes
|
|
226
|
-
),
|
|
227
|
-
[ value, colors, showMultiplePalettes ]
|
|
228
|
-
);
|
|
229
275
|
|
|
230
276
|
const customColorAccessibleLabel = !! valueWithoutLeadingHash
|
|
231
277
|
? sprintf(
|
|
@@ -238,8 +284,20 @@ export default function ColorPalette( {
|
|
|
238
284
|
)
|
|
239
285
|
: __( 'Custom color picker.' );
|
|
240
286
|
|
|
287
|
+
const paletteCommonProps = {
|
|
288
|
+
clearable,
|
|
289
|
+
clearColor,
|
|
290
|
+
onChange,
|
|
291
|
+
value,
|
|
292
|
+
actions: !! clearable && (
|
|
293
|
+
<CircularOptionPicker.ButtonAction onClick={ clearColor }>
|
|
294
|
+
{ __( 'Clear' ) }
|
|
295
|
+
</CircularOptionPicker.ButtonAction>
|
|
296
|
+
),
|
|
297
|
+
};
|
|
298
|
+
|
|
241
299
|
return (
|
|
242
|
-
<VStack spacing={ 3 }
|
|
300
|
+
<VStack spacing={ 3 } ref={ forwardedRef } { ...otherProps }>
|
|
243
301
|
{ ! disableCustomColors && (
|
|
244
302
|
<CustomColorPickerDropdown
|
|
245
303
|
isRenderedInSidebar={ __experimentalIsRenderedInSidebar }
|
|
@@ -284,22 +342,45 @@ export default function ColorPalette( {
|
|
|
284
342
|
) }
|
|
285
343
|
/>
|
|
286
344
|
) }
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
{ __( 'Clear' ) }
|
|
299
|
-
</CircularOptionPicker.ButtonAction>
|
|
300
|
-
)
|
|
301
|
-
}
|
|
302
|
-
/>
|
|
345
|
+
{ __experimentalHasMultipleOrigins ? (
|
|
346
|
+
<MultiplePalettes
|
|
347
|
+
{ ...paletteCommonProps }
|
|
348
|
+
colors={ colors as PaletteObject[] }
|
|
349
|
+
/>
|
|
350
|
+
) : (
|
|
351
|
+
<SinglePalette
|
|
352
|
+
{ ...paletteCommonProps }
|
|
353
|
+
colors={ colors as ColorObject[] }
|
|
354
|
+
/>
|
|
355
|
+
) }
|
|
303
356
|
</VStack>
|
|
304
357
|
);
|
|
305
358
|
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Allows the user to pick a color from a list of pre-defined color entries.
|
|
362
|
+
*
|
|
363
|
+
* ```jsx
|
|
364
|
+
* import { ColorPalette } from '@wordpress/components';
|
|
365
|
+
* import { useState } from '@wordpress/element';
|
|
366
|
+
*
|
|
367
|
+
* const MyColorPalette = () => {
|
|
368
|
+
* const [ color, setColor ] = useState ( '#f00' )
|
|
369
|
+
* const colors = [
|
|
370
|
+
* { name: 'red', color: '#f00' },
|
|
371
|
+
* { name: 'white', color: '#fff' },
|
|
372
|
+
* { name: 'blue', color: '#00f' },
|
|
373
|
+
* ];
|
|
374
|
+
* return (
|
|
375
|
+
* <ColorPalette
|
|
376
|
+
* colors={ colors }
|
|
377
|
+
* value={ color }
|
|
378
|
+
* onChange={ ( color ) => setColor( color ) }
|
|
379
|
+
* />
|
|
380
|
+
* );
|
|
381
|
+
* } );
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
384
|
+
export const ColorPalette = forwardRef( UnforwardedColorPalette );
|
|
385
|
+
|
|
386
|
+
export default ColorPalette;
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { CSSProperties } from 'react';
|
|
5
|
+
import type { ComponentMeta, ComponentStory } from '@storybook/react';
|
|
6
|
+
|
|
1
7
|
/**
|
|
2
8
|
* WordPress dependencies
|
|
3
9
|
*/
|
|
@@ -6,34 +12,25 @@ import { useState } from '@wordpress/element';
|
|
|
6
12
|
/**
|
|
7
13
|
* Internal dependencies
|
|
8
14
|
*/
|
|
9
|
-
import ColorPalette from '
|
|
15
|
+
import ColorPalette from '..';
|
|
10
16
|
import Popover from '../../popover';
|
|
11
17
|
import { Provider as SlotFillProvider } from '../../slot-fill';
|
|
18
|
+
import type { ColorObject, PaletteObject } from '../types';
|
|
12
19
|
|
|
13
|
-
const meta = {
|
|
20
|
+
const meta: ComponentMeta< typeof ColorPalette > = {
|
|
14
21
|
title: 'Components/ColorPalette',
|
|
15
22
|
component: ColorPalette,
|
|
16
23
|
argTypes: {
|
|
24
|
+
// Removing the control because setting this prop without changing the
|
|
25
|
+
// format of the `colors` prop can break the component.
|
|
17
26
|
__experimentalHasMultipleOrigins: {
|
|
18
27
|
control: {
|
|
19
28
|
type: null,
|
|
20
29
|
},
|
|
21
30
|
},
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
clearable: {
|
|
28
|
-
control: {
|
|
29
|
-
type: 'boolean',
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
disableCustomColors: {
|
|
33
|
-
control: {
|
|
34
|
-
type: 'boolean',
|
|
35
|
-
},
|
|
36
|
-
},
|
|
31
|
+
as: { control: { type: null } },
|
|
32
|
+
onChange: { action: 'onChange', control: { type: null } },
|
|
33
|
+
value: { control: { type: null } },
|
|
37
34
|
},
|
|
38
35
|
parameters: {
|
|
39
36
|
controls: { expanded: true },
|
|
@@ -42,14 +39,26 @@ const meta = {
|
|
|
42
39
|
};
|
|
43
40
|
export default meta;
|
|
44
41
|
|
|
45
|
-
const Template
|
|
42
|
+
const Template: ComponentStory< typeof ColorPalette > = ( {
|
|
43
|
+
onChange,
|
|
44
|
+
...args
|
|
45
|
+
} ) => {
|
|
46
46
|
const firstColor =
|
|
47
|
-
args.colors
|
|
48
|
-
|
|
47
|
+
( args.colors as ColorObject[] )[ 0 ].color ||
|
|
48
|
+
( args.colors as PaletteObject[] )[ 0 ].colors[ 0 ].color;
|
|
49
|
+
const [ color, setColor ] = useState< string | undefined >( firstColor );
|
|
49
50
|
|
|
50
51
|
return (
|
|
51
52
|
<SlotFillProvider>
|
|
52
|
-
<ColorPalette
|
|
53
|
+
<ColorPalette
|
|
54
|
+
{ ...args }
|
|
55
|
+
value={ color }
|
|
56
|
+
onChange={ ( newColor ) => {
|
|
57
|
+
setColor( newColor );
|
|
58
|
+
onChange?.( newColor );
|
|
59
|
+
} }
|
|
60
|
+
/>
|
|
61
|
+
{ /* @ts-expect-error The 'Slot' component hasn't been typed yet. */ }
|
|
53
62
|
<Popover.Slot />
|
|
54
63
|
</SlotFillProvider>
|
|
55
64
|
);
|
|
@@ -92,14 +101,16 @@ MultipleOrigins.args = {
|
|
|
92
101
|
],
|
|
93
102
|
};
|
|
94
103
|
|
|
95
|
-
export const CSSVariables = ( args ) => {
|
|
104
|
+
export const CSSVariables: ComponentStory< typeof ColorPalette > = ( args ) => {
|
|
96
105
|
return (
|
|
97
106
|
<div
|
|
98
|
-
style={
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
107
|
+
style={
|
|
108
|
+
{
|
|
109
|
+
'--red': '#f00',
|
|
110
|
+
'--yellow': '#ff0',
|
|
111
|
+
'--blue': '#00f',
|
|
112
|
+
} as CSSProperties
|
|
113
|
+
}
|
|
103
114
|
>
|
|
104
115
|
<Template { ...args } />
|
|
105
116
|
</div>
|
|
File without changes
|