@effect-ts starred 3 repositories
effect-ts/effect

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/collapsible.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Collapsible } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type CollapsibleParts = Component.Headless.Parts<typeof Collapsible>
    
    const triggerBase = "text-[13px] font-medium transition-colors duration-150 " +
      "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 [&_svg]:size-[15px] [&_svg]:shrink-0"
    
    const triggerVariants = {
      row: "flex w-full items-center gap-2 rounded-sm px-2.5 py-2 text-left text-foreground hover:bg-accent " +
        "after:mr-1 after:ml-auto after:size-1.5 after:shrink-0 after:-rotate-45 after:border-r-[1.5px] " +
        "after:border-b-[1.5px] after:border-muted-foreground after:transition-transform after:duration-200 " +
        "data-[panel-open]:after:rotate-45",
      icon: "inline-flex size-8 shrink-0 items-center justify-center rounded-sm border border-input bg-background " +
        "text-foreground shadow-sm hover:bg-accent",
      link: "inline-flex items-center gap-1.5 rounded-xs text-muted-foreground underline-offset-4 " +
        "hover:text-foreground hover:underline"
    } as const
    
    export type CollapsibleTriggerVariant = keyof typeof triggerVariants
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /** The `<div>` around the trigger and the panel. */
    export const CollapsibleRoot = <Msg>(h: Html.H<Msg>, collapsible: CollapsibleParts, options: ClassOption = {}) =>
      collapsible.root(h.div.class(cn("w-full", options.class)))
    
    /**
     * The button that opens and closes the panel: a full-width `row` with a chevron,
     * a square `icon` button, or a quiet `link`.
     */
    export const CollapsibleTrigger = <Msg>(
      h: Html.H<Msg>,
      collapsible: CollapsibleParts,
      options: ClassOption & { readonly variant?: CollapsibleTriggerVariant | undefined } = {}
    ) =>
      collapsible.trigger(
        h.button.class(cn(triggerBase, triggerVariants[options.variant ?? "row"], options.class))
      )
    
    /**
     * The panel, whose height animates from the kit's `--collapsible-panel-height`.
     * `undefined` once it has closed, so chain with `?.children(...)`. Keep padding
     * on its children, so it can shrink to nothing.
     */
    export const CollapsiblePanel = <Msg>(h: Html.H<Msg>, collapsible: CollapsibleParts, options: ClassOption = {}) =>
      collapsible.panel(
        h.div.class(cn(
          "h-[var(--collapsible-panel-height)] overflow-hidden transition-[height] duration-200 ease-out " +
            "data-[starting-style]:h-0 data-[ending-style]:h-0",
          options.class
        ))
      )
    
  3. Import it

    import { CollapsibleRoot, CollapsibleTrigger, CollapsiblePanel } from "#ui/collapsible"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Collapsible } from "@effect/ui-kit"
import { Schema } from "effect"
import { CollapsiblePanel, CollapsibleRoot, CollapsibleTrigger } from "#ui/collapsible"

const CollapsibleBasicMessage = UiSchema.TaggedEnum({
  ToggledCollapsible: { open: Schema.Boolean }
})

const row = "rounded-sm border px-3 py-2 font-mono text-[13px]"

export const CollapsibleBasic = Html.component({
  name: "CollapsibleBasic",
  model: Schema.Struct({ open: Schema.Boolean }),
  message: CollapsibleBasicMessage
}).make({
  init: () => ({ open: false }),
  update: {
    ToggledCollapsible: ($, { open }) => $.evolve({ open })
  },
  view: (h, model) =>
    Collapsible(h, {
      props: { open: model.open },
      onEmit: {
        Toggled: ({ open }) => CollapsibleBasicMessage.ToggledCollapsible({ open }),
        Settled: () => undefined
      },
      view: (collapsible) =>
        CollapsibleRoot(h, collapsible, { class: "max-w-xs" }).children(
          h.div.class("mb-2 flex items-center justify-between gap-3 text-sm font-medium").children(
            h.span.children("@effect-ts starred 3 repositories"),
            CollapsibleTrigger(h, collapsible, { variant: "icon" }).attr("aria-label", "Toggle repositories").children(
              h.el("svg").attrs({
                viewBox: "0 0 24 24",
                fill: "none",
                stroke: "currentColor",
                "stroke-width": "2",
                "stroke-linecap": "round",
                "stroke-linejoin": "round",
                "aria-hidden": "true"
              }).children(h.el("path").attr("d", "M7 15l5 5 5-5M7 9l5-5 5 5"))
            )
          ),
          h.div.class(row).children("effect-ts/effect"),
          CollapsiblePanel(h, collapsible)?.children(
            h.div.class("grid gap-2 pt-2").children(
              h.div.class(row).children("effect-ts/ui"),
              h.div.class(row).children("effect-ts/website")
            )
          )
        )
    })
})

Examples

Show more

The trigger sits after the panel and reads its label from collapsible.open.

  • Accordion
  • Avatar
  • Button

Several collapsibles in one view, each with a key, their open ids in one Model field.

Hidden until found

The browser's find-in-page searches the closed panel and asks to open it on a match.

Use your browser's find-in-page to search for "Skerryvore".

Keep mounted

With keepMounted the closed panel stays in the page, so what was typed in it survives.

The section is open.

Disabled

A disabled trigger is a disabled button, and the panel keeps the state it had.

Storgata 1, 0155 Oslo, Norway

Anatomy

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

PartDescription
rootAny element around the trigger and the panel. Optional.
triggerThe <button> that opens and closes the panel.
panelAny element with the content. Returns undefined once it has closed, unless kept mounted.
open, disabled, mounted, transitionStatusPlain values for markup that depends on the state.

API reference

Props

PropTypeDescription
openbooleanWhether the panel shows. Required: the consumer owns it.
disabledbooleanDisables the trigger, and a find-in-page match emits nothing.
keepMountedboolean | "css"true keeps the closed panel in the page with hidden. "css" keeps it with data-closed and no hidden, so CSS decides.
hiddenUntilFoundbooleanThe closed panel stays as hidden="until-found", so find-in-page can reveal it.
holdExitbooleanKeeps a closed panel in its ending state while true, for an exit animation the consumer runs.

Events

EventDescription
ToggledAsks for the value in open, with the reason: "trigger-press" or "find-in-page".
SettledThe transitions and animations of an open or a close finished.

Data attributes

AttributeDescription
data-open / data-closedOn the root and the panel.
data-panel-openOn the trigger while the panel is open.
data-disabledOn every part while disabled.
data-starting-styleFor the first frame after the panel opens.
data-ending-styleWhile the panel closes.
--collapsible-panel-height / --collapsible-panel-widthCSS variables on the panel: its content's size in px while it opens and closes, auto otherwise.

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.