/** * Segmented control — a single pill-shaped bar split into labelled segments, * each with an optional icon. One choice is active at a time; the whole bar is * one bordered surface rather than a grid of separate cards, so it reads as a * compact toggle rather than a gallery. * * Used for layout choices (container width, sidebar position, list vs. grid) * across the Content Templates workspace. */ import { useEffect, useRef, useState } from '@wordpress/element'; import type { LucideIcon } from '../icons'; export interface SegmentOption { value: string; label: string; icon?: LucideIcon; } interface SegmentedControlProps { label?: string; /** Accessible name when the visible label is drawn by a wrapper instead. */ ariaLabel?: string; value: string; options: SegmentOption[]; onChange: ( value: string ) => void; help?: string; /** * Dim the bar and refuse input. Used where a choice is real but inert under * the current layout — the control stays visible (with `help` saying why) * rather than vanishing, so the setting doesn't look lost. */ disabled?: boolean; /** * `bar` (the default) is the shared-track pill bar described above. * `cards` draws each choice as its own bordered card with the icon stacked * over the label — for a small set of choices that are picked by their * picture (Product Card's Content Alignment), where the shared track reads * as one squeezed strip rather than three things to choose between. */ /* * `pills` draws each choice as its own bordered pill, the active one tinted * in the brand colour — for a row of peers read left to right (Content * Templates' Width and Sidebar). Wraps into rows like `cards`, never stacks. * `solid` is the shared-track bar with the active segment filled in the * brand colour — for a two-way either/or (List | Grid, Text link | Button). */ variant?: 'bar' | 'cards' | 'pills' | 'solid'; } /** * Whether the segments fit across the track, watched as the pane resizes. * * A bar too narrow for its labels used to wrap, which left the last row holding * one stretched segment beside a row of two — an arrangement that reads as a * mistake rather than a choice ("Compact | Comfortable" over a full-width * "Spacious"). Stacking every segment instead reads as a deliberate list, so * the only question is which of the two a given bar should be, and that depends * on the words in it: "Left · Center · Right" fits a 240px Customizer pane * where "Compact · Comfortable · Spacious" needs 270. A breakpoint cannot know * that — and would be wrong again in every translation — so the width the * labels actually occupy is measured. * * The measurement reads the label and icon boxes rather than the segments', and * so is unaffected by the layout it is deciding: a stretched segment reports * the width it was given, but the text inside it reports the width it needs. * * Only the `bar` variant asks for this. The `cards` variant is a grid by * design — its choices are picked by their picture, and it already wraps into * balanced rows (five sidebar positions read 3 + 2) rather than leaving one * stretched pill behind. * * @param count How many segments are in the bar; re-measures when it changes. * @param enabled Whether this bar stacks at all. * @return Ref for the track, and whether its segments have to stack. */ function useStacked( count: number, enabled: boolean ) { const track = useRef< HTMLDivElement | null >( null ); const [ stacked, setStacked ] = useState( false ); useEffect( () => { const element = track.current; if ( ! enabled || ! element || 'undefined' === typeof ResizeObserver ) { return; } const measure = () => { const style = window.getComputedStyle( element ); const gap = parseFloat( style.columnGap ) || 0; const segments = Array.from( element.querySelectorAll< HTMLElement >( '.aureon-segmented__segment' ) ); if ( ! segments.length ) { return; } const needed = segments.reduce( ( total, segment ) => { const box = window.getComputedStyle( segment ); const inner = parseFloat( box.columnGap ) || 0; const parts = Array.from( segment.children ); const content = parts.reduce( ( sum, part ) => sum + part.getBoundingClientRect().width, 0 ); return ( total + content + inner * Math.max( 0, parts.length - 1 ) + parseFloat( box.paddingLeft ) + parseFloat( box.paddingRight ) ); }, gap * ( segments.length - 1 ) ); // A pixel of slack: sub-pixel text widths round against us often // enough that an exact comparison stacks a bar that fits. setStacked( needed > element.clientWidth + 1 ); }; measure(); const observer = new ResizeObserver( measure ); observer.observe( element ); return () => observer.disconnect(); }, [ count, enabled ] ); return { track, stacked }; } export default function SegmentedControl( { label, ariaLabel, value, options, onChange, help, disabled = false, variant = 'bar', }: SegmentedControlProps ) { const { track, stacked } = useStacked( options.length, 'cards' !== variant && 'pills' !== variant ); const move = ( delta: number ) => { if ( disabled ) { return; } const index = options.findIndex( ( option ) => option.value === value ); const next = options[ ( index + delta + options.length ) % options.length ]; if ( next ) { onChange( next.value ); } }; return (
{ help }
) }