बटन·मुफ़्त
टॉगल स्विच (Svelte)
Svelte 5 के लिए ऑन/ऑफ़ स्विच, जो role switch वाले बटन से बना है, इसलिए Space, Enter और स्क्रीन रीडर काम करते हैं। checked बाइंड करें या onchange दें। इसमें वैकल्पिक दूसरी लाइन, बंद (disabled) स्थिति और CSS वैरिएबल से रंग हैं।
मुफ़्त में इंस्टॉल करें।
अगर आपके Android डिवाइस पर PocketCode इंस्टॉल है, तो यह पेज ऐप में खुलेगा।
प्रीव्यू
ToggleSwitch.svelte · 138 लाइनें
<!--
An on/off switch for Svelte 5 (runes + TypeScript). It is a real <button role="switch">:
Space and Enter toggle it, and screen readers say its name and whether it is on.
Bind the value, or listen to `onchange`:
<ToggleSwitch label="Dark mode" bind:checked={dark} />
<ToggleSwitch label="Sync over mobile data" checked={sync} onchange={(on) => save(on)} />
Colours: --switch-on (the track when on), --switch-off, --switch-thumb, --switch-text.
-->
<script lang="ts">
interface Props {
label: string;
checked?: boolean;
disabled?: boolean;
/** A second line under the label. */
description?: string;
onchange?: (checked: boolean) => void;
}
let { label, checked = $bindable(false), disabled = false, description, onchange }: Props = $props();
const uid = $props.id();
function toggle() {
if (disabled) return;
checked = !checked;
onchange?.(checked);
}
</script>
<div class="row">
<span class="text">
<span id="{uid}-label" class="label">{label}</span>
{#if description}
<span id="{uid}-description" class="description">{description}</span>
{/if}
</span>
<button
type="button"
role="switch"
aria-checked={checked}
aria-labelledby="{uid}-label"
aria-describedby={description ? `${uid}-description` : undefined}
{disabled}
onclick={toggle}
>
<span class="thumb" aria-hidden="true"></span>
</button>
</div>
<style>
.row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
min-height: 48px;
font-family: system-ui, sans-serif;
color: var(--switch-text, #1d1f23);
}
.text {
display: grid;
gap: 0.15rem;
}
.label {
font-weight: 500;
}
.description {
font-size: 0.875rem;
opacity: 0.75;
}
button {
position: relative;
flex: none;
box-sizing: border-box;यह प्रीव्यू छोटा किया गया है। पूरा कंटेंट इंस्टॉल करने पर मिलेगा।
1.0.0 में बदलाव
पहला संस्करण।