Skip to content
ui8kit

primitive / kit

Kit Checkbox

UI8Kit 3 Checkbox primitive (unstyled), from @ui8kit/registry

Install

npx ui8kit add kit-checkbox

Pulls 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-cdn
npx ui8kit add kit-checkbox
kit/ui/checkbox/Checkbox.svelte
<!-- Code generated by @ui8kit/codegen. DO NOT EDIT. -->
<!--
  Checkbox renders input[type=checkbox].
-->
<script lang="ts" module>

  export type CheckboxProps = {
    checked?: boolean;
    required?: boolean;
    disabled?: boolean;
    class?: string;
    [key: string]: unknown;
  };
</script>

<script lang="ts">
  import { checkboxClasses } from "./checkbox.shared";
  import { safeRest } from "../../utils/expr";

  let {
    checked,
    required,
    disabled,
    class: className,
    ...rawRest
  }: CheckboxProps = $props();

  const rest = $derived(safeRest(rawRest, { blank: ["id", "name", "aria-invalid", "aria-describedby"] }));
  const cls = $derived(checkboxClasses({ className }));
</script>

<input type="checkbox" {...(cls ? { class: cls } : {})} checked={checked || undefined} required={required || undefined} disabled={disabled || undefined} {...rest} />
kit/ui/checkbox/checkbox.shared.ts
// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.checkbox — shared TypeScript contract (types + classes helpers)

import { cn } from "../../utils";

/** Checkbox renders input[type=checkbox]. */
export type CheckboxBaseProps = {
  checked?: boolean;
  required?: boolean;
  disabled?: boolean;
};

export type CheckboxClassesInput = {
  className?: string;
};

/** Compose Checkbox classes without rendering — the cross-runtime `asChild` substitute. */
export function checkboxClasses(input: CheckboxClassesInput = {}): string {
  return cn(input.className);
}

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 kit-checkbox
kit/ui/checkbox/checkbox.shared.ts
// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.checkbox — shared TypeScript contract (types + classes helpers)

import { cn } from "../../utils";

/** Checkbox renders input[type=checkbox]. */
export type CheckboxBaseProps = {
  checked?: boolean;
  required?: boolean;
  disabled?: boolean;
};

export type CheckboxClassesInput = {
  className?: string;
};

/** Compose Checkbox classes without rendering — the cross-runtime `asChild` substitute. */
export function checkboxClasses(input: CheckboxClassesInput = {}): string {
  return cn(input.className);
}
kit/ui/checkbox/checkbox.tsx
// Code generated by @ui8kit/codegen. DO NOT EDIT.
/**
 * Checkbox is a native input[type=checkbox]; pair it with Label.
 */

import { forwardRef, type InputHTMLAttributes } from "react";
import { checkboxClasses } from "./checkbox.shared";
import { safeRest } from "../../utils/expr";

/**
 * Checkbox renders input[type=checkbox].
 */
export type CheckboxProps = Omit<InputHTMLAttributes<HTMLInputElement>, "className" | "children" | "checked" | "required" | "disabled"> & {
  checked?: boolean;
  required?: boolean;
  disabled?: boolean;
  className?: string;
};

export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(function Checkbox(
  { checked, required, disabled, className, ...rawRest },
  ref
) {
  const rest = safeRest(rawRest, { blank: ["id", "name", "aria-invalid", "aria-describedby"] });
  const cls = checkboxClasses({ className });
  return (
    <input ref={ref} type="checkbox" className={cls || undefined} checked={checked} required={required} disabled={disabled} {...rest} />
  );
});
Checkbox.displayName = "Checkbox";

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 kit-checkbox
kit/ui/checkbox/Checkbox.vue
<!-- Code generated by @ui8kit/codegen. DO NOT EDIT. -->
<!--
  Checkbox renders input[type=checkbox].
-->
<script setup lang="ts">
import { computed, useAttrs } from "vue";
import { checkboxClasses } from "./checkbox.shared";
import { safeRest } from "../../utils/expr";

defineOptions({ name: "Checkbox", inheritAttrs: false });

const props = defineProps<{
  checked?: boolean;
  required?: 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-invalid", "aria-describedby"] });
});
const cls = computed(() => checkboxClasses({ className: callerClass.value }));
</script>

<template>
  <input type="checkbox" :class="cls || undefined" :checked="props.checked || undefined" :required="props.required || undefined" :disabled="props.disabled || undefined" v-bind="restAttrs" />
</template>
kit/ui/checkbox/checkbox.shared.ts
// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.checkbox — shared TypeScript contract (types + classes helpers)

import { cn } from "../../utils";

