/** * Mega Menu settings section. * * One page, three groups — Default style, Size & spacing (label/control rows * with a per-style Reset), Available styles (thumbnail cards, searchable, the * default style highlighted) — rather than tabs: the default-style select only lists the styles toggled on * below it, so splitting them across tabs hid the one from the other. * * The configuration surface for the Mega Menu feature module — shown in the * "Modules" sidebar group only while the module is enabled (see the Modules page * and the `navigation.mega.module_enabled` gate in App). Hosts the per-style * on/off toggles and the default-style select; the master feature toggle lives * on the Modules page, not here. * * Styles come from window.aureonAdmin.megaModules, so a newly registered style * (core or child theme) appears with no React edit. Toggles bind to * `navigation.mega.enabled.` and the select to `navigation.mega.default_style` * via the same working/setPath contract every section uses. */ import { __, sprintf } from '@wordpress/i18n'; import { useEffect, useState } from '@wordpress/element'; import { FormToggle, SearchControl, SelectControl, } from '@wordpress/components'; import { Menu, RotateCcw } from 'lucide-react'; import UnitField from '../controls/unit-field'; import ColorField from '../controls/color-field'; import ConfirmDialog from '../confirm-dialog'; import { getPath } from '../tree-utils'; import type { MegaDimensions, MegaModule, MegaSettings, SetPath, WorkingSettings, } from '../types'; import SectionIntro from '../controls/section-intro'; type Unit = { value: string; label: string }; type DimField = { key: keyof MegaDimensions; label: string; units: Unit[]; }; // Unit vocabularies per field kind. Length fields keep the common CSS units; // width/height add viewport units so a panel can be sized to the screen. No // `%`: a panel is positioned against its trigger menu item, so a percentage // resolves against that item's few-dozen pixels (100% → a sliver). const LENGTH_UNITS: Unit[] = [ { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, { value: 'em', label: 'em' }, ]; const WIDTH_UNITS: Unit[] = [ ...LENGTH_UNITS, { value: 'vw', label: 'vw' } ]; const HEIGHT_UNITS: Unit[] = [ { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, { value: 'vh', label: 'vh' }, ]; // Per-variant size overrides shared by every boxed variant, in display order. // `key` maps to navigation.mega.dimensions..; the SCSS reads // width/padding/gap as --aureon-mega-* custom properties and max_height directly // (with internal scroll). const DIMENSION_FIELDS: DimField[] = [ { key: 'width', label: __( 'Width', 'aureon' ), units: WIDTH_UNITS }, { key: 'max_height', label: __( 'Max height', 'aureon' ), units: HEIGHT_UNITS, }, { key: 'padding', label: __( 'Padding', 'aureon' ), units: LENGTH_UNITS }, { key: 'gap', label: __( 'Item spacing', 'aureon' ), units: LENGTH_UNITS }, ]; // Extra grid-gap overrides shown only for variants that lay their sections out // in columns (they carry the `columns` support flag). Column gap is the space // between columns, row gap the space between rows when sections wrap. const COLUMN_GAP_FIELDS: DimField[] = [ { key: 'col_gap', label: __( 'Column gap', 'aureon' ), units: LENGTH_UNITS, }, { key: 'row_gap', label: __( 'Row gap', 'aureon' ), units: LENGTH_UNITS }, ]; interface MegaMenuSectionProps { modules: MegaModule[]; working: WorkingSettings; setPath: SetPath; /** * Customizer host only: told which style the page is currently about (the * one being sized, else the default), so the preview can hold a panel of * that style open — mega panels otherwise only show on hover. */ onPreviewStyle?: ( id: string ) => void; } export default function MegaMenuSection( { modules, working, setPath, onPreviewStyle, }: MegaMenuSectionProps ) { const mega: MegaSettings = ( working?.navigation ?.mega as MegaSettings ) ?? { default_style: 'stacked', enabled: {}, }; const enabledMap = mega.enabled ?? {}; const isEnabled = ( id: string ): boolean => !! enabledMap[ id ]; // Default-style options are only the enabled, non-Pro styles; the current // value is kept selectable even if it was just toggled off, so the select // never silently drops the saved choice mid-edit. const styleOptions = modules .filter( ( m ) => isEnabled( m.id ) || m.id === mega.default_style ) .map( ( m ) => ( { label: m.label, value: m.id } ) ); const hasEnabled = modules.some( ( m ) => isEnabled( m.id ) ); // Panel Style edits any enabled style (full-width ones hide only Width). const sizableModules = modules.filter( ( m ) => isEnabled( m.id ) ); const dimensions: Record< string, MegaDimensions > = mega.dimensions ?? {}; // Which variant's fields Panel Style is showing. Kept as an id so a // re-order or toggle can't leave a stale index; when the remembered variant // is toggled off it falls back to the default style, then the first // still-sizable one. const [ activeDimId, setActiveDimId ] = useState< string >( '' ); const activeModule = sizableModules.find( ( m ) => m.id === activeDimId ) ?? sizableModules.find( ( m ) => m.id === mega.default_style ) ?? sizableModules[ 0 ]; const [ search, setSearch ] = useState( '' ); const previewStyle = activeModule?.id ?? mega.default_style ?? ''; useEffect( () => { onPreviewStyle?.( previewStyle ); }, [ previewStyle, onPreviewStyle ] ); const query = search.trim().toLowerCase(); const visibleModules = query ? modules.filter( ( m ) => `${ m.label } ${ m.description }` .toLowerCase() .includes( query ) ) : modules; const activeDims = activeModule ? dimensions[ activeModule.id ] ?? {} : {}; const anyOverrides = Object.values( dimensions ).some( ( dims ) => Object.keys( dims ?? {} ).length > 0 ); const [ confirmReset, setConfirmReset ] = useState( false ); return (

