One line under a form or a list that says what just happened, such as Saved or Couldn't save, read aloud when it changes.
- 1.2 kB JS 2.5 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 10 tests
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { InlineNotice } from "./ui/uicontext/components/InlineNotice.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, density, font, motion, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ form: { display: "grid", gap: space.sm, inlineSize: "min(100%, 24rem)" }, label: { fontSize: font.sm, fontWeight: 500 }, input: { minBlockSize: density.control, paddingInline: space.md, borderWidth: stroke.control, borderStyle: "solid", borderColor: color.controlLine, borderRadius: corner.control, backgroundColor: color.panel, color: color.fg, fontFamily: "inherit", fontSize: { default: font.sm, "@media (pointer: coarse)": "1rem" }, outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` }, }, actions: { display: "flex", flexWrap: "wrap", gap: space.sm }, button: { minBlockSize: density.control, paddingInline: space.md, borderWidth: stroke.control, borderStyle: "solid", borderColor: color.controlLine, borderRadius: corner.control, backgroundColor: { default: color.panel, ":hover": { default: null, "@media (hover: hover) and (pointer: fine)": color.hover } }, color: color.fg, fontFamily: "inherit", fontSize: font.sm, fontWeight: 500, cursor: "pointer", outline: { default: "none", ":focus-visible": `2px solid ${color.focusRing}` }, outlineOffset: 2, transform: { default: "none", ":active": `scale(${motion.press})` }, transitionProperty: "background-color, transform", transitionDuration: motion.fast, },})const Message = UiSchema.TaggedEnum({ Saved: {}, Failed: {} })// a settings field: the notice under it says how the last save went, and a screen reader hears itexport const WorkspaceName = Html.component({ name: "WorkspaceName", model: Schema.Struct({ result: Schema.Literals(["none", "saved", "failed"]) }), message: Message,}).make({ init: () => ({ result: "none" as const }), update: { Saved: ($) => $.evolve({ result: () => "saved" as const }), Failed: ($) => $.evolve({ result: () => "failed" as const }) }, view: (h, model) => h.div.attrs(sx(styles.form)).children( h.label.attrs({ for: "workspace-name", ...sx(styles.label) }).children("Workspace name"), h.input.attrs({ id: "workspace-name", value: "Harborview Support", "aria-describedby": "workspace-name-notice", ...sx(styles.input) }), InlineNotice(h, { id: "workspace-name-notice", tone: model.result === "failed" ? "danger" : "success", text: model.result === "saved" ? "Saved. Everyone sees the new name." : model.result === "failed" ? "Couldn't save. Check your connection and try again." : "", }), h.div.attrs(sx(styles.actions)).children( h.button.attrs({ type: "button", ...sx(styles.button) }).on("click", Message.Saved()).children("Save"), h.button.attrs({ type: "button", ...sx(styles.button) }).on("click", Message.Failed()).children("Save while offline"), ), ),})When to use
One line under a form or a list that says what just happened, such as Saved or Couldn't save, read aloud when it changes. Use Toast instead when the message isn't tied to one form or list.
Usage
import { InlineNotice } from "./ui/uicontext/components/InlineNotice.ts"export const Saved = <M>(h: Html.H<M>, savedAt: string | null) => InlineNotice(h, { tone: "success", text: savedAt === null ? "" : `Saved at ${savedAt}` })Examples
Tones
Plain for status, then info, success, warning and danger. danger is announced at once; the rest wait for a pause.
Last synced 2 minutes ago
Imports over 10,000 rows run in the background; we'll email you when it's done.
Imported 1,208 contacts.
14 rows skipped: no email address.See rows
Deploy failed at step 3 of 5: the build ran out of memory.
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { InlineNotice } from "./ui/uicontext/components/InlineNotice.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { touchArea } from "./ui/uicontext/components/touch.stylex.ts"const styles = stylex.create({ stack: { display: "grid", gap: space.md, inlineSize: "min(100%, 30rem)" }, // a finger gets 44px around the short link link: { position: "relative", "::after": { content: touchArea.content, position: "absolute", insetInline: touchArea.reach, insetBlock: touchArea.reach }, color: color.accent, fontSize: font.sm, fontWeight: 500, textDecoration: { default: "none", ":hover": { default: null, "@media (hover: hover) and (pointer: fine)": "underline" } } },})// what an import, a sync and a deploy say about themselves, one line eachexport const ImportResults = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.stack)).children( InlineNotice(h, { text: "Last synced 2 minutes ago" }), InlineNotice(h, { tone: "info", text: "Imports over 10,000 rows run in the background; we'll email you when it's done." }), InlineNotice(h, { tone: "success", text: "Imported 1,208 contacts." }), InlineNotice(h, { tone: "warning", text: "14 rows skipped: no email address.", action: h.a.attrs({ href: "#skipped", ...sx(styles.link) }).children("See rows") }), InlineNotice(h, { tone: "danger", size: "sm", text: "Deploy failed at step 3 of 5: the build ran out of memory." }), )API reference
Options
| Name | Type | Description |
|---|---|---|
textrequired | string | What happened or what's wrong. Empty draws only the live region, so the next text is heard. |
tone | "neutral" | "info" | "success" | "warning" | "danger" | danger is announced at once; the others wait for a pause. |
action | View.Content | Something to do about it, after the text: a Retry button, a link. |
size | "sm" | "md" | — |
id | string | An id, for the field whose aria-describedby points here. |
style | Style | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/inline-notice