Pressed 0 times

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/button.ts
    import { Button as KitButton, type ButtonElement, type ButtonProps } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    const base =
      "inline-flex h-8.5 shrink-0 items-center justify-center gap-1.5 rounded-md border border-transparent px-3.5 " +
      "text-[13px] leading-none font-medium whitespace-nowrap no-underline select-none " +
      "transition-[background-color,border-color,color,opacity] duration-150 active:translate-y-px " +
      "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 [&_svg]:size-[15px] [&_svg]:shrink-0 " +
      "data-[loading]:before:size-3 data-[loading]:before:animate-spin data-[loading]:before:rounded-full " +
      "data-[loading]:before:border-2 data-[loading]:before:border-current data-[loading]:before:border-r-transparent"
    
    const variants = {
      default: "bg-primary text-primary-foreground hover:bg-primary/85",
      secondary: "bg-secondary text-secondary-foreground hover:bg-accent",
      outline: "border-input bg-background text-foreground shadow-sm hover:bg-accent hover:text-accent-foreground",
      ghost: "text-foreground hover:bg-accent hover:text-accent-foreground",
      brand: "bg-brand-soft text-brand-soft-foreground hover:bg-brand-soft/85",
      destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/85",
      link: "h-auto px-0 text-foreground underline decoration-transparent underline-offset-4 hover:decoration-current"
    } as const
    
    const sizes = {
      default: "",
      sm: "h-7 rounded-sm px-2.5 text-xs",
      lg: "h-10 px-5 text-sm",
      icon: "w-8.5 px-0",
      "icon-sm": "h-7 w-7 rounded-sm px-0"
    } as const
    
    export type ButtonVariant = keyof typeof variants
    export type ButtonSize = keyof typeof sizes
    
    /** The button's classes, for an element that should look like one: a link, a kit trigger. */
    export const buttonVariants = (
      options: {
        readonly variant?: ButtonVariant | undefined
        readonly size?: ButtonSize | undefined
        readonly class?: string | undefined
      } = {}
    ): string => cn(base, variants[options.variant ?? "default"], sizes[options.size ?? "default"], options.class)
    
    /**
     * The kit's Button with a look. Pass the message for a press as `onPress`
     * and chain the label: `Button(h, { variant: "outline", onPress }).children("Save")`.
     * `loading` shows a spinner and keeps the button focusable while it's disabled.
     */
    export const Button = <Msg, R = never>(
      h: { readonly button: ButtonElement<Msg> },
      options: {
        readonly variant?: ButtonVariant | undefined
        readonly size?: ButtonSize | undefined
        readonly class?: string | undefined
        readonly loading?: boolean | undefined
        readonly element?: ButtonElement<Msg, R> | undefined
        readonly props?: ButtonProps | undefined
        readonly onPress?: NoInfer<Msg> | undefined
      } = {}
    ): ButtonElement<Msg, R> =>
      KitButton<Msg, R>(h, {
        element: options.element,
        onPress: options.onPress,
        props: options.loading === true
          ? { ...options.props, disabled: true, focusableWhenDisabled: true }
          : options.props
      })
        .class(buttonVariants(options))
        .attr("data-loading", options.loading === true ? "" : undefined)
    
  3. Import it

    import { buttonVariants, Button } from "#ui/button"
    

Usage

Variants.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"

const ButtonVariantsMessage = UiSchema.TaggedEnum({ PressedButton: {} })

const variants = ["default", "secondary", "outline", "ghost", "brand", "destructive", "link"] as const

export const ButtonVariants = Html.component({
  name: "ButtonVariants",
  model: Schema.Struct({ presses: Schema.Number }),
  message: ButtonVariantsMessage
}).make({
  init: () => ({ presses: 0 }),
  update: {
    PressedButton: ($) => $.evolve({ presses: (presses) => presses + 1 })
  },
  view: (h, model) =>
    h.div.class("flex flex-col items-center gap-4").children(
      h.div.class("flex flex-wrap items-center justify-center gap-2.5").children(
        ...variants.map((variant) =>
          Button(h, { variant, onPress: ButtonVariantsMessage.PressedButton() })
            .children(variant[0]!.toUpperCase() + variant.slice(1))
        )
      ),
      h.p.class("text-xs text-muted-foreground tabular-nums").children(`Pressed ${model.presses} times`)
    )
})

Examples

Sizes

Small, default and large, picked with the size option.

Press a button

With icon

An inline SVG before or after the label.

Press a button

Icon only

A square button, in two sizes, that takes its name from aria-label.

Press a button

Disabled

A disabled button leaves the tab order. With focusableWhenDisabled it keeps its place and uses aria-disabled instead.

Pressed 0 times

Loading

A save that runs a Command. The loading option shows a spinner and keeps focus on the button.

Nothing saved yet

Submit

A button only submits its form when its type says so.

Not subscribed

API reference

Props

PropTypeDescription
variant"default" | "secondary" | "outline" | "ghost" | "brand" | "destructive" | "link"The look. An option of the styled wrapper, beside props.
size"default" | "sm" | "lg" | "icon" | "icon-sm"The height and padding. An option of the styled wrapper.
loadingbooleanAn option of the styled wrapper: shows a spinner, sets data-loading, and disables the button while keeping it focusable.
props.disabledbooleanSends no press message and sets data-disabled.
props.focusableWhenDisabledbooleanA disabled button stays in the tab order, with aria-disabled in place of the native disabled.
props.type"button" | "submit" | "reset"The native button type. "button" by default.
elementh.buttonThe caller's button, to share it with another part such as a tooltip trigger.

Events

EventDescription
onPressThe message a click, Space or Enter sends. A disabled button sends none.

Data attributes

AttributeDescription
data-disabledPresent while the button is disabled, with or without the native attribute.
data-loadingSet by the styled wrapper while loading.

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