import { useEffect, useState, useContext } from '@wordpress/element'; import BorderAll from 'blockbite-icons/dist/BorderAll'; import BorderSplit from 'blockbite-icons/dist/BorderSplit'; import BorderHorizontal from 'blockbite-icons/dist/BorderHorizontal'; import BorderVertical from 'blockbite-icons/dist/BorderVertical'; import MetricsControl from '@components/ui/MetricsControl'; import { DropdownPicker } from '@components/ui/DropdownPicker'; import { spaceValuesToOptions, optionsToSpaceValues, } from '@components/DesignPanel/DynamicControls/SpacingHelpers'; import { useActiveOptionValues } from '@components/DesignPanel/hooks/useActiveOptionvalue'; import { useStore } from 'zustand'; import { DesignContext } from '@components/DesignPanel/DesignStore'; import has from 'lodash/has'; import { ModifierPanelProps } from '@components/DesignPanel/types'; export const Spacing: React.FC = ({ currentControlId, emitOptions, }) => { const store = useStore(useContext(DesignContext)); const [values, setValues] = useState({ top: { value: '', unit: 'arbitrary' }, right: { value: '', unit: 'arbitrary' }, bottom: { value: '', unit: 'arbitrary' }, left: { value: '', unit: 'arbitrary' }, direction: 'side', }); const [axis, setAxis] = useState<'side' | 'horizontal' | 'vertical' | 'all'>( 'side' ); const [modifierIds, setModifierIds] = useState([]); const SideOptions = [ { icon: , label: 'Side', value: 'side', onClick: () => setAxis('side'), }, { icon: , label: 'All Sides', id: 'all', value: 'all', onClick: () => setAxis('all'), }, { icon: , label: 'Horizontal', value: 'horizontal', onClick: () => setAxis('horizontal'), }, { icon: , label: 'Vertical', value: 'vertical', onClick: () => setAxis('vertical'), }, ]; const handleValueChange = ( side: 'top' | 'right' | 'bottom' | 'left', value: string, unit: string ) => { // make sure unit is a string const newValue = unit !== undefined ? String(value) : '0'; switch (axis) { case 'horizontal': if (side === 'left' || side === 'right') { setValues((prevValues) => ({ ...prevValues, left: { value: newValue, unit }, right: { value: newValue, unit }, direction: 'horizontal', })); } break; case 'vertical': if (side === 'top' || side === 'bottom') { setValues((prevValues) => ({ ...prevValues, top: { value: newValue, unit }, bottom: { value: newValue, unit }, direction: 'vertical', })); } break; case 'all': setValues({ top: { value: newValue, unit }, right: { value: newValue, unit }, bottom: { value: newValue, unit }, left: { value: newValue, unit }, direction: 'all', }); break; case 'side': default: setValues((prevValues) => ({ ...prevValues, [side]: { value: newValue, unit }, direction: 'side', })); break; } }; const activeOptionValues = useActiveOptionValues(modifierIds, store); // set initial values useEffect(() => { if (has(blockbite.codex.controls, currentControlId)) { setModifierIds(blockbite.codex.controls[currentControlId]); } const options = optionsToSpaceValues(activeOptionValues, currentControlId); setValues({ ...options, direction: 'side' }); }, [activeOptionValues]); // update options useEffect(() => { const options = spaceValuesToOptions(values, currentControlId); emitOptions(options); }, [values]); const handleUnitChange = ( side: 'top' | 'right' | 'bottom' | 'left', unit: string ) => { handleValueChange(side, values[side].value, unit); }; return (
handleValueChange('top', value, values.top.unit) } onUnitChange={(unit) => handleUnitChange('top', unit)} /> handleValueChange('left', value, values.left.unit) } onUnitChange={(unit) => handleUnitChange('left', unit)} />
{currentControlId !== 'transform' && ( setAxis(unit as any)} /> )}
handleValueChange('right', value, values.right.unit) } onUnitChange={(unit) => handleUnitChange('right', unit)} /> handleValueChange('bottom', value, values.bottom.unit) } onUnitChange={(unit) => handleUnitChange('bottom', unit)} />
); }; export default Spacing;