/** * Redesigned "Background Image" control. * * Replaces core's six Background Image controls (see * `override_background_image()` in `Aureon\Customizer\Customizer`). Reads and * writes core's own `background_*` settings directly by their literal * `wp.customize` id, so storage, validation and the live preview are exactly * core's. Core's preset control also did the job of rewriting position, size, * repeat and attachment when a preset was chosen; with that control gone, the * same table lives here. */ import { useEffect, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { ArrowDown, ArrowDownLeft, ArrowDownRight, ArrowDownUp, ArrowLeft, ArrowRight, ArrowUp, ArrowUpLeft, ArrowUpRight, Circle, Image as ImageIcon, Info, Repeat, RefreshCw, Trash2, } from 'lucide-react'; import type { LucideIcon } from 'lucide-react'; import { getRawValue, setRawValue, subscribeRawValue, } from '../customize-bridge'; import SectionIntro from '../controls/section-intro'; import SelectField from '../controls/select-field'; import ToggleRow from '../controls/toggle-row'; import { Dropzone, DropzoneGraphic } from './site-identity'; type Preset = 'default' | 'fill' | 'fit' | 'repeat' | 'custom'; interface Layout { x: string; y: string; size: string; repeat: string; attachment: string; } interface PickedFull { url: string; thumb: string; } interface MediaAttachment { url: string; sizes?: { medium?: { url?: string }; thumbnail?: { url?: string } }; } interface MediaFrame { open: () => void; on: ( event: string, callback: () => void ) => void; state: () => { get: ( key: string ) => { first: () => { toJSON: () => MediaAttachment }; }; }; } /** * The values core's own preset control wrote for each non-custom preset * (position x, position y, size, repeat, attachment). * @param preset */ function presetLayout( preset: Preset ): Layout | undefined { const defaults = ( window as unknown as { _wpCustomizeBackground?: { defaults?: Record< string, string > }; } )._wpCustomizeBackground?.defaults; switch ( preset ) { case 'default': return { x: defaults?.[ 'default-position-x' ] || 'left', y: defaults?.[ 'default-position-y' ] || 'top', size: defaults?.[ 'default-size' ] || 'auto', repeat: defaults?.[ 'default-repeat' ] || 'repeat', attachment: defaults?.[ 'default-attachment' ] || 'scroll', }; case 'fill': return { x: 'left', y: 'top', size: 'cover', repeat: 'no-repeat', attachment: 'fixed', }; case 'fit': return { x: 'left', y: 'top', size: 'contain', repeat: 'no-repeat', attachment: 'fixed', }; case 'repeat': return { x: 'left', y: 'top', size: 'auto', repeat: 'repeat', attachment: 'scroll', }; default: return undefined; } } /** * Opens the native Media Library and resolves the chosen image's full URL plus * a thumbnail, which core stores in `background_image_thumb`. */ function pickBackground(): Promise< PickedFull | undefined > { return new Promise( ( resolve ) => { const media = window.wp?.media as unknown as | ( ( options: Record< string, unknown > ) => MediaFrame ) | undefined; if ( ! media ) { resolve( undefined ); return; } const frame = media( { title: __( 'Select background image', 'aureon' ), button: { text: __( 'Use this image', 'aureon' ) }, library: { type: [ 'image' ] }, multiple: false, } ); frame.on( 'select', () => { const selected = frame.state().get( 'selection' ).first().toJSON(); resolve( { url: selected.url, thumb: selected.sizes?.thumbnail?.url ?? selected.sizes?.medium?.url ?? selected.url, } ); } ); frame.open(); } ); } const POSITIONS: { x: string; y: string; icon: LucideIcon; label: string }[] = [ { x: 'left', y: 'top', icon: ArrowUpLeft, label: __( 'Top left', 'aureon' ), }, { x: 'center', y: 'top', icon: ArrowUp, label: __( 'Top', 'aureon' ) }, { x: 'right', y: 'top', icon: ArrowUpRight, label: __( 'Top right', 'aureon' ), }, { x: 'left', y: 'center', icon: ArrowLeft, label: __( 'Left', 'aureon' ) }, { x: 'center', y: 'center', icon: Circle, label: __( 'Center', 'aureon' ) }, { x: 'right', y: 'center', icon: ArrowRight, label: __( 'Right', 'aureon' ), }, { x: 'left', y: 'bottom', icon: ArrowDownLeft, label: __( 'Bottom left', 'aureon' ), }, { x: 'center', y: 'bottom', icon: ArrowDown, label: __( 'Bottom', 'aureon' ), }, { x: 'right', y: 'bottom', icon: ArrowDownRight, label: __( 'Bottom right', 'aureon' ), }, ]; const SETTING_IDS = [ 'background_image', 'background_preset', 'background_position_x', 'background_position_y', 'background_size', 'background_repeat', 'background_attachment', ] as const; /** Mirrors the core settings into React state and keeps them in step. */ function useBackground() { const read = () => ( { image: String( getRawValue( 'background_image' ) ?? '' ), preset: ( String( getRawValue( 'background_preset' ) ?? '' ) || 'default' ) as Preset, x: String( getRawValue( 'background_position_x' ) ?? 'left' ), y: String( getRawValue( 'background_position_y' ) ?? 'top' ), size: String( getRawValue( 'background_size' ) ?? 'auto' ), repeat: String( getRawValue( 'background_repeat' ) ?? 'repeat' ), attachment: String( getRawValue( 'background_attachment' ) ?? 'scroll' ), } ); const [ state, setState ] = useState( read ); useEffect( () => { SETTING_IDS.forEach( ( id ) => subscribeRawValue( id, () => setState( read() ) ) ); // `read` only touches stable module-level helpers. // eslint-disable-next-line react-hooks/exhaustive-deps }, [] ); return state; } export default function BackgroundImageSection() { const bg = useBackground(); const setImage = ( url: string, thumb = url ) => { setRawValue( 'background_image', url ); setRawValue( 'background_image_thumb', url ? thumb : '' ); }; const choose = () => pickBackground().then( ( picked ) => picked && setImage( picked.url, picked.thumb ) ); const applyPreset = ( preset: Preset ) => { setRawValue( 'background_preset', preset ); const layout = presetLayout( preset ); if ( layout ) { setRawValue( 'background_position_x', layout.x ); setRawValue( 'background_position_y', layout.y ); setRawValue( 'background_size', layout.size ); setRawValue( 'background_repeat', layout.repeat ); setRawValue( 'background_attachment', layout.attachment ); } }; // Which options each preset exposes — core's own visibility table: Default // shows none, Fill Screen only the position, Fit to Screen the position and // repeat, Repeat the position and scroll behaviour, Custom everything. const show = { position: 'default' !== bg.preset, size: 'custom' === bg.preset, repeat: 'fit' === bg.preset || 'custom' === bg.preset, attachment: 'repeat' === bg.preset || 'custom' === bg.preset, }; return (
{ __( 'Add a background image to your site. You can control its size, position and how it behaves as the page scrolls.', 'aureon' ) }