Content that opens beside its trigger and closes when you click away.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { buttonStyle } from "./ui/uicontext/components/Button.ts"import { CopyButton } from "./ui/uicontext/components/CopyButton.ts"import { Popover, PopoverTrigger } from "./ui/uicontext/components/Popover.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ content: { display: "grid", gap: space.sm, padding: space.md, inlineSize: 300, maxInlineSize: "100%", boxSizing: "border-box" }, title: { margin: 0, fontSize: font.base, fontWeight: 600 }, text: { margin: 0, fontSize: font.sm, color: color.muted }, row: { display: "flex", alignItems: "center", justifyContent: "space-between", gap: space.md }, link: { minInlineSize: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", fontFamily: font.mono, fontSize: font.xs },})const Message = UiSchema.TaggedEnum({ ToggledShare: { open: Schema.Boolean } })export const SharePopover = Html.component({ name: "SharePopover", model: Schema.Struct({ open: Schema.Boolean }), message: Message,}).make({ init: () => ({ open: false }), update: { ToggledShare: ($, { open }) => $.evolve({ open }) }, view: (h, { open }) => h.div.children( PopoverTrigger(h, { target: "share-report", style: buttonStyle() }).children("Share…"), Popover(h, { id: "share-report", open, label: "Share this report", onToggle: (open) => Message.ToggledShare({ open }), children: [ h.div.attrs(sx(styles.content)).children( h.p.attrs(sx(styles.title)).children("Share this report"), h.p.attrs(sx(styles.text)).children("Anyone in the workspace with the link can view it."), h.div.attrs(sx(styles.row)).children( h.span.attrs(sx(styles.link)).children("northpeak.app/r/weekly-ops"), CopyButton(h, { props: { value: "https://northpeak.app/r/weekly-ops", label: "Copy link", accessibleName: "Copy the report link" } }), ), ), ], }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/popoverUsage
import { buttonStyle } from "./ui/uicontext/components/Button.ts"import { Popover, PopoverTrigger } from "./ui/uicontext/components/Popover.ts"const Message = UiSchema.TaggedEnum({ ToggledHelp: { open: Schema.Boolean } })export const Help = (h: Html.H<typeof Message.Type>, open: boolean) => [ PopoverTrigger(h, { target: "help", style: buttonStyle({ emphasis: "ghost" }) }).children("What's a cycle?"), Popover(h, { id: "help", open, label: "Cycles", onToggle: (open) => Message.ToggledHelp({ open }), children: [h.p.children("A cycle is a fixed stretch of time, usually two weeks, that a team plans work into.")], }),]Examples
Placements
placement picks the side; when there's no room there, the popover flips to the other side.
Near a screen edge it flips to the side with room.
Near a screen edge it flips to the side with room.
Near a screen edge it flips to the side with room.
Near a screen edge it flips to the side with room.
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { buttonStyle } from "./ui/uicontext/components/Button.ts"import { type Placement, Popover, PopoverTrigger } from "./ui/uicontext/components/Popover.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ row: { display: "flex", flexWrap: "wrap", justifyContent: "center", gap: space.md }, note: { margin: 0, padding: space.md, maxInlineSize: 220, fontSize: font.sm, color: color.muted },})const placements: ReadonlyArray<Placement> = ["bottom-start", "bottom-end", "top-start", "right-start"]const Message = UiSchema.TaggedEnum({ Toggled: { placement: Schema.String, open: Schema.Boolean } })export const Placements = Html.component({ name: "PopoverPlacements", model: Schema.Struct({ open: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ open: null }), update: { Toggled: ($, { placement, open }) => $.evolve({ open: open ? placement : $.model.open === placement ? null : $.model.open }), }, view: (h, { open }) => h.div.attrs(sx(styles.row)).children( placements.map((placement) => h.div.key(placement).children( PopoverTrigger(h, { target: `placement-${placement}`, style: buttonStyle() }).children(placement), Popover(h, { id: `placement-${placement}`, open: open === placement, placement, label: placement, onToggle: (next) => Message.Toggled({ placement, open: next }), children: [h.p.attrs(sx(styles.note)).children("Near a screen edge it flips to the side with room.")], }), )), ),})API reference
PopoverTriggerView function
Options
| Name | Type | Description |
|---|---|---|
targetrequired | string | The id of the popover it opens and closes. |
style | Style | Style[] | StyleX styles for the button: one style, or a list such as buttonStyle() |
PopoverView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
idrequired | string | — | The popover's id, which its PopoverTrigger names as target. |
openrequired | boolean | — | Whether it shows. The caller keeps it and follows onToggle. |
onTogglerequired | (open: boolean) => Message | — | The browser opened or closed it (its trigger, Escape, a click outside); the Model follows |
placement | "bottom-start" | "bottom-end" | "top-start" | "right-start" | bottom-start | Where it opens beside its trigger; bottom-start by default. |
label | string | — | The popover's accessible name, when its content has no heading. |
childrenrequired | View.Children | — | What the popover holds. |
style | Style | — | StyleX styles applied after the popover's own. |
Accessibility
- The trigger is a real button, and the popover is a
dialog; give it alabelwhen its content has no heading. - Escape, a click outside or another popover opening closes it, and focus goes back to the trigger.