import { useEffect, useState } from '@wordpress/element'; import { TextControl } from '@components/ui/TextControl'; import { DropdownPicker } from '@components/ui/DropdownPicker'; import { Popover } from '@components/ui/Popover'; import ButtonToggleGroup from '@components/ui/ButtonToggle'; import RangeControl from '@components/ui/RangeSlider'; import GridIcon from 'blockbite-icons/dist/Grid'; import PercentageIcon from 'blockbite-icons/dist/Percentage'; import TailwindUnitIcon from 'blockbite-icons/dist/Tailwind'; import SliderIcon from 'blockbite-icons/dist/Slider'; import has from 'lodash/has'; type MetricsControlProps = { defaultUnit: string; defaultValue: string; units?: string[] | 'native' | 'percent' | 'grid' | 'arbitrary' | 'all'; inputClassName?: string; onValueChange: (value: string) => void; onUnitChange: (unit: string) => void; }; const MetricsControl: React.FC = ({ defaultUnit, defaultValue, onValueChange, onUnitChange, inputClassName = 'w-[75px]', }) => { const [isVisible, setIsVisible] = useState(false); const [currentOptions, setCurrentOptions] = useState([]); // Use local state for defaultUnit and defaultValue const [unit, setUnit] = useState(defaultUnit); const [value, setValue] = useState(defaultValue); // Set initial state from props useEffect(() => { setUnit(defaultUnit); setValue(defaultValue); }, [defaultUnit, defaultValue]); const showOptions = (unit: string) => { if (unit !== 'arbitrary' && has(blockbite.codex.units.spacing, unit)) { const options = blockbite.codex.units.spacing[ unit as keyof typeof blockbite.codex.units.spacing ]; setCurrentOptions( Array.isArray(options) ? options : Object.keys(options) ); } else { setCurrentOptions([]); } setIsVisible(true); }; const unitOptions = [ { icon: , label: 'Tailwind CSS Units', value: 'native', }, { icon: , label: 'Percentage Units', value: 'percent' }, { icon: , label: 'Grid Units', value: 'grid' }, { icon: , label: 'Pixel Units', value: 'arbitrary' }, ]; return (
unit !== 'arbitrary' && showOptions(unit)} onChange={(newValue) => { setValue(newValue); onValueChange(newValue); }} readOnly={unit !== 'arbitrary'} > {unit === 'arbitrary' ? ( { setValue(newValue); onValueChange(newValue); }} /> ) : ( ({ value: option, label: unit === 'grid' ? option.replace('b_', '') : option, onClick: () => { setValue(option); onValueChange(option); }, }))} size="small" defaultPressed={value} onPressedChange={onValueChange} /> )} { setUnit(selectedUnit); onUnitChange(selectedUnit); showOptions(selectedUnit); }} />
); }; export default MetricsControl;