مفتاح تبديل
مفتاح تشغيل وإطفاء لإعداد يسري فورًا. يتحرك مقبضه في الاتجاه الصحيح في التخطيطات من اليمين إلى اليسار.
client - الحجم لم يُقَس بعد
مفتاح تبديل
client - الحجم لم يُقَس بعدمفتاح تشغيل وإطفاء لإعداد يسري فورًا. يتحرك مقبضه في الاتجاه الصحيح في التخطيطات من اليمين إلى اليسار.
جرّبه
size
invalid
الشيفرة
switch.tsx
import { Switch } from 'topclassui/switch'
<div className="flex items-center gap-2">
<Switch id="switch-playground" defaultChecked />
<label htmlFor="switch-playground" className="text-sm text-body">Weekly digest</label>
</div>المفتاحان xs وsm يكبران إلى هدف بمقدار 44px تحت مؤشر لمس، فيبقى المقبض سهل اللمس.
كل الحالات، بدون تمرير الفأرة
7 حالاتأو استورده من الحزمة
topclassui/switch
import { Switch } from 'topclassui/switch'ثبّت مرة واحدة:npm i topclassui@beta zod
أمثلة
- لإعداد يسري لحظة تشغيله أو إطفائه. أما الاختيار الذي يرسله النموذج لاحقًا فيناسبه Checkbox أكثر.
- داخل نموذج، المطفأ لا يرسل شيئًا والمشغَّل يرسل on، قبل الترطيب وبعده. وقيمة عدم التحديد تجعل المفتاح المطفأ يرسل نصًا خاصًا به.
- إعادة ضبط النموذج تعيد المفتاح إلى حالته الافتراضية، على الشاشة وفي النموذج.
- لا يرسم Switch تسمية خاصة به: اكتب
<label htmlFor>تشير إلىidالخاص به. size="custom"لا يرسم مقاسًا للمسار: أعطِ المسار مقاسًا من CSS الخاص بك.
basic
basic.tsx
import { Switch } from 'topclassui/switch'
/** Outside a Field: a native label points at the id of the switch, which is the button itself. */
export default function Demo() {
return (
<div className="grid justify-items-start gap-3">
<div className="flex items-center gap-2">
<Switch id="switch-basic-digest" defaultChecked />
<label htmlFor="switch-basic-digest" className="text-sm text-body">Weekly digest</label>
</div>
<div className="flex items-center gap-2">
<Switch id="switch-basic-mentions" />
<label htmlFor="switch-basic-mentions" className="text-sm text-body">Email me when I am mentioned</label>
</div>
</div>
)
}
form
form.tsx
import { Button } from 'topclassui/button'
import { Switch } from 'topclassui/switch'
/** One switch that is on, in a plain form: it sends digest=on, before any script runs and after. */
export default function Demo() {
return (
<form className="grid justify-items-start gap-3">
<div className="flex items-center gap-2">
<Switch id="switch-form-digest" name="digest" defaultChecked />
<label htmlFor="switch-form-digest" className="text-sm text-body">Weekly digest</label>
</div>
<Button type="submit">Save</Button>
</form>
)
}
unchecked-value
unchecked-value.tsx
import { Switch } from 'topclassui/switch'
/** A switch that is off still sends a value: Base UI adds an input of type hidden beside it. */
export default function Demo() {
return (
<form className="grid justify-items-start gap-3">
<div className="flex items-center gap-2">
<Switch id="switch-unchecked-news" name="news" value="yes" uncheckedValue="no" />
<label htmlFor="switch-unchecked-news" className="text-sm text-body">Product news</label>
</div>
</form>
)
}
sizes
sizes.tsx
import { Switch } from 'topclassui/switch'
/** The five sizes, on. Each target stays 24 px, and 44 px under a finger, whatever the track. */
export default function Demo() {
return (
<div className="flex flex-wrap items-center gap-4">
<Switch size="xs" defaultChecked aria-label="Extra small" />
<Switch size="sm" defaultChecked aria-label="Small" />
<Switch size="md" defaultChecked aria-label="Medium" />
<Switch size="lg" defaultChecked aria-label="Large" />
<Switch size="xl" defaultChecked aria-label="Extra large" />
</div>
)
}
بالعربية، من اليمين إلى اليسار
rtl.tsx
import { TopClassProvider } from 'topclassui/provider'
import { Switch } from 'topclassui/switch'
/** Arabic, inside TopClassProvider as the root layout would mount it. The thumb starts at the right and
* slides to the left when the switch is on. */
export default function Demo() {
return (
<TopClassProvider direction="rtl" locale="ar">
<div dir="rtl" lang="ar" className="grid justify-items-start gap-3">
<div className="flex items-center gap-2">
<Switch id="switch-rtl-digest" name="digest" defaultChecked />
<label htmlFor="switch-rtl-digest" className="text-sm text-body">الملخص الأسبوعي</label>
</div>
<div className="flex items-center gap-2">
<Switch id="switch-rtl-mentions" name="mentions" />
<label htmlFor="switch-rtl-mentions" className="text-sm text-body">راسلني عند الإشارة إليّ</label>
</div>
</div>
</TopClassProvider>
)
}
الخصائص
لوحة المفاتيح وإمكانية الوصول
- الاسم مطلوب:
idلتسمية<label htmlFor>، أوaria-label، أوaria-labelledby. والجذر هو<button role="switch">. - مساحة الهدف 24px، و44px تحت مؤشر لمس، مهما كان
sizeالمرسوم: حلقة التركيز تحيط بالمسار. invalidيضبط aria-invalid، ويتحوّل خط المسار إلى لون الخطر ويزداد سمكًا.
من اليمين إلى اليسار
يبدأ المقبض من اليمين وينزلق إلى اليسار عند التشغيل، دون أي تغيير في الشيفرة.
الخادم والمتصفح
مكوّن متصفح: يُعرض على الخادم ثم يُرطَّب في المتصفح.
الملفات: index.tsx, client.tsx.
على الشارة: client - الحجم لم يُقَس بعد.