/** * Box field — a compact four-side length editor (top / right / bottom / left) * for padding & margin. Each side is a small numeric input; a shared unit * dropdown (px/rem/em/%) sits at the end, and a link toggle syncs all four * sides at once. Replaces WP's experimental BoxControl for a cleaner, more * predictable control that matches the theme's UnitField/ColorField styling. */ import { BaseControl, Dropdown, MenuGroup, MenuItem, TextControl, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { ChevronDown, Link as LinkIcon, Unlink } from '../icons'; export interface BoxValue { top?: string; right?: string; bottom?: string; left?: string; } interface UnitOption { value: string; label: string; } interface BoxFieldProps { label: string; value: BoxValue; units?: UnitOption[]; onChange: ( value: BoxValue ) => void; } const SIDES: Array< { key: keyof BoxValue; abbr: string; name: string } > = [ { key: 'top', abbr: 'T', name: __( 'Top', 'aureon' ) }, { key: 'right', abbr: 'R', name: __( 'Right', 'aureon' ) }, { key: 'bottom', abbr: 'B', name: __( 'Bottom', 'aureon' ) }, { key: 'left', abbr: 'L', name: __( 'Left', 'aureon' ) }, ]; const DEFAULT_UNITS: UnitOption[] = [ { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, { value: 'em', label: 'em' }, { value: '%', label: '%' }, ]; function split( raw: string | undefined ): { qty: string; unit: string } { const match = /^(-?\d*\.?\d+)([a-z%]*)$/i.exec( ( raw ?? '' ).trim() ); if ( ! match ) { return { qty: '', unit: '' }; } return { qty: match[ 1 ], unit: match[ 2 ] }; } export default function BoxField( { label, value, units = DEFAULT_UNITS, onChange, }: BoxFieldProps ) { // Shared unit derives from the first side that has one, else the first // option. Sides without a quantity carry no unit until they get a value. const firstUnit = SIDES.map( ( s ) => split( value[ s.key ] ).unit ).find( ( u ) => u !== '' ) ?? units[ 0 ].value; const [ unit, setUnit ] = useState( firstUnit ); const filled = SIDES.map( ( s ) => split( value[ s.key ] ).qty ); const allEqual = filled.every( ( q ) => q === filled[ 0 ] ); const [ linked, setLinked ] = useState( allEqual ); const emit = ( next: BoxValue ) => onChange( next ); const compose = ( qty: string, u: string ) => '' === qty.trim() ? '' : `${ qty }${ u }`; const setSide = ( side: keyof BoxValue, qty: string ) => { if ( linked ) { const applied = compose( qty, unit ); emit( { top: applied, right: applied, bottom: applied, left: applied, } ); return; } emit( { ...value, [ side ]: compose( qty, unit ) } ); }; const changeUnit = ( nextUnit: string ) => { setUnit( nextUnit ); // Re-suffix every side that has a quantity so the unit stays coherent. const next: BoxValue = {}; SIDES.forEach( ( s ) => { const { qty } = split( value[ s.key ] ); next[ s.key ] = compose( qty, nextUnit ); } ); emit( next ); }; const toggleLink = () => { const nextLinked = ! linked; setLinked( nextLinked ); if ( nextLinked ) { // Sync every side to the first non-empty value so linked state is // visually consistent. const seed = SIDES.map( ( s ) => value[ s.key ] ).find( ( v ) => v && v !== '' ) ?? ''; emit( { top: seed, right: seed, bottom: seed, left: seed } ); } }; const currentUnit = units.find( ( u ) => u.value === unit ); return (
{ label }
{ SIDES.map( ( side ) => { const { qty } = split( value[ side.key ] ); return (
setSide( side.key, next ) } />
); } ) }
( ) } renderContent={ ( { onClose } ) => ( { units.map( ( option ) => ( { changeUnit( option.value ); onClose(); } } > { option.label } ) ) } ) } />
); }