跳转到内容
页面·免费

带搜索的常见问题(Svelte)

用 TypeScript 写的 Svelte 5 常见问题区块。搜索框会在输入时筛选问题,两个按钮可展开或收起全部回答。它基于 details 元素,键盘和读屏软件无需额外代码即可使用。

免费安装。

如果你的 Android 设备上装了 PocketCode,这会在应用里打开此页面。

预览

FaqPage.svelte · 181 行
<!--
  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;

此预览已截短。安装后可获得完整内容。

1.0.0 的变更

首个版本。

相关

联系表单(HTML + CSS)免费页面单个 HTML 文件里的联系表单:姓名、邮箱、可选的主题和留言。每个字段都有自己的标签,只有在用户输入过或尝试提交后才会显示红色边框。把表单的 action 改成你的接收地址。深色模式跟随系统。设置页面(Compose)免费页面Material 3 设置页面,包含开关、主题选择和文字大小滑块。它本身不保存状态:你传入 SettingsState,就会拿回修改后的副本。每一行都是至少 56 dp 的单个触摸区域,TalkBack 会把它读作开关或单选按钮。开关(Svelte)免费按钮Svelte 5 的开关组件,用带 role switch 的按钮实现,空格键、回车键和读屏软件都能正常使用。可以绑定 checked,也可以传入 onchange。支持可选的第二行说明、禁用状态,颜色来自 CSS 变量。注册表单(Vue)免费页面用 TypeScript 写的 Vue 3 单文件组件注册表单,包含姓名、邮箱和密码。离开某个字段后会提示哪里有问题,读屏软件也会读出同样的提示。不依赖任何库;颜色来自 CSS 变量。登录界面(Compose)免费页面Material 3 的邮箱和密码登录界面。点按 Sign in 时检查两个输入框,显示服务器返回的错误,还可以加上“忘记密码”和指纹按钮。BiometricPrompt 需要你自己调用。落地页首屏(Next.js + Tailwind)免费页面用于 Next.js App Router 的首屏区块:柔和渐变背景上有标题、副标题和两个链接。系统处于深色模式时会使用深色配色。