import { FileBrowser } from '@components/admin/FileBrowser.js'; import { AnchorPicker, type ContentAnchor } from '@components/common/page-builder/drawer/AnchorPicker.js'; import { Slider } from '@components/common/page-builder/drawer/index.js'; import { CtaField } from '@components/common/page-builder/fields/CtaField.js'; import type { CtaValue } from '@components/common/page-builder/fields/CtaField.js'; import { ImagePickerField } from '@components/common/page-builder/fields/ImagePickerField.js'; import { MarkdownBodyField } from '@components/common/page-builder/fields/MarkdownBodyField.js'; import { normalizeImageSrc } from '@components/common/page-builder/normalizeImageSrc.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { useScopedFormContext } from '@components/common/page-builder/WidgetSettingsScope.js'; import { Button } from '@components/common/ui/Button.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { AlignCenter, AlignLeft, AlignRight, ChevronDown, ImagePlus } from 'lucide-react'; import React, { useEffect, useState } from 'react'; // --------------------------------------------------------------------------- // Drawer-style helpers — same vocabulary as the Slideshow / Menu drawers // (compact Field with 11px label, collapsible Section card). Kept local for // now; promote to a shared module once a fourth drawer pulls them in. // --------------------------------------------------------------------------- function Field({ label, hint, children }: { label?: string; hint?: string; children: React.ReactNode; }) { return (
{label && (
{label}
)}
{children}
{hint &&
{hint}
}
); } function Section({ title, children, rightSlot }: { title: string; children: React.ReactNode; rightSlot?: React.ReactNode; }) { const [open, setOpen] = useState(true); return (
{rightSlot}
{open && (
{children}
)}
); } // --------------------------------------------------------------------------- // Banner setting form. // // Same structure as before — image picker, alignment, alt text, link — but // styled to match the page-builder drawer density (Slideshow / Menu). // --------------------------------------------------------------------------- type AlignmentType = 'left' | 'center' | 'right'; type OverlayTint = 'none' | 'dark' | 'light' | 'gradient'; interface BannerSettingProps { // Optional: page-builder drawer mounts this without GraphQL props. bannerWidget?: { src?: string; alignment?: AlignmentType; width?: number; height?: number; alt?: string; link?: string; eyebrow?: string | null; heading?: string | null; subText?: string | null; contentPosition?: ContentAnchor | null; overlayTint?: OverlayTint | null; overlayOpacity?: number | null; cta?: CtaValue | null; cta2?: CtaValue | null; mobileImage?: string | null; mobileImageWidth?: number | null; mobileImageHeight?: number | null; }; } const TINT_OPTIONS: ReadonlyArray<{ value: OverlayTint; label: string }> = [ { value: 'none', label: _('None') }, { value: 'dark', label: _('Dark') }, { value: 'light', label: _('Light') }, { value: 'gradient', label: _('Gradient') } ]; function Segmented({ value, options, onChange }: { value: T; options: ReadonlyArray<{ value: T; label: string }>; onChange: (v: T) => void; }) { return (
{options.map((opt) => { const active = value === opt.value; return ( ); })}
); } const BLANK_CTA: CtaValue = { label: _('Shop now'), url: '/', kind: 'custom', newTab: false, style: 'primary' }; const ALIGNMENT_OPTIONS: ReadonlyArray<{ value: AlignmentType; icon: React.ReactNode; label: string; }> = [ { value: 'left', icon: , label: _('Left') }, { value: 'center', icon: , label: _('Center') }, { value: 'right', icon: , label: _('Right') } ]; export default function BannerSetting({ bannerWidget }: BannerSettingProps) { const { src = '', alignment = 'left', width = 0, height = 0, alt = '', link = undefined, eyebrow = null, heading = null, subText = null, contentPosition = 'mc', overlayTint = 'none', overlayOpacity = 0.3, cta = null, cta2 = null, mobileImage = null, mobileImageWidth = null, mobileImageHeight = null } = bannerWidget ?? {}; const { register, setValue, watch } = useScopedFormContext(); const image = watch('settings.src', src) as string; const currentAlignment = (watch('settings.alignment', alignment) ?? alignment) as AlignmentType; // Overlay fields — all reactive via watch so the drawer mirrors the // canvas on auto-save changes (e.g. inline-edit, undo/redo). const eyebrowV = (watch('settings.eyebrow') as string) ?? eyebrow ?? ''; const headingV = (watch('settings.heading') as string) ?? heading ?? ''; const subTextV = (watch('settings.subText') as string) ?? subText ?? ''; const anchorV = ((watch('settings.contentPosition') as string) ?? contentPosition ?? 'mc') as ContentAnchor; const tintV = ((watch('settings.overlayTint') as string) ?? overlayTint ?? 'none') as OverlayTint; const opacityV = (watch('settings.overlayOpacity') as number) ?? overlayOpacity ?? 0.3; const ctaV = (watch('settings.cta') as CtaValue | null) ?? cta ?? null; const cta2V = (watch('settings.cta2') as CtaValue | null) ?? cta2 ?? null; const mobileImageV = (watch('settings.mobileImage') as string) ?? mobileImage ?? ''; const linkV = (watch('settings.link') as string) ?? link ?? ''; const [openFileBrowser, setOpenFileBrowser] = useState(false); const [imageDimensions, setImageDimensions] = useState({ width: width || 0, height: height || 0 }); const getImageDimensions = (imageUrl: string) => { if (!imageUrl) return; const img = new Image(); img.onload = () => { const newWidth = img.naturalWidth; const newHeight = img.naturalHeight; setImageDimensions({ width: newWidth, height: newHeight }); setValue('settings.width', newWidth); setValue('settings.height', newHeight); }; img.src = imageUrl; }; useEffect(() => { if (image) { getImageDimensions(image); } }, [image]); return (
{openFileBrowser && (
{ const normalized = normalizeImageSrc(file); setValue('settings.src', normalized); setOpenFileBrowser(false); }} close={() => setOpenFileBrowser(false)} />
)} {/* Image */}
{image ? ( ) : ( )}
{image && ( )}
{image && ( <>
{/* Subtle grid so alignment changes read clearly even when the image is white/transparent. */}
{_('Banner { // Backup dimension capture if the useEffect path missed // (e.g. cached image fires onload before effect runs). const img = e.target as HTMLImageElement; if (img.naturalWidth > 0 && img.naturalHeight > 0) { if ( imageDimensions.width !== img.naturalWidth || imageDimensions.height !== img.naturalHeight ) { setImageDimensions({ width: img.naturalWidth, height: img.naturalHeight }); setValue('settings.width', img.naturalWidth); setValue('settings.height', img.naturalHeight); } } }} />
{imageDimensions.width > 0 && (
{imageDimensions.width} × {imageDimensions.height} px
)} )} { setValue('settings.mobileImage', next || null, { shouldDirty: true }); if (!next) { setValue('settings.mobileImageWidth', null, { shouldDirty: true }); setValue('settings.mobileImageHeight', null, { shouldDirty: true }); } }} onLoadDimensions={({ width: mw, height: mh }) => { setValue('settings.mobileImageWidth', mw, { shouldDirty: true }); setValue('settings.mobileImageHeight', mh, { shouldDirty: true }); }} />
{/* Layout */}
{ALIGNMENT_OPTIONS.map((opt) => { const active = currentAlignment === opt.value; return ( ); })}
{/* Overlay copy — optional eyebrow / heading / sub-text. Banners without any of these stay image-only (matches the original behaviour byte-for-byte). */}
setValue('settings.eyebrow', e.target.value || null, { shouldDirty: true }) } placeholder={_('LIMITED EDITION')} className="w-full rounded-md border border-divider bg-card px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-primary" /> setValue('settings.heading', e.target.value || null, { shouldDirty: true }) } placeholder={_('The Summer Edit')} className="w-full rounded-md border border-divider bg-card px-3 py-2 text-sm focus:outline-none focus:ring-1 focus:ring-primary" /> setValue('settings.subText', v || null, { shouldDirty: true }) } placeholder={_('One short supporting sentence.')} rows={2} softLimit={160} />
{/* Overlay placement — content position + tint scrim + opacity */}
setValue('settings.contentPosition', v, { shouldDirty: true }) } /> value={tintV} options={TINT_OPTIONS} onChange={(v) => setValue('settings.overlayTint', v, { shouldDirty: true }) } /> {tintV !== 'none' && ( setValue('settings.overlayOpacity', v / 100, { shouldDirty: true }) } /> )}
{/* Call to actions — primary + optional secondary */}
{ctaV ? ( <>
{_('Primary')}
setValue('settings.cta', v, { shouldDirty: true }) } /> ) : ( <> )} {ctaV && cta2V && ( <>
{_('Secondary')}
setValue('settings.cta2', v, { shouldDirty: true }) } showStyle /> )} {ctaV && !cta2V && ( <>
)}
{/* Details */}
setValue('settings.link', url, { shouldDirty: true }) } />
{/* Hidden mirrors so the standalone widgetEdit form posts these values. The drawer's auto-save reads from form state directly. `valueAsNumber` on width/height is critical — without it the DOM string "0" reaches the storefront, and GraphQL's `Float` scalar rejects strings ("Float cannot represent non numeric value"). */}
); } export const query = ` query Query( $src: String $alignment: String $width: Float $height: Float $alt: String $link: String $mobileImage: String $mobileImageWidth: Float $mobileImageHeight: Float ) { bannerWidget( src: $src alignment: $alignment width: $width height: $height alt: $alt link: $link mobileImage: $mobileImage mobileImageWidth: $mobileImageWidth mobileImageHeight: $mobileImageHeight ) { src alignment width height alt link mobileImage mobileImageWidth mobileImageHeight } } `; export const variables = `{ src: getWidgetSetting("src"), alignment: getWidgetSetting("alignment"), width: getWidgetSetting("width"), height: getWidgetSetting("height"), alt: getWidgetSetting("alt"), link: getWidgetSetting("link"), mobileImage: getWidgetSetting("mobileImage"), mobileImageWidth: getWidgetSetting("mobileImageWidth"), mobileImageHeight: getWidgetSetting("mobileImageHeight") }`;