Overview

Requests are up 12% this week, with a p95 latency of 182 ms.

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/tabs.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Tabs } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the `view` closure of the kit's `Tabs` receives. */
    export type TabsParts = Component.Headless.Parts<typeof Tabs>
    
    export type TabsVariant = keyof typeof lists
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const lists = {
      underline: "gap-1 data-[orientation=horizontal]:border-b data-[orientation=vertical]:w-44",
      segmented: "w-fit gap-0.5 rounded-md bg-muted p-[3px] data-[orientation=vertical]:w-44"
    } as const
    
    const tab =
      "relative z-10 inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-sm px-3 " +
      "text-[13px] font-medium whitespace-nowrap text-muted-foreground no-underline outline-offset-0 select-none " +
      "transition-colors duration-150 hover:text-foreground data-[active]:text-foreground " +
      "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 data-[disabled]:hover:text-muted-foreground " +
      "data-[orientation=vertical]:justify-start [&_svg]:size-3.5 [&_svg]:shrink-0 " +
      "group-data-[variant=underline]/tabs-list:h-9 group-data-[variant=segmented]/tabs-list:h-7 " +
      "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:h-8 " +
      "group-data-[fill]/tabs-list:flex-1"
    
    /** Around the list and the panels. A vertical root puts the list beside the panels. */
    export const TabsRoot = <Msg>(h: Html.H<Msg>, tabs: TabsParts, options: ClassOption = {}) =>
      tabs.root(
        h.div.class(cn(
          "flex w-full gap-4 data-[orientation=horizontal]:flex-col data-[orientation=vertical]:gap-6",
          options.class
        ))
      )
    
    /**
     * The row or column of tabs. `underline` draws a line under the active tab,
     * `segmented` a raised pill behind it; a vertical list always fills the
     * active tab's background. `fill` stretches the tabs over the list's width.
     */
    export const TabsList = <Msg>(
      h: Html.H<Msg>,
      tabs: TabsParts,
      options: ClassOption & {
        readonly variant?: TabsVariant | undefined
        readonly fill?: boolean | undefined
      } = {}
    ) =>
      tabs.list(
        h.div
          .attrs({ "data-variant": options.variant ?? "underline", "data-fill": options.fill === true ? "" : undefined })
          .class(cn(
            "group/tabs-list relative isolate flex shrink-0 data-[orientation=vertical]:flex-col",
            "data-[fill]:w-full",
            lists[options.variant ?? "underline"],
            options.class
          ))
      )
    
    /** One tab, a button. */
    export const TabsTab = <Msg>(
      h: Html.H<Msg>,
      tabs: TabsParts,
      options: ClassOption & { readonly value: string }
    ) => tabs.tab({ element: h.button.class(cn(tab, options.class)), value: options.value })
    
    /**
     * One tab that is a link: a click and Enter also follow its `href`. With
     * `follow: false` the tab only activates and the address stays, for a list
     * whose panels are all on this page.
     */
    export const TabsLink = <Msg>(
      h: Html.H<Msg>,
      tabs: TabsParts,
      options: ClassOption & { readonly value: string; readonly href: string; readonly follow?: boolean | undefined }
    ) => {
      const link = h.a.attr("href", options.href).class(cn(tab, options.class))
      return tabs.tab({
        element: options.follow === false ? link.on("click", () => undefined, { preventDefault: "always" }) : link,
        value: options.value
      })
    }
    
    /**
     * The mark that slides to the active tab, placed with the `--active-tab-*`
     * variables the kit measures. Put it inside the list. `undefined` while no tab
     * is active.
     */
    export const TabsIndicator = <Msg>(h: Html.H<Msg>, tabs: TabsParts, options: ClassOption = {}) =>
      tabs.indicator(
        h.span.class(cn(
          "pointer-events-none absolute top-0 left-0 transition-[translate,width,height] duration-200 ease-out",
          "w-[var(--active-tab-width)] translate-x-[var(--active-tab-left)]",
          "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:top-auto",
          "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:-bottom-px",
          "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:h-0.5",
          "group-data-[variant=underline]/tabs-list:data-[orientation=horizontal]:bg-brand",
          "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:h-[var(--active-tab-height)]",
          "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:translate-y-[var(--active-tab-top)]",
          "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:rounded-sm",
          "group-data-[variant=underline]/tabs-list:data-[orientation=vertical]:bg-accent",
          "group-data-[variant=segmented]/tabs-list:h-[var(--active-tab-height)]",
          "group-data-[variant=segmented]/tabs-list:translate-y-[var(--active-tab-top)]",
          "group-data-[variant=segmented]/tabs-list:rounded-sm",
          "group-data-[variant=segmented]/tabs-list:bg-background",
          "group-data-[variant=segmented]/tabs-list:shadow-sm",
          options.class
        ))
      )
    
    /** The content of one tab: `undefined` while hidden, unless it is kept mounted. Fades in. */
    export const TabsPanel = <Msg>(
      h: Html.H<Msg>,
      tabs: TabsParts,
      options: ClassOption & { readonly value: string }
    ) =>
      tabs.panel({
        element: h.div.class(cn(
          "min-w-0 flex-1 rounded-sm text-[13px] transition-opacity duration-200",
          "data-[starting-style]:opacity-0 data-[ending-style]:hidden",
          options.class
        )),
        value: options.value
      })
    
  3. Import it

    import { TabsRoot, TabsList, TabsTab, TabsLink, TabsIndicator, TabsPanel } from "#ui/tabs"
    

