/** * Scale field — label, size preview, unit-aware length input, reset-to- * default, and a description. Used by scale-type sections (Spacing, Buttons/ * radius) whose fields are CSS length tokens rather than colors, mirroring * ColorCard's shell so both "scale" sections read consistently with Colors. */ import { Button, Tooltip } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { Undo2 } from '../icons'; import UnitField from './unit-field'; const LENGTH_UNITS = [ { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, { value: 'em', label: 'em' }, { value: '%', label: '%' }, ]; /** * Rough px equivalent for a CSS length, for the preview bar only. * @param length */ function toPx( length: string ): number { const match = /^(-?\d*\.?\d+)(px|rem|em)?$/.exec( length.trim() ); if ( ! match ) { return 0; } const value = parseFloat( match[ 1 ] ); const unit = match[ 2 ] ?? 'px'; return 'px' === unit ? value : value * 16; } interface ScaleFieldProps { label: string; description?: string; value: string; defaultValue?: string; preview?: 'bar' | 'radius'; onChange: ( value: string ) => void; } export default function ScaleField( { label, description, value, defaultValue = '', preview = 'bar', onChange, }: ScaleFieldProps ) { const isDefault = ! defaultValue || value === defaultValue; return (
{ label } { ! isDefault && (
{ 'bar' === preview ? ( ) : ( ) }
{ 'radius' === preview && ( onChange( `${ event.target.value }px` ) } /> ) } { description && (

{ description }

) }
); }