/** * Dark Mode section — enable flag (via the Modules-page card, master flag at * `working.modules.dark_mode.enabled`), default scheme, floating-toggle flag, * and a dark colour palette. The section itself is a bespoke component (not * theme-owned data) — Aureon Nexus owns the `dark_mode` values below (scope, * default, show_toggle, colors) behind its own REST route, plus the toggle + * anti-FOUC script + palette CSS on the front end and the wp-admin skin. This * component just renders whatever schema/values it's handed; see * THEME-REVIEW-REMOVALS.md item 9 for why the feature lives there now. * * Laid out as three cards under the intro — Applies To, Appearance, Colour * Palette — so each question the reader answers is its own titled block rather * than one uppercase-labelled stack. */ import type { ReactNode } from 'react'; import { __ } from '@wordpress/i18n'; import { useEffect } from '@wordpress/element'; import { ToggleControl } from '@wordpress/components'; import { ArrowDownLeft, ArrowDownRight, ArrowLeftToLine, ArrowRight, ArrowRightToLine, Database, Globe, Info, Monitor, MoonStar, } from '../icons'; import type { LucideIcon } from '../icons'; import FieldGroup from '../controls/field-group'; import InfoNote from '../controls/info-note'; import PaletteGallery from '../controls/palette-gallery'; import type { Palette } from '../controls/palette-gallery'; import SegmentedControl from '../controls/segmented-control'; import SelectField from '../controls/select-field'; import CssVariablesCallout from '../controls/css-variables-callout'; import SectionIntro from '../controls/section-intro'; interface Option { value: string; label: string; } interface DarkField { key: string; label: string; description?: string; } interface DarkModeSchema { label: string; scopes?: Option[]; defaults?: Option[]; toggle_positions?: Option[]; palettes?: Palette[]; fields?: DarkField[]; // Optional docs link for the intro's "Learn more" — shown only when the // companion plugin supplies one. learn_more_url?: string; } const SCOPES = [ 'both', 'frontend', 'backend' ]; const SCOPE_ICONS: Record< string, LucideIcon > = { both: Globe, frontend: Monitor, backend: Database, }; const POSITION_ICONS: Record< string, LucideIcon > = { 'bottom-left': ArrowDownLeft, 'bottom-right': ArrowDownRight, 'middle-left': ArrowLeftToLine, 'middle-right': ArrowRightToLine, }; // What the notice under "Show floating toggle button" says, by where a Dark // Mode Toggle element is placed. const placementNotice = ( placements: string[] ): string => { if ( placements.includes( 'header' ) && placements.includes( 'footer' ) ) { return __( 'A Dark Mode Toggle is placed in your header and footer, so visitors can already switch there — you may not need the floating button too.', 'aureon' ); } return placements.includes( 'header' ) ? __( 'A Dark Mode Toggle is placed in your header, so visitors can already switch there — you may not need the floating button too.', 'aureon' ) : __( 'A Dark Mode Toggle is placed in your footer, so visitors can already switch there — you may not need the floating button too.', 'aureon' ); }; const placementAction = ( placements: string[] ): string => 'header' === placements[ 0 ] ? __( 'Open Header Builder', 'aureon' ) : __( 'Open Footer Builder', 'aureon' ); // A muted one-line note under a control, marked with an outlined "i". const Hint = ( { children }: { children: ReactNode } ) => (

); interface DarkModeSectionProps { schema?: DarkModeSchema; // Master on/off flag, resolved by the caller from the theme's own settings // tree (`working.modules.dark_mode.enabled`) — the same flag the Modules // page's card writes. enabled: boolean; setEnabled: ( enabled: boolean ) => void; // Builders a Dark Mode Toggle element is placed in ('header', 'footer'). placements?: string[]; // Jump to a builder section by zone. onOpenBuilder?: ( zone: string ) => void; // This surface's own values (scope/default/show_toggle/colors), from // Aureon Nexus's route. values: Record< string, any >; setValue: ( field: string, value: unknown ) => void; } export default function DarkModeSection( { schema, enabled, setEnabled, placements = [], onOpenBuilder, values, setValue, }: DarkModeSectionProps ) { const dark = values ?? {}; const scope = SCOPES.includes( dark.scope ) ? dark.scope : 'frontend'; // What the form shows follows the scope alone — the module flag gates the // whole section, so gating the fields on it too only ever mislabelled a // frontend scope as "wp-admin only" while the module was switched off. const frontendScope = 'backend' !== scope; const showToggle = dark.show_toggle !== false; const adminActive = enabled && 'frontend' !== scope; // Live-toggle the admin-bar button's visibility as the "Enable dark // mode" / scope controls change — the PHP node // (Admin_Dark_Mode_Feature::add_toggle, in Aureon Nexus) already exists in // the DOM whenever dark mode was enabled at page load, so this needs no // save/reload to reflect unsaved changes made in this session. useEffect( () => { const node = document.getElementById( 'wp-admin-bar-aureon-dark-mode-toggle' ); node?.classList.toggle( 'is-hidden', ! adminActive ); }, [ adminActive ] ); if ( ! schema ) { return null; } const colors = ( dark.colors ?? {} ) as Record< string, string >; const fields = schema.fields ?? []; const cssVariables = fields.map( ( field ) => ( { name: `--aureon-color-${ field.key }`, value: colors[ field.key ] ?? '—', } ) ); return (

{ schema.label }

{ __( 'Offer a dark colour scheme, switched by the visitor or their system.', 'aureon' ) }

{ schema.learn_more_url && ( { __( 'Learn more', 'aureon' ) } ) }
{ null } { enabled && ( <> ( { ...option, icon: SCOPE_ICONS[ option.value ], } ) ) } onChange={ ( value ) => setValue( 'scope', value ) } /> { __( '“Backend” themes wp-admin and leaves the public site light. “Both” themes both surfaces.', 'aureon' ) } { frontendScope ? ( <> setValue( 'default', value ) } /> { __( '“Follow system” uses the visitor’s OS preference until they choose.', 'aureon' ) } setValue( 'show_toggle', checked ) } /> { placements.length > 0 && ( onOpenBuilder?.( placements[ 0 ] ) } > { placementNotice( placements ) } ) } { showToggle && !! schema.toggle_positions?.length && (
( { ...option, icon: POSITION_ICONS[ option.value ], } ) ) } onChange={ ( value ) => setValue( 'toggle_position', value ) } />
) } ) : ( { __( 'Dark mode applies to wp-admin only. The admin area uses Aureon’s built-in dark theme, so the public-site scheme and floating toggle don’t apply in this scope.', 'aureon' ) } ) }
{ frontendScope && ( setValue( 'colors', { ...colors, ...next, } ) } /> ) } { frontendScope && ( ) } ) }
); }