Usage

Underline.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Tabs } from "@effect/ui-kit"
import { Schema } from "effect"
import { TabsIndicator, TabsList, TabsPanel, TabsRoot, TabsTab } from "#ui/tabs"

const sections = [
  { value: "overview", label: "Overview", text: "Requests are up 12% this week, with a p95 latency of 182 ms." },
  { value: "analytics", label: "Analytics", text: "4,812 visitors from 63 countries. Most came from search." },
  { value: "reports", label: "Reports", text: "Three reports are scheduled. The next one goes out on Monday." },
  { value: "notifications", label: "Notifications", text: "You have no unread notifications." }
]

const TabsUnderlineMessage = UiSchema.TaggedEnum({ SelectedTab: { value: Schema.String } })

export const TabsUnderline = Html.component({
  name: "TabsUnderline",
  model: Schema.Struct({ tab: Schema.String }),
  message: TabsUnderlineMessage
}).make({
  init: () => ({ tab: "overview" }),
  update: {
    SelectedTab: ($, { value }) => $.evolve({ tab: value })
  },
  view: (h, model) =>
    Tabs(h, {
      props: { value: model.tab, tabs: sections.map(({ value }) => ({ value })) },
      onEmit: { Selected: ({ value }) => TabsUnderlineMessage.SelectedTab({ value }) },
      view: (tabs) =>
        TabsRoot(h, tabs, { class: "max-w-lg" }).children(
          TabsList(h, tabs).children(
            sections.map(({ label, value }) =>
              TabsTab(h, tabs, { value }).children(label)
            ),
            // Placed by the --active-tab-left and --active-tab-width variables the kit measures.
            TabsIndicator(h, tabs)
          ),
          sections.map(({ label, text, value }) =>
            TabsPanel(h, tabs, { value })?.children(
              h.h4.class("m-0 text-sm font-semibold").children(label),
              h.p.class("m-0 text-[13px] text-muted-foreground").children(text)
            )
          )
        )
    })
})

Examples

Segmented

The same indicator as a raised pill behind the active tab.

$12

per seat, billed every month

Vertical

Up and Down move between the tabs, and with activateOnFocus they switch the panel too.

General

The workspace name, its URL and its default language.

Icons and a disabled tab

A disabled tab keeps its place in the arrow keys' order and never activates.

The page as your visitors see it.

Forms in panels

With keepMounted a hidden panel stays in the page, so a half-filled form survives a switch.

Account

Change how you appear to your team.

A tab can be an <a>, with an address to copy or open in a new tab. Here follow is off, so a click only activates.

Anatomy

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

PartDescription
rootAny element around the list and the panels. It points each tab at its panel.
listAny element around the tabs: the tablist.
tab({ element, value })A <button> or an <a> in the list.
indicatorAny element in the list, placed against it. undefined while no tab is active, hidden until measured.
panel({ element, value })Any element in the root. undefined while hidden, unless kept mounted.

API reference

Props

PropTypeDescription
valuestring | nullThe active tab. Required: the consumer owns it.
tabsReadonlyArray<{ value, disabled?, keepMounted? }>Every tab, in the order the list shows them.
orientation"horizontal" | "vertical"Which arrow keys move between tabs. Default "horizontal".
activateOnFocusbooleanMoving focus with the arrow keys also activates the tab.
loopFocusbooleanThe arrow keys wrap around. Default true.
keepMountedbooleanKeeps every panel drawn, hidden while its tab isn't active.

Events

EventDescription
Selected({ value, activationDirection })Asks for value to be the tab the user activated.

Data attributes

AttributeDescription
data-orientationOn every part: horizontal or vertical.
data-activation-directionOn every part: left, right, up, down or none, where the active tab moved.
data-activeOn the active tab.
data-disabledOn a disabled tab.
data-hiddenOn a panel that is kept mounted while hidden.
data-starting-style / data-ending-styleOn a panel for its first frame, and while it plays its exit.
--active-tab-left / -top / -width / -heightCSS variables on the indicator, in pixels from the list's padding box.

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.