CheckboxGroup
Shares one list of ticked values among the checkboxes inside it, with an optional parent checkbox.
Picked email
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(", ")}`
)
)
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.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 )) )Import it
import { CheckboxGroupRoot, CheckboxGroupLegend, CheckboxGroupItem, CheckboxGroupMembers, CheckboxGroupChip } from "#ui/checkbox-group"
Usage
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
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, CheckboxGroupMembers, CheckboxGroupRoot } from "#ui/checkbox-group"
const CheckboxGroupParentMessage = UiSchema.TaggedEnum({
UpdatedPermissions: { value: Schema.Array(Schema.String) }
})
const permissions = [
{ value: "read", label: "Read" },
{ value: "write", label: "Write" },
{ value: "deploy", label: "Deploy" },
{ value: "billing", label: "Manage billing" }
]
export const CheckboxGroupParent = Html.component({
name: "CheckboxGroupParent",
model: Schema.Struct({ permissions: Schema.Array(Schema.String) }),
message: CheckboxGroupParentMessage
}).make({
init: () => ({ permissions: ["read", "write"] }),
update: {
UpdatedPermissions: ($, { value }) => $.evolve({ permissions: value })
},
view: (h, model) =>
h.div.class("flex flex-col items-start gap-3.5").children(
CheckboxGroup(h, {
props: { value: model.permissions, allValues: permissions.map(({ value }) => value) },
onEmit: { Updated: ({ value }) => CheckboxGroupParentMessage.UpdatedPermissions({ value }) },
view: (checkboxGroup) =>
CheckboxGroupRoot(h, checkboxGroup).attr("aria-label", "Permissions").children(
// The parent is mixed while some are ticked. A click ticks all, then none, then brings the mix back.
CheckboxGroupItem(h).children(
Checkbox(h, {
key: "all",
props: { parent: true },
view: (checkbox) =>
CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
}),
"All permissions"
),
CheckboxGroupMembers(h).children(
permissions.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.permissions.length} of ${permissions.length} granted`)
)
})
Horizontal chips
The members in a row, each root drawn as a pill with its text inside.
Repeats on Mon, Wed, Fri
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Checkbox, CheckboxGroup } from "@effect/ui-kit"
import { Schema } from "effect"
import { CheckboxGroupChip, CheckboxGroupLegend, CheckboxGroupRoot } from "#ui/checkbox-group"
const CheckboxGroupHorizontalMessage = UiSchema.TaggedEnum({
UpdatedDays: { value: Schema.Array(Schema.String) }
})
const days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
export const CheckboxGroupHorizontal = Html.component({
name: "CheckboxGroupHorizontal",
model: Schema.Struct({ days: Schema.Array(Schema.String) }),
message: CheckboxGroupHorizontalMessage
}).make({
init: () => ({ days: ["Mon", "Wed", "Fri"] }),
update: {
UpdatedDays: ($, { value }) => $.evolve({ days: value })
},
view: (h, model) =>
h.div.class("flex flex-col items-start gap-3.5").children(
CheckboxGroupLegend(h).attr("id", "checkbox-group-days").children("Repeat on"),
CheckboxGroup(h, {
props: { value: model.days },
onEmit: { Updated: ({ value }) => CheckboxGroupHorizontalMessage.UpdatedDays({ value }) },
view: (checkboxGroup) =>
CheckboxGroupRoot(h, checkboxGroup, { orientation: "horizontal", class: "gap-1.5" })
.attr("aria-labelledby", "checkbox-group-days")
.children(
// The root needs no indicator: the text inside the button names it.
days.map((day) =>
Checkbox(h, {
key: day,
props: { value: day },
view: (checkbox) => CheckboxGroupChip(h, checkbox).children(day)
})
)
)
}),
h.p.class("text-xs text-muted-foreground tabular-nums").children(
model.days.length === 0
? "Never repeats"
: `Repeats on ${days.filter((day) => model.days.includes(day)).join(", ")}`
)
)
})
Cards
A grid of cards, each a label around its member.
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 { CheckboxGroupRoot } from "#ui/checkbox-group"
const CheckboxGroupCardsMessage = UiSchema.TaggedEnum({
UpdatedIntegrations: { value: Schema.Array(Schema.String) }
})
const integrations = [
{ value: "github", label: "GitHub", description: "Link pull requests to issues." },
{ value: "slack", label: "Slack", description: "Post updates to a channel." },
{ value: "linear", label: "Linear", description: "Sync status both ways." },
{ value: "sentry", label: "Sentry", description: "Attach errors to releases." }
]
export const CheckboxGroupCards = Html.component({
name: "CheckboxGroupCards",
model: Schema.Struct({ integrations: Schema.Array(Schema.String) }),
message: CheckboxGroupCardsMessage
}).make({
init: () => ({ integrations: ["github", "slack"] }),
update: {
UpdatedIntegrations: ($, { value }) => $.evolve({ integrations: value })
},
view: (h, model) =>
CheckboxGroup(h, {
props: { value: model.integrations },
onEmit: { Updated: ({ value }) => CheckboxGroupCardsMessage.UpdatedIntegrations({ value }) },
view: (checkboxGroup) =>
CheckboxGroupRoot(h, checkboxGroup, { class: "w-full max-w-md grid-cols-2" })
.attr("aria-label", "Integrations")
.children(
integrations.map(({ description, label, value }) =>
h.label
.class(
"flex cursor-pointer items-start gap-2.5 rounded-md border bg-card px-3.5 py-3 text-[13px] transition-colors duration-150 select-none hover:bg-accent has-[[data-checked]]:border-brand has-[[data-checked]]:bg-brand-wash"
).children(
Checkbox(h, {
key: value,
props: { value },
view: (checkbox) =>
CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
}),
h.span.class("grid gap-0.5").children(
h.span.class("leading-4 font-medium").children(label),
h.span.class("text-muted-foreground").children(description)
)
)
)
)
})
})
Disabled
A disabled member keeps its state under the parent. A disabled group disables every member.
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, CheckboxGroupMembers, CheckboxGroupRoot } from "#ui/checkbox-group"
const CheckboxGroupDisabledMessage = UiSchema.TaggedEnum({
UpdatedSections: { value: Schema.Array(Schema.String) },
UpdatedExports: { value: Schema.Array(Schema.String) }
})
const sections = [
{ value: "summary", label: "Summary", locked: true },
{ value: "charts", label: "Charts", locked: false },
{ value: "appendix", label: "Appendix", locked: false }
]
const formats = ["PDF", "CSV", "JSON"]
export const CheckboxGroupDisabled = Html.component({
name: "CheckboxGroupDisabled",
model: Schema.Struct({ sections: Schema.Array(Schema.String), formats: Schema.Array(Schema.String) }),
message: CheckboxGroupDisabledMessage
}).make({
init: () => ({ sections: ["summary", "charts"], formats: ["PDF"] }),
update: {
UpdatedSections: ($, { value }) => $.evolve({ sections: value }),
UpdatedExports: ($, { value }) => $.evolve({ formats: value })
},
view: (h, model) =>
h.div.class("grid grid-cols-2 items-start gap-12").children(
// A disabled member keeps its state when the parent ticks or unticks the rest.
CheckboxGroup(h, {
key: "sections",
props: { value: model.sections, allValues: sections.map(({ value }) => value) },
onEmit: { Updated: ({ value }) => CheckboxGroupDisabledMessage.UpdatedSections({ value }) },
view: (checkboxGroup) =>
CheckboxGroupRoot(h, checkboxGroup).attr("aria-label", "Report sections").children(
CheckboxGroupItem(h).children(
Checkbox(h, {
key: "all-sections",
props: { parent: true },
view: (checkbox) =>
CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
}),
"Report sections"
),
CheckboxGroupMembers(h).children(
sections.map(({ label, locked, value }) =>
CheckboxGroupItem(h).children(
Checkbox(h, {
key: value,
props: { value, disabled: locked },
view: (checkbox) =>
CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
}),
locked ? `${label} (always included)` : label
)
)
)
)
}),
CheckboxGroup(h, {
key: "formats",
props: { value: model.formats, disabled: true },
onEmit: { Updated: ({ value }) => CheckboxGroupDisabledMessage.UpdatedExports({ value }) },
view: (checkboxGroup) =>
CheckboxGroupRoot(h, checkboxGroup).attr("aria-labelledby", "checkbox-group-formats")
.children(
CheckboxGroupLegend(h).attr("id", "checkbox-group-formats").children(
"Export formats (disabled group)"
),
formats.map((format) =>
CheckboxGroupItem(h).children(
Checkbox(h, {
key: format,
props: { value: format },
view: (checkbox) =>
CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
}),
format
)
)
)
})
)
})
In a form
Every ticked member submits its value under the group's name.
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"
import { Button } from "#ui/button"
const CheckboxGroupFormMessage = UiSchema.TaggedEnum({
UpdatedToppings: { value: Schema.Array(Schema.String) },
SubmittedForm: { toppings: Schema.Array(Schema.String) }
})
const toppings = [
{ value: "mushroom", label: "Mushroom" },
{ value: "olive", label: "Olive" },
{ value: "basil", label: "Basil" },
{ value: "chili", label: "Chili" }
]
export const CheckboxGroupForm = Html.component({
name: "CheckboxGroupForm",
model: Schema.Struct({
toppings: Schema.Array(Schema.String),
ordered: Schema.NullOr(Schema.Array(Schema.String))
}),
message: CheckboxGroupFormMessage
}).make({
init: () => ({ toppings: ["mushroom", "basil"], ordered: null }),
update: {
UpdatedToppings: ($, { value }) => $.evolve({ toppings: value }),
SubmittedForm: ($, { toppings }) => $.evolve({ ordered: toppings })
},
view: (h, model) =>
h.form
.class("flex flex-col items-start gap-3.5")
// Every ticked member submits under the group's name, so read `entries`, not `fields`.
.on("submit", ({ entries }) =>
CheckboxGroupFormMessage.SubmittedForm({
toppings: entries.filter(([name]) => name === "topping").map(([, value]) => value)
}))
.children(
CheckboxGroup(h, {
props: { value: model.toppings, name: "topping" },
onEmit: { Updated: ({ value }) => CheckboxGroupFormMessage.UpdatedToppings({ value }) },
view: (checkboxGroup) =>
CheckboxGroupRoot(h, checkboxGroup).attr("aria-labelledby", "checkbox-group-toppings")
.children(
h.div.children(
CheckboxGroupLegend(h).attr("id", "checkbox-group-toppings").children(
"Toppings"
),
h.p.class("text-[13px] text-muted-foreground").children("Pick as many as you like.")
),
toppings.map(({ label, value }) =>
CheckboxGroupItem(h).children(
Checkbox(h, {
key: value,
props: { value },
view: (checkbox) =>
CheckboxRoot(h, checkbox).children(CheckboxIndicator(h, checkbox))
}),
label
)
)
)
}),
Button(h, { size: "sm", props: { type: "submit" } }).children("Order"),
h.p.class("text-xs text-muted-foreground tabular-nums").children(
model.ordered === null
? "Nothing ordered yet"
: model.ordered.length === 0
? "Ordered a plain one"
: `Ordered with ${model.ordered.join(", ")}`
)
)
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
root | Any element, usually a <div>. It gets role="group" and adds no markup. |
value, disabled | The group's state, for markup that depends on it. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
value | ReadonlyArray<string> | The values of the ticked members. Required. |
allValues | ReadonlyArray<string> | The values of every member, in order. Needed for a parent checkbox. |
disabled | boolean | Disables every checkbox in the group. |
name | string | The form name of each member that has no name of its own. |
Checkbox value | string | On a Checkbox inside the group: makes it a member. The group decides whether it is ticked. |
Checkbox parent | boolean | On a Checkbox inside the group: makes it the parent, mixed while some members are ticked. |
Events
| Event | Description |
|---|---|
Updated | { value }: the ticked values after a member or the parent was clicked. |
Data attributes
| Attribute | Description |
|---|---|
data-disabled | On the root while the group is disabled. |
data-parent | On the root of the parent Checkbox. |
data-checked / data-unchecked / data-indeterminate | On each Checkbox, as the group decided. |
Related components
The components this page's file and examples import.