import { styled } from "goober"; import { PropsWithChildren, useEffect, useState } from "react"; type AccordionProps = PropsWithChildren<{ title: string; storageKey: string; }>; export function Accordion({ title, children, storageKey }: AccordionProps) { const [open, setOpen] = useStoragedState(storageKey, false); return (
{ e.preventDefault(); setOpen((v) => !v); }} > {title} {children}
); } function useStoragedState( key: string, defaultValue: T, ): [T, React.Dispatch>] { const [state, setState] = useState(() => { if (typeof window === "undefined") return defaultValue; const stored = localStorage.getItem(key); return stored ? JSON.parse(stored) : defaultValue; }); useEffect(() => { localStorage.setItem(key, JSON.stringify(state)); }, [state]); return [state, setState]; } const StyledSummary = styled("summary")` font-size: 1.125rem; cursor: pointer; font-weight: 500; color: var(--j-text-color-strong); `;