/** * Redesigned "Header Image" control. * * Core's own Header Image control stays in the page, hidden (see * `override_header_image()` in `Aureon\Customizer\Customizer` and the matching * rule in `src/customize-controls/style.scss`), because everything that makes * the control work lives in its JS: the crop step, the uploaded-images list and * the `header_image` / `header_image_data` settings it keeps in sync. This card * reads core's `wp.customize.HeaderTool` models and drives them, so cropping, * saving and removal behave exactly as core does — only the UI is ours. */ import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { DropdownMenu } from '@wordpress/components'; import { Check, Image as ImageIcon, MoreVertical, Shuffle, Trash2, Upload, RefreshCw, } from '../icons'; import { getRawValue, subscribeRawValue } from '../customize-bridge'; import SectionIntro from '../controls/section-intro'; interface HeaderData { attachment_id?: number; url?: string; thumbnail_url?: string; } interface HeaderModel { get: ( key: string ) => unknown; save: () => void; destroy: () => void; } interface Emitter { on: ( events: string, callback: () => void ) => void; off: ( events: string, callback: () => void ) => void; } interface HeaderCollection extends Emitter { models: HeaderModel[]; } interface HeaderCurrent extends Emitter { get: ( key: string ) => unknown; trigger: ( event: string ) => void; } interface HeaderToolApi { currentHeader?: HeaderCurrent; UploadsList?: HeaderCollection; combinedList?: { trigger: ( event: string ) => void }; } interface HeaderDimensions { data?: { width?: number | string; height?: number | string }; } const CONTROL_SELECTOR = '#customize-control-header_image'; function headerTool(): HeaderToolApi | undefined { const wp = window.wp as unknown as | { customize?: { HeaderTool?: HeaderToolApi } } | undefined; return wp?.customize?.HeaderTool; } /** * Dimensions the theme crops to, as printed by core for its own control. */ function themeDimensions(): { width: number; height: number } { const globals = window as unknown as { _wpCustomizeHeader?: HeaderDimensions; }; const data = globals._wpCustomizeHeader?.data; return { width: Number( data?.width ) || 1920, height: Number( data?.height ) || 480, }; } /** * Core's models exist only once its (hidden) control has run `ready()`, which * happens just after this card is mounted, so wait for them and then follow * every change they report. */ function useHeaderTool(): HeaderToolApi | undefined { const [ tool, setTool ] = useState< HeaderToolApi | undefined >(); const [ , setTick ] = useState( 0 ); useEffect( () => { let timer = 0; let cleanup: () => void = () => undefined; const attach = () => { const found = headerTool(); if ( ! found?.currentHeader || ! found.UploadsList ) { timer = window.setTimeout( attach, 100 ); return; } const refresh = () => setTick( ( value ) => value + 1 ); const { currentHeader, UploadsList } = found; currentHeader.on( 'change', refresh ); UploadsList.on( 'add remove change reset destroy', refresh ); setTool( found ); cleanup = () => { currentHeader.off( 'change', refresh ); UploadsList.off( 'add remove change reset destroy', refresh ); }; }; attach(); // `header_image` also changes from outside the models (the Hide image // path writes it directly), so re-render on it too. subscribeRawValue( 'header_image', () => setTick( ( value ) => value + 1 ) ); return () => { window.clearTimeout( timer ); cleanup(); }; }, [] ); return tool; } /** Opens core's media frame, which continues into its crop step. */ function openMediaFrame(): void { document .querySelector< HTMLButtonElement >( `${ CONTROL_SELECTOR } .actions .new` ) ?.click(); } export default function HeaderImageSection() { const tool = useHeaderTool(); const { width, height } = themeDimensions(); const setting = String( getRawValue( 'header_image' ) ?? '' ); const isRandom = /^random-/.test( setting ); const isHidden = 'remove-header' === setting || '' === setting; const header = ( tool?.currentHeader?.get( 'header' ) ?? {} ) as HeaderData; const currentUrl = ! isRandom && ! isHidden ? header.url ?? setting : ''; const uploads = ( tool?.UploadsList?.models ?? [] ).filter( ( model ) => ! model.get( 'random' ) ); const randomModel = ( tool?.UploadsList?.models ?? [] ).find( ( model ) => Boolean( model.get( 'random' ) ) ); const removeCurrent = () => { tool?.currentHeader?.trigger( 'hide' ); tool?.combinedList?.trigger( 'control:removeImage' ); }; const clearAll = () => uploads.slice().forEach( ( model ) => model.destroy() ); return (

{ __( 'Header Image', 'aureon' ) }

{ __( 'Add an image to display under your site header. For best results use an image', 'aureon' ) }{ ' ' } { width } × { height } { ' ' } { __( 'pixels. You can crop it after choosing.', 'aureon' ) }

{ ( currentUrl || isRandom ) && (

{ __( 'Current header', 'aureon' ) }

{ currentUrl ? (
) : (

{ __( 'A random uploaded header is shown on each page load.', 'aureon' ) }

) }
) } { uploads.length > 0 && (

{ __( 'Previously uploaded', 'aureon' ) }

) } { /* Randomizing needs more than one image to pick from. Core keeps its random entry in the list even after every upload is removed, so gate on the uploads themselves, not on that entry. */ } { randomModel && uploads.length > 1 && ( ) }
); }