{ __( 'Mega Menu', 'aureon' ) }

{ __( 'Dropdown panel styles for your menus.', 'aureon' ) }

{ __( 'Pick a style per menu item from Appearance → Menus.', 'aureon' ) }

{ 0 === modules.length ? (

{ __( 'No mega-menu styles are registered.', 'aureon' ) }

) : ( <>

{ __( 'Default Style', 'aureon' ) }

{ __( 'Used by a menu item set to “Inherit default”. Only enabled styles are listed.', 'aureon' ) }

setPath( 'navigation.mega.default_style', value ) } />
{ activeModule && (

{ __( 'Panel Style', 'aureon' ) }

{ __( 'Pick a style, then adjust its look and size. Empty fields use the style’s default; empty colors follow the header menu.', 'aureon' ) }

setConfirmReset( true ) } />
{ __( 'Style', 'aureon' ) } ( { label: m.label, value: m.id, } ) ) } onChange={ setActiveDimId } />
) }

{ __( 'Available Styles', 'aureon' ) }

{ __( 'Only enabled styles load their CSS.', 'aureon' ) }

{ visibleModules.map( ( module ) => ( ) ) } { 0 === visibleModules.length && (

{ __( 'No styles match your search.', 'aureon' ) }

) }
) } { confirmReset && activeModule && ( { setPath( 'navigation.mega.dimensions', {} ); setConfirmReset( false ); } } confirmLabel={ sprintf( /* translators: %s: mega menu style name. */ __( 'Reset “%s”', 'aureon' ), activeModule.label ) } onConfirm={ () => { setPath( `navigation.mega.dimensions.${ activeModule.id }`, {} ); setConfirmReset( false ); } } onCancel={ () => setConfirmReset( false ) } /> ) }
); } // The selected variant's look (colors, radius, shadow, header gap) and size // fields plus, for columned variants, the column/row gap sub-group. Split out so each field's onChange closes over the // one module without re-deriving it in a map, and so the panel remounts (via // key) when the selected style changes. function DimensionPanel( { module, dims, inherited, setPath, }: { module: MegaModule; dims: MegaDimensions; inherited: MegaDimensions; setPath: SetPath; } ) { // Live token/palette values win over the module's static defaults, so a // field shows what the panel actually renders with today. const defs: MegaDimensions = { ...module.defaults, ...inherited }; const hasColumns = ( module.supports ?? [] ).includes( 'columns' ); const set = ( key: keyof MegaDimensions ) => ( value: string ) => setPath( `navigation.mega.dimensions.${ module.id }.${ key }`, value ); const row = ( key: string, label: string, control: JSX.Element ) => (
{ label } { control }
); const renderField = ( field: DimField ) => row( field.key, field.label, ); // Full-width variants always span the header, so width doesn't apply. const sizeFields = module.full_width ? DIMENSION_FIELDS.filter( ( field ) => 'width' !== field.key ) : DIMENSION_FIELDS; return ( <>
{ __( 'Look', 'aureon' ) } { row( 'bg', __( 'Background', 'aureon' ), ) } { row( 'text', __( 'Text', 'aureon' ), ) } { row( 'hover_bg', __( 'Hover background', 'aureon' ), ) } { row( 'hover_text', __( 'Hover text', 'aureon' ), ) } { renderField( { key: 'radius', label: __( 'Corner radius', 'aureon' ), units: LENGTH_UNITS, } ) } { row( 'shadow', __( 'Shadow', 'aureon' ), ) } { renderField( { key: 'drop_gap', label: __( 'Gap from header', 'aureon' ), units: LENGTH_UNITS, } ) }
{ __( 'Size & spacing', 'aureon' ) } { sizeFields.map( renderField ) }
{ hasColumns && (
{ __( 'Column layout', 'aureon' ) } { COLUMN_GAP_FIELDS.map( renderField ) }
) } ); } // What each Panel Style field inherits when left empty, resolved from the // working settings so it tracks the site's own palette and tokens — mirrors the // SCSS fallbacks: colors come from the Primary Menu element's submenu colors, // else the palette (hover background is the 6% text tint over the background); // radius is `--aureon-radius-lg`, the header gap and boxed padding // `--aureon-space-sm`, and the full-width shell's padding/gaps `--aureon-space-md` // / `--aureon-space-lg`. function inheritedDefaults( working: WorkingSettings, module: MegaModule ): MegaDimensions { const colors = working?.design?.colors ?? {}; const space = working?.design?.spacing ?? {}; const radius = working?.design?.radius ?? {}; const menu = ( getPath( working, 'header.elements.primary-menu' ) ?? {} ) as Record< string, string >; const bg = menu.submenu_bg || colors.background || ''; const text = menu.submenu_text || colors.text || ''; const out: MegaDimensions = { bg, text, hover_bg: menu.submenu_hover_bg || mixHex( bg, text, 0.06 ), hover_text: menu.link_hover_color || colors.primary || '', radius: radius.lg, drop_gap: space.sm, padding: module.full_width ? space.md : space.sm, }; if ( module.full_width ) { out.row_gap = space.md; if ( 'full-width' === module.id ) { out.col_gap = space.lg; } } return Object.fromEntries( Object.entries( out ).filter( ( [ , v ] ) => !! v ) ) as MegaDimensions; } // `amount` of hex `b` blended over hex `a` (6-digit hex only; '' otherwise). function mixHex( a: string, b: string, amount: number ): string { const rgb = ( hex: string ) => /^#[0-9a-f]{6}$/i.test( hex ) ? [ 1, 3, 5 ].map( ( i ) => parseInt( hex.slice( i, i + 2 ), 16 ) ) : null; const from = rgb( a ); const to = rgb( b ); if ( ! from || ! to ) { return ''; } return ( '#' + from .map( ( c, i ) => Math.round( c + ( to[ i ] - c ) * amount ) .toString( 16 ) .padStart( 2, '0' ) ) .join( '' ) ); } // A stored value whose unit the field no longer offers (e.g. a `100%` width // saved before `%` was dropped) shows as empty rather than being relabelled // with the first unit — the server drops it too (see sanitize_length()). function usable( value: string, units: Unit[] ): string { const unit = /[a-z%]*$/i.exec( value.trim() )?.[ 0 ] ?? ''; return '' === unit || units.some( ( u ) => u.value === unit ) ? value : ''; } // Group-head reset: a plain bordered button. It stays visible (disabled when // no style has changes) and opens a confirm that asks whether to reset the // selected style or all of them. function ResetButton( { disabled, onClick, }: { disabled: boolean; onClick: () => void; } ) { return ( ); } const SHADOW_OPTIONS = [ { value: 'none', label: __( 'None', 'aureon' ) }, { value: 'sm', label: __( 'Soft', 'aureon' ) }, { value: 'md', label: __( 'Medium', 'aureon' ) }, { value: 'lg', label: __( 'Strong', 'aureon' ) }, ]; // Miniature of each built-in style's real panel, drawn in a 48×36 box: a // rounded card with bold title lines (`i`), grey description lines (`m`), // icon tiles / footer areas (`b`) and an accent link (`a`) — see the // `.aureon-mega-thumb` rules for the colors. Keyed by module id; a style // registered elsewhere (child theme, plugin) falls back to the simple list, // or to a dashed "add" frame when its id reads as a builder. type ThumbTone = 'i' | 'm' | 'b' | 'a'; function StyleThumb( { id }: { id: string } ) { const r = ( x: number, y: number, w: number, h: number, tone: ThumbTone, rx = h / 2 ) => ( ); // Boxed panel: rounded card with a 2px inset. const panel = ( ); // Edge-to-edge panel for the full-width styles: square sides, no gutter. const widePanel = ( ); // Icon tile + title + one description line, as in Stacked. const iconItem = ( y: number ) => ( { r( 7, y, 6, 6, 'b', 1.5 ) } { r( 16, y + 0.5, 12, 2, 'i' ) } { r( 16, y + 4, 22, 1.5, 'm' ) } ); // Section heading + link lines, as in the columned styles. const section = ( x: number, y: number, w: number, rows: number ) => ( { r( x, y, w * 0.55, 1.5, 'm' ) } { Array.from( { length: rows }, ( _, n ) => r( x, y + 4.5 + n * 4.5, w - ( n % 2 ) * 3, 2, 'i' ) ) } ); let body; switch ( id ) { case 'simple-descriptions': body = ( <> { panel } { [ 6, 15, 24 ].map( ( y, n ) => ( { r( 7, y, 10 + n * 2, 2, 'i' ) } { r( 7, y + 3.5, 33, 1.5, 'm' ) } { r( 7, y + 6, 20 - n * 3, 1.5, 'm' ) } ) ) } ); break; case 'stacked': body = ( <> { panel } { [ 5, 14, 23 ].map( iconItem ) } ); break; case 'footer-actions': body = ( <> { panel } { [ 5, 12.5 ].map( iconItem ) } { iconItem( 20 ) } { r( 12, 29.75, 8, 1.5, 'i' ) } { r( 32, 29.75, 9, 1.5, 'i' ) } ); break; case 'footer-list': body = ( <> { panel } { [ 4.5, 11.5 ].map( iconItem ) } { r( 7, 21.5, 11, 1.5, 'm' ) } { r( 35, 21.5, 6, 1.5, 'a' ) } { r( 7, 25, 6, 1, 'm' ) } { r( 7, 26.5, 22, 1.75, 'i' ) } { r( 7, 29.5, 6, 1, 'm' ) } { r( 7, 31, 28, 1.75, 'i' ) } ); break; case 'two-column': body = ( <> { panel } { section( 7, 7, 15, 4 ) } { section( 26, 7, 15, 4 ) } ); break; case 'full-width': body = ( <> { widePanel } { section( 4, 9, 11, 4 ) } { section( 17, 9, 11, 4 ) } { r( 31, 9, 14, 18, 'b', 2 ) } { r( 33, 11, 10, 7, 'm', 1 ) } { r( 33, 20.5, 8, 1.75, 'i' ) } { r( 33, 23.5, 10, 1.25, 'm' ) } ); break; case 'full-width-two-column': body = ( <> { widePanel } { section( 5, 9, 17, 4 ) } { section( 27, 9, 17, 4 ) } ); break; default: if ( /builder|custom/.test( id ) ) { body = ( <> ); break; } body = ( <> { panel } { [ 14, 18, 12, 17, 13 ].map( ( w, n ) => r( 7, 6 + n * 5, w, 2, 'i' ) ) } ); } return ( { body } ); }