/** * Palette gallery. * * A list of ready-made palettes, one row each: the palette's colours as a solid * block on the left, its name and description beside it, and the action at the * end. A row, not a card in a grid, because the colours are the thing being * compared and a full-width block shows far more of each than a card's strip * does — and because names and descriptions read down a column. * * Preset rows apply directly — their colours are fixed, not editable — through * a trailing "Apply" button; the applied one trades that button for an * "Applied" chip in the same slot, matched against the working set. The trailing * "Custom" row (Dark Mode's only editor; the Colors section gives Custom a tab * of its own instead) opens the modal seeded with the current colours. * Applying either way writes back through onApply. */ import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; import { Check, Plus } from '../icons'; import PaletteModal from './palette-modal'; export interface Palette { id: string; label: string; description?: string; colors: Record< string, string >; // Optional bundle extras (Style Presets carry these; the dark palettes // don't). The gallery never reads them — it hands the whole palette to // onApply so the owning section can write them where they belong. radius?: Record< string, string >; pairing?: string; } interface RoleField { key: string; label: string; description?: string; } interface PaletteGalleryProps { palettes: Palette[]; fields: RoleField[]; current: Record< string, string >; // `palette` is passed only when a preset card is applied — the Custom modal // has colours but no bundle. onApply: ( colors: Record< string, string >, palette?: Palette ) => void; // Hides the trailing Custom card + its modal — used by the Colors section, // which gives Custom its own tab (an inline editor) instead. Dark Mode // keeps the default (true), since it has no second tab for this gallery. showCustomCard?: boolean; } // Swatch order shown on each card's colour strip. const CARD_SWATCHES = [ 'primary', 'accent', 'heading', 'background', 'border', ]; // True when every colour the palette defines already matches the working set. const paletteMatches = ( palette: Palette, current: Record< string, string > ): boolean => { const keys = Object.keys( palette.colors ); if ( 0 === keys.length ) { return false; } return keys.every( ( key ) => ( current[ key ] ?? '' ).toLowerCase() === ( palette.colors[ key ] ?? '' ).toLowerCase() ); }; const Swatches = ( { colors }: { colors: Record< string, string > } ) => ( ); /** Stands in for an applied row's Apply button, in the same slot. */ const AppliedChip = () => ( { __( 'Applied', 'aureon' ) } ); export default function PaletteGallery( { palettes, fields, current, onApply, showCustomCard = true, }: PaletteGalleryProps ) { // Whether the Custom editor modal is open. const [ editing, setEditing ] = useState( false ); // Custom is "set" only when the working colours match no preset — until // then the Custom card shows an empty placeholder, not a palette. const customApplied = ! palettes.some( ( palette ) => paletteMatches( palette, current ) ); return ( <>