import { Editor } from '@components/common/Editor.js'; import { Row } from '@components/common/form/Editor.js'; import { Editable, EditableMarkdown, WidgetEmptyState } from '@components/common/page-builder/index.js'; import React from 'react'; /** * FAQ block — a flexible mix of prose sections (rendered through the * existing EditorJS renderer) and accordion FAQ groups. Editorial copy * and Q&A live in the same widget so the homepage doesn't fragment into * twelve small ones. * * Prose: takes `content` as the EditorJS Row[] shape used by the * text-block widget — same renderer keeps visual consistency. * * FAQ items: native `
` / `` so keyboard + screen * reader work for free. The "single open at a time" mode is layered on * via a tiny inline script — no React hydration boundary required. */ export type FaqMaxWidth = 'narrow' | 'normal' | 'wide'; interface ProseSection { id: string; type: 'prose'; content: Row[] | string; } interface FaqItem { id: string; question: string; answer: string; } interface FaqSection { id: string; type: 'faq'; heading: string | null; items: FaqItem[]; } export type FaqSection_ = ProseSection | FaqSection; export interface FaqBlockProps { faqBlockWidget: { heading: string | null; sections: FaqSection_[]; maxWidth: FaqMaxWidth | null; allowMultipleOpen: boolean | null; }; } const MAX_WIDTH_CLASS: Record = { narrow: 'max-w-[560px]', normal: 'max-w-[720px]', wide: 'max-w-none' }; function normalizeProse(content: Row[] | string): Row[] { if (Array.isArray(content)) return content; if (typeof content !== 'string' || !content.trim()) return []; const trimmed = content.trim(); if (!(trimmed.startsWith('[') || trimmed.startsWith('{'))) return []; try { const parsed = JSON.parse(trimmed); return Array.isArray(parsed) ? (parsed as Row[]) : []; } catch { return []; } } // Progressive-enhancement script body. Closes sibling
when one // opens, scoped to the immediate FAQ group container (so a prose section // next to it stays open). Inlined as a string so SSR ships it without // adding a hydration boundary. const SINGLE_OPEN_SCRIPT = `(() => { document.querySelectorAll('[data-evershop-faq-group][data-single-open="true"]').forEach((group) => { group.addEventListener('toggle', (e) => { const target = e.target; if (!(target instanceof HTMLDetailsElement) || !target.open) return; group.querySelectorAll('details').forEach((d) => { if (d !== target && d.open) d.open = false; }); }, true); }); })();`; export default function FaqBlock({ faqBlockWidget }: FaqBlockProps) { const { heading, sections = [], maxWidth, allowMultipleOpen } = faqBlockWidget; const widthClass = MAX_WIDTH_CLASS[maxWidth ?? 'normal']; const singleOpen = allowMultipleOpen === false; if (!Array.isArray(sections) || sections.length === 0) { return ( ); } return (
{heading && ( {heading} )}
{sections.map((section, sectionIdx) => { if (section.type === 'prose') { return (
); } return (
{section.heading && ( {section.heading} )}
{(section.items ?? []).map((item, itemIdx) => (
{item.question} {item.answer}
))}
); })}
{singleOpen && (