مربع اختيار
محدد أو غير محدد أو مختلط. حقل مخفي يرسل القيمة الافتراضية قبل الترطيب، وإعادة ضبط النموذج تعيدها.
client - الحجم لم يُقَس بعد
مربع اختيار
client - الحجم لم يُقَس بعدمحدد أو غير محدد أو مختلط. حقل مخفي يرسل القيمة الافتراضية قبل الترطيب، وإعادة ضبط النموذج تعيدها.
جرّبه
size
invalid
الشيفرة
checkbox.tsx
import { Checkbox } from 'topclassui/checkbox'
<div className="flex items-center gap-2">
<Checkbox id="checkbox-playground" defaultChecked />
<label htmlFor="checkbox-playground" className="text-sm text-body">I accept the terms</label>
</div>المربع زر أصلي <button role="checkbox">، وبجانبه مربع اختيار مخفي هو ما يرسله النموذج.
كل الحالات، بدون تمرير الفأرة
9 حالاتأو استورده من الحزمة
topclassui/checkbox
import { Checkbox } from 'topclassui/checkbox'ثبّت مرة واحدة:npm i topclassui@beta zod
أمثلة
- لاختيار يرسله النموذج لاحقًا. أما الإعداد الذي يسري فورًا فيناسبه Switch أكثر.
- غير المحدد لا يرسل شيئًا، والمحدد يرسل on، قبل الترطيب وبعده. ويمكن لقيمة عدم التحديد أن تجعل المربع غير المحدد يرسل نصًا خاصًا به.
- المربع المختلط يرسم شَرطة ويضبط aria-checked على mixed؛ وإلغاؤه مسؤوليتك، ولا يرسل شيئًا ما دام مختلطًا.
- لا يرسم Checkbox تسمية خاصة به: اكتب التسمية بجانبه أو حوله، وأعطِ المربع
id. - في سمة Ocean يكون المربعان
xsوsmدائريين: لا يميّزهما عن زر الراديو إلا علامة الاختيار أو الشَرطة.
basic
basic.tsx
import { Checkbox } from 'topclassui/checkbox'
/** Outside a Field: a native label points at the id of the checkbox, 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">
<Checkbox id="checkbox-basic-terms" defaultChecked />
<label htmlFor="checkbox-basic-terms" className="text-sm text-body">I accept the terms</label>
</div>
<div className="flex items-center gap-2">
<Checkbox id="checkbox-basic-news" />
<label htmlFor="checkbox-basic-news" className="text-sm text-body">Send me product news</label>
</div>
</div>
)
}
labelled
labelled.tsx
import { Checkbox } from 'topclassui/checkbox'
/** The two native ways to name a box: a label that points at its id, and a label that wraps it. A click on either presses it. */
export default function Demo() {
return (
<div className="grid justify-items-start gap-3">
<div className="flex items-center gap-2">
<Checkbox id="checkbox-labelled-for" />
<label htmlFor="checkbox-labelled-for" className="text-sm text-body">Named by a label that points at it</label>
</div>
<label className="flex items-center gap-2 text-sm text-body">
<Checkbox id="checkbox-labelled-wrap" />
Named by a label that wraps it
</label>
</div>
)
}
form
form.tsx
import { Button } from 'topclassui/button'
import { Checkbox } from 'topclassui/checkbox'
/** A checked box and an unchecked one with uncheckedValue, in a plain form: terms=on and news=no, 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">
<Checkbox id="checkbox-form-terms" name="terms" defaultChecked />
<label htmlFor="checkbox-form-terms" className="text-sm text-body">I accept the terms</label>
</div>
<div className="flex items-center gap-2">
<Checkbox id="checkbox-form-news" name="news" value="yes" uncheckedValue="no" />
<label htmlFor="checkbox-form-news" className="text-sm text-body">Send me product news</label>
</div>
<Button type="submit">Save</Button>
</form>
)
}
sizes
sizes.tsx
import { Checkbox } from 'topclassui/checkbox'
/** The five sizes, checked. Each target stays 24 px, and 44 px under a finger, whatever the box. */
export default function Demo() {
return (
<div className="flex flex-wrap items-center gap-4">
<Checkbox size="xs" defaultChecked aria-label="Extra small" />
<Checkbox size="sm" defaultChecked aria-label="Small" />
<Checkbox size="md" defaultChecked aria-label="Medium" />
<Checkbox size="lg" defaultChecked aria-label="Large" />
<Checkbox size="xl" defaultChecked aria-label="Extra large" />
</div>
)
}
بالعربية، من اليمين إلى اليسار
rtl.tsx
import { TopClassProvider } from 'topclassui/provider'
import { Checkbox } from 'topclassui/checkbox'
/** Arabic, inside TopClassProvider as the root layout would mount it. The box sits at the right, before its label. */
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">
<Checkbox id="checkbox-rtl-terms" name="terms" defaultChecked />
<label htmlFor="checkbox-rtl-terms" className="text-sm text-body">أوافق على الشروط</label>
</div>
<div className="flex items-center gap-2">
<Checkbox id="checkbox-rtl-news" name="news" />
<label htmlFor="checkbox-rtl-news" className="text-sm text-body">أرسل لي أخبار المنتج</label>
</div>
</div>
</TopClassProvider>
)
}
الخصائص
لوحة المفاتيح وإمكانية الوصول
- الاسم مطلوب:
idلتسمية<label htmlFor>، أوaria-label، أوaria-labelledby. والنقر على التسمية يضغط المربع. - مساحة الهدف 24px، و44px تحت مؤشر لمس، مهما كان
sizeالمرسوم: حلقة التركيز تحيط بالمربع. invalidيضبط aria-invalid، ويتحوّل خط المربع إلى لون الخطر ويزداد سمكًا، محددًا كان أو لا.
من اليمين إلى اليسار
يقع المربع على اليمين، قبل تسميته، دون أي تغيير في شيفرتك: الترتيب منطقي لا فيزيائي.
الخادم والمتصفح
مكوّن متصفح: يُعرض على الخادم ثم يُرطَّب في المتصفح.
الملفات: index.tsx, client.tsx.
على الشارة: client - الحجم لم يُقَس بعد.