/** * Per-element settings forms. * * `ElementForm` dispatches on element id and renders that element's controls. * Each control reads from `header.elements.` and writes back through * setPath with the element's own path — the same shape the PHP renderers and * sanitizer expect (see Elements.php / Header_Builder::sanitize_elements()). */ import { __ } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { TextControl, TextareaControl, ToggleControl, CheckboxControl, Button, } from '@wordpress/components'; import { Plus, Trash2, List, Palette } from '../../icons'; import ColorField from '../../controls/color-field'; import ColorOverrideField from '../../controls/color-override-field'; import SelectField from '../../controls/select-field'; import Tabs, { TabPanel } from '../../controls/tabs'; import UnitField from '../../controls/unit-field'; import MediaImageField from '../../controls/media-image-field'; import Segmented from './segmented'; import CustomPageField from './custom-page-field'; import { cartIconFor, DEFAULT_CART_ICON } from './cart-icons'; import { accountIconFor, DEFAULT_ACCOUNT_ICON } from './account-icons'; import type { SetPath } from '../../types'; const LENGTH_UNITS = [ { value: 'px', label: 'px' }, { value: 'rem', label: 'rem' }, { value: 'em', label: 'em' }, { value: '%', label: '%' }, ]; interface MenuChoice { id: number; label: string; } interface PostTypeChoice { slug: string; label: string; } interface IconChoice { id: string; label: string; } interface ElementFormProps { type: string; elements: Record< string, Record< string, unknown > >; menus: MenuChoice[]; postTypes: PostTypeChoice[]; /** Selectable cart icons ({id,label}); mirrors the PHP registry. */ cartIcons?: IconChoice[]; /** Selectable account icons ({id,label}); mirrors the PHP registry. */ accountIcons?: IconChoice[]; /** Footer widget areas ({id,label}) for the Widget Area element picker. */ widgetAreas?: IconChoice[]; /** Whether WooCommerce is active (gates the account "My Account page" option). */ woo?: boolean; /** The site's global color palette, used to show inherited color defaults. */ palette?: Record< string, string >; setPath: SetPath; } type Settings = Record< string, unknown >; const str = ( v: unknown, fallback = '' ): string => typeof v === 'string' ? v : fallback; const num = ( v: unknown, fallback = 0 ): number => typeof v === 'number' ? v : fallback; // The "Default" menu option for each menu element names the theme location the // element pulls from when no specific menu is chosen (assignable in // Appearance → Menus). const DEFAULT_MENU_LABEL = __( 'Default', 'aureon' ); const MENU_DEFAULT_LABEL: Record< string, string > = { 'primary-menu': __( 'Default (Primary Menu)', 'aureon' ), 'secondary-menu': __( 'Default (Secondary Menu)', 'aureon' ), 'offcanvas-menu': __( 'Default (Off-Canvas Menu)', 'aureon' ), }; // Selectable payment badges — mirrors Elements::payment_badges() on the PHP side. const PAYMENT_BADGES: { id: string; label: string }[] = [ { id: 'visa', label: __( 'Visa', 'aureon' ) }, { id: 'mastercard', label: __( 'Mastercard', 'aureon' ) }, { id: 'amex', label: __( 'American Express', 'aureon' ) }, { id: 'paypal', label: __( 'PayPal', 'aureon' ) }, { id: 'applepay', label: __( 'Apple Pay', 'aureon' ) }, { id: 'googlepay', label: __( 'Google Pay', 'aureon' ) }, { id: 'stripe', label: __( 'Stripe', 'aureon' ) }, { id: 'discover', label: __( 'Discover', 'aureon' ) }, ]; export default function ElementForm( { type, elements, menus, postTypes, cartIcons = [], accountIcons = [], widgetAreas = [], woo = false, palette = {}, setPath, }: ElementFormProps ) { const s: Settings = elements?.[ type ] ?? {}; const set = ( key: string, value: unknown ) => setPath( `header.elements.${ type }.${ key }`, value ); // Menu elements split their controls across Content / Style sub-tabs. const [ menuTab, setMenuTab ] = useState( 'content' ); // Inherited color defaults shown (but not stored) when a menu color field is // left empty, so the swatches reflect the live palette instead of looking // blank. Clearing a field keeps it linked to these palette colors. const menuColorFallback = { link: palette.text ?? '', linkHover: palette.primary ?? '', submenuBg: palette.background ?? '', submenuText: palette.text ?? '', submenuHoverBg: palette.border ?? '', }; // What the Button element's colors inherit while left global — matches the // var fallbacks in style.scss (--aureon-btn-bg → --aureon-color-primary, etc.), // so the globe tooltip names the token the button actually follows. const buttonColorGlobals = { bg: { label: __( 'Primary', 'aureon' ), value: palette.primary ?? '', }, text: { label: __( 'Button text', 'aureon' ), value: palette[ 'button-text' ] ?? '', }, hoverBg: { label: __( 'Primary (hover)', 'aureon' ), value: palette[ 'primary-hover' ] ?? '', }, }; // Repeatable elements are placed as numbered copies (link-list-2, …); every // copy reads/writes its own settings via `type` above, but renders the same // form as its base element. Strip a trailing `-` to pick the form. const baseType = type.replace( /-\d+$/, '' ); switch ( baseType ) { case 'logo': return (

