/** * Border field — a compact width + color control on a single row. Width is a * plain pixel number (borders are stored as integer px); the color reuses * ColorField's swatch/picker. Keeps the two halves of a border reading as one * control instead of a stray slider plus a separate color field. * * Optionally also edits *which edges* the border is drawn on (`sides`). That is * a column-only concern — a row's border is bottom-only by definition — so the * edge toggles only appear when a `sides` handler is supplied, and only once * there's a width to place. */ import { BaseControl, Button, TextControl } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import ColorField from './color-field'; /** An edge a border can be drawn on, in CSS shorthand order. */ export type BorderSide = 'top' | 'right' | 'bottom' | 'left'; const SIDES: Array< { value: BorderSide; label: string } > = [ { value: 'top', label: __( 'Top', 'aureon' ) }, { value: 'right', label: __( 'Right', 'aureon' ) }, { value: 'bottom', label: __( 'Bottom', 'aureon' ) }, { value: 'left', label: __( 'Left', 'aureon' ) }, ]; interface BorderFieldProps { label: string; width: number; color: string; /** Inherited color shown when `color` is empty (see ColorField.fallback). */ colorFallback?: string; max?: number; onWidthChange: ( width: number ) => void; onColorChange: ( color: string ) => void; /** Undefined means every side — the default, and what legacy columns imply. */ sides?: BorderSide[]; /** Supply to expose the edge toggles; omit for a single-edge border. */ onSidesChange?: ( sides: BorderSide[] ) => void; } export default function BorderField( { label, width, color, colorFallback = '', max = 20, onWidthChange, onColorChange, sides, onSidesChange, }: BorderFieldProps ) { const active = sides ?? SIDES.map( ( side ) => side.value ); const toggleSide = ( side: BorderSide ) => { if ( ! onSidesChange ) { return; } onSidesChange( active.includes( side ) ? active.filter( ( candidate ) => candidate !== side ) : // Keep canonical top/right/bottom/left order however they're picked. SIDES.map( ( o ) => o.value ).filter( ( candidate ) => candidate === side || active.includes( candidate ) ) ); }; return ( { label }
{ const n = Math.max( 0, Math.min( max, parseInt( next, 10 ) || 0 ) ); onWidthChange( n ); } } /> px
{ onSidesChange && width > 0 ? (
{ SIDES.map( ( side ) => ( ) ) }
) : null }
); }