import { useBlockEditContext, __experimentalSpacingSizesControl as SpacingSizesControl, } from "@wordpress/block-editor"; import { BaseControl, __experimentalToolsPanelItem as ToolsPanelItem, } from "@wordpress/components"; function mutateOutgoingPaddingValue(padding: Padding) { for (const side in padding) { const key = side as keyof Padding; const value = padding[key]; const slug = value?.match(/var:preset\|spacing\|(.+)/); if (!slug) { continue; } padding[key] = `var(--wp--preset--spacing--${slug[1]})`; } return padding; } function mutateIncomingPaddingValue(padding: Padding) { const newPadding = { ...padding }; for (const side in newPadding) { const key = side as keyof Padding; const value = newPadding[key]; const slug = value?.match(/var\(--wp--preset--spacing--(.+)\)/); if (!slug) { continue; } newPadding[key] = `var:preset|spacing|${slug[1]}`; } return newPadding; } type Padding = { top: string; right: string; bottom: string; left: string; }; interface Props { label: string; /** A line under the control saying what the space is. */ help?: string; value: Padding; hasValue: () => boolean; onChange: (newPadding: Padding) => any; resetAllFilter?: () => any; onDeselect: () => any; isShownByDefault?: boolean; } const SpacingControl = ({ label, help, value, hasValue, onChange = () => {}, resetAllFilter, onDeselect = () => {}, }: Props) => { const { clientId } = useBlockEditContext(); const sides = ["top", "right", "bottom", "left"]; if (!resetAllFilter) { resetAllFilter = onDeselect; } return ( {/* The spacing control has no help of its own; BaseControl's is the one ToggleControl and the other controls show. */} { onChange(mutateOutgoingPaddingValue(newValue)); }} /> ); }; export default SpacingControl;