{ __( 'Title, tagline, logo, and favicon are your global WordPress Site Identity — changes here apply across the whole site.', 'aureon' ) }

set( 'title', value ) } /> set( 'tagline', value ) } /> { set( 'logo_id', id ); set( 'logo_url', url ); } } /> { set( 'favicon_id', id ); set( 'favicon_url', url ); } } /> set( 'source', value ) } /> set( 'max_width', value ) } /> set( 'show_tagline', value ) } /> { set( 'logo_dark_id', id ); set( 'logo_dark_url', url ); } } />

{ __( 'In the footer', 'aureon' ) }

{ set( 'footer_logo_id', id ); set( 'footer_logo_url', url ); } } /> { num( s.footer_logo_id ) > 0 && ( { set( 'footer_logo_dark_id', id ); set( 'footer_logo_dark_url', url ); } } /> ) } set( 'footer_max_width', value ) } />
); case 'primary-menu': case 'secondary-menu': case 'offcanvas-menu': { const menuIdPrefix = `hb-menu-${ type }`; return (
( { value: String( m.id ), label: m.label, } ) ), ] } onChange={ ( value ) => set( 'menu', parseInt( value, 10 ) || 0 ) } /> set( 'depth', parseInt( value, 10 ) ) } /> set( 'submenu_trigger', value ) } /> set( 'submenu_style', value ) } /> { 'offcanvas-menu' !== baseType && ( set( 'overflow', value ) } /> ) } set( 'hover_animation', value ) } /> set( 'submenu_animation', value ) } />
set( 'link_color', value ) } /> set( 'link_hover_color', value ) } /> set( 'submenu_bg', value ) } /> set( 'submenu_text', value ) } /> set( 'submenu_hover_bg', value ) } />
set( 'submenu_width', value ) } /> set( 'submenu_font_size', value ) } /> set( 'submenu_radius', value ) } />
); } case 'button': return (
set( 'text', value ) } /> set( 'url', value ) } /> set( 'style', value ) } /> set( 'size', value ) } />
set( 'bg_color', value ) } /> set( 'text_color', value ) } /> set( 'hover_bg', value ) } />
= 0 ? String( num( s.radius, -1 ) ) : '' } onChange={ ( value ) => set( 'radius', '' === value ? -1 : parseInt( value, 10 ) || 0 ) } /> set( 'new_tab', value ) } />
); case 'search': { const style = str( s.style, 'box' ); const scope = Array.isArray( s.post_types ) ? ( s.post_types as string[] ) : []; const toggleScope = ( slug: string ) => set( 'post_types', scope.includes( slug ) ? scope.filter( ( item ) => item !== slug ) : [ ...scope, slug ] ); return (
set( 'style', value ) } /> { 'icon' === style && ( set( 'reveal', value ) } /> ) } set( 'placeholder', value ) } /> set( 'live', value ) } />
{ __( 'Search in', 'aureon' ) }

{ __( 'Leave all unchecked to search everything.', 'aureon' ) }

