Notify me by

Picked email

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-group.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Checkbox, CheckboxGroup } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type CheckboxGroupParts = Component.Headless.Parts<typeof CheckboxGroup>
    type CheckboxParts = Component.Headless.Parts<typeof Checkbox>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const orientations = {
      vertical: "grid gap-2.5",
      horizontal: "flex flex-wrap items-center gap-x-4 gap-y-2"
    } as const
    
    /**
     * The group. Its members are checkboxes from `#ui/checkbox`, each with a
     * `value` prop, drawn inside it.
     */
    export const CheckboxGroupRoot = <Msg>(
      h: Html.H<Msg>,
      checkboxGroup: CheckboxGroupParts,
      options: ClassOption & { readonly orientation?: keyof typeof orientations | undefined } = {}
    ) =>
      checkboxGroup.root(
        h.div.class(cn("min-w-0", orientations[options.orientation ?? "vertical"], options.class))
      )
    
    /** The group's name. Give it an `id` and point the root's `aria-labelledby` at it. */
    export const CheckboxGroupLegend = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.div.class(cn("text-[13px] leading-5 font-medium", options.class))
    
    /** A label around one member and its text. It dims with a disabled member, and is bolder around the parent. */
    export const CheckboxGroupItem = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.label.class(cn(
        "inline-flex cursor-pointer items-center gap-2 text-[13px] leading-4 select-none " +
          "has-[[data-parent]]:font-medium " +
          "has-[[data-disabled]]:cursor-not-allowed has-[[data-disabled]]:text-muted-foreground",
        options.class
      ))
    
    /** The members under a parent checkbox, indented. */
    export const CheckboxGroupMembers = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.div.class(cn("grid gap-2.5 pl-6", options.class))
    
    /** A member drawn as a pill: the checkbox's root is the whole button, with its text inside. */
    export const CheckboxGroupChip = <Msg>(h: Html.H<Msg>, checkbox: CheckboxParts, options: ClassOption = {}) =>
      checkbox.root(
        h.button.class(cn(
          "inline-flex h-7.5 min-w-10 items-center justify-center rounded-full border border-input bg-background px-2.5 " +
            "text-xs font-medium text-foreground transition-colors duration-150 hover:bg-accent " +
            "data-[checked]:border-brand data-[checked]:bg-brand data-[checked]:text-brand-foreground " +
            "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50",
          options.class
        ))
      )
    
  3. Import it

    import { CheckboxGroupRoot, CheckboxGroupLegend, CheckboxGroupItem, CheckboxGroupMembers, CheckboxGroupChip } from "#ui/checkbox-group"
    

Usage

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

const CheckboxGroupBasicMessage = UiSchema.TaggedEnum({
  UpdatedChannels: { value: Schema.Array(Schema.String) }
})

const channels = [
  { value: "email", label: "Email" },
  { value: "sms", label: "Text message" },
  { value: "push", label: "Push notification" }
]

export const CheckboxGroupBasic = Html.component({
  name: "CheckboxGroupBasic",
  model: Schema.Struct({ channels: Schema.Array(Schema.String) }),
  message: CheckboxGroupBasicMessage
}).make({
  init: () => ({ channels: ["email"] }),
  update: {
    UpdatedChannels: ($, { value }) => $.evolve({ channels: value })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-start gap-3.5").children(
      CheckboxGroup(h, {
        props: { value: model.channels },
        onEmit: { Updated: ({ value }) => CheckboxGroupBasicMessage.UpdatedChannels({ value }) },
        view: (checkboxGroup) =>
          CheckboxGroupRoot(h, checkboxGroup).attr("aria-labelledby", "checkbox-group-channels")
            .children(
              CheckboxGroupLegend(h).attr("id", "checkbox-group-channels").children(
                "Notify me by"
              ),
              // A checkbox with a `value` is a member: the group ticks it, so it takes no `checked`.
              channels.map(({ label, value }) =>
                CheckboxGroupItem(h).children(
                  Checkbox(h, {
                    key: value,
                    props: { value },
                    view: (checkbox) =>
                      CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
                  }),
                  label
                )
              )
            )
      }),
      h.p.class("text-xs text-muted-foreground tabular-nums").children(
        model.channels.length === 0 ? "No channel picked" : `Picked ${model.channels.join(", ")}`
      )
    )
})

Examples

Parent checkbox

Given allValues, a parent checkbox ticks all, none, or brings the last mix back.

2 of 4 granted

Horizontal chips

The members in a row, each root drawn as a pill with its text inside.

Repeat on

Repeats on Mon, Wed, Fri

Cards

A grid of cards, each a label around its member.

Disabled

A disabled member keeps its state under the parent. A disabled group disables every member.

Export formats (disabled group)

In a form

Every ticked member submits its value under the group's name.

Toppings

Pick as many as you like.

Nothing ordered yet

Anatomy

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

PartDescription
rootAny element, usually a <div>. It gets role="group" and adds no markup.
value, disabledThe group's state, for markup that depends on it.

API reference

Props

PropTypeDescription
valueReadonlyArray<string>The values of the ticked members. Required.
allValuesReadonlyArray<string>The values of every member, in order. Needed for a parent checkbox.
disabledbooleanDisables every checkbox in the group.
namestringThe form name of each member that has no name of its own.
Checkbox valuestringOn a Checkbox inside the group: makes it a member. The group decides whether it is ticked.
Checkbox parentbooleanOn a Checkbox inside the group: makes it the parent, mixed while some members are ticked.

Events

EventDescription
Updated{ value }: the ticked values after a member or the parent was clicked.

Data attributes

AttributeDescription
data-disabledOn the root while the group is disabled.
data-parentOn the root of the parent Checkbox.
data-checked / data-unchecked / data-indeterminateOn each Checkbox, as the group decided.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.
  • Checkbox: A controlled checkbox with a mixed state and a form value.

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