A panel that slides over the page from a side or the bottom.
Showing every issue.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button, buttonStyle } from "./ui/uicontext/components/Button.ts"import { Checkbox } from "./ui/uicontext/components/Checkbox.ts"import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"import { Sheet } from "./ui/uicontext/components/Sheet.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ stack: { display: "flex", flexDirection: "column", gap: space.lg }, group: { display: "flex", flexDirection: "column", gap: space.xxs }, legend: { margin: 0, marginBlockEnd: space.xs, fontSize: font.sm, fontWeight: 500, color: color.fg }, center: { display: "grid", justifyItems: "center", gap: 12 }, status: { margin: 0, fontSize: font.sm, color: color.muted },})const labels = ["Bug", "Feature", "Design", "Performance"]const Message = UiSchema.TaggedEnum({ RequestedOpen: {}, RequestedClose: {}, ToggledLabel: { label: Schema.String, checked: Schema.Boolean }, ChangedAssignee: { assignee: Schema.String }, ClickedApply: {},})export const FilterSheet = Html.component({ name: "FilterSheet", model: Schema.Struct({ open: Schema.Boolean, labels: Schema.Array(Schema.String), assignee: Schema.String, applied: Schema.String, }), message: Message,}).make({ init: () => ({ open: false, labels: ["Bug"], assignee: "", applied: "Showing every issue." }), update: { RequestedOpen: ($) => $.evolve({ open: true }), RequestedClose: ($) => $.evolve({ open: false }), ToggledLabel: ($, { label, checked }) => $.evolve({ labels: (list) => (checked ? [...list, label] : list.filter((l) => l !== label)) }), ChangedAssignee: ($, { assignee }) => $.evolve({ assignee }), ClickedApply: ($) => $.evolve({ open: false, applied: `Showing ${$.model.labels.length === 0 ? "every label" : $.model.labels.join(", ")}${$.model.assignee.trim() === "" ? "" : ` for ${$.model.assignee.trim()}`}.`, }), }, view: (h, model) => h.div.attrs(sx(styles.center)).children( Sheet(h, { open: model.open, title: "Filters", description: "Narrow the issues on this board.", onRequestedOpen: Message.RequestedOpen(), onRequestedClose: Message.RequestedClose(), trigger: (sheet) => sheet.trigger(h.button).attrs(sx(...buttonStyle())).children("Filters"), children: [ h.div.attrs(sx(styles.stack)).children( FormField(h, { id: h.id("assignee"), label: "Assignee" }, (attrs) => Input(h).attrs({ ...attrs, placeholder: "Anyone" }).value(model.assignee) .on("input", ({ value }) => Message.ChangedAssignee({ assignee: value }))), h.div.attrs({ role: "group", "aria-labelledby": h.id("labels"), ...sx(styles.group) }).children( h.p.attrs({ id: h.id("labels"), ...sx(styles.legend) }).children("Labels"), ...labels.map((label) => Checkbox(h, { key: label, checked: model.labels.includes(label), label, onToggle: (checked) => Message.ToggledLabel({ label, checked }), })), ), ), ], footer: [ Button(h, { emphasis: "ghost" }).on("click", Message.RequestedClose()).children("Cancel"), Button(h, { tone: "primary" }).on("click", Message.ClickedApply()).children("Apply filters"), ], }), h.p.attrs({ role: "status", ...sx(styles.status) }).children(model.applied), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/sheetUsage
import { buttonStyle } from "./ui/uicontext/components/Button.ts"import { Sheet } from "./ui/uicontext/components/Sheet.ts"import { sx } from "./ui/uicontext/components/Sx.ts"const Message = UiSchema.TaggedEnum({ RequestedOpen: {}, RequestedClose: {} })// `open` lives in your Model: RequestedOpen sets it, RequestedClose clears itexport const DetailsSheet = (h: Html.H<typeof Message.Type>, open: boolean) => Sheet(h, { open, title: "Invoice INV-2041", onRequestedOpen: Message.RequestedOpen(), onRequestedClose: Message.RequestedClose(), trigger: (sheet) => sheet.trigger(h.button).attrs(sx(...buttonStyle())).children("Details"), children: [h.p.children("Due on March 3. Sent to [email protected].")], })Examples
Bottom and left
side picks the edge: bottom for a quick look on a phone, left for navigation. On touch, swipe it back toward its edge to close it.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Badge } from "./ui/uicontext/components/Badge.ts"import { buttonStyle } from "./ui/uicontext/components/Button.ts"import { Sheet } from "./ui/uicontext/components/Sheet.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", justifyContent: "center", gap: 8 }, props: { display: "grid", gridTemplateColumns: "96px minmax(0, 1fr)", rowGap: space.md, margin: 0, fontSize: font.sm }, term: { color: color.muted }, value: { margin: 0, color: color.fg }, nav: { display: "flex", flexDirection: "column", gap: space.xxs }, link: { display: "flex", alignItems: "center", height: 36, paddingInline: space.md, borderRadius: radius.md, color: color.fg, textDecoration: "none", fontSize: font.base, backgroundColor: { default: "transparent", ":hover": color.hover }, outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` }, },})const Which = Schema.Literals(["bottom", "left"])const Message = UiSchema.TaggedEnum({ RequestedOpen: { which: Which }, RequestedClose: { which: Which },})export const SheetSides = Html.component({ name: "SheetSides", model: Schema.Struct({ open: Schema.NullOr(Which) }), message: Message,}).make({ init: () => ({ open: null }), update: { RequestedOpen: ($, { which }) => $.evolve({ open: which }), RequestedClose: ($, { which }) => ($.model.open === which ? $.evolve({ open: null }) : $.model), }, view: (h, { open }) => h.div.attrs(sx(styles.row)).children( // from the bottom: a quick look at one record, easy to reach with a thumb Sheet(h, { key: "bottom", open: open === "bottom", side: "bottom", title: "ENG-142 · Invoices fail for annual plans", onRequestedOpen: Message.RequestedOpen({ which: "bottom" }), onRequestedClose: Message.RequestedClose({ which: "bottom" }), trigger: (sheet) => sheet.trigger(h.button).attrs(sx(...buttonStyle())).children("Bottom sheet"), children: [ h.dl.attrs(sx(styles.props)).children( h.dt.attrs(sx(styles.term)).children("Status"), h.dd.attrs(sx(styles.value)).children(Badge(h, { tone: "info", dot: true }).children("In progress")), h.dt.attrs(sx(styles.term)).children("Assignee"), h.dd.attrs(sx(styles.value)).children("Ana Lima"), h.dt.attrs(sx(styles.term)).children("Priority"), h.dd.attrs(sx(styles.value)).children(Badge(h, { tone: "danger" }).children("Urgent")), ), ], }), // from the left, small: the app's navigation on a phone Sheet(h, { key: "left", open: open === "left", side: "left", size: "sm", title: "Acme Studio", onRequestedOpen: Message.RequestedOpen({ which: "left" }), onRequestedClose: Message.RequestedClose({ which: "left" }), trigger: (sheet) => sheet.trigger(h.button).attrs(sx(...buttonStyle({ emphasis: "ghost" }))).children("Left sheet"), children: [ h.nav.attrs({ "aria-label": "Workspace", ...sx(styles.nav) }).children( ["Inbox", "My issues", "Projects", "Views", "Settings"].map((item) => h.a.key(item).attrs({ href: "#", ...sx(styles.link) }).children(item)), ), ], }), ),})API reference
SheetPreviewView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
side | "right" | "left" | "bottom" | right | The edge it's drawn against; right by default |
size | "sm" | "md" | "lg" | — | The width of a side sheet |
titlerequired | string | — | The sheet's heading |
description | string | — | A sentence under the heading |
childrenrequired | View.Children | — | The sheet's body |
footer | View.Children | — | Actions at the bottom, the primary one last |
style | Style | — | Extra StyleX styles for the panel |
SheetView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
key | string | — | Names the sheet among others in the same view; needed for more than one |
openrequired | boolean | — | Whether the sheet shows; the caller keeps it |
side | "right" | "left" | "bottom" | right | The edge it slides in from; right by default |
size | "sm" | "md" | "lg" | — | The width of a side sheet |
titlerequired | string | — | The sheet's heading and accessible name |
description | string | — | A sentence under the heading, read with it as the sheet's description |
onRequestedOpen | Message | — | A trigger asked to open it |
onRequestedCloserequired | Message | — | Escape, a press outside, a swipe or the close button asked to close it |
trigger | (sheet: SheetParts<Message>) => View.Input | — | The button that opens it: apply sheet.trigger to a button builder, so focus comes back to it |
childrenrequired | View.Children | — | The sheet's body |
footer | View.Children | — | Actions at the bottom, the primary one last |
onKeyDown | (press: SheetKeyPress) => Message | — | Keys the sheet itself answers, wherever focus is inside it (its close button included): a message, or undefined to let the key through. Keys typed into a text field never reach it. |
style | Style | — | Extra StyleX styles for the panel |
Accessibility
- A modal dialog named by its title: focus moves in and stays, and goes back to what opened it when it closes.
- Escape, a press on the dimmed page, a swipe toward its edge or the Close button ask to close; your Model decides whether it does.