# FAQ

A help center FAQ: topic filters as toggle `Button`s, questions as native `<details>` disclosures, a search `Input` that filters questions and highlights every match, and a Still have questions? `Card` with ways to reach support.

**Example: Frequently asked questions** (`examples/recipes/faq.tsx`)

```tsx
'use client'

import { Body, Button, Card, Heading, Input, Stack, Text } from '@blocks/design-system'
import { ChevronDown, Mail, MessageCircle, Search } from 'lucide-react'
import { Fragment, type ReactNode, useState } from 'react'
import styles from './faq.module.css'

const categories = ['Getting started', 'Billing', 'Security', 'Integrations'] as const
type Category = (typeof categories)[number]

const questions: { category: Category; question: string; answer: string }[] = [
  {
    category: 'Getting started',
    question: 'How do I invite my team?',
    answer: 'Open Settings, then Members, and paste up to 50 email addresses. Invites expire after 7 days; you can resend them from the same page.',
  },
  {
    category: 'Getting started',
    question: 'Can I import projects from a spreadsheet?',
    answer: 'Yes. Choose Import, then CSV, and map your columns to fields. Rows with errors are skipped and listed in a report you can download.',
  },
  {
    category: 'Billing',
    question: 'When am I charged?',
    answer: 'Monthly plans are charged on the day you subscribed. Annual plans are charged once a year, and adding seats mid-term is prorated to the day.',
  },
  {
    category: 'Billing',
    question: 'Do you offer discounts for nonprofits?',
    answer: 'Registered nonprofits and schools get 40% off any plan. Email us your registration number and we’ll apply the discount to your next invoice.',
  },
  {
    category: 'Billing',
    question: 'How do I download invoices?',
    answer: 'Owners and billing admins can download every invoice as a PDF from Settings, then Billing. Each invoice lists your tax ID if you’ve added one.',
  },
  {
    category: 'Security',
    question: 'Is my data encrypted?',
    answer: 'Data is encrypted in transit with TLS 1.3 and at rest with AES-256. Backups are encrypted with separate keys and kept for 30 days.',
  },
  {
    category: 'Security',
    question: 'Can we require single sign-on?',
    answer: 'On the Business plan, admins can require SAML single sign-on for every member and remove password sign-in for the workspace.',
  },
  {
    category: 'Integrations',
    question: 'Which calendar apps do you support?',
    answer: 'Any calendar that subscribes to an iCal feed. Each project has a private feed URL that updates every 15 minutes.',
  },
]

// Wraps each case-insensitive match of `query` in <mark>.
function highlight(text: string, query: string): ReactNode {
  if (!query) return text
  const escaped = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  return text.split(new RegExp(`(${escaped})`, 'gi')).map((part, index) =>
    // Splitting on a capture group puts the matches at the odd indexes.
    index % 2 === 1 ? (
      // biome-ignore lint/suspicious/noArrayIndexKey: the parts of one string never reorder
      <mark key={index} className={styles.mark}>
        {part}
      </mark>
    ) : (
      // biome-ignore lint/suspicious/noArrayIndexKey: the parts of one string never reorder
      <Fragment key={index}>{part}</Fragment>
    ),
  )
}

export default function Faq() {
  const [category, setCategory] = useState<Category | 'All'>('All')
  const [query, setQuery] = useState('')
  const terms = query.trim()

  const shown = questions.filter(
    (item) =>
      (category === 'All' || item.category === category) &&
      `${item.question} ${item.answer}`.toLowerCase().includes(terms.toLowerCase()),
  )

  return (
    <Stack as="section" gap="lg" aria-labelledby="faq-title">
      <Stack gap="sm">
        <Heading as="h2" id="faq-title" fontSize="xl">
          Frequently asked questions
        </Heading>
        <Body color="text-muted" margin="none">
          Answers about accounts, billing and keeping your work safe.
        </Body>
      </Stack>

      <search className={styles.search}>
        <Search aria-hidden size={16} className={styles.searchIcon} />
        <Input type="search" aria-label="Search questions" placeholder="Search questions" value={query} onChange={(event) => setQuery(event.target.value)} />
      </search>

      <fieldset className={styles.chips}>
        <legend className={styles.srOnly}>Filter by topic</legend>
        {(['All', ...categories] as const).map((option) => (
          <Button
            key={option}
            type="button"
            size="sm"
            borderRadius="pill"
            variant={category === option ? 'primary' : 'base'}
            aria-pressed={category === option}
            onClick={() => setCategory(option)}
          >
            {option}
          </Button>
        ))}
      </fieldset>

      <Text fontSize="sm" color="text-muted" role="status">
        {shown.length === 1 ? '1 question' : `${shown.length} questions`}
        {terms && ` matching “${terms}”`}
      </Text>

      {shown.length === 0 ? (
        <Stack gap="sm" align="start" dangerouslySetClassName={styles.empty}>
          <Text fontSize="sm">Try a shorter word, or another topic. You can also ask us directly below.</Text>
          <Button
            type="button"
            size="sm"
            onClick={() => {
              setQuery('')
              setCategory('All')
            }}
          >
            Show all questions
          </Button>
        </Stack>
      ) : (
        <div className={styles.list}>
          {shown.map((item) => (
            // While searching every match opens, so highlights in answers show.
            <details key={item.question} className={styles.item} open={Boolean(terms)}>
              <summary className={styles.summary}>
                <span>{highlight(item.question, terms)}</span>
                <ChevronDown aria-hidden size={18} className={styles.chevron} />
              </summary>
              <Body fontSize="sm" color="text-muted" margin="none" dangerouslySetClassName={styles.answer}>
                {highlight(item.answer, terms)}
              </Body>
            </details>
          ))}
        </div>
      )}

      <Card as="aside" padding="lg" borderRadius="xl" aria-labelledby="faq-contact" dangerouslySetClassName={styles.contact}>
        <Stack gap="xs">
          <Heading as="h3" id="faq-contact" fontSize="md">
            Still have questions?
          </Heading>
          <Text fontSize="sm" color="text-muted">
            Our support team answers within one business day.
          </Text>
        </Stack>
        <Stack direction="row" gap="sm" wrap="wrap">
          <Button as="a" href="#faq-chat" variant="primary">
            <MessageCircle aria-hidden size={16} />
            Start a chat
          </Button>
          <Button as="a" href="#faq-email">
            <Mail aria-hidden size={16} />
            Email us
          </Button>
        </Stack>
      </Card>
    </Stack>
  )
}
```

