UIBeginnerSSR-safehooks/ui/useAccordion.ts

useAccordion

Single or multi open panels.

Import

TypeScript
import { useAccordion } from '@/hooks';

API

Signature

useAccordion(options?) → { value, isOpen, toggle, ... }

When to use

Use useAccordion instead of re-implementing the same pattern in multiple components.

SSR / Next.js

DOM extras (scroll lock, focus trap) run only in effects.

Live playground

Edit the example and see it run against the real @/hooks package in this app.

Live playgroundEdit · run with real @/hooks · useAccordion
Editor
function Faq() {
  const acc = useAccordion({ multiple: false, defaultValue: 'a' });
  return (
    <div>
      <button onClick={() => acc.toggle('a')}>A {acc.isOpen('a') ? '−' : '+'}</button>
      {acc.isOpen('a') && <p>Answer A</p>}
      <button onClick={() => acc.toggle('b')}>B {acc.isOpen('b') ? '−' : '+'}</button>
      {acc.isOpen('b') && <p>Answer B</p>}
    </div>
  );
}
Preview

Implementation source

Full library source for useAccordion — copy into your project when you are not installing a package yet.

hooks/ui/useAccordion.ts
import { useCallback, useState } from 'react';

export interface UseAccordionOptions {
  /** Allow multiple open items. @default false */
  multiple?: boolean;
  defaultValue?: string | string[];
}

/** Accordion open-item state. */
export function useAccordion(options: UseAccordionOptions = {}) {
  const { multiple = false, defaultValue } = options;
  const [value, setValue] = useState<string[]>(() => {
    if (defaultValue == null) return [];
    return Array.isArray(defaultValue) ? defaultValue : [defaultValue];
  });

  const isOpen = useCallback((id: string) => value.includes(id), [value]);

  const toggle = useCallback(
    (id: string) => {
      setValue((prev) => {
        const open = prev.includes(id);
        if (multiple) {
          return open ? prev.filter((x) => x !== id) : [...prev, id];
        }
        return open ? [] : [id];
      });
    },
    [multiple],
  );

  const open = useCallback(
    (id: string) => {
      setValue((prev) => {
        if (prev.includes(id)) return prev;
        return multiple ? [...prev, id] : [id];
      });
    },
    [multiple],
  );

  const close = useCallback((id: string) => {
    setValue((prev) => prev.filter((x) => x !== id));
  }, []);

  return { value, setValue, isOpen, toggle, open, close };
}

Copy-paste example

useAccordion.example.tsx
'use client';

import { useAccordion } from '@/hooks';

export function Faq() {
  const acc = useAccordion({ multiple: false, defaultValue: 'a' });
  return (
    <div>
      <button onClick={() => acc.toggle('a')}>A {acc.isOpen('a') ? '−' : '+'}</button>
      {acc.isOpen('a') && <p>Answer A</p>}
      <button onClick={() => acc.toggle('b')}>B {acc.isOpen('b') ? '−' : '+'}</button>
      {acc.isOpen('b') && <p>Answer B</p>}
    </div>
  );
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: These are headless — you own the markup and styles.
  • Source of truth: open hooks/ui/useAccordion.ts for implementation details