# Effect UI components: the basics The shape of a component and the conventions every component follows: naming, Model updates, Commands and view functions. - `effect/ui`: components, Commands, queries, routing, validation and shared state. - `effect/ui/html`: HTML elements, the browser host and router, server rendering. - `effect/ui/kit`: unstyled, accessible components such as `Dialog` and `Select`. - `effect/ui/testing` and `effect/ui/html/testing`: `Story`, `Scene` and `Play`. Effect UI requires `strict` and `exactOptionalPropertyTypes` in `tsconfig.json`. Without `exactOptionalPropertyTypes`, some component types are inferred too widely and mounting them fails to type-check. The examples in every Effect UI guide share one notes app in `../fixtures`, laid out as it would be under `src/` in a real app. Read an example, then the fixtures it imports. ## Writing a component A note title editor: Schemas, `init`/`update`/`view`, a `Command.Service` save and a `BrowserHost` mount. ```ts import { Note } from "#domain/Note" import { Effect, Layer, Schema } from "effect" import { FetchHttpClient } from "effect/http" import { Command, Component, UiSchema } from "effect/ui" import { BrowserHost, Html } from "effect/ui/html" import { Notes } from "../fixtures/notes/services/Notes.ts" // Messages are facts, verb first in the past tense. Events go to the parent. const NoteTitleMessage = UiSchema.TaggedEnum({ UpdatedTitle: { title: Schema.String }, SubmittedTitleForm: {}, SucceededRenameNote: { note: Note }, FailedRenameNote: { reason: Schema.String } }) const NoteTitleEvents = UiSchema.TaggedEnum({ Renamed: { note: Note } }) // I/O goes in a `Command.Service`. `toLayer` captures `Notes`, so the // component requires only `RenameNote`. The body returns this component's // messages, typed failure included. class RenameNote extends Command.Service()("notes/RenameNote", { args: { note: Note }, success: NoteTitleMessage }) { static readonly layer = this.toLayer(Effect.gen(function*() { const notes = yield* Notes return ({ note }) => notes.save(note).pipe(Effect.match({ onSuccess: (note) => NoteTitleMessage.SucceededRenameNote({ note }), onFailure: ({ reason }) => NoteTitleMessage.FailedRenameNote({ reason }) })) })) } export const NoteTitle = Html.component({ name: "NoteTitle", props: Schema.Struct({ note: Note }), model: Schema.Struct({ title: Schema.String, error: Schema.NullOr(Schema.String) }), message: NoteTitleMessage, events: NoteTitleEvents }).make({ init: ($) => ({ title: $.props.note.title, error: null }), // One pure handler per tag. `.run` issues a Command as data, `.emit` // reports to the parent. update: { UpdatedTitle: ($, { title }) => $.evolve({ title }), SubmittedTitleForm: ($) => $.evolve({ error: null }) .run(RenameNote.make({ note: { ...$.props.note, title: $.model.title } })), SucceededRenameNote: ($, { note }) => $.emit(NoteTitleEvents.Renamed({ note })), FailedRenameNote: ($, { reason }) => $.evolve({ error: reason }) }, view: (h, model) => h.form.on("submit", NoteTitleMessage.SubmittedTitleForm()).children( h.input.value(model.title) .on("input", ({ value }) => NoteTitleMessage.UpdatedTitle({ title: value })), h.button.disabled(h.isRunning(RenameNote)).children("Rename"), model.error && h.p.attr("role", "alert").children(model.error) ) }).pipe( // A component provides its own domain Layers. Component.provide(RenameNote.layer.pipe(Layer.provide(Notes.layerHttp))) ) // The browser entry provides only platform Layers. BrowserHost.mount( NoteTitle, { note: { id: "welcome", title: "Welcome", body: "" } }, document.getElementById("app")! ).pipe(Layer.provide(FetchHttpClient.layer), Layer.launch, Effect.runFork) ``` ## More examples - **[Mounting an app from an entry module](./10_entry-module.ts)**: Mount a large component from an entry module that provides only platform Layers. - **[Naming messages, events, Commands and mounts](./20_naming.ts)**: How to name messages, events, Commands, mounts and a component's Schemas. - **[Updating the Model with evolve](./30_updating-the-model.ts)**: `$.evolve` and `UiStruct.evolve` on rows, nested structs, unions and nullable fields. - **[Command results and lifetime](./40_command-results.ts)**: Return your own messages from a Command, `Command.match` a shared one, and pick which instance issues it. - **[Tracing a workflow across Commands](./50_tracing-a-workflow.ts)**: Continue one trace across several Commands with `Command.SpanContext` and `withParentSpan`. - **[Reusable view functions](./60_view-functions.ts)**: Stateless wrappers such as `Card` and `Field` as plain functions of `h`.