Página·Gratis
Preguntas frecuentes con buscador (Svelte)
Una sección de preguntas frecuentes para Svelte 5 en TypeScript. Un buscador filtra las preguntas mientras escribes, y dos botones abren o cierran todas las respuestas. Usa elementos details, así que el teclado y los lectores de pantalla funcionan sin código extra.
Se instala gratis.
Si tienes PocketCode instalado en tu Android, esto abre la página en la app.
Vista previa
FaqPage.svelte · 181 líneas
<!--
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;Esta vista previa está recortada. El contenido completo llega al instalarlo.
Cambios en la 1.0.0
Primera versión.