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/toggle.ts
    import { Toggle as KitToggle, type ToggleElement, type ToggleProps } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    const base =
      "inline-flex h-8.5 min-w-8.5 shrink-0 items-center justify-center gap-1.5 rounded-md border border-transparent " +
      "bg-transparent px-2 text-[13px] leading-none font-medium whitespace-nowrap text-foreground select-none " +
      "transition-colors duration-150 hover:bg-muted hover:text-muted-foreground " +
      "data-[pressed]:bg-brand-soft data-[pressed]:text-brand-soft-foreground " +
      "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 [&_svg]:size-[15px] [&_svg]:shrink-0 " +
      "data-[pressed]:[&_svg[data-fill]]:fill-current"
    
    const variants = {
      default: "",
      outline: "border-input shadow-sm data-[pressed]:border-brand-soft"
    } as const
    
    const sizes = {
      default: "",
      sm: "h-7 min-w-7 rounded-sm px-1.5 text-xs",
      lg: "h-10 min-w-10 px-3 text-sm"
    } as const
    
    export type ToggleVariant = keyof typeof variants
    export type ToggleSize = keyof typeof sizes
    
    /** The toggle's classes, for an element that should look like one: a kit part that sets `data-pressed`. */
    export const toggleVariants = (
      options: {
        readonly variant?: ToggleVariant | undefined
        readonly size?: ToggleSize | undefined
        readonly class?: string | undefined
      } = {}
    ): string => cn(base, variants[options.variant ?? "default"], sizes[options.size ?? "default"], options.class)
    
    /**
     * The kit's Toggle with a look. Pass the value as `props.pressed`, store what
     * `onToggle` reports, and chain the label:
     * `Toggle(h, { props: { pressed }, onToggle }).attr("aria-label", "Bold").children(icon)`.
     */
    export const Toggle = <Msg, R = never>(
      h: { readonly button: ToggleElement<Msg> },
      options: {
        readonly variant?: ToggleVariant | undefined
        readonly size?: ToggleSize | undefined
        readonly class?: string | undefined
        readonly element?: ToggleElement<Msg, R> | undefined
        readonly props: ToggleProps
        readonly onToggle?: ((event: { readonly pressed: boolean }) => NoInfer<Msg> | undefined) | undefined
      }
    ): ToggleElement<Msg, R> =>
      KitToggle<Msg, R>(h, {
        element: options.element,
        props: options.props,
        onToggle: options.onToggle
      }).class(toggleVariants(options))
    
  3. Import it

    import { toggleVariants, Toggle } from "#ui/toggle"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Toggle } from "#ui/toggle"

const ToggleBasicMessage = UiSchema.TaggedEnum({ ToggledBold: { pressed: Schema.Boolean } })

export const ToggleBasic = Html.component({
  name: "ToggleBasic",
  model: Schema.Struct({ bold: Schema.Boolean }),
  message: ToggleBasicMessage
}).make({
  init: () => ({ bold: false }),
  update: {
    ToggledBold: ($, { pressed }) => $.evolve({ bold: pressed })
  },
  view: (h, model) =>
    // The name stays "Bold" in both states: aria-pressed says which one it is in.
    Toggle(h, {
      props: { pressed: model.bold },
      onToggle: ({ pressed }) => ToggleBasicMessage.ToggledBold({ pressed })
    })
      .attr("aria-label", "Bold")
      .children(
        h.el("svg")
          .attrs({
            viewBox: "0 0 24 24",
            fill: "none",
            stroke: "currentColor",
            "stroke-width": "2.5",
            "stroke-linecap": "round",
            "stroke-linejoin": "round",
            "aria-hidden": "true"
          })
          .children(h.el("path").attr("d", "M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z"))
      )
})

Examples

Outline

A bordered look, set with data-variant.

With text

An icon that fills while pressed, beside a label that stays the same.

Not bookmarked

Sizes

Small, default and large, set with data-size.

Disabled

A disabled toggle ignores clicks and leaves the tab order, pressed or not.

Release Lock to change Pinned

Formatting

Three independent toggles, each with its own field in the Model.

The quick brown fox jumps over the lazy dog.

API reference

Props

PropTypeDescription
pressedbooleanWhether the toggle is on. Required.
disabledbooleanSets the native disabled attribute: skipped by Tab, ignores clicks.
elementh.buttonAn option beside props: the consumer's button, to share it with another part such as a tooltip trigger.

Events

EventDescription
onToggle({ pressed }) => message: the value the user asked for. Store it and pass it back as pressed.

Data attributes

AttributeDescription
data-pressedPresent while the toggle is pressed.
data-disabledPresent while the toggle is disabled.

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