import { useState } from '@wordpress/element'; import { BaseControl, Button, RangeControl, __experimentalHStack as HStack, __experimentalVStack as VStack, __experimentalUnitControl as UnitControl, Icon } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { sidesHorizontal, sidesVertical, sidesTop, sidesBottom, sidesLeft, sidesRight, settings } from '@wordpress/icons'; export default function SpacingControl({ label, value = {}, onChange, themeSpacing = {} }) { const [verticalAdvanced, setVerticalAdvanced] = useState(false); const [horizontalAdvanced, setHorizontalAdvanced] = useState(false); const [advancedMode, setAdvancedMode] = useState(false); // full advanced toggle const parseValue = (val) => (val ? parseInt(val) : 0); const handleChange = (sides, val) => { const unit = val.toString().replace(/[0-9]/g, '') || 'px'; const newValue = { ...value }; sides.forEach((side) => { newValue[side] = `${parseInt(val)}${unit}`; }); onChange(newValue); }; // --- Generate marks for RangeControl from theme.json spacing --- const spacingMarks = themeSpacing?.spacingSizes?.map((s) => ({ value: parseInt(s.size), // you may need to strip "px" or clamp() label: s.name, })) || []; // --- Advanced 4-row mode --- if (advancedMode) { const sides = [ { key: 'top', sideIcon: sidesTop }, { key: 'bottom', sideIcon: sidesBottom }, { key: 'left', sideIcon: sidesLeft }, { key: 'right', sideIcon: sidesRight }, ]; return ( {sides.map(({ key, sideIcon }) => (
handleChange([key], val)} min={0} max={Math.max(...spacingMarks.map((m) => m.value))} step={1} marks={spacingMarks} withInputField={false} />
handleChange([key], val)} min={0} max={Math.max(...spacingMarks.map((m) => m.value))} />
))}
); } // --- Simple vertical + horizontal mode --- return ( {/* Vertical */} {verticalAdvanced && ( handleChange(['top', 'bottom'], val)} min={0} max={Math.max(...spacingMarks.map((m) => m.value))} /> )}
handleChange(['top', 'bottom'], val)} min={0} max={Math.max(...spacingMarks.map((m) => m.value))} step={1} marks={spacingMarks} withInputField={false} />