Components › Components
Demos show your theme.
Switch
An on/off switch on a native checkbox.
Wi-Fi: off
Usage #
tscl
@import uiSwitch from './switch.tscl'
<script>
let wifi = state(false)
</script>
<div class="demo">
<uiSwitch label="Wi-Fi" [(checked)]="wifi" />
<p>
Wi-Fi: <span class="out">{{ wifi ? 'on' : 'off' }}</span>
</p>
</div>
<style scoped>
:scope {
display: grid;
gap: 12px;
justify-items: start;
padding: 24px;
border: 1px solid var(--ui-border);
border-radius: var(--ui-radius);
background: var(--ui-bg);
color: var(--ui-fg);
}
p {
margin: 0;
}
</style>Source #
Copy it into your components folder (next to the others) as switch.tscl. Its <style global> is the same as @clarusjs/ui's rules for this part, so using both looks the same.
tscl
{{-- Switch: a native checkbox with role="switch" and its label, bound two ways with [(checked)]. --}}
@import uiSwitch from '@clarusjs/ui-primitives/switch.tscl'
@input label: string
@input name: string = ''
@input disabled: boolean = false
@model checked: boolean = false
<uiSwitch [label]="label" [name]="name" [disabled]="disabled" [(checked)]="checked" />
<style global>
/* The same rules as @clarusjs/ui's ui.css, for this part: change them here to change its look. */
@layer theme, base, components, utilities;
@layer components {
/* Focus is always visible, on every part and what's inside it. */
:where([data-part]):focus-visible,
:where([data-part]) :focus-visible {
outline: 2px solid var(--ui-ring);
outline-offset: 2px;
}
/* Switch and checkbox: the native input, tinted; the label next to it. */
:where([data-part='switch'], [data-part='checkbox']) {
display: inline-flex;
align-items: center;
gap: 8px;
font: var(--ui-font-size) var(--ui-font);
color: var(--ui-fg);
cursor: pointer;
}
:where([data-part='switch'], [data-part='checkbox']) input {
accent-color: var(--ui-accent);
width: 18px;
height: 18px;
margin: 0;
}
:where([data-part='switch']) input {
appearance: none;
width: 36px;
height: 20px;
border-radius: 999px;
background: var(--ui-border);
position: relative;
transition: background 150ms;
}
:where([data-part='switch']) input::after {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: 16px;
height: 16px;
border-radius: 999px;
background: var(--ui-bg);
transition: transform 150ms;
}
:where([data-part='switch']) input:checked {
background: var(--ui-accent);
}
:where([data-part='switch']) input:checked::after {
transform: translateX(16px);
}
:where([data-part='switch'], [data-part='checkbox'])[data-disabled] {
opacity: 0.55;
cursor: not-allowed;
}
@media (prefers-reduced-motion: reduce) {
:where([data-part]),
:where([data-part]) * {
transition: none !important;
}
}
}
</style>Styling #
- On the root: this wrapper is an island around
switch.tsclfrom@clarusjs/ui-primitives, and attributes on its tag don't reach that part yet (a framework limit). Forclass,style,id,aria-*ordata-*on it, put them on a wrapping element of your own, or useswitch.tsclfrom@clarusjs/ui-primitivesdirectly: its root takes them, and these styles apply the same. - Selectors:
data-partnamesswitch,checkbox. - Your rules win: every rule here has zero specificity (
:where()) and sits in@layer components, so your own CSS and Tailwind utilities override it.
Needs #
- theme.css, once per project
-
@clarusjs/ui-primitives≥ 0.1.0: switch.tscl