Skip to content

Latest commit

 

History

History
165 lines (112 loc) · 5.73 KB

switch.md

File metadata and controls

165 lines (112 loc) · 5.73 KB

Switch

ui.switch · parts: Switch · elements: <button>

Purpose

Switch is an on/off toggle: button[role=switch] with aria-checked. State changes are the consumer's (onClick via attrs/rest).

Notes

  • Renders a <button role="switch"> with aria-checked; wire the toggle yourself (click handler via rest/attrs).
  • Always give an accessible name: aria-label, aria-labelledby or text children.

Switch renders button[type=button][role=switch]; children render the thumb.

Props

Canonical (templ, Jinja2, MiniJinja) React / Solid HTML (Svelte, Vue, WC) Type Default Kind Description
Checked checked checked boolean — prop
Disabled disabled disabled boolean — prop
ID id id string — passthrough attr
Name name name string — passthrough attr
Value value value string — passthrough attr
AriaLabel aria-label aria-label string — passthrough attr
AriaLabelledBy aria-labelledby aria-labelledby string — passthrough attr

Also accepts: class (className in React), extra attributes (rest props in UI frameworks, Attrs in templ/Jinja2/MiniJinja, attrs in Twig/Latte; caller wins), children.

Variants

No variants: the primitive ships no visual classes; style it with token classes via class.

Children

Accepts children (default slot / children / { children... } / trusted HTML string in Twig, Latte, Jinja2, MiniJinja — see runtimes). Void tags selected via tag render no children.

ARIA

  • Always rendered: role="switch".
  • Computed/forwarded: aria-checked, aria-label, aria-labelledby (omitted when empty).
  • Typed ARIA props: aria-label, aria-labelledby — static values only.

Classes escape hatch

Compose the part's classes without rendering it (custom root element, no asChild outside React):

Runtime Helper
React / Solid / Vue / Svelte / WC switchClasses({ ... }) from switch/switch.shared.ts
templ formswitch.SwitchClasses(formswitch.SwitchProps{...})
Twig ui8kit_classes('switchClasses', { class: '…' })
Latte \UI8Kit\Latte\Classes::switchClasses(['class' => '…'])
Jinja2 switch_classes(p)
MiniJinja switch_classes(p) (registered by ui8kit_minijinja::register)

Rendered DOM per state

Canonical renderer output (children = Content); all 10 runtimes are parity-tested to produce this DOM.

State Props HTML
defaults — <button type="button" role="switch" aria-checked="false" data-state="false">Content</button>
on {"Checked":true,"AriaLabel":"Wi-Fi"} <button type="button" role="switch" aria-checked="true" data-state="true" aria-label="Wi-Fi">Content</button>
disabled {"Disabled":true,"Name":"s","Value":"on","ID":"sw","AriaLabelledBy":"l"} <button type="button" role="switch" aria-checked="false" data-state="false" disabled id="sw" name="s" value="on" aria-labelledby="l">Content</button>
Checked {"Checked":true} <button type="button" role="switch" aria-checked="true" data-state="true">Content</button>
Disabled {"Disabled":true} <button type="button" role="switch" aria-checked="false" data-state="false" disabled>Content</button>

Usage

Fixture on ({"Checked":true,"AriaLabel":"Wi-Fi"}) — every snippet renders:

<button type="button" role="switch" aria-checked="true" data-state="true" aria-label="Wi-Fi">Content</button>

React (runtime notes)

import { Switch } from "./ui/switch/switch";

<Switch checked aria-label="Wi-Fi">Content</Switch>

Vue 3 (runtime notes)

<script setup lang="ts">
import Switch from "./ui/switch/Switch.vue";
</script>

<template>
  <Switch checked aria-label="Wi-Fi">Content</Switch>
</template>

Svelte 5 (runtime notes)

<script lang="ts">
  import Switch from "./ui/switch/Switch.svelte";
</script>

<Switch checked={true} aria-label="Wi-Fi">Content</Switch>

SolidJS (runtime notes)

import { Switch } from "./ui/switch/switch.solid";

<Switch checked aria-label="Wi-Fi">Content</Switch>

Web Components (runtime notes)

import { renderSwitch } from "./ui/switch/switch.wc";

document.body.append(renderSwitch({ checked: true, "aria-label": "Wi-Fi" }, "Content"));

Go templ (runtime notes)

import "github.com/ui8kit/codegen/generated/templ/ui/switch" // package formswitch

@formswitch.Switch(formswitch.SwitchProps{Checked: true, AriaLabel: "Wi-Fi"}) {
	Content
}

Twig (runtime notes)

{% include 'switch/Switch.html.twig' with { checked: true, ariaLabel: 'Wi-Fi', children: 'Content' } only %}

Latte (runtime notes)

{include 'switch/Switch.latte', checked: true, ariaLabel: 'Wi-Fi', children: 'Content'}

Jinja2 (runtime notes)

{% with p = {'Checked': True, 'AriaLabel': 'Wi-Fi'}, children = 'Content' %}{% include 'ui/switch/switch.html.j2' %}{% endwith %}

MiniJinja (Rust) (runtime notes)

{% import 'ui8kit.html' as ui %}
{{ ui.switch({'Checked': true, 'AriaLabel': 'Wi-Fi'}, children='Content') }}

Rules: contract · Index: docs