PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / components / editor / SpacingControl.tsx

SpacingControl.tsx in Tableberg – Simple Gutenberg Table Block 1.1.5, at components/editor/SpacingControl.tsx

95 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {
2 useBlockEditContext,
3 __experimentalSpacingSizesControl as SpacingSizesControl,
4 } from "@wordpress/block-editor";
5 import { __experimentalToolsPanelItem as ToolsPanelItem } from "@wordpress/components";
6
7 function mutateOutgoingPaddingValue(padding: Padding) {
8 for (const side in padding) {
9 const key = side as keyof Padding;
10 const value = padding[key];
11
12 const slug = value?.match(/var:preset\|spacing\|(.+)/);
13 if (!slug) {
14 continue;
15 }
16 padding[key] = `var(--wp--preset--spacing--${slug[1]})`;
17 }
18
19 return padding;
20 }
21
22 function mutateIncomingPaddingValue(padding: Padding) {
23 const newPadding = { ...padding };
24
25 for (const side in newPadding) {
26 const key = side as keyof Padding;
27 const value = newPadding[key];
28
29 const slug = value?.match(/var\(--wp--preset--spacing--(.+)\)/);
30 if (!slug) {
31 continue;
32 }
33 newPadding[key] = `var:preset|spacing|${slug[1]}`;
34 }
35
36 return newPadding;
37 }
38
39 type Padding = {
40 top: string;
41 right: string;
42 bottom: string;
43 left: string;
44 };
45
46 interface Props {
47 label: string;
48 value: Padding;
49 hasValue: () => boolean;
50 onChange: (newPadding: Padding) => any;
51 resetAllFilter?: () => any;
52 onDeselect: () => any;
53 isShownByDefault?: boolean;
54 }
55
56 const SpacingControl = ({
57 label,
58 value,
59 hasValue,
60 onChange = () => {},
61 resetAllFilter,
62 onDeselect = () => {},
63 }: Props) => {
64 const { clientId } = useBlockEditContext();
65 const sides = ["top", "right", "bottom", "left"];
66
67 if (!resetAllFilter) {
68 resetAllFilter = onDeselect;
69 }
70
71 return (
72 <ToolsPanelItem
73 panelId={clientId}
74 isShownByDefault={true}
75 resetAllFilter={resetAllFilter}
76 className={"tools-panel-item-spacing"}
77 label={label}
78 onDeselect={onDeselect}
79 hasValue={hasValue}
80 >
81 <SpacingSizesControl
82 allowReset={true}
83 label={label}
84 values={mutateIncomingPaddingValue(value)}
85 sides={sides}
86 onChange={newValue => {
87 onChange(mutateOutgoingPaddingValue(newValue));
88 }}
89 />
90 </ToolsPanelItem>
91 );
92 };
93
94 export default SpacingControl;
95