Theme Toggle
Theme Toggle primitive
Install
npx ui8kit add theme-togglePulls in: kit-button, kit-icon, recipes, utils
Preview
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 theme-togglecompose/ThemeToggle.svelte
<!-- Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. -->
<!-- ThemeToggle: Button + Lucide icons (data-ui8kit="theme-toggle" behaviour). -->
<script lang="ts">
import type { ComponentProps } from "svelte";
import Prim from "../kit/ui/button/Button.svelte";
import Icon from "../kit/ui/icon/Icon.svelte";
import { cn } from "../kit/utils";
import { rc } from "./recipes";
let { variant, size, "aria-label": ariaLabel, class: className, ...rest }: Omit<ComponentProps<typeof Prim>, "children"> & { variant?: string; size?: string; "aria-label"?: string; class?: string } = $props();
</script>
<Prim type="button" class={cn(rc("themetoggle", { variant, size }), className)} data-ui8kit="theme-toggle" aria-label={ariaLabel || "Dark mode"} aria-pressed="false" {...rest}><Icon class="hidden size-5 dark:block"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg></Icon><Icon class="block size-5 dark:hidden"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg></Icon></Prim>
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 theme-togglecompose/theme-toggle.tsx
// Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT.
import type { ComponentProps } from "react";
import { Button as Prim } from "../kit/ui/button/button";
import { Icon } from "../kit/ui/icon/icon";
import { cn } from "../kit/utils";
import { rc } from "./recipes";
export type ThemeToggleProps = Omit<ComponentProps<typeof Prim>, "children"> & { variant?: string; size?: string; "aria-label"?: string; className?: string };
/** ThemeToggle: Button + Lucide icons (data-ui8kit="theme-toggle" behaviour). */
export function ThemeToggle({ variant, size, "aria-label": ariaLabel, className, ...rest }: ThemeToggleProps) {
return (
<Prim type="button" className={cn(rc("themetoggle", { variant, size }), className)} data-ui8kit="theme-toggle" aria-label={ariaLabel || "Dark mode"} aria-pressed="false" {...rest}>
<Icon className="hidden size-5 dark:block"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg></Icon><Icon className="block size-5 dark:hidden"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg></Icon>
</Prim>
);
}
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 theme-togglecompose/ThemeToggle.vue
<!-- Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. -->
<!-- ThemeToggle: Button + Lucide icons (data-ui8kit="theme-toggle" behaviour). -->
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import Prim from "../kit/ui/button/Button.vue";
import Icon from "../kit/ui/icon/Icon.vue";
import { cn } from "../kit/utils";
import { rc } from "./recipes";
defineOptions({ name: "ThemeToggle", inheritAttrs: false });
const props = defineProps<{ variant?: string; size?: string; ariaLabel?: string }>();
const attrs = useAttrs();
const rest = computed(() => {
const { class: _class, ...r } = attrs;
return r;
});
const cls = computed(() => cn(rc("themetoggle", { variant: props.variant, size: props.size }), attrs.class as string | undefined));
</script>
<template>
<Prim type="button" :class="cls" data-ui8kit="theme-toggle" :aria-label="props.ariaLabel || 'Dark mode'" aria-pressed="false" v-bind="rest"><Icon class="hidden size-5 dark:block"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg></Icon><Icon class="block size-5 dark:hidden"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg></Icon></Prim>
</template>
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 theme-togglecompose/theme-toggle.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 { Button as Prim } from "../kit/ui/button/button.solid";
import { Icon } from "../kit/ui/icon/icon.solid";
import { cn } from "../kit/utils";
import { rc } from "./recipes";
export type ThemeToggleProps = Omit<ComponentProps<typeof Prim>, "children" | "class" | "className"> & { variant?: string; size?: string; "aria-label"?: string; class?: string; className?: string };
/** ThemeToggle: Button + Lucide icons (data-ui8kit="theme-toggle" behaviour). */
export function ThemeToggle(props: ThemeToggleProps) {
const [local, rest] = splitProps(props, ["variant", "size", "aria-label", "class", "className"]);
return (
<Prim type="button" class={cn(rc("themetoggle", { variant: local.variant, size: local.size }), local.class ?? local.className)} data-ui8kit="theme-toggle" aria-label={local["aria-label"] || "Dark mode"} aria-pressed="false" {...rest}>
<Icon class="hidden size-5 dark:block"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg></Icon><Icon class="block size-5 dark:hidden"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg></Icon>
</Prim>
);
}
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 theme-togglecompose/theme-toggle.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/button"
"github.com/ui8kit/codegen/generated/templ/ui/icon"
)
// ThemeToggleProps: Button + Lucide icons (data-ui8kit="theme-toggle").
type ThemeToggleProps struct {
Variant string
Size string
AriaLabel string
button.ButtonProps
}
func themeToggleInner(p ThemeToggleProps) button.ButtonProps {
in := p.ButtonProps
in.Type = "button"
in.Class = Join(Rc("themetoggle", map[string]string{"variant": p.Variant, "size": p.Size}), p.ButtonProps.Class)
fixed := map[string]string{"data-ui8kit": "theme-toggle", "aria-pressed": "false", "aria-label": Or(p.AriaLabel, "Dark mode")}
in.Attrs = WithAttrs(in.Attrs, fixed)
return in
}
// ThemeToggle renders the button with its Lucide icons.
templ ThemeToggle(p ThemeToggleProps) {
@button.Button(themeToggleInner(p)) {
@icon.Icon(icon.IconProps{Class: "hidden size-5 dark:block"}) {
@templ.Raw(lucideSun)
}
@icon.Icon(icon.IconProps{Class: "block size-5 dark:hidden"}) {
@templ.Raw(lucideMoon)
}
}
}
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 theme-togglecompose/ThemeToggle.html.twig
{# Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. #}
{# ThemeToggle: Button + Lucide icons. Params: variant, size, ariaLabel, class, attrs. #}
{% set a = {'data-ui8kit': 'theme-toggle', 'aria-pressed': 'false', 'aria-label': ariaLabel|default('') ?: 'Dark mode'}|merge(attrs|default({})) %}
{% set icons %}{{ include('ui/icon/Icon.html.twig', {class: 'hidden size-5 dark:block', children: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg>'}, with_context = false)|trim|raw }}{{ include('ui/icon/Icon.html.twig', {class: 'block size-5 dark:hidden', children: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg>'}, with_context = false)|trim|raw }}{% endset %}
{{ include('ui/button/Button.html.twig', {type: 'button', class: compose_join([compose_rc('themetoggle', {variant: variant|default(''), size: size|default('')}), class|default('')]), attrs: a, children: icons}, with_context = false)|trim|raw }}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 theme-togglecompose/ThemeToggle.latte
{* Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT. *}
{* ThemeToggle: Button + Lucide icons. Includes inherit variables: every primitive param this template shadows is passed explicitly. *}
{parameters
$variant = null,
$size = null,
string $controls = '',
string $ariaLabel = '',
string $class = '',
array $attrs = [],
}
{var $a = ['data-ui8kit' => 'theme-toggle', 'aria-pressed' => 'false', 'aria-label' => $ariaLabel !== '' ? $ariaLabel : 'Dark mode', ...(false && $controls !== '' ? ['aria-controls' => $controls] : []), ...$attrs]}
{capture $icons}{include '../ui/icon/Icon.latte', size: '', label: '', class: 'hidden size-5 dark:block', attrs: [], children: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg>'}{include '../ui/icon/Icon.latte', size: '', label: '', class: 'block size-5 dark:hidden', attrs: [], children: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg>'}{/capture}
{include '../ui/button/Button.latte', type: 'button', ariaLabel: null, ariaControls: null, class: \CodegenX\Compose\Latte\Compose::join([\CodegenX\Compose\Latte\Compose::rc('themetoggle', ['variant' => $variant, 'size' => $size]), $class]), attrs: $a, children: (string) $icons}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 theme-togglecompose/theme-toggle.html.j2
{#- Code generated by packages/kit-compose/src/build.ts from src/map.ts + variants/. DO NOT EDIT.
ThemeToggle: Button + Lucide icons. Props (p): Variant, Size, AriaLabel, Class, Attrs. -#}
{%- set a = {'data-ui8kit': 'theme-toggle', 'aria-pressed': 'false', 'aria-label': p['AriaLabel'] or 'Dark mode'} %}
{%- set _ = a.update(p['Attrs'] or {}) %}
{%- set icons %}{% with p = {'Class': 'hidden size-5 dark:block'}, children = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4"></circle><path d="M12 2v2"></path><path d="M12 20v2"></path><path d="m4.93 4.93 1.41 1.41"></path><path d="m17.66 17.66 1.41 1.41"></path><path d="M2 12h2"></path><path d="M20 12h2"></path><path d="m6.34 17.66-1.41 1.41"></path><path d="m19.07 4.93-1.41 1.41"></path></svg>'|safe %}{% include 'ui/icon/icon.html.j2' %}{% endwith %}{% with p = {'Class': 'block size-5 dark:hidden'}, children = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"></path></svg>'|safe %}{% include 'ui/icon/icon.html.j2' %}{% endwith %}{% endset %}
{%- with p = {'Type': 'button', 'Class': compose_join([compose_rc('themetoggle', {'variant': p['Variant'], 'size': p['Size']}), p['Class']]), 'Attrs': a}, children = icons %}{% include 'ui/button/button.html.j2' %}{% endwith %}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.