## How it works

- Each question is a `<details>` with a styled `<summary>`, so the browser provides the button, the expanded state and the keyboard behavior. The module hides the default marker and turns the chevron with `[open]`.
- The search matches questions and answers. While it has a query every match opens, so highlights inside answers are visible. Matches are wrapped in `<mark>`, with the query escaped before it becomes a regular expression.
- The topic filters are buttons with `aria-pressed` in a `<fieldset>` with a visually hidden legend. A `role="status"` line counts what’s showing, and when nothing matches a single button resets the search and the topic.

## App-owned CSS

`examples/recipes/faq.module.css`:

```css
.search {
  position: relative;
  max-inline-size: 26rem;

  & input {
    padding-left: calc(var(--ds-space-md) * 2 + var(--ds-icon-size-sm));
  }
}

.searchIcon {
  position: absolute;
  top: 50%;
  left: var(--ds-space-md);
  translate: 0 -50%;
  color: var(--ds-color-text-muted);
  pointer-events: none;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--ds-space-sm);
  min-inline-size: 0;
  margin: 0;
  padding: 0;
  border: none;
}

.srOnly {
  position: absolute;
  inline-size: var(--ds-space-xxs);
  block-size: var(--ds-space-xxs);
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.empty {
  padding: var(--ds-space-lg);
  background-color: var(--ds-color-bg-subtle);
  border-radius: var(--ds-border-radius-md);
}

.list {
  border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
}

.item {
  border-bottom: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
}

/* A native disclosure: summary is the button, the chevron turns when open */
.summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--ds-space-lg);
  padding: var(--ds-space-lg) var(--ds-space-sm);
  font-weight: var(--ds-font-weight-semibold);
  list-style: none;
  cursor: pointer;
  border-radius: var(--ds-border-radius-sm);

  &::-webkit-details-marker {
    display: none;
  }

  &:hover {
    color: var(--ds-color-accent-text);
  }

  &:focus-visible {
    outline: var(--ds-border-width-md) solid var(--ds-color-focus-ring);
    outline-offset: calc(var(--ds-border-width-md) * -1);
  }
}

.chevron {
  flex-shrink: 0;
  color: var(--ds-color-text-muted);
  transition: rotate var(--ds-duration-normal) var(--ds-easing-standard);

  .item[open] & {
    rotate: 180deg;
  }
}

.answer {
  max-inline-size: 36rem;
  padding: 0 var(--ds-space-sm) var(--ds-space-lg);
}

.mark {
  padding: 0 var(--ds-space-xs);
  color: inherit;
  background-color: var(--ds-color-warning-subtle);
  border-radius: var(--ds-border-radius-sm);
}

.contact {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--ds-space-lg);
  background-color: var(--ds-color-bg-subtle);
}

@media (prefers-reduced-motion: reduce) {
  .chevron {
    transition: none;
  }
}
```
