/** * Footer section — multi-template visual footer builder. * * The footer edition of the header's builder (see the header section), split * across the same two surfaces sharing one DndContext: * * - The controls pane holds two tabs: **Elements** (a searchable card grid * to drag from) and **Settings** (display conditions). * - The rows canvas docks under the Customizer preview (see * ../header/builder-dock.tsx), with Desktop/Tablet/Mobile tabs that follow — * and drive — core's own preview device, plus the template switcher and * layout presets. * * The footer has no off-canvas drawer and no sticky rows. Element settings are * global and shared with the header (`header.elements.*`). * * Placement writes to `footer.templates..rows` / `.rows_tablet` / * `.rows_mobile`; display rules to `footer.templates..display`. The PHP * Footer_Builder resolves the first enabled matching template per request. */ import { createPortal, useEffect, useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { DndContext, DragOverlay, closestCenter, pointerWithin, type CollisionDetection, type DragEndEvent, type DragOverEvent, type DragStartEvent, } from '../../dnd'; import { LayoutGrid, SlidersHorizontal } from '../../icons'; import Tabs, { TabPanel } from '../../controls/tabs'; import ConfirmDialog from '../../confirm-dialog'; import TemplateModal from '../header/template-modal'; import TemplateSwitcher from '../header/template-switcher'; import DisplayConditions from '../header/display-conditions'; import ElementGrid from '../header/element-grid'; import ElementEditor from '../header/element-editor'; import BuilderDock, { MAIN_ROW } from '../header/builder-dock'; import { DockChip } from '../header/dock-row'; import { useCanvasDnd, PALETTE } from '../header/use-canvas-dnd'; import { useRowOps } from '../header/use-row-ops'; import { FOOTER_TEMPLATES } from './footer-templates'; import { columnsToContainers, mergeElementsIntoRows, newRow, newColumn, padToSlots, rowsHaveElements, type DeviceKey, type HeaderRow, } from '../header/canvas-model'; import { followSectionInPreview, onEditRequest, isCustomizerHost, setPreviewedDevice, watchPreviewedDevice, watchSectionExpanded, } from '../../customize-bridge'; import type { HeaderTemplate } from '../header/template-list'; import type { WorkingSettings, SetPath } from '../../types'; interface Def { id: string; label: string; } interface FooterSchema { label?: string; elements?: Def[]; cartIcons?: Def[]; widgetAreas?: Def[]; woo?: boolean; menus?: { id: number; label: string }[]; limits?: { maxRows?: number; maxCols?: number; maxTemplates?: number }; postTypes?: { slug: string; label: string }[]; } interface FooterData { templates?: HeaderTemplate[]; } interface HeaderElementsData { elements?: Record< string, Record< string, unknown > >; } const DEVICE_LABELS: Record< DeviceKey, string > = { desktop: __( 'Desktop', 'aureon' ), tablet: __( 'Tablet', 'aureon' ), mobile: __( 'Mobile', 'aureon' ), }; const SECTION_ID = 'aureon_footer'; const TABS = [ { key: 'elements', label: __( 'Elements', 'aureon' ), icon: LayoutGrid }, { key: 'settings', label: __( 'Settings', 'aureon' ), icon: SlidersHorizontal, }, ]; const isDevice = ( value: string ): value is DeviceKey => 'desktop' === value || 'tablet' === value || 'mobile' === value; /** * Seed layout applied when a footer device's custom layout is first enabled: * one row, logo left, footer menu right (no Toggle — footers don't collapse). */ const seededFooterRows = (): HeaderRow[] => { const row = newRow(); return [ { ...row, columns: [ { ...newColumn( 'left' ), elements: [ 'logo' ] }, { ...newColumn( 'right' ), elements: [ 'link-list' ] }, ], }, ]; }; interface FooterSectionProps { schema?: FooterSchema; /** Registered defaults — the footer's own is what Reset restores. */ defaults?: Partial< WorkingSettings >; working: WorkingSettings; setPath: SetPath; } export default function FooterSection( { schema, defaults, working, setPath, }: FooterSectionProps ) { 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 ); 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, 'footer' ), [] ); // A pencil on a footer element in the preview opens that element. useEffect( () => onEditRequest( SECTION_ID, setEditing ), [] ); const changeDevice = ( next: DeviceKey ) => { setDevice( next ); setPreviewedDevice( next ); }; const footer = ( working?.footer ?? {} ) as FooterData; // Element settings are global and live under the header section. const headerData = ( working?.header ?? {} ) as HeaderElementsData; const templates = ( footer.templates ?? [] ) as HeaderTemplate[]; const maxTemplates = schema?.limits?.maxTemplates ?? 10; const maxCols = schema?.limits?.maxCols ?? 6; // Live global palette — lets color fields show inherited defaults. 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 = `footer.templates.${ selected }`; let rowsKey: 'rows' | 'rows_tablet' | 'rows_mobile' = 'rows'; if ( 'tablet' === device ) { rowsKey = 'rows_tablet'; } else if ( 'mobile' === device ) { rowsKey = 'rows_mobile'; } 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 ); const elements: Def[] = schema?.elements ?? []; const labelFor = ( id: string ) => elements.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 ) ), } ); if ( ! schema ) { return null; } const paletteIds = dnd.items[ PALETTE ] ?? []; const available = elements.filter( ( el ) => paletteIds.includes( el.id ) ); const rowIds = new Set( rows.map( ( r ) => r.id ) ); // Toggle a device's custom layout. On enable, seed a usable layout when // the device has none yet, so the canvas isn't blank. const setDeviceCustom = ( enabled: boolean ) => { if ( 'desktop' === device ) { return; } setPath( `${ tplPath }.${ device }_custom`, enabled ); if ( enabled && ! rowsHaveElements( savedRows ) ) { setPath( rowsPath, seededFooterRows() ); } }; 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 ); } }; // Reset: back to the registered default footer. Only layout — display // conditions and the template's name stay, and element settings are shared // with the header, so they aren't this template's to reset. const defaultFooter = ( defaults?.footer ?? {} ) as unknown as HeaderTemplate; const resetDevice = () => { if ( 'desktop' === device ) { setPath( rowsPath, structuredClone( defaultFooter.rows ?? [] ) ); return; } setPath( `${ tplPath }.${ device }_custom`, false ); setPath( rowsPath, [] ); }; const resetFooter = () => { setPath( `${ tplPath }.rows`, structuredClone( defaultFooter.rows ?? [] ) ); setPath( `${ tplPath }.rows_tablet`, [] ); setPath( `${ tplPath }.tablet_custom`, false ); setPath( `${ tplPath }.rows_mobile`, [] ); setPath( `${ tplPath }.mobile_custom`, false ); }; let resetDescription: string = __( 'Every device layout goes back to the default footer. The name, display conditions and element settings stay.', 'aureon' ); if ( 'device' === pendingReset ) { resetDescription = 'desktop' === device ? __( 'The desktop rows go back to the default footer.', '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; elements with whatever is under the pointer // (the palette and the dock sit far apart, so "closest" would misfire). const collisionDetection: CollisionDetection = ( args ) => { const isRow = rowIds.has( String( args.active.id ) ); const scoped = { ...args, droppableContainers: args.droppableContainers.filter( ( c ) => isRow === rowIds.has( String( c.id ) ) ), }; return isRow ? closestCenter( scoped ) : pointerWithin( scoped ); }; const onDragStart = ( event: DragStartEvent ) => { if ( rowIds.has( String( event.active.id ) ) ) { setDraggingRow( true ); } else { dnd.onDragStart( event ); } }; const onDragOver = ( event: DragOverEvent ) => { if ( ! draggingRow ) { dnd.onDragOver( event ); } }; const onDragEnd = ( event: DragEndEvent ) => { if ( draggingRow ) { setDraggingRow( false ); if ( event.over ) { ops.moveRow( String( event.active.id ), String( event.over.id ) ); } return; } dnd.onDragEnd( event ); }; const onDragCancel = () => { setDraggingRow( false ); dnd.cancel(); }; const overlay = ( { dnd.activeId ? ( undefined } overlay /> ) : null } ); return (
} 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={ setEditing } onRemove={ dnd.removeItem } ops={ ops } maxCols={ maxCols } dragging={ !! dnd.activeId } /> { pendingReset && ( { if ( 'template' === pendingReset ) { resetFooter(); } else { resetDevice(); } setPendingReset( null ); } } onCancel={ () => setPendingReset( null ) } /> ) } { /* 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 ) } /> ) }
); }