/** * Elements tab — a searchable card grid of every header (or footer) element, grouped into * collapsible Basic / Advanced sections. * * An unplaced card is a drag source for the docked builder below the preview * (both live under the same DndContext; the dock is a portal), and a click * places it straight into the main row. A placed card stays in the grid, * marked as in use, and a click opens its settings. The whole grid is also the * palette drop target: dragging a chip back here removes it from the zone. */ import { useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { useDroppable } from '@dnd-kit/core'; import { SortableContext, useSortable } from '@dnd-kit/sortable'; import { CSS } from '@dnd-kit/utilities'; import { Check, ChevronDown, PanelRight, Search } from 'lucide-react'; import { PALETTE } from './use-canvas-dnd'; import { iconFor } from './element-registry'; import { cardLabel, groupElements, type ElementDef } from './element-groups'; type Zone = 'header' | 'footer'; type Title = ( label: string ) => string; const CARD_TITLES: Record< Zone, { add: Title; placed: Title } > = { header: { add: ( label ) => sprintf( /* translators: %s: element name. */ __( 'Drag %s into the header, or click to add it', 'aureon' ), label ), placed: ( label ) => sprintf( /* translators: %s: element name. */ __( '%s is in the header — click to edit it', 'aureon' ), label ), }, footer: { add: ( label ) => sprintf( /* translators: %s: element name. */ __( 'Drag %s into the footer, or click to add it', 'aureon' ), label ), placed: ( label ) => sprintf( /* translators: %s: element name. */ __( '%s is in the footer — click to edit it', 'aureon' ), label ), }, }; // Palette cards never reorder among themselves, so they shouldn't shuffle // aside while something is dragged over the grid. const noShuffle = () => null; interface CardProps { el: ElementDef; zone: Zone; onClick: () => void; inDrawer?: boolean; } function DrawerBadge() { return ( ); } function DraggableCard( { el, zone, onClick, inDrawer }: CardProps ) { const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable( { id: el.id } ); const Icon = iconFor( el.id ); return ( ); } function PlacedCard( { el, zone, onClick, inDrawer }: CardProps ) { const Icon = iconFor( el.id ); return ( ); } interface ElementGridProps { elements: ElementDef[]; /** Which builder the grid feeds — card titles, grouping and DOM ids. */ zone?: Zone; /** Unplaced element ids (the dnd palette container). */ paletteIds: string[]; /** Element ids in the off-canvas drawer (badged separately). */ drawerIds?: string[]; onAdd: ( id: string ) => void; onEdit: ( id: string ) => void; } export default function ElementGrid( { elements, zone = 'header', paletteIds, drawerIds = [], onAdd, onEdit, }: ElementGridProps ) { const [ query, setQuery ] = useState( '' ); const [ collapsed, setCollapsed ] = useState< Record< string, boolean > >( {} ); const { setNodeRef, isOver } = useDroppable( { id: PALETTE } ); const needle = query.trim().toLowerCase(); const matches = needle ? elements.filter( ( el ) => el.label.toLowerCase().includes( needle ) || cardLabel( el ).toLowerCase().includes( needle ) ) : elements; const groups = groupElements( matches, zone ); // Header and footer grids are both mounted in the Customizer's panes. const idBase = 'header' === zone ? 'aureon-hbx' : 'aureon-fbx'; const unplaced = new Set( paletteIds ); return (
{ 0 === groups.length && (

{ __( 'No elements match your search.', 'aureon' ) }

) } { groups.map( ( group ) => { const isCollapsed = !! collapsed[ group.key ] && ! needle; const panelId = `${ idBase }-group-${ group.key }`; return (
{ ! isCollapsed && (
{ group.elements.map( ( el ) => unplaced.has( el.id ) ? ( onAdd( el.id ) } /> ) : ( onEdit( el.id ) } /> ) ) }
) }
); } ) }
); }