पेज·मुफ़्त
सर्च वाला FAQ (Svelte)
TypeScript में Svelte 5 के लिए FAQ सेक्शन। सर्च बॉक्स टाइप करते ही सवालों को छाँटता है, और दो बटन सारे जवाब खोलते या बंद करते हैं। यह 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 में बदलाव
पहला संस्करण।