/** * Header section — multi-template visual header builder. * * Split across two surfaces that share one DndContext: * * - The controls pane holds two tabs: **Elements** (a searchable card grid * to drag from) and **Settings** (display conditions, sticky behaviour). * Rows, columns and elements are styled from their own gear in the dock. * - The rows canvas docks under the Customizer preview (see builder-dock.tsx), * with Desktop/Tablet/Mobile tabs that follow — and drive — core's own * preview device, plus the template switcher and layout presets. On * tablet/mobile the off-canvas drawer sits beside the rows (drawer-card.tsx). * * Placement writes to `header.templates..rows` / `.rows_tablet` / * `.rows_mobile` / `.mobile`; display rules to `header.templates..display`; * global element settings to `header.elements.*`. The PHP Header_Builder * resolves the first enabled matching template per request. */ import { createPortal, useEffect, useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { Button, FormToggle } from '@wordpress/components'; import { DndContext, DragOverlay, closestCenter, pointerWithin, type CollisionDetection, type DragEndEvent, type DragOverEvent, type DragStartEvent, } from '../../dnd'; import { arrayMove } from '../../dnd'; import Tabs, { TabPanel } from '../../controls/tabs'; import InfoNote from '../../controls/info-note'; import ConfirmDialog from '../../confirm-dialog'; import TemplateModal from './template-modal'; import TemplateSwitcher from './template-switcher'; import DisplayConditions from './display-conditions'; import ElementGrid from './element-grid'; import ElementEditor from './element-editor'; import BuilderModal from './builder-modal'; import { LayoutGrid, PanelRight, SlidersHorizontal } from '../../icons'; import BuilderDock, { MAIN_ROW } from './builder-dock'; import { DockChip } from './dock-row'; import DrawerCard, { DrawerTab, DrawerSettingsForm, DRAWER_PREFIX, fromDrawerId, isDrawerId, type DrawerSettings, } from './drawer-card'; import { useCanvasDnd, PALETTE } from './use-canvas-dnd'; import { useRowOps } from './use-row-ops'; import { columnsToContainers, mergeElementsIntoRows, seededDeviceRows, rowsHaveElements, padToSlots, type DeviceKey, type HeaderRow, } from './canvas-model'; import { followSectionInPreview, onEditRequest, isCustomizerHost, setPreviewedDevice, watchPreviewedDevice, watchSectionExpanded, } from '../../customize-bridge'; import type { HeaderTemplate } from './template-list'; import type { WorkingSettings, SetPath } from '../../types'; interface Def { id: string; label: string; } interface HeaderSchema { label?: string; elements?: Def[]; toggleElement?: Def; toggleIcons?: Def[]; cartIcons?: Def[]; accountIcons?: Def[]; widgetAreas?: Def[]; woo?: boolean; menus?: { id: number; label: string }[]; limits?: { maxRows?: number; maxCols?: number; maxTemplates?: number }; postTypes?: { slug: string; label: string }[]; } interface HeaderData { sticky_shrink?: boolean; sticky_reveal?: boolean; elements?: Record< string, Record< string, unknown > >; templates?: HeaderTemplate[]; } const DEVICE_LABELS: Record< DeviceKey, string > = { desktop: __( 'Desktop', 'aureon' ), tablet: __( 'Tablet', 'aureon' ), mobile: __( 'Mobile', 'aureon' ), }; const SECTION_ID = 'aureon_header'; const TABS = [ { key: 'elements', label: __( 'Elements', 'aureon' ), icon: LayoutGrid }, { key: 'settings', label: __( 'Settings', 'aureon' ), icon: SlidersHorizontal, }, ]; /** Placeable only in the off-canvas drawer. */ const DRAWER_ONLY = 'offcanvas-menu'; const isDevice = ( value: string ): value is DeviceKey => 'desktop' === value || 'tablet' === value || 'mobile' === value; interface HeaderSectionProps { schema?: HeaderSchema; /** Registered defaults — the header's own is what Reset restores. */ defaults?: Partial< WorkingSettings >; working: WorkingSettings; setPath: SetPath; } export default function HeaderSection( { schema = {}, defaults, working, setPath, }: HeaderSectionProps ) { const [ tab, setTab ] = useState( 'elements' ); const [ device, setDevice ] = useState< DeviceKey >( 'desktop' ); const [ presetsOpen, setPresetsOpen ] = useState( false ); const [ selectedRaw, setSelected ] = useState( 0 ); const [ editing, setEditing ] = useState< string | null >( null ); const [ draggingRow, setDraggingRow ] = useState( false ); // A drawer item (`oc:`) being dragged — handled here, not by useCanvasDnd. const [ draggingDrawer, setDraggingDrawer ] = useState< string | null >( null ); const [ drawerOpen, setDrawerOpen ] = useState( false ); // The Toggle chip and the drawer card it opens highlight together. const [ toggleHot, setToggleHot ] = useState( false ); const [ pendingReset, setPendingReset ] = useState< 'device' | 'template' | null >( null ); // Off the Customizer there's no section to watch — the dock renders inline. const [ expanded, setExpanded ] = useState( ! isCustomizerHost() ); // Follow core's preview device, so the dock's tabs and the footer's // device buttons are one control. useEffect( () => watchPreviewedDevice( ( next ) => { if ( isDevice( next ) ) { setDevice( next ); } } ), [] ); useEffect( () => watchSectionExpanded( SECTION_ID, setExpanded ), [] ); useEffect( () => followSectionInPreview( SECTION_ID, 'top' ), [] ); // A pencil on a header element in the preview opens that element. useEffect( () => onEditRequest( SECTION_ID, ( id ) => { setTab( 'elements' ); if ( 'toggle' === id ) { setDrawerOpen( true ); } else { setEditing( id ); } } ), [] ); const changeDevice = ( next: DeviceKey ) => { setDevice( next ); setPreviewedDevice( next ); }; const header = ( working?.header ?? {} ) as HeaderData; const templates = ( header.templates ?? [] ) as HeaderTemplate[]; const maxTemplates = schema.limits?.maxTemplates ?? 10; const maxCols = schema.limits?.maxCols ?? 6; // The live global palette — passed to element editors so menu color fields // can show the color they'd inherit as their default swatch. const palette = working?.design?.colors ?? {}; // The server always hydrates at least one template; guard defensively. const selected = templates.length ? Math.min( selectedRaw, templates.length - 1 ) : 0; const activeTemplate = ( templates[ selected ] ?? {} ) as HeaderTemplate; const tplPath = `header.templates.${ selected }`; const rowsKeyFor = ( d: DeviceKey ) => { if ( 'tablet' === d ) { return 'rows_tablet' as const; } return 'mobile' === d ? ( 'rows_mobile' as const ) : ( 'rows' as const ); }; const rowsKey = rowsKeyFor( device ); const rowsPath = `${ tplPath }.${ rowsKey }`; const savedRows = ( activeTemplate[ rowsKey ] ?? [] ) as HeaderRow[]; const rows = padToSlots( savedRows ); const customized = 'desktop' === device || ( 'tablet' === device ? !! activeTemplate.tablet_custom : !! activeTemplate.mobile_custom ); // What the rows (and the Elements grid) offer. The Toggle only on the // tablet/mobile canvases (desktop collapses to an automatic toggle); the // Off-Canvas Menu never — it's the drawer's own menu, offered by the drawer // card alone. const allElements: Def[] = [ ...( schema.elements ?? [] ), ...( schema.toggleElement ? [ schema.toggleElement ] : [] ), ]; const elements = allElements.filter( ( el ) => DRAWER_ONLY !== el.id && ( 'toggle' !== el.id || 'desktop' !== device ) ); // Labels from the full list, so an element placed before these limits // existed still reads right. const labelFor = ( id: string ) => allElements.find( ( el ) => el.id === id )?.label ?? id; const ops = useRowOps( { rows, rowsPath, setPath, maxRows: rows.length, maxCols, } ); const dnd = useCanvasDnd( { containers: columnsToContainers( rows ), allIds: elements.map( ( el ) => el.id ), onCommit: ( zones ) => setPath( rowsPath, mergeElementsIntoRows( rows, zones ) ), } ); const paletteIds = dnd.items[ PALETTE ] ?? []; const available = elements.filter( ( el ) => paletteIds.includes( el.id ) ); const rowIds = new Set( rows.map( ( r ) => r.id ) ); // Off-canvas drawer: the template's `mobile` list, and its options (kept on // the Toggle element, since the button and its panel are edited together). const drawerPath = `${ tplPath }.mobile`; const drawerItems = ( activeTemplate.mobile ?? [] ) as string[]; const drawerSettings = ( header.elements?.toggle ?? {} ) as DrawerSettings; const drawerElements = ( schema.elements ?? [] ).filter( ( el ) => ! drawerItems.includes( el.id ) ); const setDrawer = ( next: string[] ) => setPath( drawerPath, next ); const addToDrawer = ( id: string, before?: string ) => { if ( drawerItems.includes( id ) ) { return; } const next = [ ...drawerItems ]; const at = before ? next.indexOf( before ) : -1; next.splice( -1 === at ? next.length : at, 0, id ); setDrawer( next ); }; // Toggle "Custom layout" for a device. On enable, seed a Logo + Toggle // layout when the device has no usable layout yet, so the canvas is // immediately useful instead of blank. const setDeviceCustom = ( enabled: boolean ) => { if ( 'desktop' === device ) { return; } setPath( `${ tplPath }.${ device }_custom`, enabled ); if ( enabled && ! rowsHaveElements( savedRows ) ) { setPath( rowsPath, seededDeviceRows() ); } }; const addToMainRow = ( id: string ) => { if ( ! customized ) { setDeviceCustom( true ); return; } const main = rows[ MAIN_ROW ]; if ( main?.columns.length ) { ops.addElement( main.id, main.columns[ 0 ].id, id ); } }; const editElement = ( id: string ) => { if ( 'toggle' === id ) { setDrawerOpen( true ); } else { setEditing( id ); } }; // Reset: back to the registered default header. Only layout — display // conditions and the template's name stay, and element settings are shared // by every template, so they aren't this template's to reset. const defaultHeader = ( defaults?.header ?? {} ) as unknown as HeaderTemplate; const resetDevice = () => { if ( 'desktop' === device ) { setPath( rowsPath, structuredClone( defaultHeader.rows ?? [] ) ); return; } setPath( `${ tplPath }.${ device }_custom`, false ); setPath( rowsPath, [] ); }; const resetHeader = () => { setPath( `${ tplPath }.rows`, structuredClone( defaultHeader.rows ?? [] ) ); setPath( `${ tplPath }.rows_tablet`, [] ); setPath( `${ tplPath }.tablet_custom`, false ); setPath( `${ tplPath }.rows_mobile`, [] ); setPath( `${ tplPath }.mobile_custom`, false ); setPath( drawerPath, [ ...( defaultHeader.mobile ?? [] ) ] ); }; let resetDescription: string = __( 'Every device layout and the off-canvas panel go back to the default header. The name, display conditions and element settings stay.', 'aureon' ); if ( 'device' === pendingReset ) { resetDescription = 'desktop' === device ? __( 'The desktop rows go back to the default header.', 'aureon' ) : __( 'This device drops its own layout and follows the larger one again.', 'aureon' ); } const applyPreset = ( next: HeaderRow[] ) => { if ( 'desktop' !== device ) { setPath( `${ tplPath }.${ device }_custom`, true ); } setPath( rowsPath, next ); }; // Rows only collide with rows. Drawer items only with the drawer and the // palette. Header elements with zones, chips, the palette and the drawer — // and only with the one actually under the pointer, since the palette and // the dock sit far apart on screen and "closest" would snap a drag over the // preview to whichever happened to be nearer. const collisionDetection: CollisionDetection = ( args ) => { const activeId = String( args.active.id ); const isRow = rowIds.has( activeId ); const scoped = { ...args, droppableContainers: args.droppableContainers.filter( ( c ) => { const id = String( c.id ); if ( isRow || rowIds.has( id ) ) { return isRow && rowIds.has( id ); } if ( activeId.startsWith( DRAWER_PREFIX ) ) { return ( isDrawerId( id ) || PALETTE === id || paletteIds.includes( id ) ); } return true; } ), }; return isRow ? closestCenter( scoped ) : pointerWithin( scoped ); }; const overId = ( event: DragOverEvent | DragEndEvent ) => event.over ? String( event.over.id ) : ''; const onDragStart = ( event: DragStartEvent ) => { const id = String( event.active.id ); if ( rowIds.has( id ) ) { setDraggingRow( true ); } else if ( id.startsWith( DRAWER_PREFIX ) ) { setDraggingDrawer( id ); } else { dnd.onDragStart( event ); } }; const onDragOver = ( event: DragOverEvent ) => { if ( draggingRow || draggingDrawer ) { return; } // Over the drawer, a header element is being copied rather than moved: // put back anything it had shifted on its way across the rows. if ( isDrawerId( overId( event ) ) ) { dnd.reset(); return; } dnd.onDragOver( event ); }; const onDragEnd = ( event: DragEndEvent ) => { const over = overId( event ); if ( draggingRow ) { setDraggingRow( false ); if ( over ) { ops.moveRow( String( event.active.id ), over ); } return; } if ( draggingDrawer ) { setDraggingDrawer( null ); const id = fromDrawerId( draggingDrawer ); if ( over.startsWith( DRAWER_PREFIX ) ) { const from = drawerItems.indexOf( id ); const to = drawerItems.indexOf( fromDrawerId( over ) ); if ( -1 !== from && -1 !== to && from !== to ) { setDrawer( arrayMove( drawerItems, from, to ) ); } } else if ( over && ! isDrawerId( over ) ) { // Dropped on the Elements grid: out of the drawer. setDrawer( drawerItems.filter( ( item ) => item !== id ) ); } return; } if ( isDrawerId( over ) ) { const id = String( event.active.id ); dnd.cancel(); addToDrawer( id, over.startsWith( DRAWER_PREFIX ) ? fromDrawerId( over ) : undefined ); return; } dnd.onDragEnd( event ); }; const onDragCancel = () => { setDraggingRow( false ); setDraggingDrawer( null ); dnd.cancel(); }; const overlay = ( { dnd.activeId || draggingDrawer ? ( undefined } overlay /> ) : null } ); return (
changeDevice( 'mobile' ) } > { __( 'The Menu Toggle is available on tablet and mobile. It opens the off-canvas panel, shown beside the header rows on those tabs — drag elements into it like any row.', 'aureon' ) }

{ __( 'Sticky behaviour', 'aureon' ) }

{ __( 'Mark individual rows as sticky or transparent in each row’s settings (the gear beside its name in the builder). These options apply to how sticky rows behave on scroll.', 'aureon' ) }

setPath( 'header.sticky_shrink', value ) } /> setPath( 'header.sticky_reveal', value ) } />
} onOpenPresets={ () => setPresetsOpen( true ) } onReset={ setPendingReset } customized={ customized } onCustomize={ setDeviceCustom } inheritsFrom={ 'mobile' === device && activeTemplate.tablet_custom ? __( 'tablet', 'aureon' ) : __( 'desktop', 'aureon' ) } rows={ rows } items={ dnd.items } available={ available } labelFor={ labelFor } onEdit={ editElement } onRemove={ dnd.removeItem } ops={ ops } maxCols={ maxCols } dragging={ !! dnd.activeId } toggleHot={ toggleHot } onToggleHover={ setToggleHot } drawerSide={ 'left' === drawerSettings.side && 'fullscreen' !== drawerSettings.style ? 'left' : 'right' } drawer={ 'desktop' === device ? ( changeDevice( 'mobile' ) } /> ) : ( addToDrawer( id ) } onRemove={ ( id ) => setDrawer( drawerItems.filter( ( item ) => item !== id ) ) } onEdit={ setEditing } onOpenSettings={ () => setDrawerOpen( true ) } hot={ toggleHot } onHover={ setToggleHot } /> ) } /> { pendingReset && ( { if ( 'template' === pendingReset ) { resetHeader(); } else { resetDevice(); } setPendingReset( null ); } } onCancel={ () => setPendingReset( null ) } /> ) } { drawerOpen && ( setDrawerOpen( false ) } footerEnd={ } > setPath( `header.elements.toggle.${ key }`, value ) } /> ) } { /* On : the controls pane slides with transforms, which would otherwise trap the fixed-position overlay inside it. */ } { createPortal( overlay, document.body ) } { editing && ( setEditing( null ) } /> ) } { presetsOpen && ( setPresetsOpen( false ) } /> ) }
); } interface StickyOptionProps { id: string; label: string; desc: string; checked: boolean; onChange: ( value: boolean ) => void; } function StickyOption( { id, label, desc, checked, onChange, }: StickyOptionProps ) { return ( ); }