Switch
Genel görünümimport { Switch } from '@nili/ui';Bu, kütüphanenin kendi genel görünüm sayfası — canlı olarak render ediliyor. Üstten dili veya temayı değiştirin, o da değişir.
Durumlar
Anahtar hemen etki eder — değişiklik bir Kaydet düğmesi gerektiriyorsa istediğiniz şey Checkbox’tır. onCheckedChange yeni durumla tetiklenir; ham olay gerçekten gerekirse onInputChange olarak durur.
import { Switch } from "@nili/ui"
export function Example() {
const [enabled, setEnabled] = useState(true)
return (
<>
<Switch label={t('states.off')} />
<Switch label={t('states.on')} defaultChecked />
<Switch
label="Controlled"
checked={enabled}
onCheckedChange={setEnabled}
/>
<Switch label={t('states.disabled')} disabled />
<Switch label={t('states.disabledOn')} disabled defaultChecked />
</>
)
}Etiket satırı
Checkbox ve Radio ile aynı dört yuva — sublabel, badge, description, linkButton — böylece üçünden kurulmuş bir ayarlar listesi hizalanır.
import { Switch, Badge, LinkButton } from "@nili/ui"
export function Example() {
return (
<Switch
defaultChecked
label="Two-factor authentication"
sublabel="(recommended)"
badge={<Badge size="small" color="green" appearance="lighter">Secure</Badge>}
description="We will ask for a code from your authenticator app at every sign-in."
linkButton={<LinkButton href="#" size="sm">Set up a new device</LinkButton>}
/>
)
}Etiket konumu
Bir ayarlar listesinde anahtar genelde satırın sonunda, metinden uzakta durur. Mantıksaldır, okuma yönünü izler.
import { Switch } from "@nili/ui"
export function Example() {
return (
<>
<Switch label="Switch after the label" labelPosition="start" />
<Switch label="Switch before the label" labelPosition="end" />
</>
)
}Etiketsiz
Çıplak bir anahtara aria-label verin — adsız bir anahtar yalnızca “anahtar” diye duyurulur.
import { Switch } from "@nili/ui"
export function Example() {
return <Switch aria-label="Dark mode" />
}Bir ayarlar listesinde
Olağan biçim: açıklamalı bir anahtar satırı, önce etiket, sonda denetim.
import { Switch } from "@nili/ui"
export function Example() {
return (
<div className="divide-y divide-soft-200 rounded-xl border border-soft-200">
<div className="p-4">
<Switch
labelPosition="start"
label="Email notifications"
description="Product updates and security alerts."
defaultChecked
/>
</div>
…
</div>
)
}Switch
extends Omit< ComponentPropsWithoutRef<'input'>, 'type' | 'size' | 'onChange' >
Diğer proplar
| Prop | Tip | Varsayılan | Notlar |
|---|---|---|---|
| disabled | boolean | — | |
| required | boolean | — | |
| readOnly | boolean | — | |
| checked | boolean | — | |
| defaultChecked | boolean | — | |
| placeholder | string | — | |
| value | string | — | |
| defaultValue | string | — | |
| name | string | — | |
| maxLength | number | — | |
| autoComplete | string | — | |
| aria-label | string | — | |
| label | ReactNode | — | |
| description | ReactNode | — | Secondary line under the label. Figma's "Description" toggle. |
| sublabel | ReactNode | — | Muted text inline after the label — Figma's "Sublabel". |
| badge | ReactNode | — | Trailing slot on the label row — Figma's "Badge". |
| linkButton | ReactNode | — | Action under the description — Figma's "Link Button". |
| labelPosition | start | end | end | Which side the label sits on. Figma calls this "Flip". Logical, so `start` is left in English and right in Persian. |
| onCheckedChange | (checked: boolean) => void | — | Fires with the new state. The raw `onChange` event is still available as `onInputChange` when the event object is genuinely needed. |
| onInputChange | ComponentPropsWithoutRef<'input'>['onChange'] | — | |
| rootClassName | string | — | Wrapper class. Use `className` for the control itself. |
SwitchControl
dahiliSwitchRoot
dahiliVaryant eksenleri — bileşenin cva tanımından
| Prop | Tip | Varsayılan | Notlar |
|---|---|---|---|
| labelPosition | end | start | end | |
| align | center | start | center |