Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/checkbox.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Checkbox } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type CheckboxParts = Component.Headless.Parts<typeof Checkbox>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /**
     * The box. Draw it inside the kit's `view` and put the indicator in it:
     * `CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))`.
     */
    export const CheckboxRoot = <Msg>(h: Html.H<Msg>, checkbox: CheckboxParts, options: ClassOption = {}) =>
      checkbox.root(
        h.button.class(cn(
          "inline-grid size-4 shrink-0 place-items-center rounded-xs border border-input bg-background p-0 " +
            "text-brand-foreground shadow-sm transition-colors duration-150 hover:border-muted-foreground " +
            "data-[checked]:border-brand data-[checked]:bg-brand " +
            "data-[indeterminate]:border-brand data-[indeterminate]:bg-brand " +
            "data-[readonly]:cursor-default data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50",
          options.class
        ))
      )
    
    /** The tick, or a dash while the checkbox is indeterminate. Both are borders, so there is no icon to ship. */
    export const CheckboxIndicator = <Msg>(h: Html.H<Msg>, checkbox: CheckboxParts, options: ClassOption = {}) =>
      checkbox.indicator(
        h.span.class(cn(
          "-mt-0.5 h-[5px] w-[9px] -rotate-45 border-b-[1.5px] border-l-[1.5px] border-current " +
            "data-[unchecked]:hidden " +
            "data-[indeterminate]:mt-0 data-[indeterminate]:h-0 data-[indeterminate]:w-2 " +
            "data-[indeterminate]:rotate-0 data-[indeterminate]:border-l-0",
          options.class
        ))
      )
    
    /** A label for the checkbox: around the root, or beside it with `for`. It dims with a disabled checkbox inside it. */
    export const CheckboxLabel = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.label.class(cn(
        "inline-flex cursor-pointer items-center gap-2 text-[13px] leading-4 font-medium select-none " +
          "has-[[data-disabled]]:cursor-not-allowed has-[[data-disabled]]:text-muted-foreground",
        options.class
      ))
    
  3. Import it

    import { CheckboxRoot, CheckboxIndicator, CheckboxLabel } from "#ui/checkbox"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Checkbox } from "@effect/ui-kit"
import { Schema } from "effect"
import { CheckboxIndicator, CheckboxLabel, CheckboxRoot } from "#ui/checkbox"

const CheckboxBasicMessage = UiSchema.TaggedEnum({ ToggledTerms: { checked: Schema.Boolean } })

export const CheckboxBasic = Html.component({
  name: "CheckboxBasic",
  model: Schema.Struct({ terms: Schema.Boolean }),
  message: CheckboxBasicMessage
}).make({
  init: () => ({ terms: false }),
  update: {
    ToggledTerms: ($, { checked }) => $.evolve({ terms: checked })
  },
  view: (h, model) =>
    Checkbox(h, {
      props: { checked: model.terms },
      onEmit: { Toggled: ({ checked }) => CheckboxBasicMessage.ToggledTerms({ checked }) },
      view: (checkbox) =>
        CheckboxLabel(h).children(
          CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox)),
          "Accept terms and conditions"
        )
    })
})

Examples

With description

A label that points at the root's id, and a description beside it.

A short email when we ship something new. No more than one a month.

Indeterminate

A select-all checkbox that is mixed while only some rows are ticked.

1 of 3 selected

Disabled and read-only

A disabled checkbox leaves the tab order. A read-only one keeps focus and ignores clicks.

Toggled 0 times

Card

The whole card is the label, styled from the checkbox inside it.

In a form

The root carries a hidden input, so the form submits name and value.

Nothing submitted yet

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <button role="checkbox">. It appends the hidden input when there is a value to submit.
indicatorA <span> inside the root for the tick. It stays in the DOM, so hide it with [data-unchecked].
checked, indeterminate, disabled, readOnly, requiredThe state resolved to booleans, for markup that depends on it.

API reference

Props

PropTypeDescription
checkedbooleanWhether the checkbox is ticked. Unticked when left out.
indeterminatebooleanShows neither state, with aria-checked="mixed". A click still emits the opposite of checked.
disabledbooleanSets the native disabled attribute: skipped by Tab, ignores clicks.
readOnlybooleanIgnores clicks but stays focusable.
requiredbooleanSets aria-required and data-required. It doesn't block a submit.
namestringThe form field's name. Without it nothing is submitted.
valuestringWhat a ticked checkbox submits, "on" by default. Inside a CheckboxGroup it makes the checkbox a member.
uncheckedValuestringWhat an unticked checkbox submits. Nothing when unset.
parentbooleanMakes the checkbox the parent of the CheckboxGroup around it.
idstringThe root's id, for a label's for attribute.

Events

EventDescription
Toggled{ checked }: the value the user asked for. Store it and pass it back as checked.

Data attributes

AttributeDescription
data-checkedOn root and indicator while ticked.
data-uncheckedOn root and indicator while unticked. Neither is present while indeterminate.
data-indeterminateOn root and indicator while mixed.
data-disabledOn root and indicator while disabled.
data-readonlyOn root and indicator while read-only.
data-requiredOn root and indicator while required.
data-parentOn the root of a group's parent checkbox.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.