A view of records inside a document, with its title and a switcher between its views.
4
4 rows
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { viewOf } from "./ui/uicontext/components/domain/Record.ts"import { Board } from "./ui/uicontext/components/kit/Board.ts"import { InlineDatabase } from "./ui/uicontext/components/kit/InlineDatabase.ts"import { Item, Value, type Property } from "./ui/uicontext/components/kit/Property.ts"import { Table } from "./ui/uicontext/components/kit/Table.ts"import { sx } from "./ui/uicontext/components/Sx.ts"// A hiring tracker inside a page. InlineDatabase draws the frame: the collection's title, its saved// views and the add button. The app draws the records in the `view` slot with any record view, here// a Table for "All candidates" and a Board for "By stage".const styles = stylex.create({ frame: { inlineSize: "100%" }, grid: { blockSize: "var(--rows)" }, board: { blockSize: 340 },})const fields: ReadonlyArray<Property> = [ { key: "stage", label: "Stage", kind: "select", options: [ { value: "screen", label: "Screen", tone: "gray" }, { value: "interview", label: "Interview", tone: "blue" }, { value: "offer", label: "Offer", tone: "green" }, ], }, { key: "role", label: "Role", kind: "text" },]const seed: ReadonlyArray<Item> = [ { id: "c1", title: "Amara Okafor", values: { stage: "interview", role: "Backend engineer" } }, { id: "c2", title: "Jonas Weber", values: { stage: "screen", role: "Product designer" } }, { id: "c3", title: "Sofia Marino", values: { stage: "offer", role: "Backend engineer" } }, { id: "c4", title: "Kenji Sato", values: { stage: "screen", role: "Support lead" } },]const views = [ { id: "all", label: "All candidates", layout: "table" }, { id: "stage", label: "By stage", layout: "board" },] as constconst Message = UiSchema.TaggedEnum({ PickedView: { view: Schema.String }, RenamedDatabase: { title: Schema.String }, AddedCandidate: {}, EditedCandidate: { item: Schema.String, key: Schema.String, value: Value },})export const HiringTracker = Html.component({ name: "HiringTracker", model: Schema.Struct({ title: Schema.String, view: Schema.String, records: Schema.Array(Item) }), message: Message,}).make({ init: () => ({ title: "Hiring tracker", view: "all", records: seed }), update: { PickedView: ($, { view }) => $.evolve({ view }), RenamedDatabase: ($, { title }) => $.evolve({ title }), AddedCandidate: ($) => $.evolve({ records: (r) => [...r, { id: `c${r.length + 1}`, title: "New candidate", values: { stage: "screen" } }] }), EditedCandidate: ($, { item, key, value }) => $.evolve({ records: (r) => r.map((i) => (i.id !== item ? i : key === "title" ? { ...i, title: String(value) } : { ...i, values: { ...i.values, [key]: value } })), }), }, view: (h, { title, view, records }) => { const body = ( view === "stage" ? h.div.attrs(sx(styles.board)).children( Board(h, { key: "board", props: { label: title, records, fields, view: viewOf({ groupBy: "stage" }, "board"), create: "New candidate" }, onEmit: { RequestedEdit: ({ item, key, value }) => Message.EditedCandidate({ item, key, value }), RequestedCreate: () => Message.AddedCandidate(), RequestedView: () => undefined, RequestedReorder: () => undefined, RequestedOpen: () => undefined, RequestedAction: () => undefined, Selected: () => undefined, }, }), ) : h.div.attrs(sx(styles.grid)).style({ "--rows": `${(records.length + 1) * 37 + 46}px` }).children( Table(h, { key: "table", props: { label: title, records, fields, title: { label: "Candidate", width: 220 }, selectable: false, rowHeight: 37 }, onEmit: { RequestedEdit: ({ item, key, value }) => Message.EditedCandidate({ item, key, value }), RequestedCreate: () => Message.AddedCandidate(), RequestedOpen: () => undefined, RequestedAction: () => undefined, RequestedAddColumn: () => undefined, RequestedCellAction: () => undefined, RequestedColumnAction: () => undefined, RequestedView: () => undefined, RequestedReorder: () => undefined, Selected: () => undefined, }, }), ) ) return h.div.attrs(sx(styles.frame)).children( InlineDatabase(h, { props: { block: "hiring", title, views, view, count: records.length, create: "New candidate" }, onEmit: { RequestedView: ({ view }) => Message.PickedView({ view }), RequestedTitle: ({ title }) => Message.RenamedDatabase({ title }), RequestedCreate: () => Message.AddedCandidate(), RequestedOpen: () => undefined, }, slots: { view: body }, }), ) },})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/inline-databaseUsage
import { InlineDatabase } from "./ui/uicontext/components/kit/InlineDatabase.ts"const Message = UiSchema.TaggedEnum({ PickedView: { view: Schema.String }, AddedTask: {},})// the frame around a record view: pass the Table, Board or List that shows the current viewexport const Tasks = (h: Html.H<typeof Message.Type>, view: string, count: number, records: View.Content<typeof Message.Type>) => InlineDatabase(h, { props: { block: "tasks", title: "Launch tasks", views: [{ id: "all", label: "All tasks", layout: "table" }, { id: "board", label: "By status", layout: "board" }], view, count, create: "New task", }, onEmit: { RequestedView: ({ view }) => Message.PickedView({ view }), RequestedCreate: () => Message.AddedTask(), RequestedTitle: () => undefined, RequestedOpen: () => undefined, }, slots: { view: records }, })Examples
Read-only
With can: [] the title can't be renamed and there's no add button; views still switch.
3
- Offline draftsFeb 18
- Shared templatesJan 27
- Faster searchJan 9
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { InlineDatabase } from "./ui/uicontext/components/kit/InlineDatabase.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"// A database someone can view but not change: `can: []` keeps the title fixed and hides the add// button, while views still switch. The slot can hold any view; here, a short list of releases.const styles = stylex.create({ frame: { inlineSize: "100%" }, list: { margin: 0, padding: 0, listStyle: "none", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.md }, row: { display: "flex", justifyContent: "space-between", gap: space.md, paddingBlock: space.sm, paddingInline: space.md, fontSize: font.sm, color: color.fg }, date: { color: color.muted },})const releases = [ { id: "r3", name: "Offline drafts", date: "Feb 18", shipped: false }, { id: "r2", name: "Shared templates", date: "Jan 27", shipped: true }, { id: "r1", name: "Faster search", date: "Jan 9", shipped: true },]const Message = UiSchema.TaggedEnum({ PickedView: { view: Schema.String } })export const ReleaseLog = Html.component({ name: "ReleaseLog", model: Schema.Struct({ view: Schema.String }), message: Message,}).make({ init: () => ({ view: "all" }), update: { PickedView: ($, { view }) => $.evolve({ view }) }, view: (h, { view }) => { const shown = view === "shipped" ? releases.filter((r) => r.shipped) : releases return h.div.attrs(sx(styles.frame)).children( InlineDatabase(h, { props: { block: "releases", title: "Release log", views: [{ id: "all", label: "All releases", layout: "list" }, { id: "shipped", label: "Shipped", layout: "list" }], view, count: shown.length, can: [], }, onEmit: { RequestedView: ({ view }) => Message.PickedView({ view }), RequestedTitle: () => undefined, RequestedCreate: () => undefined, RequestedOpen: () => undefined, }, slots: { view: h.ul.attrs(sx(styles.list)).children(shown.map((r) => h.li.key(r.id).attrs(sx(styles.row)).children(h.span.children(r.name), h.span.attrs(sx(styles.date)).children(r.date)))), }, }), ) },})Many views, loading and empty
In a narrow column the views fold into a menu. loading marks the view while its records arrive, and empty speaks for a view with none.
3
- Invoice shows the wrong tax ratePriya
- Can't reset password from the appMarco
- Export stops at 10,000 rowsPriya
import { Command, UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Effect, Schema } from "effect"import { InlineDatabase } from "./ui/uicontext/components/kit/InlineDatabase.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, radius, space } from "./ui/uicontext/components/tokens.stylex.ts"// A narrow column of a support handbook with six saved views: below 520px the views fold into one// menu, so none hides off the edge. While a view loads, the last one stays, dimmed; "Escalated" has// no tickets, so it says so and offers to add one.const styles = stylex.create({ frame: { inlineSize: "min(26rem, 100%)" }, list: { margin: 0, padding: 0, listStyle: "none", borderWidth: 1, borderStyle: "solid", borderColor: color.line, borderRadius: radius.md }, row: { display: "flex", justifyContent: "space-between", gap: space.md, paddingBlock: space.sm, paddingInline: space.md, fontSize: font.sm, color: color.fg }, meta: { color: color.muted, flexShrink: 0 },})const tickets = [ { id: "t1", title: "Invoice shows the wrong tax rate", owner: "Priya", view: ["open", "mine", "billing"] }, { id: "t2", title: "Can't reset password from the app", owner: "Marco", view: ["open", "accounts"] }, { id: "t3", title: "Export stops at 10,000 rows", owner: "Priya", view: ["open", "mine"] }, { id: "t4", title: "Refund for a duplicate charge", owner: "Lena", view: ["billing", "closed"] },]const views = [ { id: "open", label: "Open", layout: "list" }, { id: "mine", label: "Assigned to me", layout: "list" }, { id: "billing", label: "Billing", layout: "table" }, { id: "accounts", label: "Accounts", layout: "table" }, { id: "escalated", label: "Escalated", layout: "board" }, { id: "closed", label: "Closed this week", layout: "list" },] as constconst Message = UiSchema.TaggedEnum({ PickedView: { view: Schema.String }, LoadedView: { view: Schema.String } })// stands in for the server: a view's records arrive after a momentconst LoadView = Command.make("Example.LoadView", { args: { view: Schema.String } }, ({ view }) => Effect.sleep("700 millis").pipe(Effect.as(Message.LoadedView({ view }))),)export const SupportQueue = Html.component({ name: "SupportQueue", model: Schema.Struct({ view: Schema.String, loaded: Schema.NullOr(Schema.String) }), message: Message,}).make({ init: () => ({ view: "open", loaded: "open" }), update: { PickedView: ($, { view }) => $.evolve({ view }).run(LoadView.make({ view })), // a late answer for a view the person already left changes nothing LoadedView: ($, { view }) => (view === $.model.view ? $.evolve({ loaded: view }) : $.model), }, view: (h, { view, loaded }) => { const loading = loaded !== view const shown = tickets.filter((t) => t.view.includes(view)) return h.div.attrs(sx(styles.frame)).children( InlineDatabase(h, { props: { block: "support-queue", title: "Support queue", views, view, ...(loading ? {} : { count: shown.length }), loading, empty: "Nothing escalated. Tickets an agent hands up show here.", create: "New ticket", }, onEmit: { RequestedView: ({ view }) => Message.PickedView({ view }), RequestedTitle: () => undefined, RequestedCreate: () => undefined, RequestedOpen: () => undefined, }, slots: { view: h.ul.attrs(sx(styles.list)).children( (loading ? tickets.filter((t) => loaded !== null && t.view.includes(loaded)) : shown).map((t) => h.li.key(t.id).attrs(sx(styles.row)).children(h.span.children(t.title), h.span.attrs(sx(styles.meta)).children(t.owner)), ), ), }, }), ) },})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
blockrequired | string | — | The database block's id in the document (or any id unique on the page outside an editor) |
titlerequired | string | — | The collection's name |
viewsrequired | DatabaseView[] | — | The saved views the switcher offers: an id, a name and a layout each |
viewrequired | string | — | The view shown now, by id |
count | number | — | How many records the view shows |
can | Can(["edit", "create"]) | both | "edit" renames the collection, "create" adds records; both by default |
create | string | — | The add button's words: "New task" |
loading | boolean | — | The records are on their way: the view says so instead of looking empty |
empty | string | — | What to say when count is 0, in place of the view: "No tasks in this view yet" |
Events
| Event | Payload | Description |
|---|---|---|
RequestedView | { block: string; view: string } | Show another saved view |
RequestedTitle | { block: string; title: string } | The person renamed the collection |
RequestedOpen | { block: string } | Open the collection as a page of its own |
RequestedCreate | { block: string } | Add a new record to the collection |
Slots
view
Accessibility
- The views are tabs: one Tab stop, the arrow keys move between them and show each.
- The title is a button that turns into a field; Enter keeps the new name and Escape cancels.