/** * Global-or-custom switch. * * A two-segment shell for any setting that can either follow a global token or * carry a local override. The left segment (globe) means "inherit"; the right * segment holds the override editor — until one is set it shows a pencil, so an * unset field reads as "click to customize" instead of looking blank. * * Storage is the caller's business: this control never invents a sentinel. The * theme stores '' for colors/lengths and -1 for the numeric button radius, so * the owning section passes `isCustom` and the two switch handlers. Switching to * global does NOT forget the override — `onUseCustom` is called with the last * value the caller handed us via `lastCustom`, so toggling back restores it * without a second trip to the picker. */ import { useRef } from '@wordpress/element'; import type { ReactNode } from 'react'; import { __ } from '@wordpress/i18n'; import { BaseControl, Button, Tooltip } from '@wordpress/components'; import { Globe, Pencil } from '../icons'; interface GlobalOrCustomProps { label: string; /** True when the setting currently holds an override. */ isCustom: boolean; /** * The value this setting falls back to while global — shown in the globe * segment's tooltip so the inherited value is discoverable without * switching. Pass the resolved token value (e.g. the palette color). */ globalValue?: string; /** Names what the global side inherits, e.g. "Primary" or "Text". */ globalLabel?: string; /** Write the inherit sentinel ('' / -1 / …). */ onUseGlobal: () => void; /** * Seed an override. Receives the last override this control saw, so the * caller can restore it; falls back to the global value on first use. */ onUseCustom: ( seed: string ) => void; /** The override editor — rendered only while `isCustom`. */ children: ReactNode; /** Current override, remembered across a trip through global. */ lastCustom?: string; } export default function GlobalOrCustom( { label, isCustom, globalValue = '', globalLabel = '', onUseGlobal, onUseCustom, children, lastCustom = '', }: GlobalOrCustomProps ) { // Sticky memory of the override so switching to global and back doesn't // drop it. A ref (not state) — it never needs to trigger a render. const remembered = useRef( lastCustom ); if ( isCustom && lastCustom ) { remembered.current = lastCustom; } const globalHint = globalLabel ? `${ __( 'Global', 'aureon' ) }: ${ globalLabel }${ globalValue ? ` (${ globalValue })` : '' }` : __( 'Use the global value', 'aureon' ); return ( { label }
{ isCustom ? (
{ children }
) : ( ) }
); }