/** * Generic renderer for a plugin-contributed surface — a Settings section or a * top-level area. * * The panel's own sections are hand-written React; a plugin can't add one of * those, so it describes its surface as a list of typed fields instead (see * Aureon\Customizer\Admin_Panel::panel_sections()) and this component renders * them with the same controls the built-in sections use — the result reads as * native even though the theme knows nothing about what it configures. * * Values are plugin-owned: they arrive from, and save back to, the surface's own * REST route (App handles the load/save cycle so the shared Save button and dirty * state cover these surfaces too). `action` fields bypass Save entirely — they * POST an action name and adopt whatever state the route returns. * * Several field types are structural rather than value-bound, so that a * declarative surface can have the same shape as a hand-written section instead * of being stuck as one flat column of controls: `group` wraps its own fields in * a `FieldGroup` card (optionally with a head switch named by `toggleKey`, a * state pill named by `statusKey`, and a two-column body), `copy` hands the * reader a string to paste elsewhere — an OAuth redirect URI, say — `link` is a * bare outbound link, and `helpcard` is the "need a hand?" callout that tends to * close a setup screen. A surface may also carry `docs`, which draws a * documentation card at the end of its heading row. * * Runs of fields can also lay out together rather than one per row; see the loop * in `PanelFieldsList` for the three that do. */ import { useState, useEffect, useRef } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { ToggleControl, CheckboxControl, TextControl, TextareaControl, Button, ExternalLink, } from '@wordpress/components'; import { ShieldCheck, ShieldAlert, ShieldX, KeyRound, Copy, Check, Eye, EyeOff, ExternalLink as ExternalLinkIcon, ArrowRight, ArrowUpRight, ChevronRight, FileText, CircleCheck, CircleAlert, } from '../icons'; import SelectField from '../controls/select-field'; import FieldGroup from '../controls/field-group'; import RepeaterField from '../controls/repeater-field'; import ConfirmDialog from '../confirm-dialog'; import moduleIcon from '../module-icons'; import Tabs, { TabPanel } from '../controls/tabs'; import type { PanelField, PanelDetailRow, PanelSurface, PanelTone, PanelRepeaterItem, SetPanelValue, RunPanelAction, } from '../types'; import SectionIntro from '../controls/section-intro'; interface PanelSectionProps { section: PanelSurface; values: Record< string, unknown >; setValue: SetPanelValue; // Runs an `action` field against the surface's route. runAction: RunPanelAction; // The action currently in flight on this surface, if any. pendingAction?: string; } export default function PanelSection( { section, values, setValue, runAction, pendingAction, }: PanelSectionProps ) { const Icon = moduleIcon( section.icon ); const tabs = section.tabs ?? []; // `tabs` keeps its raw (string) icon for the `key`/`filter` bookkeeping // above; `` wants an actual component, resolved the same way every // other icon field on a surface is. const tabItems = tabs.map( ( tab ) => ( { ...tab, icon: tab.icon ? moduleIcon( tab.icon ) : undefined, } ) ); const [ activeTab, setActiveTab ] = useState( tabs[ 0 ]?.key ?? '' ); // Fields with no `tab` (or naming one the surface didn't declare — see // Admin_Panel::panel_fields()) render above the tab bar, visible // regardless of which tab is active; a shared notice, typically. const untabbed = section.fields.filter( ( field ) => ! field.tab ); return (
{ section.docs?.url && ( // A plain anchor rather than `ExternalLink`: that component // appends its own "opens in a new tab" glyph, which would sit // beside the arrow this already draws. The affordance is kept // as assistive text instead. // // With a supporting line it's a two-line card; without one // there's nothing to stack, so it collapses to a button — // which is all a bare "View documentation" ever needed to be. { section.docs.label } { section.docs.help && ( { section.docs.help } ) } ) } } >

{ section.label }

{ section.description && (

{ section.description }

) }
{ tabs.length > 0 && ( <> { tabs.map( ( tab ) => ( field.tab === tab.key ) } values={ values } setValue={ setValue } runAction={ runAction } pendingAction={ pendingAction } /> ) ) } ) }
); } interface PanelFieldsListProps { fields: PanelField[]; values: Record< string, unknown >; setValue: SetPanelValue; runAction?: RunPanelAction; pendingAction?: string; } /** * Renders one surface's fields, with `showIf`/`hideIf` visibility resolved * against sibling values. Its own component so a built-in section (Custom * Code, Performance, WooCommerce, …) can drop a plugin-targeted surface's * fields inline — see SECTION_EXTRAS in customizer/sections.tsx — without pulling in the * heading/icon chrome above, which only makes sense for a surface with a nav * entry of its own. * @param root0 * @param root0.fields * @param root0.values * @param root0.setValue * @param root0.runAction * @param root0.pendingAction */ export function PanelFieldsList( { fields, values, setValue, runAction = () => {}, pendingAction, }: PanelFieldsListProps ) { // `showIf`/`hideIf` render a field only while a sibling's value is truthy or // falsy — how a descriptor expresses "these roles only matter once the // restriction is on", or "offer Activate only while inactive", without the // theme knowing what any of it means. const visible = ( field: PanelField ): boolean => { if ( field.showIf && ! values[ field.showIf ] ) { return false; } return ! ( field.hideIf && values[ field.hideIf ] ); }; const shown = fields.filter( visible ); const fieldKey = ( field: PanelField, index: number ) => field.key || field.action || `${ field.type }-${ index }`; const control = ( field: PanelField, index: number ) => ( ); // Three runs get a layout of their own instead of one field per row: // consecutive `status` fields become one status card, a `text` field // directly followed by `action`s puts the buttons beside the input // (a license key and its Activate button, say), and a `link` followed by // `action`s becomes a footer row with the link at one end and the buttons // at the other — how a provider card ends with "create credentials" on the // left and its own Save on the right. const out: JSX.Element[] = []; for ( let i = 0; i < shown.length; i++ ) { const field = shown[ i ]; if ( 'status' === field.type ) { const run: PanelField[] = []; while ( i < shown.length && 'status' === shown[ i ].type ) { run.push( shown[ i ] ); i++; } i--; out.push( ); continue; } // A state card's own actions belong inside it, at the end of its row — // "Deactivate" reads as something done to the state it sits beside, // not as a loose button under the card. if ( 'statecard' === field.type ) { const start = i; const actions: PanelField[] = []; while ( 'action' === shown[ i + 1 ]?.type ) { actions.push( shown[ i + 1 ] ); i++; } out.push( control( action, start + n + 1 ) ) } /> ); continue; } if ( 'link' === field.type && 'action' === shown[ i + 1 ]?.type ) { const start = i; const actions: PanelField[] = []; while ( 'action' === shown[ i + 1 ]?.type ) { actions.push( shown[ i + 1 ] ); i++; } out.push(
{ control( field, start ) }
{ actions.map( ( action, n ) => control( action, start + n + 1 ) ) }
); continue; } if ( 'text' === field.type && 'action' === shown[ i + 1 ]?.type ) { const start = i; const actions: PanelField[] = []; while ( 'action' === shown[ i + 1 ]?.type ) { actions.push( shown[ i + 1 ] ); i++; } out.push(
{ control( { ...field, help: undefined }, start ) }
{ actions.map( ( action, n ) => control( action, start + n + 1 ) ) }
{ field.help && (

{ field.help }

) }
); continue; } out.push( control( field, i ) ); } return <>{ out }; } const TONE_ICONS = { success: ShieldCheck, warning: ShieldAlert, error: ShieldX, info: KeyRound, }; /** * A run of `status` fields as one card: the first toned field is the headline * (big icon, tinted by its tone), the rest are detail tiles beside it. * * @param root0 * @param root0.fields * @param root0.values */ function StatusCard( { fields, values, }: { fields: PanelField[]; values: Record< string, unknown >; } ) { const text = ( field: PanelField ): string => { const value = field.key ? values[ field.key ] : undefined; return 'string' === typeof value && '' !== value ? value : '—'; }; const primary = fields.find( ( field ) => field.toneKey ); const details = fields.filter( ( field ) => field !== primary ); const tone = ( primary?.toneKey ? values[ primary.toneKey ] : '' ) as PanelTone; // The tone glyphs are shields and a key — right for the license screen this // card was built for, wrong for anything else, so a field may name its own. const Icon = primary?.icon ? moduleIcon( primary.icon ) : TONE_ICONS[ tone as keyof typeof TONE_ICONS ] ?? KeyRound; return (
{ primary && (

{ primary.label }

{ text( primary ) }

{ primary.help && (

{ primary.help }

) }
) } { details.length > 0 && (
{ details.map( ( field, index ) => (
{ field.label }
{ text( field ) }
) ) }
) }
); } function PanelFieldControl( { field, values, setValue, runAction, pendingAction, }: { field: PanelField; values: Record< string, unknown >; setValue: SetPanelValue; runAction: RunPanelAction; pendingAction?: string; } ) { // A field may be rendered but inert while a sibling is truthy — e.g. a license // key that can't be edited while the license is active — or the mirror of // that, inert *until* a sibling is filled in, which is how a per-card Save // says it has nothing to save yet. const disabled = !! ( field.disabledIf && values[ field.disabledIf ] ) || !! ( field.disabledUnless && ! values[ field.disabledUnless ] ); // An `action`/`helpcard` field carrying a `confirm` descriptor pauses on a // modal instead of firing on the first click — for one that can't be undone // (a reset, a delete). Anything without one runs immediately, same as today. const [ confirming, setConfirming ] = useState< PanelField | null >( null ); const runWithConfirm = ( target: PanelField ) => { if ( target.confirm ) { setConfirming( target ); return; } runAction( target.action as string ); }; const confirmDialog = confirming && ( { runAction( confirming.action as string ); setConfirming( null ); } } onCancel={ () => setConfirming( null ) } /> ); // A group is a bordered card wrapping its own fields — the same FieldGroup // the theme's hand-written sections use, reached declaratively. `toggleKey` // draws that sibling's switch in the card's head instead of as a row of the // body, so the body can hide behind it without orphaning the toggle; the // field itself is dropped from the body to avoid rendering it twice. if ( 'group' === field.type ) { const toggleKey = field.toggleKey; const body = ( field.fields ?? [] ).filter( ( child ) => ! ( toggleKey && 'toggle' === child.type && child.key === toggleKey ) ); // Without a head switch a card may instead state where it stands, from // a sibling value — "Connected" against a provider that has its // credentials, say. Read-only, so it costs the body nothing. const badgeText = field.statusKey ? values[ field.statusKey ] : ''; return ( setValue( toggleKey, checked ), } : undefined } > ); } // A row lays its children out side by side instead of stacked — a screen's // closing row, where a reset button and a help card sit at opposite ends. // Purely layout: it holds no value of its own. if ( 'row' === field.type ) { return (
); } // A read-only label/value table — a licence's key, plan, seat count and // expiry. A run of `status` rows could carry the same values, but each would // be its own bordered tile; these belong together as one record. if ( 'detaillist' === field.type ) { return (
{ ( field.detailRows ?? [] ).map( ( row ) => ( ) ) }
); } // Static ticked lines — what a plan includes. Not values: an entitlement // list is the same on every site, so it lives in the descriptor. if ( 'checklist' === field.type ) { return (
    { ( field.items ?? [] ).map( ( item ) => (
  • ) ) }
); } if ( 'copy' === field.type ) { // A copy field may show a string an action can change while the panel is // open — an account page URL that exists only once Create has run — so it // can read from the value set rather than from descriptor text, which is // fixed at page load. const live = field.textKey ? values[ field.textKey ] : undefined; return ( ); } // A bare outbound link, with its own leading glyph and a trailing arrow — // "go and create the thing this card needs" rather than prose with a link // buried in it, which is what a `notice` would give. if ( 'link' === field.type ) { // A link may present as a button — same destination, but weighted as one // of a card's actions rather than as a sentence with a link in it. const asButton = 'button' === field.layout || 'button-primary' === field.layout; return ( ); } // The callout that closes a setup screen: a tinted card pointing at the // documentation for everything above it. if ( 'helpcard' === field.type ) { const HelpIcon = moduleIcon( field.icon || 'Lightbulb' ); // With a button label the card is a container and the button is the // target; without one the card itself has to be, or the chevron beside // it would only look clickable. An `action` card is never the link // itself — its button is the one thing on it that does anything. const runsAction = !! field.action; const asLink = ! runsAction && !! field.url && ! field.urlLabel; const Wrapper = asLink ? 'a' : 'div'; const linkProps = asLink ? { href: field.url, target: '_blank', rel: 'noopener noreferrer', } : {}; const running = runsAction && pendingAction === field.action; const destructive = 'destructive' === field.variant; return ( <>
{ field.label &&

{ field.label }

} { field.text && (

{ field.text }

) }
{ runsAction && field.urlLabel && ( ) } { ! runsAction && field.url && field.urlLabel && ( ) } { asLink && (
{ confirmDialog } ); } if ( 'notice' === field.type ) { return (

{ field.text } { field.url && ( <> { ' ' } { field.urlLabel || field.url } ) }

); } if ( 'action' === field.type ) { const action = field.action as string; const running = pendingAction === action; const ActionIcon = field.icon ? moduleIcon( field.icon ) : null; const destructive = 'destructive' === field.variant; return ( <> { confirmDialog } ); } // Every remaining type is value-bound, and the PHP normalizer guarantees a // key for those — this only satisfies the optional type. const key = field.key; if ( ! key ) { return null; } const value = values[ key ]; if ( 'status' === field.type ) { const tone = ( field.toneKey ? values[ field.toneKey ] : '' ) as PanelTone; return (
{ field.label && (

{ field.label }

) }

{ 'string' === typeof value ? value : '—' }

{ field.help && (

{ field.help }

) }
); } if ( 'toggle' === field.type ) { return ( setValue( key, checked ) } /> ); } if ( 'repeater' === field.type ) { return ( setValue( key, next ) } /> ); } if ( 'select' === field.type ) { const select = ( setValue( key, next ) } /> ); if ( ! field.icon ) { return select; } // An icon says what the choice is *about* at a glance — the two // redirects on the account screen read as a pair of doors rather than // as two similar dropdowns. Decorative, so the tile is hidden from // assistive tech and the label still carries the meaning. const SelectIcon = moduleIcon( field.icon ); return (
{ select }
); } // A credential: same control as `text`, but masked until asked for, so a // stored secret isn't legible over a shoulder or in a screen share. if ( 'secret' === field.type ) { return ( setValue( key, next ) } /> ); } if ( 'text' === field.type ) { const input = ( setValue( key, next ) } /> ); if ( ! field.icon ) { return input; } // A leading tile, same as a `copy` field's — for an input whose subject // is worth a glyph (a licence key beside a key icon). const TextIcon = moduleIcon( field.icon ); return (
{ input }
); } if ( 'textarea' === field.type ) { return ( setValue( key, next ) } /> ); } // checkboxes — a multi-select stored as an array of the checked values. const selected = Array.isArray( value ) ? ( value as string[] ) : []; // `chips` gives each box its own bordered tile and flows them across the // card, for a list that reads as a set of things to pick rather than as a // column of unrelated switches. const chips = 'chips' === field.layout; return (

