/** * @title Using web components from other libraries * * Use a third-party web component with typed properties and events via * `Html.customElement`. */ import { Effect, Schema } from "effect" import { UiSchema } from "effect/ui" import { Html } from "effect/ui/html" import { HtmlScene } from "effect/ui/html/testing" import { Locator } from "effect/ui/testing" // A binding describes an element another library defines, here // vanilla-colorful's ``. Define it once, at module level, // like a Command. It doesn't register the element: the app's entry module // imports the library (`import "vanilla-colorful/hex-color-picker.js"`), so // a test, which has no DOM, can import the component without it. // // Use a binding when the element takes properties and sends events. For a // library you call methods on, such as a map or an editor, use a mount. export const ColorPicker = Html.customElement({ tag: "hex-color-picker", // Each property gets a typed method, here `.color(value)`. properties: { color: Schema.String }, events: { "color-changed": Schema.Struct({ value: Schema.String }) } }) // A property whose name the builder already uses, such as Shoelace's // `content`, `id` or `class`, gets no method of its own: set it with // `.prop("content", text)`, which is typed the same way. export const Tooltip = Html.customElement({ tag: "sl-tooltip", properties: { content: Schema.String, placement: Schema.Literals(["top", "right", "bottom", "left"]) } }) // The event is a fact about what the user did, as for a click. const NoteColorMessage = UiSchema.TaggedEnum({ PickedColor: { color: Schema.String } }) // `ColorPicker(h)` takes the view's `h`, which fixes the handler's message // type, and returns an HTML element builder, so `.attr`, `.class` and the // native events chain with the property methods. export const NoteColor = Html.component({ name: "NoteColor", model: Schema.Struct({ color: Schema.String }), message: NoteColorMessage }).make({ init: () => ({ color: "#fde68a" }), update: { PickedColor: ($, { color }) => $.evolve({ color: () => color }) }, view: (h, model) => h.section( Tooltip(h) .prop("content", "The note's card color") .placement("top") .children( // The view passes the Model's color on every render, but the host // writes the property only when it changes, so a drag in the // picker isn't interrupted. ColorPicker(h) .attr("aria-label", "Note color") .color(model.color) .on("color-changed", ({ value }) => NoteColorMessage.PickedColor({ color: value })) ), h.output(model.color) ) }) // The server renders the tags and attributes but never the properties. // Hydration adopts the server's elements and sets the properties on them, // keeping what the library already added if it loaded first. // `fireElement` takes the binding, so the event name and `detail` are // checked against its Schemas, and the handler gets `detail` decoded, as in // the browser. `HtmlPlay` has the same step for whole-app tests. export const picked = Effect.gen(function*() { const s = yield* HtmlScene.make(NoteColor, {}) s.fireElement( ColorPicker, Locator.tag("hex-color-picker"), "color-changed", { value: "#bfdbfe" } ) .expect(Locator.tag("output")) .toHaveText("#bfdbfe") })