import { useState } from '@wordpress/element'; import { BaseControl, Button, __experimentalUnitControl as UnitControl, RangeControl, __experimentalHStack as HStack, __experimentalVStack as VStack, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { link, linkOff } from '@wordpress/icons'; export default function BorderRadiusControl({ label, value = { radius: '0px' }, onChange }) { // Determine if value is advanced initially const initialAdvanced = value.topLeft || value.topRight || value.bottomLeft || value.bottomRight ? true : false; const [advanced, setAdvanced] = useState(initialAdvanced); const parseValue = (val) => { if (!val) return 0; if (typeof val === 'string') return parseInt(val); if (val.radius) return parseInt(val.radius); return 0; }; // Flat handlers const handleFlatChange = (val) => { const newValue = { radius: val }; console.log('Flat change →', newValue); onChange(newValue); }; const handleFlatRangeChange = (val) => { const currentUnit = value.radius?.replace(/[0-9]/g, '') || 'px'; const newValue = { radius: `${val}${currentUnit}` }; console.log('Flat range change →', newValue); onChange(newValue); }; // Advanced handlers const handleCornerChange = (corner, val) => { // Always return a full object with all four corners const newValue = { topLeft: corner === 'topLeft' ? val : value.topLeft || '0px', topRight: corner === 'topRight' ? val : value.topRight || '0px', bottomRight: corner === 'bottomRight' ? val : value.bottomRight || '0px', bottomLeft: corner === 'bottomLeft' ? val : value.bottomLeft || '0px', }; console.log('Corner change →', newValue); onChange(newValue); }; return ( {/* Left column: controls */} {!advanced && (
)} {advanced && (
handleCornerChange('topLeft', val)} min={0} max={500} /> handleCornerChange('topRight', val)} min={0} max={500} /> handleCornerChange('bottomLeft', val)} min={0} max={500} /> handleCornerChange('bottomRight', val)} min={0} max={500} />
)}
{/* Right column: toggle button */}