diff --git a/src/components/elements/accordion.tsx b/src/components/elements/accordion.tsx index a3a302e..e7611fd 100644 --- a/src/components/elements/accordion.tsx +++ b/src/components/elements/accordion.tsx @@ -2,11 +2,13 @@ import {HTMLAttributes, JSX, useId} from "react" import {useBoolean} from "usehooks-ts" -import {H2, H3, H4} from "@components/elements/headers" +import {H2, H3, H4, H5} from "@components/elements/headers" import {ChevronDownIcon} from "@heroicons/react/20/solid" import {clsx} from "clsx" import {twMerge} from "tailwind-merge" +export type AccordionHeaderChoice = "h2" | "h3" | "h4" | "h5" + type Props = HTMLAttributes & { /** * Button clickable element or string. @@ -15,7 +17,7 @@ type Props = HTMLAttributes & { /** * Heading level element. */ - headingLevel?: "h2" | "h3" | "h4" + headingLevel?: AccordionHeaderChoice /** * If the accordion should be visible on first render. */ @@ -63,7 +65,23 @@ const Accordion = ({ // When the accordion is externally controlled. const isExpanded = onClick ? isVisible : expanded - const Heading = headingLevel === "h2" ? H2 : headingLevel === "h3" ? H3 : H4 + let Heading + switch (headingLevel) { + case "h3": + Heading = H3 + break + + case "h4": + Heading = H4 + break + + case "h5": + Heading = H5 + break + + default: + Heading = H2 + } return (
diff --git a/src/components/paragraphs/stanford-faq/expand-collapse-all.tsx b/src/components/paragraphs/stanford-faq/expand-collapse-all.tsx index 36dfbbc..bffd332 100644 --- a/src/components/paragraphs/stanford-faq/expand-collapse-all.tsx +++ b/src/components/paragraphs/stanford-faq/expand-collapse-all.tsx @@ -3,11 +3,13 @@ import Button from "@components/elements/button" import {useBoolean} from "usehooks-ts" import {HTMLAttributes, useEffect, useRef} from "react" +import {MinusIcon, PlusIcon} from "@heroicons/react/16/solid" +import twMerge from "@lib/utils/twMergeConfig" type Props = HTMLAttributes const ExpandCollapseAll = ({...props}: Props) => { - const {value: expand, toggle} = useBoolean(false) + const {value: expand, toggle} = useBoolean(true) const ref = useRef(null) useEffect(() => { @@ -24,9 +26,18 @@ const ExpandCollapseAll = ({...props}: Props) => { }, [expand]) return ( - // @ts-expect-error ref object type issue. - ) } diff --git a/src/components/paragraphs/stanford-faq/faq-paragraph.tsx b/src/components/paragraphs/stanford-faq/faq-paragraph.tsx index 3f7f7a8..cf83b6c 100644 --- a/src/components/paragraphs/stanford-faq/faq-paragraph.tsx +++ b/src/components/paragraphs/stanford-faq/faq-paragraph.tsx @@ -1,20 +1,42 @@ import {HtmlHTMLAttributes} from "react" import {ParagraphStanfordFaq} from "@lib/gql/__generated__/drupal.d" -import {H2} from "@components/elements/headers" +import {H2, H3, H4} from "@components/elements/headers" import Wysiwyg from "@components/elements/wysiwyg" -import Accordion from "@components/elements/accordion" +import Accordion, {AccordionHeaderChoice} from "@components/elements/accordion" import twMerge from "@lib/utils/twMergeConfig" import ExpandCollapseAll from "@components/paragraphs/stanford-faq/expand-collapse-all" +import {getParagraphBehaviors} from "@components/paragraphs/get-paragraph-behaviors" +import {FAQParagraphBehaviors} from "@lib/drupal/drupal-jsonapi.d" type Props = HtmlHTMLAttributes & { paragraph: ParagraphStanfordFaq } const FaqParagraph = ({paragraph, ...props}: Props) => { + const behaviors = getParagraphBehaviors(paragraph) + const headerTag = behaviors.faq_accordions?.heading || "h2" + + let Header = H2 + if (headerTag === "h3") Header = H3 + if (headerTag == "h4") Header = H4 + + const heading = paragraph.suFaqHeadline + + let accordionHeadingLevel: AccordionHeaderChoice = "h2" + if (heading) { + if (headerTag === "h2") accordionHeadingLevel = "h3" + if (headerTag === "h3") accordionHeadingLevel = "h4" + if (headerTag === "h4") accordionHeadingLevel = "h5" + } + return ( -
+
- {paragraph.suFaqHeadline &&

{paragraph.suFaqHeadline}

} + {heading && ( +
+ {heading} +
+ )}
@@ -27,7 +49,7 @@ const FaqParagraph = ({paragraph, ...props}: Props) => { buttonProps={{className: "mt-6"}} key={question.id} button={question.suAccordionTitle} - headingLevel={paragraph.suFaqHeadline ? "h3" : "h2"} + headingLevel={accordionHeadingLevel} > diff --git a/src/lib/drupal/drupal-jsonapi.d.tsx b/src/lib/drupal/drupal-jsonapi.d.tsx index 62280b2..d3f8586 100644 --- a/src/lib/drupal/drupal-jsonapi.d.tsx +++ b/src/lib/drupal/drupal-jsonapi.d.tsx @@ -41,9 +41,19 @@ export type TeaserParagraphBehaviors = { } } +export type FAQParagraphBehaviors = { + faq_accordions?: {heading?: "h2" | "h3" | "h4"} +} + type ParagraphBehaviorBase = { layout_paragraphs?: LayoutParagraphBehaviors } export type ParagraphBehaviors = ParagraphBehaviorBase & - (ListParagraphBehaviors | CardParagraphBehaviors | BannerParagraphBehaviors | TeaserParagraphBehaviors) + ( + | ListParagraphBehaviors + | CardParagraphBehaviors + | BannerParagraphBehaviors + | TeaserParagraphBehaviors + | FAQParagraphBehaviors + )