{ field.label }

{ field.help && (

{ field.help }

) }
{ ( field.options ?? [] ).map( ( option ) => ( setValue( key, checked ? [ ...selected, option.value ] : selected.filter( ( item ) => item !== option.value ) ) } /> ) ) }
); } /** * The screen-leading state card: what the thing this surface configures is * currently *doing*, before any of the controls that change it. * * Distinct from `StatusCard` (a run of `status` rows) in weight rather than in * kind — that one is a strip of small facts, this one is a single answer the * whole screen hangs off, tinted by its tone and carrying its own actions. The * licence screen is what it was built for: "Active" or "No license" is the first * thing to know, and every other card on the page only makes sense after it. * * @param root0 * @param root0.field * @param root0.values * @param root0.actions */ function StateCard( { field, values, actions, }: { field: PanelField; values: Record< string, unknown >; actions: JSX.Element[]; } ) { const text = field.key ? values[ field.key ] : ''; const tone = ( field.toneKey ? values[ field.toneKey ] : '' ) as PanelTone; const help = field.helpKey ? values[ field.helpKey ] : ''; // Success gets a tick, anything unresolved gets the descriptor's own glyph // — a key on the licence screen, which reads as "not yet" without the alarm // an error icon would raise over a site that simply hasn't been set up. const unresolved = 'error' === tone || 'warning' === tone; let Icon = moduleIcon( field.icon ); if ( 'success' === tone ) { Icon = CircleCheck; } else if ( unresolved && ! field.icon ) { Icon = CircleAlert; } return (
{ field.eyebrow && (

{ field.eyebrow }

) }

{ 'string' === typeof text ? text : '—' }

{ 'string' === typeof help && '' !== help && (

{ help }

) }
{ actions.length > 0 && (
{ actions }
) } { /* A watermark of the card's own glyph, standing in for the illustration this card is shaped around. Decorative only. */ }
); } /** * One row of a `detaillist`. * * @param root0 * @param root0.row * @param root0.values */ function DetailRow( { row, values, }: { row: PanelDetailRow; values: Record< string, unknown >; } ) { const raw = values[ row.key ]; const value = 'string' === typeof raw ? raw : ''; const tone = ( row.toneKey ? values[ row.toneKey ] : '' ) as PanelTone; const [ copied, setCopied ] = useState( false ); const timer = useRef< ReturnType< typeof setTimeout > >(); useEffect( () => () => clearTimeout( timer.current ), [] ); const copy = () => { navigator.clipboard?.writeText( value ).then( () => { setCopied( true ); clearTimeout( timer.current ); timer.current = setTimeout( () => setCopied( false ), 2000 ); }, () => {} ); }; return (
{ row.label }
{ value || '—' } { row.copy && '' !== value && (
); } /** * A `copy` field: one string the reader has to get *out* of the panel and into * somewhere else — an OAuth redirect URI pasted into a provider's console, the * case it was built for. A notice with the URL in its body technically says the * same thing, but leaves the reader hand-selecting a long string out of a * paragraph and hoping they caught the whole of it. * * The value is static descriptor copy, not a setting, so nothing here saves. * * @param root0 * @param root0.field * @param root0.text */ function CopyCard( { field, text }: { field: PanelField; text: string } ) { const [ copied, setCopied ] = useState( false ); const timer = useRef< ReturnType< typeof setTimeout > >(); const Icon = field.icon ? moduleIcon( field.icon ) : Copy; // `field` sits in a column beside other controls, so it drops the card // surface and the button's text, keeping the label/value/help stack that // makes it read as one more labelled control. const inline = 'field' === field.layout; useEffect( () => () => clearTimeout( timer.current ), [] ); // `navigator.clipboard` needs a secure context — plain-http local installs // are common enough that the button has to stay useful without it, so a // failure selects the text instead and lets the reader press ctrl-C. const value = useRef< HTMLInputElement >( null ); const select = () => { value.current?.select(); }; const copy = () => { const done = () => { setCopied( true ); clearTimeout( timer.current ); timer.current = setTimeout( () => setCopied( false ), 2000 ); }; if ( ! navigator.clipboard ) { select(); return; } navigator.clipboard.writeText( text ).then( done, select ); }; // The value sits in a read-only input rather than loose text: it reads as // something to take away, it can be selected and copied by hand when the // clipboard API isn't available, and it can't be edited into something that // no longer matches what the callback actually is. const button = ( ); const input = ( event.target.select() } aria-label={ field.label } /> ); if ( inline ) { return (
{ field.label && (

{ field.label }

) }
{ input } { button }
{ field.help && (

{ field.help }

) }
); } return (
{ field.label && (

{ field.label }

) }
{ input } { button }
{ field.help && (

{ field.help }

) }
); } /** * A `secret` field — a `text` control that renders its value masked, with a * button to reveal it. Only presentation: the value travels and saves exactly * as a `text` field's does, and a surface that wants a secret write-only has to * arrange that in its own REST route. * * @param root0 * @param root0.field * @param root0.value * @param root0.disabled * @param root0.onChange */ function SecretField( { field, value, disabled, onChange, }: { field: PanelField; value: string; disabled: boolean; onChange: ( next: string ) => void; } ) { const [ shown, setShown ] = useState( false ); // Help text renders as a sibling rather than through the control's own // `help` prop, the same way `aureon-panel-inline` handles it: the reveal // button is positioned against the box below, and help inside that box // would make it taller than the input the button has to sit on. return (
{ field.help && (

{ field.help }

) }
); } // Tones are a closed set on the PHP side, so this only has to map them. const toneClass = ( tone?: PanelTone ): string => tone ? ` is-tone-${ tone }` : '';