{ postTypes.map( ( pt ) => ( toggleScope( pt.slug ) } /> ) ) }
); } case 'text-html': return (
set( 'content', value ) } />
); case 'link-list': { const source = str( s.source, 'custom' ); const links = Array.isArray( s.links ) ? ( s.links as { text?: string; url?: string }[] ) : []; const setLinks = ( next: { text?: string; url?: string }[] ) => set( 'links', next ); return (
set( 'heading', value ) } /> set( 'orientation', value ) } /> set( 'source', value ) } /> { 'menu' === source ? ( ( { value: String( m.id ), label: m.label, } ) ), ] } onChange={ ( value ) => set( 'menu', parseInt( value, 10 ) || 0 ) } /> ) : ( <> { links.map( ( link, index ) => ( // eslint-disable-next-line react/no-array-index-key
{ const next = [ ...links ]; next[ index ] = { ...next[ index ], text: value, }; setLinks( next ); } } /> { const next = [ ...links ]; next[ index ] = { ...next[ index ], url: value, }; setLinks( next ); } } />
) ) } ) }
); } case 'widget-area': return (
( { value: area.id, label: area.label, } ) ) } onChange={ ( value ) => set( 'area', parseInt( value, 10 ) || 1 ) } />
); case 'newsletter': return (
set( 'heading', value ) } /> set( 'text', value ) } />
); case 'image': return (
{ set( 'image_id', id ); set( 'image_url', url ); } } /> set( 'alt', value ) } /> set( 'link_url', value ) } /> set( 'max_width', value ) } /> set( 'new_tab', value ) } />
); case 'payment': { const items = Array.isArray( s.items ) ? ( s.items as string[] ) : []; const toggleBadge = ( id: string ) => set( 'items', items.includes( id ) ? items.filter( ( item ) => item !== id ) : [ ...items, id ] ); return (
{ __( 'Payment methods', 'aureon' ) } { PAYMENT_BADGES.map( ( badge ) => ( toggleBadge( badge.id ) } /> ) ) }
set( 'size', parseInt( value, 10 ) || 0 ) } /> set( 'gap', parseInt( value, 10 ) || 0 ) } />
); } case 'social': { const items = Array.isArray( s.items ) ? ( s.items as { network?: string; url?: string }[] ) : []; const setItems = ( next: { network?: string; url?: string }[] ) => set( 'items', next ); return (
{ items.map( ( link, index ) => ( // eslint-disable-next-line react/no-array-index-key
{ const next = [ ...items ]; next[ index ] = { ...next[ index ], network: value, }; setItems( next ); } } /> { const next = [ ...items ]; next[ index ] = { ...next[ index ], url: value, }; setItems( next ); } } />
) ) } set( 'size', parseInt( value, 10 ) || 0 ) } /> set( 'gap', parseInt( value, 10 ) || 0 ) } /> set( 'shape', value ) } /> set( 'weight', value ) } /> set( 'color_style', value ) } /> { 'custom' === str( s.color_style, 'mono' ) && ( set( 'color', value ) } /> ) } set( 'new_tab', value ) } />
); } case 'account': { const currentAccountIcon = str( s.icon, DEFAULT_ACCOUNT_ICON ); const displayOptions = [ { value: 'icon_text', label: __( 'Icon + text', 'aureon' ) }, { value: 'icon', label: __( 'Icon', 'aureon' ) }, { value: 'text', label: __( 'Text', 'aureon' ) }, { value: 'avatar', label: __( 'Avatar', 'aureon' ) }, ]; const loggedOutDisplay = str( s.logged_out_display, 'icon_text' ); const loggedInDisplay = str( s.logged_in_display, 'icon_text' ); const phoneDisplay = str( s.phone_display, 'same' ); // "Icon only on phones" shows the icon for a text-only view too, so // the icon picker is needed then as well. const usesIcon = 'icon' === phoneDisplay || [ loggedOutDisplay, loggedInDisplay ].some( ( d ) => 'icon_text' === d || 'icon' === d ); return (
set( 'style', value ) } />

{ __( 'Logged-out (Login) view', 'aureon' ) }

set( 'logged_out_display', value ) } /> set( 'logged_out_label', value ) } />

{ __( 'Logged-in (Account) view', 'aureon' ) }

set( 'logged_in_display', value ) } /> set( 'logged_in_label', value ) } /> set( 'phone_display', value ) } /> { usesIcon && accountIcons.length > 0 && (
{ __( 'Account icon', 'aureon' ) }
{ accountIcons.map( ( { id, label } ) => { const Icon = accountIconFor( id ); const selected = id === currentAccountIcon; return ( ); } ) }
) } set( 'target', value ) } /> { 'url' === str( s.target, 'profile' ) && ( set( 'custom_url', value ) } /> ) } { 'custom' === str( s.target, 'profile' ) && ( set( 'custom_page_id', pageId ) } /> ) }
); } case 'divider': { const orientation = str( s.orientation, 'vertical' ); return (
set( 'orientation', value ) } /> { 'vertical' === orientation && ( set( 'height', parseInt( value, 10 ) || 0 ) } /> ) } set( 'thickness', parseInt( value, 10 ) || 1 ) } /> set( 'color', value ) } />
); } case 'spacer': return (
set( 'height', parseInt( value, 10 ) || 0 ) } />
); case 'copyright': return (
set( 'content', value ) } />
); case 'back-to-top': return (
set( 'label', value ) } />
); case 'cart': { const currentIcon = str( s.icon, DEFAULT_CART_ICON ); return (
set( 'behavior', value ) } /> { cartIcons.length > 0 && (
{ __( 'Cart icon', 'aureon' ) }
{ cartIcons.map( ( { id, label } ) => { const Icon = cartIconFor( id ); const selected = id === currentIcon; return ( ); } ) }
) } set( 'show_count', value ) } /> set( 'show_label', value ) } />
); } default: return null; } }