Skip to content
Page·Free

FAQ with search (Svelte)

A FAQ section for Svelte 5 in TypeScript. The search box filters questions as you type, and two buttons open or close all the answers. It is built on details elements, so keyboard and screen reader support comes from the browser.

Free to install.

If PocketCode is installed on your Android device, this opens the page in the app.

Preview

FaqPage.svelte · 181 lines
<!--
  FAQ page for Svelte 5 (runes + TypeScript), with no libraries. A search box filters the
  questions as you type, every answer opens and closes on its own (native <details>, so it
  works with the keyboard and screen readers), and two buttons open or close them all.

    <FaqPage title="Help" items={[{ question: 'Is it free?', answer: 'Yes, the basic plan is.' }]} />

  Colours come from CSS variables with fallbacks: --faq-text, --faq-muted, --faq-border.
-->
<script lang="ts">
  export interface FaqItem {
    question: string;
    answer: string;
  }

  interface Props {
    title?: string;
    items?: FaqItem[];
  }

  let {
    title = 'Frequently asked questions',
    items = [
      { question: 'How do I reset my password?', answer: 'Open Settings, then Account, and choose Reset password. We send a link to your email.' },
      { question: 'Can I use it offline?', answer: 'Yes. Your files stay on the device and sync the next time you are online.' },
      { question: 'How do I delete my account?', answer: 'Open Settings, then Account, and choose Delete account. This removes your data from our servers.' },
    ],
  }: Props = $props();

  const uid = $props.id();
  let query = $state('');
  let open = $state<boolean[]>([]);

  const words = $derived(query.toLowerCase().split(/\s+/).filter(Boolean));
  const visible = $derived(
    items
      .map((item, index) => ({ item, index }))
      .filter(({ item }) => {
        const text = `${item.question} ${item.answer}`.toLowerCase();
        return words.every((word) => text.includes(word));
      }),
  );

  function setAll(value: boolean) {
    open = items.map(() => value);
  }
</script>

<section class="faq" aria-labelledby="{uid}-title">
  <h2 id="{uid}-title">{title}</h2>

  <div class="tools">
    <label for="{uid}-search">Search the questions</label>
    <input id="{uid}-search" type="search" bind:value={query} autocomplete="off" />
    <div class="all">
      <button type="button" onclick={() => setAll(true)}>Open all</button>
      <button type="button" onclick={() => setAll(false)}>Close all</button>
    </div>
  </div>

  <p class="count" role="status">
    {#if words.length}
      {visible.length} of {items.length} {items.length === 1 ? 'question' : 'questions'}
    {/if}
  </p>

  {#each visible as { item, index } (index)}
    <details bind:open={open[index]}>
      <summary>{item.question}</summary>
      <p>{item.answer}</p>
    </details>
  {:else}
    <p class="empty">No questions match "{query}".</p>
  {/each}
</section>

<style>
  .faq {
    max-width: 40rem;
    font-family: system-ui, sans-serif;

This preview is shortened. You get the full content when you install it.

Changes in 1.0.0

First version.

Related

Contact form (HTML + CSS)FreePageA contact form in one HTML file: name, email, an optional topic and the message. Every field has a label, and a red border appears only after someone types in it or tries to send. Set the form's action to your endpoint. Dark mode follows the system.Settings screen (Compose)FreePageMaterial 3 settings screen with switches, a theme choice and a text size slider. It holds no state: you pass a SettingsState in and get the changed copy back. Each row is one touch target of 56 dp or more that TalkBack reads as a switch or radio button.Toggle switch (Svelte)FreeButtonAn on/off switch for Svelte 5. It is a button with role switch, so Space, Enter and screen readers work. You can bind checked or pass onchange, add a second line of text or disable it. The colours come from CSS variables.Sign-up form (Vue)FreePageA sign-up form for Vue 3 as a single-file component in TypeScript, with name, email and password. When you leave a field, it says what is wrong, and screen readers announce the same message. It uses no libraries, and the colours come from CSS variables.Login screen (Compose)FreePageEmail and password screen in Material 3. It checks both fields when you tap Sign in, shows the error your server returns, and can add Forgot password and fingerprint buttons. You call BiometricPrompt yourself.Landing hero (Next.js + Tailwind)FreePageHero section for the Next.js App Router: a title, a subtitle and two links over a soft gradient. It uses dark colours when the system is in dark mode.