Skip to content
ui8kit

primitive / primitives

Switch

Switch primitive

Install

npx ui8kit add switch

Pulls in: kit-switch, recipes, utils

Preview

size: default
size: sm

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-cdn
npx ui8kit add switch
compose/Switch.svelte
<!-- Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. -->
<!-- Switch: Switch + recipe switch. -->
<script lang="ts">
  import type { ComponentProps, Snippet } from "svelte";
  import Prim from "../kit/ui/switch/Switch.svelte";
  import { cn } from "../kit/utils";
  import { rc } from "./recipes";

  let { "size": v_size, class: className, children, ...rest }: ComponentProps<typeof Prim> & { "size"?: string | number; class?: string; children?: Snippet } = $props();
  const cls = $derived(cn(rc("switch", { "size": v_size }), className));
</script>

<Prim class={cls} {...rest} />

Svelte registry item (JSON)

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-cdn
npx ui8kit add switch
compose/switch.tsx
// Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT.
import type { ComponentProps } from "react";
import { Switch as Prim } from "../kit/ui/switch/switch";
import { cn } from "../kit/utils";
import { rc } from "./recipes";

export type SwitchProps = Omit<ComponentProps<typeof Prim>, "tag" | "size"> & { "size"?: string | number; className?: string };

/** Switch: Switch + recipe switch. */
export function Switch({ "size": v_size, className, ...rest }: SwitchProps) {
  return <Prim className={cn(rc("switch", { "size": v_size }), className)} {...rest} />;
}

React registry item (JSON)

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-cdn
npx ui8kit add switch
compose/Switch.vue
<!-- Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. -->
<!-- Switch: Switch + recipe switch. -->
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import Prim from "../kit/ui/switch/Switch.vue";
import { cn } from "../kit/utils";
import { rc } from "./recipes";

defineOptions({ name: "Switch", inheritAttrs: false });
const props = defineProps<{ "size"?: string | number }>();
const attrs = useAttrs();
const rest = computed(() => {
  const { class: _class, ...r } = attrs;
  return r;
});
const cls = computed(() => cn(rc("switch", { "size": props["size"] }), attrs.class as string | undefined));
const tagValue = computed(() => "");
</script>

<template>
  <Prim :class="cls" v-bind="rest" />
</template>

Vue registry item (JSON)

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-cdn
npx ui8kit add switch
compose/switch.solid.tsx
// Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT.
import { splitProps, type ComponentProps } from "solid-js";
import { Switch as Prim } from "../kit/ui/switch/switch.solid";
import { cn } from "../kit/utils";
import { rc } from "./recipes";

export type SwitchProps = Omit<ComponentProps<typeof Prim>, "tag" | "class" | "className" | "size"> & { "size"?: string | number; class?: string; className?: string };

/** Switch: Switch + recipe switch. */
export function Switch(props: SwitchProps) {
  const [local, rest] = splitProps(props, ["size", "class", "className"]);
  return <Prim class={cn(rc("switch", { "size": local["size"] }), local.class ?? local.className)} {...rest} />;
}

Solid registry item (JSON)

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-cdn
npx ui8kit add switch
compose/switch.templ
// Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT.

package compose

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

// SwitchProps: Switch: Switch + recipe switch.
type SwitchProps struct {
	Size string
	formswitch.SwitchProps
}

func switchInner(p SwitchProps) formswitch.SwitchProps {
	in := p.SwitchProps
	in.Class = Join(Rc("switch", map[string]string{"size": p.Size}), p.SwitchProps.Class)
	return in
}

// Switch renders Switch with the composition classes.
templ Switch(p SwitchProps) {
	@formswitch.Switch(switchInner(p))
}

templ registry item (JSON)

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-cdn
npx ui8kit add switch
compose/Switch.html.twig
{# Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. #}
{# Switch: Switch + recipe switch. Params: size, class, attrs + the params of Switch. #}
{% include 'ui/switch/Switch.html.twig' with compose_props('Switch', _context) only %}

Twig registry item (JSON)

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-cdn
npx ui8kit add switch
compose/Switch.latte
{* Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. *}
{* Switch: Switch + recipe switch. *}
{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 = '',
  $size = null,
}
{include '../ui/switch/Switch.latte', ...\CodegenX\Compose\Latte\Compose::props('Switch', ['class' => $class, 'checked' => $checked, 'disabled' => $disabled, 'id' => $id, 'name' => $name, 'value' => $value, 'ariaLabel' => $ariaLabel, 'ariaLabelledBy' => $ariaLabelledBy, 'attrs' => $attrs, 'children' => $children, 'size' => $size])}

Latte registry item (JSON)

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-cdn
npx ui8kit add switch
compose/switch.html.j2
{#- Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT.
    Switch: Switch + recipe switch. Props (p): Size, Class, Attrs + the props of Switch. -#}
{%- with p = compose_props('Switch', p) %}{% include 'ui/switch/switch.html.j2' %}{% endwith %}

Jinja2 registry item (JSON)

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.

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

Props of Switch
PropTypeDefault Description
checked boolean-
disabled boolean-