Button
A native button that can stay focusable while disabled.
Pressed 0 times
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`)
)
})
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/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)Import it
import { buttonVariants, Button } from "#ui/button"
Usage
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
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
const ButtonSizesMessage = UiSchema.TaggedEnum({ PressedButton: { size: Schema.String } })
const sizes = [
{ size: "sm", label: "Small" },
{ size: "default", label: "Default" },
{ size: "lg", label: "Large" }
] as const
export const ButtonSizes = Html.component({
name: "ButtonSizes",
model: Schema.Struct({ last: Schema.NullOr(Schema.String) }),
message: ButtonSizesMessage
}).make({
init: () => ({ last: null }),
update: {
PressedButton: ($, { size }) => $.evolve({ last: size })
},
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(
...sizes.map(({ label, size }) =>
Button(h, { size, onPress: ButtonSizesMessage.PressedButton({ size }) }).children(label)
)
),
h.div.class("flex flex-wrap items-center justify-center gap-2.5").children(
...sizes.map(({ label, size }) =>
Button(h, { variant: "outline", size, onPress: ButtonSizesMessage.PressedButton({ size }) })
.children(label)
)
),
h.p.class("text-xs text-muted-foreground").children(
model.last === null ? "Press a button" : `Pressed the ${model.last} size`
)
)
})
With icon
An inline SVG before or after the label.
Press a button
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
const ButtonWithIconMessage = UiSchema.TaggedEnum({
PressedNewBranch: {},
PressedDownload: {},
PressedContinue: {}
})
export const ButtonWithIcon = Html.component({
name: "ButtonWithIcon",
model: Schema.Struct({ last: Schema.NullOr(Schema.String) }),
message: ButtonWithIconMessage
}).make({
init: () => ({ last: null }),
update: {
PressedNewBranch: ($) => $.evolve({ last: "New branch" }),
PressedDownload: ($) => $.evolve({ last: "Download" }),
PressedContinue: ($) => $.evolve({ last: "Continue" })
},
view: (h, model) => {
const icon = (d: string) =>
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", d))
return 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(
Button(h, { onPress: ButtonWithIconMessage.PressedNewBranch() }).children(
icon("M6 3v12M18 9a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM6 21a3 3 0 1 0 0-6 3 3 0 0 0 0 6zM18 9a9 9 0 0 1-9 9"),
"New branch"
),
Button(h, { variant: "outline", onPress: ButtonWithIconMessage.PressedDownload() }).children(
icon("M12 3v12M7 10l5 5 5-5M5 21h14"),
"Download"
),
Button(h, { variant: "secondary", onPress: ButtonWithIconMessage.PressedContinue() }).children(
"Continue",
icon("M5 12h14M13 6l6 6-6 6")
)
),
h.p.class("text-xs text-muted-foreground").children(
model.last === null ? "Press a button" : `Pressed ${model.last}`
)
)
}
})
Icon only
A square button, in two sizes, that takes its name from aria-label.
Press a button
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
const ButtonIconOnlyMessage = UiSchema.TaggedEnum({ PressedAction: { action: Schema.String } })
const actions = [
{ action: "Add", variant: "default", size: "icon", d: "M12 5v14M5 12h14" },
{ action: "Edit", variant: "outline", size: "icon", d: "M4 20h4L19 9l-4-4L4 16v4zM13.5 6.5l4 4" },
{
action: "Share",
variant: "secondary",
size: "icon",
d: "M12 15V3M8 7l4-4 4 4M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2v-7"
},
{ action: "More", variant: "ghost", size: "icon", d: "M5 12h.01M12 12h.01M19 12h.01" },
{ action: "Delete", variant: "destructive", size: "icon", d: "M4 7h16M10 11v6M14 11v6M6 7l1 13h10l1-13M9 7V4h6v3" },
{ action: "Close", variant: "outline", size: "icon-sm", d: "M6 6l12 12M18 6L6 18" }
] as const
export const ButtonIconOnly = Html.component({
name: "ButtonIconOnly",
model: Schema.Struct({ last: Schema.NullOr(Schema.String) }),
message: ButtonIconOnlyMessage
}).make({
init: () => ({ last: null }),
update: {
PressedAction: ($, { action }) => $.evolve({ last: action })
},
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(
...actions.map(({ action, d, size, variant }) =>
// An icon has no text, so the button takes its name from aria-label.
Button(h, { variant, size, onPress: ButtonIconOnlyMessage.PressedAction({ action }) })
.attr("aria-label", action)
.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", d))
)
)
),
h.p.class("text-xs text-muted-foreground").children(
model.last === null ? "Press a button" : `Pressed ${model.last}`
)
)
})
Disabled
A disabled button leaves the tab order. With focusableWhenDisabled it keeps its place and uses aria-disabled instead.
Pressed 0 times
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
const ButtonDisabledMessage = UiSchema.TaggedEnum({
PressedButton: {},
PressedToggleDisabled: {}
})
export const ButtonDisabled = Html.component({
name: "ButtonDisabled",
model: Schema.Struct({ disabled: Schema.Boolean, presses: Schema.Number }),
message: ButtonDisabledMessage
}).make({
init: () => ({ disabled: true, presses: 0 }),
update: {
PressedButton: ($) => $.evolve({ presses: (presses) => presses + 1 }),
PressedToggleDisabled: ($) => $.evolve({ disabled: (disabled) => !disabled })
},
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(
Button(h, {
props: { disabled: model.disabled },
onPress: ButtonDisabledMessage.PressedButton()
}).children("Disabled"),
// Stays in the tab order with aria-disabled, and cancels its clicks.
Button(h, {
props: { disabled: model.disabled, focusableWhenDisabled: true },
onPress: ButtonDisabledMessage.PressedButton()
}).children("Focusable when disabled"),
Button(h, {
variant: "outline",
props: { disabled: model.disabled },
onPress: ButtonDisabledMessage.PressedButton()
}).children("Outline")
),
h.div.class("flex items-center gap-4").children(
Button(h, { variant: "link", size: "sm", onPress: ButtonDisabledMessage.PressedToggleDisabled() })
.children(model.disabled ? "Enable the buttons" : "Disable the buttons"),
h.p.class("text-xs text-muted-foreground tabular-nums").children(`Pressed ${model.presses} times`)
)
)
})
Loading
A save that runs a Command. The loading option shows a spinner and keeps focus on the button.
Nothing saved yet
import { Command, UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Effect, Schema } from "effect"
import { Button } from "#ui/button"
const ButtonLoadingMessage = UiSchema.TaggedEnum({
PressedSave: {},
CompletedSaveChanges: {}
})
const SaveChanges = Command.make("SaveChanges", { args: {} }, () =>
Effect.sleep("1500 millis").pipe(
Effect.as(ButtonLoadingMessage.CompletedSaveChanges())
))
export const ButtonLoading = Html.component({
name: "ButtonLoading",
model: Schema.Struct({ saving: Schema.Boolean, saves: Schema.Number }),
message: ButtonLoadingMessage
}).make({
init: () => ({ saving: false, saves: 0 }),
update: {
PressedSave: ($) =>
$.model.saving
? $.model
: $.evolve({ saving: true }).run(SaveChanges.make({})),
CompletedSaveChanges: ($) =>
$.evolve({ saving: false, saves: (saves) => saves + 1 })
},
view: (h, model) =>
h.div.class("flex flex-col items-center gap-4").children(
// `loading` disables the button but keeps it focusable, so a keyboard user isn't dropped.
Button(h, { loading: model.saving, onPress: ButtonLoadingMessage.PressedSave() })
.attr("aria-busy", model.saving ? "true" : undefined)
.children(model.saving ? "Saving" : "Save changes"),
h.p.class("text-xs text-muted-foreground tabular-nums").attr("aria-live", "polite").children(
model.saving
? "Saving your changes"
: model.saves === 0
? "Nothing saved yet"
: `Saved ${model.saves} times`
)
)
})
Submit
A button only submits its form when its type says so.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Button } from "#ui/button"
const ButtonSubmitMessage = UiSchema.TaggedEnum({
UpdatedEmail: { value: Schema.String },
SubmittedForm: { email: Schema.String },
PressedReset: {}
})
export const ButtonSubmit = Html.component({
name: "ButtonSubmit",
model: Schema.Struct({ email: Schema.String, subscribed: Schema.NullOr(Schema.String) }),
message: ButtonSubmitMessage
}).make({
init: () => ({ email: "", subscribed: null }),
update: {
UpdatedEmail: ($, { value }) => $.evolve({ email: value }),
SubmittedForm: ($, { email }) => $.evolve({ subscribed: email }),
PressedReset: ($) => $.evolve({ email: "", subscribed: null })
},
view: (h, model) =>
h.form
.class("flex w-full max-w-xs flex-col gap-3")
.on("submit", ({ fields }) => ButtonSubmitMessage.SubmittedForm({ email: fields.email ?? "" }))
.children(
h.label.class("text-[13px] font-medium").attr("for", "button-submit-email").children("Email"),
h.input
.class(
"h-8.5 w-full rounded-md border border-input bg-background px-3 text-[13px] shadow-sm placeholder:text-muted-foreground"
)
.attrs({
id: "button-submit-email",
name: "email",
type: "email",
required: "",
placeholder: "you@example.com"
})
.value(model.email)
.on("input", ({ value }) => ButtonSubmitMessage.UpdatedEmail({ value })),
h.div.class("flex items-center gap-2.5").children(
// `type` is "button" unless the props say otherwise.
Button(h, { props: { type: "submit" } }).children("Subscribe"),
Button(h, { variant: "ghost", onPress: ButtonSubmitMessage.PressedReset() }).children("Reset")
),
h.p.class("text-xs text-muted-foreground").children(
model.subscribed === null ? "Not subscribed" : `Subscribed ${model.subscribed}`
)
)
})
API reference
Props
| Prop | Type | Description |
|---|---|---|
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. |
loading | boolean | An option of the styled wrapper: shows a spinner, sets data-loading, and disables the button while keeping it focusable. |
props.disabled | boolean | Sends no press message and sets data-disabled. |
props.focusableWhenDisabled | boolean | A 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. |
element | h.button | The caller's button, to share it with another part such as a tooltip trigger. |
Events
| Event | Description |
|---|---|
onPress | The message a click, Space or Enter sends. A disabled button sends none. |
Data attributes
| Attribute | Description |
|---|---|
data-disabled | Present while the button is disabled, with or without the native attribute. |
data-loading | Set by the styled wrapper while loading. |