/** * Ordered element-list editor. * * Renders every available element as a row with an eye toggle and an optional * expand chevron for the element's own settings. Hiding an element keeps its * row where it is — greyed out, the eye swapped for a crossed-out one — so it * can be brought back in place with one click rather than re-added at the end. * * The saved value is still just the visible ids in order; hidden rows' places * are kept for this visit only, so after a reload they sit after the visible * ones. Elements placed elsewhere in the same region aren't listed at all. * * The whole row is the drag target (native HTML5 drag): grab anywhere on an * item to reorder it, not just the grip. The grip stays as a visual affordance * and the keyboard handle — focus it and use the arrow keys. The eye and * chevron buttons opt out of dragging so a click there never starts a drag. */ import type { ReactNode } from 'react'; import { __ } from '@wordpress/i18n'; import { useRef, useState } from '@wordpress/element'; import { GripVertical, Eye, EyeOff, ChevronDown } from 'lucide-react'; interface ElementOption { id: string; label: string; } interface ElementListProps { items?: string[]; available?: ElementOption[]; placedElsewhere?: string[]; onChange: ( items: string[] ) => void; /** * An element's own settings panel, shown when its row is expanded — e.g. * "Show review count" behind Rating's chevron. Return `undefined` (or * omit the prop) for an id with nothing to configure: that row gets no * chevron at all, rather than one that opens onto an empty panel. */ renderSettings?: ( id: string ) => ReactNode | undefined; } export default function ElementList( { items = [], available = [], placedElsewhere = [], onChange, renderSettings, }: ElementListProps ) { const [ expanded, setExpanded ] = useState< string | null >( null ); const labelFor = ( id: string ) => available.find( ( el ) => el.id === id )?.label ?? id; const visible = new Set( items ); const elsewhere = new Set( placedElsewhere ); const listable = [ ...items, ...available .map( ( el ) => el.id ) .filter( ( id ) => ! visible.has( id ) && ! elsewhere.has( id ) ), ]; // Where each row sits, hidden ones included. Only hidden rows actually // take their place from here: the visible rows fill the slots visible ids // hold, in the saved order, so a change made outside this list (a reset, // an undo) still shows exactly what was saved. const [ order, setOrder ] = useState< string[] >( listable ); const known = order.filter( ( id ) => listable.includes( id ) ); const slots = [ ...known, ...listable.filter( ( id ) => ! known.includes( id ) ), ]; const queue = [ ...items ]; const display = slots.map( ( id ) => visible.has( id ) ? ( queue.shift() as string ) : id ); const visibleIn = ( list: string[], also?: string ) => list.filter( ( id ) => visible.has( id ) || id === also ); const dragFrom = useRef< number | null >( null ); const [ overIndex, setOverIndex ] = useState< number | null >( null ); const [ dragIndex, setDragIndex ] = useState< number | null >( null ); const reorder = ( from: number, to: number ) => { if ( from === to || from < 0 || to < 0 || to >= display.length ) { return; } const next = [ ...display ]; const [ moved ] = next.splice( from, 1 ); next.splice( to, 0, moved ); setOrder( next ); onChange( visibleIn( next ) ); }; const move = ( index: number, delta: number ) => reorder( index, index + delta ); const toggle = ( id: string ) => { setOrder( display ); if ( visible.has( id ) ) { if ( expanded === id ) { setExpanded( null ); } onChange( items.filter( ( item ) => item !== id ) ); } else { onChange( visibleIn( display, id ) ); } }; return (
{ __( 'No elements available here.', 'aureon' ) }
) } { display.length > 0 && (