import { asArray, ColorSwatchField, drawerInputClass, Field, ImagePickerField, MarkdownBodyField, RepeatableAccordion, Section, Segmented, Slider, Toggle, useArraySetting, useScopedFormContext } from '@components/common/page-builder/index.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import type { BentoGap, BentoTextColor, BentoTile } from './BentoGrid.js'; interface BentoGridSettingProps { bentoGridWidget?: { tiles?: BentoTile[]; gap?: BentoGap; minHeight?: number; }; } const GAP_OPTIONS: ReadonlyArray<{ value: BentoGap; label: string }> = [ { value: 'sm', label: _('S') }, { value: 'md', label: _('M') }, { value: 'lg', label: _('L') } ]; const TEXT_COLOR_OPTIONS: ReadonlyArray<{ value: BentoTextColor; label: string; }> = [ { value: 'light', label: _('Light') }, { value: 'dark', label: _('Dark') } ]; function makeId(): string { return typeof crypto !== 'undefined' && 'randomUUID' in crypto ? crypto.randomUUID() : `t-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; } function makeBlankTile(isHero: boolean): BentoTile { return { id: makeId(), size: isHero ? 'lg' : 'sm', image: null, imageAlt: '', imageWidth: null, imageHeight: null, backgroundColor: '#f4f4f4', eyebrow: isHero ? null : null, heading: isHero ? 'Hero headline' : 'New tile', body: isHero ? 'Supporting copy for the hero.' : null, link: { label: 'Shop', url: '/', newTab: false }, textColor: isHero ? 'light' : 'dark' }; } export default function BentoGridSetting({ bentoGridWidget }: BentoGridSettingProps) { const { tiles: initialTiles = [], gap, minHeight } = bentoGridWidget ?? {}; const { register, setValue, watch, getValues } = useScopedFormContext(); const tiles = useArraySetting('settings.tiles', initialTiles); const gapV = ((watch('settings.gap') as string) ?? gap ?? 'md') as BentoGap; const minHeightV = (watch('settings.minHeight') as number) ?? minHeight ?? 360; // Mutation helpers read via getValues to avoid the stale-closure race โ€” // two updates fired in quick succession (e.g. image URL + onLoadDimensions // dimensions) would otherwise both compute from the same pre-update array. const readTiles = (): BentoTile[] => asArray(getValues('settings.tiles'), initialTiles); const updateTile = (i: number, patch: Partial) => { const current = readTiles(); const next = current.map((t, idx) => (idx === i ? { ...t, ...patch } : t)); setValue('settings.tiles', next, { shouldDirty: true }); }; const moveTile = (from: number, to: number) => { const current = readTiles(); // First tile is the hero โ€” block reordering into/out of position 0. if (from === 0 || to <= 0 || to >= current.length) return; const next = current.slice(); const [moved] = next.splice(from, 1); next.splice(to, 0, moved); setValue('settings.tiles', next, { shouldDirty: true }); }; const removeTile = (i: number) => { if (i === 0) return; // hero is undeletable const current = readTiles(); if (current.length <= 2) return; setValue( 'settings.tiles', current.filter((_, idx) => idx !== i), { shouldDirty: true } ); }; const addTile = () => { const current = readTiles(); if (current.length >= 5) return; setValue('settings.tiles', [...current, makeBlankTile(false)], { shouldDirty: true }); }; return (
items={tiles} onAdd={addTile} onRemove={removeTile} onMove={moveTile} addLabel={_('Add tile')} minItems={2} maxItems={5} initiallyOpenFirst renderHeader={({ item, index }) => ( {index === 0 ? _('Hero ยท ') : ''} {item.heading || _('Untitled')} )} renderItem={({ item, index }) => { const isHero = index === 0; return ( <> updateTile(index, { image: v ? v : null, ...(v ? null : { imageWidth: null, imageHeight: null }) }) } onLoadDimensions={({ width: w, height: h }) => updateTile(index, { imageWidth: w, imageHeight: h }) } /> updateTile(index, { backgroundColor: v || '#f4f4f4' }) } allowEmpty={false} /> {isHero && ( updateTile(index, { eyebrow: e.target.value || null }) } placeholder={_('THE SUMMER EDIT')} className={drawerInputClass} /> )} updateTile(index, { heading: e.target.value }) } placeholder={isHero ? _('The Summer Edit') : _('New Arrivals')} className={drawerInputClass} /> {isHero && ( updateTile(index, { body: v || null }) } placeholder={_('Big card โ€” the headline of the grid.')} rows={2} softLimit={160} /> )} value={item.textColor} options={TEXT_COLOR_OPTIONS} onChange={(v) => updateTile(index, { textColor: v })} /> updateTile(index, { link: { ...item.link, label: e.target.value } }) } placeholder={_('Shop')} className={drawerInputClass} /> updateTile(index, { link: { ...item.link, url } }) } /> updateTile(index, { link: { ...item.link, newTab: v } }) } /> ); }} />
value={gapV} options={GAP_OPTIONS} onChange={(v) => setValue('settings.gap', v, { shouldDirty: true }) } /> setValue('settings.minHeight', v, { shouldDirty: true }) } />
); } export const query = ` query Query($tiles: JSON, $gap: String, $minHeight: Float) { bentoGridWidget(tiles: $tiles, gap: $gap, minHeight: $minHeight) { tiles gap minHeight } } `; export const variables = `{ tiles: getWidgetSetting("tiles", []), gap: getWidgetSetting("gap", "md"), minHeight: getWidgetSetting("minHeight", 360) }`;