/** Checkbox renders input[type=checkbox]. */
export type CheckboxBaseProps = {
  checked?: boolean;
  required?: boolean;
  disabled?: boolean;
};

export type CheckboxClassesInput = {
  className?: string;
};

/** Compose Checkbox classes without rendering — the cross-runtime `asChild` substitute. */
export function checkboxClasses(input: CheckboxClassesInput = {}): string {
  return cn(input.className);
}

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 kit-checkbox
kit/ui/checkbox/checkbox.shared.ts
// Code generated by @ui8kit/codegen. DO NOT EDIT.
// ui.checkbox — shared TypeScript contract (types + classes helpers)

import { cn } from "../../utils";

/** Checkbox renders input[type=checkbox]. */
export type CheckboxBaseProps = {
  checked?: boolean;
  required?: boolean;
  disabled?: boolean;
};

export type CheckboxClassesInput = {
  className?: string;
};

/** Compose Checkbox classes without rendering — the cross-runtime `asChild` substitute. */
export function checkboxClasses(input: CheckboxClassesInput = {}): string {
  return cn(input.className);
}
kit/ui/checkbox/checkbox.solid.tsx
// Code generated by @ui8kit/codegen. DO NOT EDIT.
/**
 * Checkbox is a native input[type=checkbox]; pair it with Label.
 */

import { splitProps, type JSX, type Component } from "solid-js";
import { checkboxClasses } from "./checkbox.shared";
import { safeRest } from "../../utils/expr";

/**
 * Checkbox renders input[type=checkbox].
 */
export type CheckboxProps = Omit<JSX.InputHTMLAttributes<HTMLInputElement>, "class" | "className" | "children" | "checked" | "required" | "disabled"> & {
  checked?: boolean;
  required?: boolean;
  disabled?: boolean;
  class?: string;
  className?: string;
};

export const Checkbox: Component<CheckboxProps> = (props) => {
  const [local, rest] = splitProps(props, ["checked", "required", "disabled", "class", "className"]);
  const cls = () => checkboxClasses({ className: local.class ?? local.className });
  return (
    <input type="checkbox" {...(cls() ? { class: cls() } : {})} checked={local.checked || undefined} required={local.required || undefined} disabled={local.disabled || undefined} {...safeRest(rest, { blank: ["id", "name", "aria-invalid", "aria-describedby"] })} />
  );
};

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 kit-checkbox
ui/checkbox/checkbox.templ
// Code generated by @ui8kit/codegen. DO NOT EDIT.

/*
Checkbox is a native input[type=checkbox]; pair it with Label.
*/

package checkbox

import (
	uiutils "github.com/ui8kit/codegen/generated/templ/utils"
)

// CheckboxProps configures Checkbox.
type CheckboxProps struct {
	// Class — token class names supplied by the consumer
	Class string
	ID string
	Name string
	Value *string
	Checked bool
	Required bool
	Disabled bool
	AriaInvalid string
	AriaDescribedBy string
	Attrs templ.Attributes
}

// CheckboxClasses composes Checkbox classes without rendering —
// the server-composition escape hatch for custom root elements (no asChild in templ).
func CheckboxClasses(p CheckboxProps) string {
	return uiutils.Cn(p.Class)
}

