@wordpress-gcb/fields 0.2.0 → 0.2.2
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/README.md +51 -35
- package/dist/conditional-logic.js +83 -0
- package/{src → dist}/control-context.js +3 -2
- package/{src → dist}/controls/MediaCapabilityGate.js +12 -8
- package/dist/controls/MediaPicker.js +149 -0
- package/dist/controls/MediaTriggerBadges.js +35 -0
- package/{src → dist}/controls/PopoverOrModal.js +49 -43
- package/dist/controls/SortableItem.js +126 -0
- package/dist/controls/button-group.js +46 -0
- package/dist/controls/checkbox-group.js +65 -0
- package/dist/controls/checkbox.js +15 -0
- package/dist/controls/code.js +24 -0
- package/dist/controls/color.js +241 -0
- package/dist/controls/date.js +55 -0
- package/dist/controls/datetime.js +61 -0
- package/dist/controls/email.js +17 -0
- package/dist/controls/file.js +163 -0
- package/dist/controls/gallery.js +371 -0
- package/dist/controls/google-map.js +143 -0
- package/dist/controls/heading-level.js +93 -0
- package/dist/controls/icon.js +292 -0
- package/dist/controls/image.js +360 -0
- package/dist/controls/index.js +88 -0
- package/dist/controls/message.js +86 -0
- package/dist/controls/number.js +19 -0
- package/dist/controls/oembed.js +42 -0
- package/{src → dist}/controls/page-link.js +1 -2
- package/dist/controls/post-object.js +913 -0
- package/dist/controls/radio.js +19 -0
- package/dist/controls/range.js +108 -0
- package/{src → dist}/controls/relationship.js +12 -7
- package/dist/controls/repeater.js +277 -0
- package/dist/controls/richtext.js +494 -0
- package/dist/controls/select.js +144 -0
- package/dist/controls/size.js +59 -0
- package/dist/controls/spacing.js +141 -0
- package/dist/controls/taxonomy.js +569 -0
- package/dist/controls/text.js +16 -0
- package/dist/controls/textarea.js +17 -0
- package/dist/controls/toggle-group.js +28 -0
- package/dist/controls/toggle.js +15 -0
- package/dist/controls/url.js +235 -0
- package/dist/controls/user.js +383 -0
- package/{src → dist}/controls/wysiwyg.js +1 -1
- package/{src → dist}/hooks/useTokens.js +25 -21
- package/{src → dist}/index.js +2 -8
- package/dist/inspector.js +163 -0
- package/{src → dist}/provider.js +18 -17
- package/dist/utils/map-utils.js +54 -0
- package/dist/utils/token-helper.js +396 -0
- package/{src → dist}/validation-context.js +4 -4
- package/package.json +35 -13
- package/src/conditional-logic.js +0 -77
- package/src/controls/MediaPicker.js +0 -139
- package/src/controls/MediaTriggerBadges.js +0 -31
- package/src/controls/SortableItem.js +0 -110
- package/src/controls/button-group.js +0 -49
- package/src/controls/checkbox-group.js +0 -55
- package/src/controls/checkbox.js +0 -13
- package/src/controls/code.js +0 -21
- package/src/controls/color.js +0 -235
- package/src/controls/date.js +0 -37
- package/src/controls/datetime.js +0 -54
- package/src/controls/email.js +0 -15
- package/src/controls/file.js +0 -134
- package/src/controls/gallery.js +0 -338
- package/src/controls/google-map.js +0 -117
- package/src/controls/heading-level.js +0 -99
- package/src/controls/icon.js +0 -301
- package/src/controls/image.js +0 -334
- package/src/controls/index.js +0 -95
- package/src/controls/message.js +0 -56
- package/src/controls/number.js +0 -17
- package/src/controls/oembed.js +0 -32
- package/src/controls/post-object.js +0 -788
- package/src/controls/radio.js +0 -18
- package/src/controls/range.js +0 -110
- package/src/controls/repeater.js +0 -290
- package/src/controls/richtext.js +0 -505
- package/src/controls/select.js +0 -141
- package/src/controls/size.js +0 -49
- package/src/controls/spacing.js +0 -141
- package/src/controls/taxonomy.js +0 -488
- package/src/controls/text.js +0 -14
- package/src/controls/textarea.js +0 -15
- package/src/controls/toggle-group.js +0 -34
- package/src/controls/toggle.js +0 -13
- package/src/controls/url.js +0 -164
- package/src/controls/user.js +0 -343
- package/src/inspector.js +0 -174
- package/src/utils/map-utils.js +0 -51
- package/src/utils/token-helper.js +0 -243
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SpacingField — preset spacing (None / S / M / L) with a custom-value escape hatch.
|
|
3
|
+
*
|
|
4
|
+
* Stored value is either one of the preset keys (`'small'`, etc.) or a CSS
|
|
5
|
+
* length string (e.g. `'2rem'`). When set to a custom value, the toggle group
|
|
6
|
+
* is disabled and a "custom value applied" hint shows.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { __ } from '@wordpress/i18n';
|
|
10
|
+
import { Button, TextControl, Notice } from '@wordpress/components';
|
|
11
|
+
import { useState } from '@wordpress/element';
|
|
12
|
+
import { __experimentalToggleGroupControl as ToggleGroupControl, __experimentalToggleGroupControlOption as ToggleGroupControlOption, __experimentalHStack as HStack } from '@wordpress/components';
|
|
13
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
14
|
+
const DEFAULT_PRESETS = [{
|
|
15
|
+
label: 'None',
|
|
16
|
+
value: 'none'
|
|
17
|
+
}, {
|
|
18
|
+
label: 'S',
|
|
19
|
+
value: 'small'
|
|
20
|
+
}, {
|
|
21
|
+
label: 'M',
|
|
22
|
+
value: 'medium'
|
|
23
|
+
}, {
|
|
24
|
+
label: 'L',
|
|
25
|
+
value: 'large'
|
|
26
|
+
}];
|
|
27
|
+
const PRESET_KEYS = new Set(['none', 'small', 'medium', 'large']);
|
|
28
|
+
function isValidCSSValue(input) {
|
|
29
|
+
if (!input) return true;
|
|
30
|
+
return /^(\d*\.?\d+)(px|rem|em|%|vw|vh|vmin|vmax)?$/.test(String(input).trim());
|
|
31
|
+
}
|
|
32
|
+
export default function SpacingField({
|
|
33
|
+
control,
|
|
34
|
+
value,
|
|
35
|
+
onChange
|
|
36
|
+
}) {
|
|
37
|
+
// Decide if value is a preset or a custom string.
|
|
38
|
+
const isCustom = typeof value === 'string' && value !== '' && !PRESET_KEYS.has(value);
|
|
39
|
+
const presetValue = isCustom ? 'medium' : value || 'medium';
|
|
40
|
+
const [showCustom, setShowCustom] = useState(isCustom);
|
|
41
|
+
const [customInput, setCustomInput] = useState(isCustom ? value : '');
|
|
42
|
+
const [error, setError] = useState(null);
|
|
43
|
+
const presets = control.presets || DEFAULT_PRESETS;
|
|
44
|
+
const handlePreset = next => {
|
|
45
|
+
setShowCustom(false);
|
|
46
|
+
setCustomInput('');
|
|
47
|
+
setError(null);
|
|
48
|
+
onChange(next);
|
|
49
|
+
};
|
|
50
|
+
const handleCustom = next => {
|
|
51
|
+
setCustomInput(next);
|
|
52
|
+
if (next && !isValidCSSValue(next)) {
|
|
53
|
+
setError(__('Invalid spacing value. Use a number with a unit (e.g. 2rem, 20px).', 'gcblite'));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
setError(null);
|
|
57
|
+
onChange(next || 'medium');
|
|
58
|
+
};
|
|
59
|
+
const handleReset = () => {
|
|
60
|
+
setShowCustom(false);
|
|
61
|
+
setCustomInput('');
|
|
62
|
+
setError(null);
|
|
63
|
+
onChange('medium');
|
|
64
|
+
};
|
|
65
|
+
const displayHint = showCustom ? 'Custom' : presetValue.charAt(0).toUpperCase() + presetValue.slice(1);
|
|
66
|
+
return /*#__PURE__*/_jsx("div", {
|
|
67
|
+
className: `gcb-spacing-field components-base-control ${control.className || ''}`.trim(),
|
|
68
|
+
children: /*#__PURE__*/_jsxs("div", {
|
|
69
|
+
className: "components-base-control__field",
|
|
70
|
+
children: [/*#__PURE__*/_jsxs(HStack, {
|
|
71
|
+
children: [/*#__PURE__*/_jsxs("span", {
|
|
72
|
+
className: "components-base-control__label",
|
|
73
|
+
children: [control.label, /*#__PURE__*/_jsx("span", {
|
|
74
|
+
className: "components-font-size-picker__header__hint",
|
|
75
|
+
children: displayHint
|
|
76
|
+
})]
|
|
77
|
+
}), /*#__PURE__*/_jsx(Button, {
|
|
78
|
+
size: "small",
|
|
79
|
+
label: __('Set custom spacing', 'gcblite'),
|
|
80
|
+
onClick: () => setShowCustom(true),
|
|
81
|
+
isPressed: showCustom,
|
|
82
|
+
icon: /*#__PURE__*/_jsxs("svg", {
|
|
83
|
+
xmlns: "http://www.w3.org/2000/svg",
|
|
84
|
+
viewBox: "0 0 24 24",
|
|
85
|
+
width: "24",
|
|
86
|
+
height: "24",
|
|
87
|
+
"aria-hidden": true,
|
|
88
|
+
focusable: "false",
|
|
89
|
+
children: [/*#__PURE__*/_jsx("path", {
|
|
90
|
+
d: "m19 7.5h-7.628c-.3089-.87389-1.1423-1.5-2.122-1.5-.97966 0-1.81309.62611-2.12197 1.5h-2.12803v1.5h2.12803c.30888.87389 1.14231 1.5 2.12197 1.5.9797 0 1.8131-.62611 2.122-1.5h7.628z"
|
|
91
|
+
}), /*#__PURE__*/_jsx("path", {
|
|
92
|
+
d: "m19 15h-2.128c-.3089-.8739-1.1423-1.5-2.122-1.5s-1.8131.6261-2.122 1.5h-7.628v1.5h7.628c.3089.8739 1.1423 1.5 2.122 1.5s1.8131-.6261 2.122-1.5h2.128z"
|
|
93
|
+
})]
|
|
94
|
+
})
|
|
95
|
+
})]
|
|
96
|
+
}), error && /*#__PURE__*/_jsx(Notice, {
|
|
97
|
+
status: "warning",
|
|
98
|
+
isDismissible: true,
|
|
99
|
+
onRemove: () => setError(null),
|
|
100
|
+
children: error
|
|
101
|
+
}), showCustom && /*#__PURE__*/_jsxs("div", {
|
|
102
|
+
style: {
|
|
103
|
+
marginBottom: 16,
|
|
104
|
+
fontSize: 13
|
|
105
|
+
},
|
|
106
|
+
children: [/*#__PURE__*/_jsx("span", {
|
|
107
|
+
children: __('Custom value applied', 'gcblite')
|
|
108
|
+
}), /*#__PURE__*/_jsx(Button, {
|
|
109
|
+
variant: "link",
|
|
110
|
+
onClick: handleReset,
|
|
111
|
+
style: {
|
|
112
|
+
marginLeft: 8,
|
|
113
|
+
fontSize: 13
|
|
114
|
+
},
|
|
115
|
+
children: __('Reset', 'gcblite')
|
|
116
|
+
})]
|
|
117
|
+
}), /*#__PURE__*/_jsx(ToggleGroupControl, {
|
|
118
|
+
label: control.label,
|
|
119
|
+
value: presetValue,
|
|
120
|
+
onChange: handlePreset,
|
|
121
|
+
isBlock: true,
|
|
122
|
+
hideLabelFromVision: true,
|
|
123
|
+
disabled: showCustom,
|
|
124
|
+
className: showCustom ? 'is-disabled' : '',
|
|
125
|
+
__nextHasNoMarginBottom: true,
|
|
126
|
+
__next40pxDefaultSize: true,
|
|
127
|
+
children: presets.map(preset => /*#__PURE__*/_jsx(ToggleGroupControlOption, {
|
|
128
|
+
value: preset.value,
|
|
129
|
+
label: preset.label
|
|
130
|
+
}, preset.value))
|
|
131
|
+
}), showCustom && /*#__PURE__*/_jsx(TextControl, {
|
|
132
|
+
label: __('Custom Spacing', 'gcblite'),
|
|
133
|
+
value: customInput,
|
|
134
|
+
onChange: handleCustom,
|
|
135
|
+
placeholder: "e.g. 2rem or 20px",
|
|
136
|
+
help: __('Enter a value with unit (e.g. 2rem, 20px, 5%) or leave empty for 0.', 'gcblite'),
|
|
137
|
+
__nextHasNoMarginBottom: true
|
|
138
|
+
})]
|
|
139
|
+
})
|
|
140
|
+
});
|
|
141
|
+
}
|