Lay out an automation's trigger and steps, and connect them.
9 steps · 2 need setup
- Drag, or press to move with the arrow keys
- Drag, or press to move with the arrow keys
- Drag, or press to move with the arrow keys
- Drag, or press to move with the arrow keys
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { type CanvasPath, CanvasStep, ROOT, WorkflowCanvas } from "./ui/uicontext/components/kit/Canvas.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { leadSteps } from "./steps.ts"const styles = stylex.create({ frame: { inlineSize: "100%", minInlineSize: 0 } })const Message = UiSchema.TaggedEnum({ Selected: { id: Schema.String }, Added: { path: Schema.String, index: Schema.Number }, AddedPath: { step: Schema.String }, Moved: { path: Schema.String, from: Schema.Number, to: Schema.Number }, Duplicated: { step: Schema.String }, Disabled: { step: Schema.String, disabled: Schema.Boolean }, Removed: { step: Schema.String },})// The app's side: apply each change to one sequence, wherever it is.const inSequence = (steps: ReadonlyArray<CanvasStep>, path: string, change: (s: ReadonlyArray<CanvasStep>) => ReadonlyArray<CanvasStep>): ReadonlyArray<CanvasStep> => path === ROOT ? change(steps) : steps.map((s) => (s.paths === undefined ? s : { ...s, paths: s.paths.map((p): CanvasPath => (p.id === path ? { ...p, steps: change(p.steps) } : { ...p, steps: inSequence(p.steps, path, change) })) }))const onStep = (steps: ReadonlyArray<CanvasStep>, id: string, change: (s: CanvasStep) => ReadonlyArray<CanvasStep>): ReadonlyArray<CanvasStep> => steps.flatMap((s) => (s.id === id ? change(s) : [s.paths === undefined ? s : { ...s, paths: s.paths.map((p) => ({ ...p, steps: onStep(p.steps, id, change) })) }]))const numbered = (steps: ReadonlyArray<CanvasStep>, prefix: string): ReadonlyArray<CanvasStep> => steps.map((s, i) => ({ ...s, number: `${prefix}${i + 1}`, ...(s.paths === undefined ? {} : { paths: s.paths.map((p, j) => ({ ...p, steps: numbered(p.steps, `${prefix}${i + 1}${s.kind === "loop" ? "" : String.fromCharCode(65 + j)}.`) })) }), }))export const LeadWorkflow = Html.component({ name: "LeadWorkflow", model: Schema.Struct({ steps: Schema.Array(CanvasStep), selected: Schema.NullOr(Schema.String), count: Schema.Number }), message: Message,}).make({ init: () => ({ steps: leadSteps, selected: "contact", count: 0 }), update: { Selected: ($, { id }) => $.evolve({ selected: id }), Added: ($, { path, index }) => { const step: CanvasStep = { id: `new-${$.model.count}`, kind: "action", title: "Choose an app and event", status: "attention", statusText: "Choose an app" } return $.evolve({ steps: inSequence($.model.steps, path, (s) => [...s.slice(0, index), step, ...s.slice(index)]), selected: step.id, count: (n) => n + 1 }) }, AddedPath: ($, { step }) => $.evolve({ steps: onStep($.model.steps, step, (s) => [{ ...s, paths: [...(s.paths ?? []), { id: `path-${$.model.count}`, label: `Path ${String.fromCharCode(65 + (s.paths?.length ?? 0))}`, status: "attention", steps: [] }] }]), count: (n) => n + 1, }), Moved: ($, { path, from, to }) => $.evolve({ steps: inSequence($.model.steps, path, (s) => { const next = [...s] next.splice(to, 0, ...next.splice(from, 1)) return next }) }), Duplicated: ($, { step }) => $.evolve({ steps: onStep($.model.steps, step, (s) => [s, { ...s, id: `${s.id}-copy-${$.model.count}`, title: `${s.title} (copy)`, paths: undefined }]), count: (n) => n + 1 }), Disabled: ($, { step, disabled }) => $.evolve({ steps: onStep($.model.steps, step, (s) => [{ ...s, disabled }]) }), Removed: ($, { step }) => $.evolve({ steps: onStep($.model.steps, step, () => []), selected: $.model.selected === step ? null : $.model.selected }), }, view: (h, model) => h.div.attrs(sx(styles.frame)).children( WorkflowCanvas(h, { props: { id: "lead-intake", agentId: "canvas:lead-demo", label: "New lead intake", steps: numbered(model.steps, ""), selected: model.selected, can: ["add", "move", "remove", "duplicate", "disable"], }, onEmit: { RequestedSelect: ({ step }) => Message.Selected({ id: step }), RequestedSelectPath: ({ path }) => Message.Selected({ id: path }), RequestedAdd: ({ path, index }) => Message.Added({ path, index }), RequestedAddPath: ({ step }) => Message.AddedPath({ step }), RequestedMove: ({ path, from, to }) => Message.Moved({ path, from, to }), RequestedDuplicate: ({ step }) => Message.Duplicated({ step }), RequestedDisable: ({ step, disabled }) => Message.Disabled({ step, disabled }), RequestedRemove: ({ step }) => Message.Removed({ step }), RequestedEdit: () => undefined, }, }), ),})Installation
pnpm dlx shadcn@latest add checkfu/uicontext/canvasUsage
import { type CanvasStep, WorkflowCanvas } from "./ui/uicontext/components/kit/Canvas.ts"const Message = UiSchema.TaggedEnum({ SelectedStep: { step: Schema.String }, AddedStep: { path: Schema.String, index: Schema.Number }, MovedStep: { path: Schema.String, from: Schema.Number, to: Schema.Number }, RemovedStep: { step: Schema.String },})export const Steps = (h: Html.H<typeof Message.Type>, steps: ReadonlyArray<CanvasStep>, selected: string | null) => WorkflowCanvas(h, { props: { id: "lead-intake", label: "New lead intake", steps, selected, can: ["add", "move", "remove"] }, onEmit: { RequestedSelect: ({ step }) => Message.SelectedStep({ step }), RequestedAdd: ({ path, index }) => Message.AddedStep({ path, index }), RequestedMove: ({ path, from, to }) => Message.MovedStep({ path, from, to }), RequestedRemove: ({ step }) => Message.RemovedStep({ step }), RequestedSelectPath: () => undefined, RequestedAddPath: () => undefined, RequestedDuplicate: () => undefined, RequestedDisable: () => undefined, RequestedEdit: () => undefined, }, })Examples
A published version
Read-only, with no add, move or menu controls, until the person opens it for editing.
You’re viewing version 3, which is live.
6 steps · 2 need setup
import { UiSchema } from "@effect/ui"import { Html } from "@effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { WorkflowCanvas } from "./ui/uicontext/components/kit/Canvas.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { leadSteps } from "./steps.ts"const styles = stylex.create({ frame: { inlineSize: "100%", minInlineSize: 0 } })const Message = UiSchema.TaggedEnum({ ClickedEdit: {} })export const PublishedVersion = Html.component({ name: "PublishedVersion", model: Schema.Boolean, message: Message,}).make({ init: () => true, update: { ClickedEdit: () => false }, view: (h, readOnly) => h.div.attrs(sx(styles.frame)).children( WorkflowCanvas(h, { props: { id: "lead-v3", agentId: "canvas:lead-read-only", label: "New lead intake, version 3", steps: leadSteps.slice(0, 3).map((s, i) => ({ ...s, number: String(i + 1) })), can: ["add", "move", "remove", "duplicate", "disable"], ...(readOnly ? { readOnly: { notice: { tone: "neutral" as const, text: "You’re viewing version 3, which is live." }, editLabel: "Edit workflow" } } : {}), }, onEmit: { RequestedEdit: () => Message.ClickedEdit(), RequestedSelect: () => undefined, RequestedSelectPath: () => undefined, RequestedAdd: () => undefined, RequestedAddPath: () => undefined, RequestedMove: () => undefined, RequestedDuplicate: () => undefined, RequestedDisable: () => undefined, RequestedRemove: () => undefined, }, }), ),})A new workflow
With no steps yet, the board asks for the trigger first.
import { Html } from "@effect/ui/html"import { Schema } from "effect"import { WorkflowCanvas } from "./ui/uicontext/components/kit/Canvas.ts"export const EmptyWorkflow = Html.component({ name: "EmptyWorkflow", model: Schema.Struct({}), message: Schema.Never,}).make({ init: () => ({}), update: ($) => $.model, view: (h) => WorkflowCanvas(h, { props: { id: "new", agentId: "canvas:empty", label: "Untitled workflow", steps: [], can: ["add"] }, onEmit: { RequestedAdd: () => undefined, RequestedSelect: () => undefined, RequestedSelectPath: () => undefined, RequestedAddPath: () => undefined, RequestedMove: () => undefined, RequestedDuplicate: () => undefined, RequestedDisable: () => undefined, RequestedRemove: () => undefined, RequestedEdit: () => undefined, }, }),})API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
idrequired | string | — | The workflow's id, which names it to agents |
labelrequired | string | — | The workflow's name, read by screen readers and agents |
stepsrequired | CanvasStep[] | — | The trigger first, then the steps; a paths step carries its branches |
status | "loading" | "ready" | — | loading shows placeholder steps while the workflow loads |
selected | string | null | — | The id of the step or path whose setup is open |
canrequired | ("add" | "move" | "remove" | "duplicate" | "disable")[] | — | What the viewer may do to the steps: add, move, remove, duplicate or turn them off |
maxSteps | number | — | At most this many steps in all |
readOnly | { notice, editLabel } | — | Why it can't be changed, with an action that opens it for editing: "Edit workflow" |
outcome | Outcome | null | — | The app's answer to the request in flight; the request id is the action ("add", "move", "remove") |
agentId | string | canvas:<id> | The id agents know this canvas by; canvas:<id> by default |
Events
| Event | Payload | Description |
|---|---|---|
RequestedSelect | { step: string } | A step was clicked: open its setup |
RequestedSelectPath | { path: string } | A path's label was clicked: open its conditions |
RequestedAdd | { path: string; index: number } | Add a step in path ("root" for the main column) at index |
RequestedAddPath | { step: string } | Add another path to a paths step |
RequestedMove | { path: string; from: number; to: number } | Move the step at from in path to to (in "root", indices count the trigger) |
RequestedDuplicate | { step: string } | Copy a step, with its setup, right after it |
RequestedDisable | { step: string; disabled: boolean } | Turn a step off (runs pass over it) or back on |
RequestedRemove | { step: string } | Remove a step, after the person confirmed |
RequestedEdit | {} | The workflow is read-only: open it for editing |
Agent actions
| Action | Description |
|---|---|
ClickedStep | Select a step by id to open its setup |
ClickedPath | Open a path's conditions by path id |
ClickedNextProblem | Open the next step that needs setup |
ClickedAdd | Add a step in a sequence ("root" or a path id) at an index |
ClickedAddPath | Add another path to a paths step |
MovedStep | Move a step within its sequence ("root" or a path id), from one index to another |
ClickedDuplicate | Copy a step, with its setup, right after it |
ClickedDisable | Turn a step off (runs pass over it) or back on |
ClickedRemove | Ask to remove a step by id (opens the confirmation) |
ConfirmedRemoveasks first | Remove the step being confirmed. Name it as your target. |
ClickedEdit | Open the read-only workflow for editing |
Accessibility
- Each step is a button that says its number, kind and title, and whether its setup is open (
aria-pressed). - A step moves from its handle without a pointer: press the handle to pick it up, the arrow keys move it, the handle again drops it and Escape cancels. Each move is announced.
- Removing a step asks first, and every add button names the step it adds after.
- The up and down arrows move between steps, and each step's menu duplicates, turns off, moves or deletes it without a pointer.
- The bar above the board announces how many steps need setup, and its button opens the next one.