Kit Switch
UI8Kit 3 Switch primitive (unstyled), from @ui8kit/registry
Install
npx ui8kit add kit-switchPulls in: utils
Preview
No static preview for this item: it needs content or a parent that this page cannot supply. The code is below.
Code
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework svelte --dir src --registry-url https://registry.buildy.tw/svelte --strict-cdnnpx ui8kit add kit-switch<!-- Code generated by @ui8kit/codegen. DO NOT EDIT. -->
<!--
Switch renders button[type=button][role=switch]; children render the thumb.
-->
<script lang="ts" module>
import type { Snippet } from "svelte";
export type SwitchProps = {
checked?: boolean;
disabled?: boolean;
class?: string;
children?: Snippet;
[key: string]: unknown;
};
</script>
<script lang="ts">
import { switchClasses } from "./switch.shared";
import { boolStr, safeRest } from "../../utils/expr";
let {
checked,
disabled,
class: className,
children,
...rawRest
}: SwitchProps = $props();
const rest = $derived(safeRest(rawRest, { blank: ["id", "name", "aria-label", "aria-labelledby"] }));
const cls = $derived(switchClasses({ className }));
</script>
<button type="button" role="switch" aria-checked={boolStr(checked)} data-state={boolStr(checked)} {...(cls ? { class: cls } : {})} disabled={disabled || undefined} {...rest}>
{@render children?.()}
</button>
// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.switch — shared TypeScript contract (types + classes helpers)
import { cn } from "../../utils";
/** Switch renders button[type=button][role=switch]; children render the thumb. */
export type SwitchBaseProps = {
checked?: boolean;
disabled?: boolean;
};
export type SwitchClassesInput = {
className?: string;
};
/** Compose Switch classes without rendering — the cross-runtime `asChild` substitute. */
export function switchClasses(input: SwitchClassesInput = {}): string {
return cn(input.className);
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework react --dir src --registry-url https://registry.buildy.tw/react --strict-cdnnpx ui8kit add kit-switch// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.switch — shared TypeScript contract (types + classes helpers)
import { cn } from "../../utils";
/** Switch renders button[type=button][role=switch]; children render the thumb. */
export type SwitchBaseProps = {
checked?: boolean;
disabled?: boolean;
};
export type SwitchClassesInput = {
className?: string;
};
/** Compose Switch classes without rendering — the cross-runtime `asChild` substitute. */
export function switchClasses(input: SwitchClassesInput = {}): string {
return cn(input.className);
}
// Code generated by @ui8kit/codegen. DO NOT EDIT.
/**
* Switch is an on/off toggle: button[role=switch] with aria-checked.
* State changes are the consumer's (onClick via attrs/rest).
*/
import { forwardRef, type ReactNode, type ButtonHTMLAttributes } from "react";
import { switchClasses } from "./switch.shared";
import { boolStr, safeRest } from "../../utils/expr";
/**
* Switch renders button[type=button][role=switch]; children render the thumb.
*/
export type SwitchProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, "className" | "children" | "checked" | "disabled"> & {
checked?: boolean;
disabled?: boolean;
className?: string;
children?: ReactNode;
};
export const Switch = forwardRef<HTMLButtonElement, SwitchProps>(function Switch(
{ checked, disabled, className, children, ...rawRest },
ref
) {
const rest = safeRest(rawRest, { blank: ["id", "name", "aria-label", "aria-labelledby"] });
const cls = switchClasses({ className });
return (
<button ref={ref} type="button" role="switch" aria-checked={boolStr(checked)} data-state={boolStr(checked)} className={cls || undefined} disabled={disabled} {...rest}>
{children}
</button>
);
});
Switch.displayName = "Switch";
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework vue --dir src --registry-url https://registry.buildy.tw/vue --strict-cdnnpx ui8kit add kit-switch<!-- Code generated by @ui8kit/codegen. DO NOT EDIT. -->
<!--
Switch renders button[type=button][role=switch]; children render the thumb.
-->
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import { switchClasses } from "./switch.shared";
import { boolStr, safeRest } from "../../utils/expr";
defineOptions({ name: "Switch", inheritAttrs: false });
const props = defineProps<{
checked?: boolean;
disabled?: boolean;
}>();
const attrs = useAttrs();
const callerClass = computed(() => attrs.class as string | undefined);
const restAttrs = computed(() => {
const { class: _class, ...restEntries } = attrs;
return safeRest(restEntries, { blank: ["id", "name", "aria-label", "aria-labelledby"] });
});
const cls = computed(() => switchClasses({ className: callerClass.value }));
</script>
<template>
<button type="button" role="switch" :aria-checked="boolStr(props.checked)" :data-state="boolStr(props.checked)" :class="cls || undefined" :disabled="props.disabled || undefined" v-bind="restAttrs">
<slot />
</button>
</template>
// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.switch — shared TypeScript contract (types + classes helpers)
import { cn } from "../../utils";
/** Switch renders button[type=button][role=switch]; children render the thumb. */
export type SwitchBaseProps = {
checked?: boolean;
disabled?: boolean;
};
export type SwitchClassesInput = {
className?: string;
};
/** Compose Switch classes without rendering — the cross-runtime `asChild` substitute. */
export function switchClasses(input: SwitchClassesInput = {}): string {
return cn(input.className);
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework solid --dir src --registry-url https://registry.buildy.tw/solid --strict-cdnnpx ui8kit add kit-switch// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.switch — shared TypeScript contract (types + classes helpers)
import { cn } from "../../utils";
/** Switch renders button[type=button][role=switch]; children render the thumb. */
export type SwitchBaseProps = {
checked?: boolean;
disabled?: boolean;
};
export type SwitchClassesInput = {
className?: string;
};
/** Compose Switch classes without rendering — the cross-runtime `asChild` substitute. */
export function switchClasses(input: SwitchClassesInput = {}): string {
return cn(input.className);
}
// Code generated by @ui8kit/codegen. DO NOT EDIT.
/**
* Switch is an on/off toggle: button[role=switch] with aria-checked.
* State changes are the consumer's (onClick via attrs/rest).
*/
import { splitProps, type JSX, type Component } from "solid-js";
import { switchClasses } from "./switch.shared";
import { boolStr, safeRest } from "../../utils/expr";
/**
* Switch renders button[type=button][role=switch]; children render the thumb.
*/
export type SwitchProps = Omit<JSX.ButtonHTMLAttributes<HTMLButtonElement>, "class" | "className" | "children" | "checked" | "disabled"> & {
checked?: boolean;
disabled?: boolean;
class?: string;
className?: string;
children?: JSX.Element;
};
export const Switch: Component<SwitchProps> = (props) => {
const [local, rest] = splitProps(props, ["checked", "disabled", "class", "className", "children"]);
const cls = () => switchClasses({ className: local.class ?? local.className });
return (
<button type="button" role="switch" aria-checked={boolStr(local.checked)} data-state={boolStr(local.checked)} {...(cls() ? { class: cls() } : {})} disabled={local.disabled || undefined} {...safeRest(rest, { blank: ["id", "name", "aria-label", "aria-labelledby"] })}>
{local.children}
</button>
);
};
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework templ --dir src --registry-url https://registry.buildy.tw/templ --strict-cdnnpx ui8kit add kit-switch// Code generated by @ui8kit/codegen. DO NOT EDIT.
/*
Switch is an on/off toggle: button[role=switch] with aria-checked.
State changes are the consumer's (onClick via attrs/rest).
*/
package formswitch
import (
uiutils "github.com/ui8kit/codegen/generated/templ/utils"
)
// SwitchProps configures Switch.
type SwitchProps struct {
// Class — token class names supplied by the consumer
Class string
Checked bool
Disabled bool
ID string
Name string
Value *string
AriaLabel string
AriaLabelledBy string
Attrs templ.Attributes
}
// SwitchClasses composes Switch classes without rendering —
// the server-composition escape hatch for custom root elements (no asChild in templ).
func SwitchClasses(p SwitchProps) string {
return uiutils.Cn(p.Class)
}
// Switch renders the Switch contract.
templ Switch(p SwitchProps) {
<button
if !uiutils.HasAttr(p.Attrs, "type") {
type="button"
}
if !uiutils.HasAttr(p.Attrs, "role") {
role="switch"
}
if !uiutils.HasAttr(p.Attrs, "aria-checked") {
aria-checked={ uiutils.BoolStr(p.Checked) }
}
if !uiutils.HasAttr(p.Attrs, "data-state") {
data-state={ uiutils.BoolStr(p.Checked) }
}
if SwitchClasses(p) != "" {
class={ SwitchClasses(p) }
}
if !uiutils.HasAttr(p.Attrs, "disabled") {
disabled?={ p.Disabled }
}
if !uiutils.HasAttr(p.Attrs, "id") {
if uiutils.IsSetStr(p.ID) {
id={ p.ID }
}
}
if !uiutils.HasAttr(p.Attrs, "name") {
if uiutils.IsSetStr(p.Name) {
name={ p.Name }
}
}
if !uiutils.HasAttr(p.Attrs, "value") {
if p.Value != nil {
value={ *p.Value }
}
}
if !uiutils.HasAttr(p.Attrs, "aria-label") {
if uiutils.IsSetStr(p.AriaLabel) {
aria-label={ p.AriaLabel }
}
}
if !uiutils.HasAttr(p.Attrs, "aria-labelledby") {
if uiutils.IsSetStr(p.AriaLabelledBy) {
aria-labelledby={ p.AriaLabelledBy }
}
}
{ uiutils.RestAttrs(p.Attrs, false, "id", "name", "aria-label", "aria-labelledby")... }
>
{ children... }
</button>
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework twig --dir src --registry-url https://registry.buildy.tw/twig --strict-cdnnpx ui8kit add kit-switch{# Code generated by @ui8kit/codegen. DO NOT EDIT. #}
{# Switch — Switch renders button[type=button][role=switch]; children render the thumb. #}
{# @param class (string) #}
{# @param checked (bool) #}
{# @param disabled (bool) #}
{# @param id (string) #}
{# @param name (string) #}
{# @param value (string) #}
{# @param ariaLabel (string) #}
{# @param ariaLabelledBy (string) #}
{# @param attrs (attrs) #}
{# @param children (children) #}
{% set class = class|default('') %}
{% set checked = checked|default(false) %}
{% set disabled = disabled|default(false) %}
{% set id = id ?? null %}
{% set name = name ?? null %}
{% set value = value ?? null %}
{% set ariaLabel = ariaLabel ?? null %}
{% set ariaLabelledBy = ariaLabelledBy ?? null %}
{% set attrs = attrs|default([]) %}
{% set children = children|default('') %}
<button{{ ui8kit_attrs({'type': 'button', 'role': 'switch', 'aria-checked': ui8kit_bool_str(checked), 'data-state': ui8kit_bool_str(checked), 'class': ui8kit_or_null(ui8kit_classes('switchClasses', {'class': class, 'checked': checked, 'disabled': disabled, 'id': id, 'name': name, 'value': value, 'ariaLabel': ariaLabel, 'ariaLabelledBy': ariaLabelledBy})), 'disabled': disabled, 'id': ui8kit_or_null(id), 'name': ui8kit_or_null(name), 'value': value, 'aria-label': ui8kit_or_null(ariaLabel), 'aria-labelledby': ui8kit_or_null(ariaLabelledBy)}, attrs, false, ['id', 'name', 'aria-label', 'aria-labelledby']) }}>{{ children|raw }}</button>
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework latte --dir src --registry-url https://registry.buildy.tw/latte --strict-cdnnpx ui8kit add kit-switch{* Code generated by @ui8kit/codegen. DO NOT EDIT. *}
{* Switch — Switch renders button[type=button][role=switch]; children render the thumb. *}
{parameters
string $class = '',
bool $checked = false,
bool $disabled = false,
?string $id = null,
?string $name = null,
?string $value = null,
?string $ariaLabel = null,
?string $ariaLabelledBy = null,
array $attrs = [],
string $children = '',
}
<button n:attr="[
'type' => 'button',
'role' => 'switch',
'aria-checked' => \UI8Kit\Latte\Rt::boolStr($checked),
'data-state' => \UI8Kit\Latte\Rt::boolStr($checked),
'class' => \UI8Kit\Latte\Rt::orNull(\UI8Kit\Latte\Classes::switchClasses(['class' => $class, 'checked' => $checked, 'disabled' => $disabled, 'id' => $id, 'name' => $name, 'value' => $value, 'ariaLabel' => $ariaLabel, 'ariaLabelledBy' => $ariaLabelledBy])),
'disabled' => $disabled,
'id' => \UI8Kit\Latte\Rt::orNull($id),
'name' => \UI8Kit\Latte\Rt::orNull($name),
'value' => $value,
'aria-label' => \UI8Kit\Latte\Rt::orNull($ariaLabel),
'aria-labelledby' => \UI8Kit\Latte\Rt::orNull($ariaLabelledBy),
...\UI8Kit\Latte\Rt::restAttrs($attrs, false, ['id', 'name', 'aria-label', 'aria-labelledby']),
]">{$children|noescape}</button>Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework jinja2 --dir src --registry-url https://registry.buildy.tw/jinja2 --strict-cdnnpx ui8kit add kit-switch{#- Code generated by @ui8kit/codegen. DO NOT EDIT.
Switch: Switch renders button[type=button][role=switch]; children render the thumb.
Props (p): Class (string), Checked (bool), Disabled (bool), ID (string), Name (string), Value (string), AriaLabel (string), AriaLabelledBy (string), Attrs (attrs), Children (children).
children: TRUSTED HTML printed with |safe — never pass unescaped user input.
Classes without rendering: switch_classes(p) (no asChild in Jinja2). -#}
<button
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'type') %} type="button"{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'role') %} role="switch"{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'aria-checked') %} aria-checked="{{ ui_bool_str(ui_def(p['Checked'], false)) }}"{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'data-state') %} data-state="{{ ui_bool_str(ui_def(p['Checked'], false)) }}"{%- endif %}
{%- with cls = switch_classes(p) %}{%- if cls %} class="{{ cls }}"{%- endif %}{%- endwith %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'disabled') %}{%- if ui_def(p['Disabled'], false) %} disabled{%- endif %}{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'id') %}{%- if ui_is_set_str(p['ID']) %} id="{{ p['ID'] }}"{%- endif %}{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'name') %}{%- if ui_is_set_str(p['Name']) %} name="{{ p['Name'] }}"{%- endif %}{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'value') %}{%- if p['Value'] is defined and p['Value'] is not none %} value="{{ p['Value'] }}"{%- endif %}{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'aria-label') %}{%- if ui_is_set_str(p['AriaLabel']) %} aria-label="{{ p['AriaLabel'] }}"{%- endif %}{%- endif %}
{%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'aria-labelledby') %}{%- if ui_is_set_str(p['AriaLabelledBy']) %} aria-labelledby="{{ p['AriaLabelledBy'] }}"{%- endif %}{%- endif %}
{{ ui_attr_html(ui_def(p['Attrs'], {}), false, ['id', 'name', 'aria-label', 'aria-labelledby']) }}
>{{ children|default('', true)|safe }}</button>
Props
Names and types come from the xgen specs of the source. Standard HTML attributes (id, role, tabIndex, aria-*) pass through to the root element; the class prop is className in React.
No props data for this item.