/** * @title Sharing state with descendants * * Share an owner's state with its descendants with `shares`, `reads` and * `$.send`. */ import { Schema } from "effect" import { UiSchema } from "effect/ui" import { Html } from "effect/ui/html" const TooltipProviderMessage = UiSchema.TaggedEnum({ FocusedTooltip: { id: Schema.String }, BlurredTooltip: { id: Schema.String } }) // The owner: a headless component with `shares`. The state is its ordinary // Model, so every change is logged and replayable. `value` picks what readers // see. Keep it small: every reader re-renders when it changes. export const TooltipProvider = Html.component({ name: "TooltipProvider", model: Schema.Struct({ open: Schema.NullOr(Schema.String) }), message: TooltipProviderMessage, shares: { value: ({ model }) => ({ open: model.open }), // The messages readers may send with `$.send(TooltipProvider, ...)`. // They are facts, named like any other message. receives: ["FocusedTooltip", "BlurredTooltip"] }, // After Back, start from `init` rather than reopen a tooltip nobody is // focused on. restore: false }).make({ init: () => ({ open: null }), update: ($, message) => TooltipProviderMessage.match(message, { FocusedTooltip: ({ id }) => ({ open: id }), BlurredTooltip: ({ id }) => $.model.open === id ? { open: null } : $.model }) // An owner with no parts of its own leaves `parts` out, and its `view` // closure receives `{}`. }) const TooltipMessage = UiSchema.TaggedEnum({ FocusedTooltip: { id: Schema.String }, BlurredTooltip: { id: Schema.String } }) // A reader. `h.reads.provider` in `view` and `$.reads.provider` in `update` // are the nearest TooltipProvider's value. Read it there rather than copy it // into the Model. Reading or sending puts // `Component.Shared<"TooltipProvider">` in the requirements: the reader // compiles anywhere, and a tree with no provider above it fails to type-check // where its mount is run. Give owners distinct names, since the name is the // type-level key. export const Tooltip = Html.component({ name: "Tooltip", props: Schema.Struct({ label: Schema.String, tip: Schema.String }), model: Schema.Null, message: TooltipMessage, reads: { provider: TooltipProvider } }).make({ init: () => null, // A blur of a tooltip that isn't open would change nothing, so skip the // send. `$.reads` is the value as this `update` runs: after a send it still // holds the old value until the owner's `update` has run. update: ($, message) => message._tag === "BlurredTooltip" && $.reads.provider.open !== message.id ? $.model : $.send(TooltipProvider, message), view: (h) => { const id = h.id("tip") return h.span( h.button .attr("aria-describedby", id) .on("focus", TooltipMessage.FocusedTooltip({ id })) .on("blur", TooltipMessage.BlurredTooltip({ id })) .children(h.props.label), h.reads.provider.open === id && h.span.id(id).attr("role", "tooltip").children(h.props.tip) ) } }) // The tree: readers go in the owner's closure, at any depth, which removes // the requirement, so `FormatToolbar` requires nothing. // // Every owner update re-runs the closure, and a headless child in it // re-renders each time. Put normal components directly under an owner, as // here, or one normal component that holds the rest. export const FormatToolbar = Html.component({ name: "FormatToolbar", model: Schema.Null, message: Schema.Never }).make({ init: () => null, update: ($) => $.model, view: (h) => TooltipProvider(h, { view: () => h.div.attr("role", "toolbar").children( Tooltip(h, { key: "bold", props: { label: "B", tip: "Bold (Ctrl+B)" } }), Tooltip(h, { key: "italic", props: { label: "I", tip: "Italic (Ctrl+I)" } }) ) }) }) // In a `Scene` of `Tooltip` no provider is mounted: `s.shared(TooltipProvider, // value)` gives the reader a value, and `s.sent(TooltipProvider, message)` // acknowledges each send.