// Checkbox renders the Checkbox contract.
templ Checkbox(p CheckboxProps) {
	<input
		if !uiutils.HasAttr(p.Attrs, "type") {
			type="checkbox"
		}
		if CheckboxClasses(p) != "" {
			class={ CheckboxClasses(p) }
		}
		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, "checked") {
			checked?={ p.Checked }
		}
		if !uiutils.HasAttr(p.Attrs, "required") {
			required?={ p.Required }
		}
		if !uiutils.HasAttr(p.Attrs, "disabled") {
			disabled?={ p.Disabled }
		}
		if !uiutils.HasAttr(p.Attrs, "aria-invalid") {
			if uiutils.IsSetStr(p.AriaInvalid) {
				aria-invalid={ p.AriaInvalid }
			}
		}
		if !uiutils.HasAttr(p.Attrs, "aria-describedby") {
			if uiutils.IsSetStr(p.AriaDescribedBy) {
				aria-describedby={ p.AriaDescribedBy }
			}
		}
		{ uiutils.RestAttrs(p.Attrs, false, "id", "name", "aria-invalid", "aria-describedby")... }
	/>
}

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 kit-checkbox
ui/checkbox/Checkbox.html.twig
{# Code generated by @ui8kit/codegen. DO NOT EDIT. #}
{# Checkbox — Checkbox renders input[type=checkbox]. #}
{# @param class (string) #}
{# @param id (string) #}
{# @param name (string) #}
{# @param value (string) #}
{# @param checked (bool) #}
{# @param required (bool) #}
{# @param disabled (bool) #}
{# @param ariaInvalid (string) #}
{# @param ariaDescribedBy (string) #}
{# @param attrs (attrs) #}
{% set class = class|default('') %}
{% set id = id ?? null %}
{% set name = name ?? null %}
{% set value = value ?? null %}
{% set checked = checked|default(false) %}
{% set required = required|default(false) %}
{% set disabled = disabled|default(false) %}
{% set ariaInvalid = ariaInvalid ?? null %}
{% set ariaDescribedBy = ariaDescribedBy ?? null %}
{% set attrs = attrs|default([]) %}
<input{{ ui8kit_attrs({'type': 'checkbox', 'class': ui8kit_or_null(ui8kit_classes('checkboxClasses', {'class': class, 'id': id, 'name': name, 'value': value, 'checked': checked, 'required': required, 'disabled': disabled, 'ariaInvalid': ariaInvalid, 'ariaDescribedBy': ariaDescribedBy})), 'id': ui8kit_or_null(id), 'name': ui8kit_or_null(name), 'value': value, 'checked': checked, 'required': required, 'disabled': disabled, 'aria-invalid': ui8kit_or_null(ariaInvalid), 'aria-describedby': ui8kit_or_null(ariaDescribedBy)}, attrs, false, ['id', 'name', 'aria-invalid', 'aria-describedby']) }}>

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 kit-checkbox
ui/checkbox/Checkbox.latte
{* Code generated by @ui8kit/codegen. DO NOT EDIT. *}
{* Checkbox — Checkbox renders input[type=checkbox]. *}
{parameters
  string $class = '',
  ?string $id = null,
  ?string $name = null,
  ?string $value = null,
  bool $checked = false,
  bool $required = false,
  bool $disabled = false,
  ?string $ariaInvalid = null,
  ?string $ariaDescribedBy = null,
  array $attrs = [],
}
<input n:attr="[
  'type' => 'checkbox',
  'class' => \UI8Kit\Latte\Rt::orNull(\UI8Kit\Latte\Classes::checkboxClasses(['class' => $class, 'id' => $id, 'name' => $name, 'value' => $value, 'checked' => $checked, 'required' => $required, 'disabled' => $disabled, 'ariaInvalid' => $ariaInvalid, 'ariaDescribedBy' => $ariaDescribedBy])),
  'id' => \UI8Kit\Latte\Rt::orNull($id),
  'name' => \UI8Kit\Latte\Rt::orNull($name),
  'value' => $value,
  'checked' => $checked,
  'required' => $required,
  'disabled' => $disabled,
  'aria-invalid' => \UI8Kit\Latte\Rt::orNull($ariaInvalid),
  'aria-describedby' => \UI8Kit\Latte\Rt::orNull($ariaDescribedBy),
  ...\UI8Kit\Latte\Rt::restAttrs($attrs, false, ['id', 'name', 'aria-invalid', 'aria-describedby']),
]">

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 kit-checkbox
ui/checkbox/checkbox.html.j2
{#- Code generated by @ui8kit/codegen. DO NOT EDIT.
    Checkbox: Checkbox renders input[type=checkbox].
    Props (p): Class (string), ID (string), Name (string), Value (string), Checked (bool), Required (bool), Disabled (bool), AriaInvalid (string), AriaDescribedBy (string), Attrs (attrs).
    Classes without rendering: checkbox_classes(p) (no asChild in Jinja2). -#}
<input
  {%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'type') %} type="checkbox"{%- endif %}
  {%- with cls = checkbox_classes(p) %}{%- if cls %} class="{{ cls }}"{%- endif %}{%- endwith %}
  {%- 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'], {}), 'checked') %}{%- if ui_def(p['Checked'], false) %} checked{%- endif %}{%- endif %}
  {%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'required') %}{%- if ui_def(p['Required'], false) %} required{%- endif %}{%- endif %}
  {%- 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'], {}), 'aria-invalid') %}{%- if ui_is_set_str(p['AriaInvalid']) %} aria-invalid="{{ p['AriaInvalid'] }}"{%- endif %}{%- endif %}
  {%- if not ui_has_attr(ui_def(p['Attrs'], {}), 'aria-describedby') %}{%- if ui_is_set_str(p['AriaDescribedBy']) %} aria-describedby="{{ p['AriaDescribedBy'] }}"{%- endif %}{%- endif %}
  {{ ui_attr_html(ui_def(p['Attrs'], {}), false, ['id', 'name', 'aria-invalid', 'aria-describedby']) }}
>

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.

No props data for this item.