Head unstable
The host-neutral part of a component's head: the title, title templates, and the fold that turns a tree of entries into one value.
Any component can contribute head entries through a pure head option,
a function of its Model, props and queries. On every flush that changed a
view, the runtime folds the entries of the placed tree, fallback content
included, into one value with the root vocabulary's merge, and a mount that
owns the document hands it to its host. A component on a host-neutral
vocabulary may contribute only title and titleTemplate. The HTML
vocabulary adds meta, link, JSON-LD and the <html> and <body>
attributes in HtmlHead.
The fold
Each node's value is its own entries followed by its children's values, so
a descendant overrides its ancestors. Children are taken in the order of
their paths, never in document order, so moving a component in its
parent's view can't change the head. The node's reduce entries then map
that value in written order, and the root's result is final.
- Titles. A
titleTemplateapplies to a title set by a descendant, never to its own instance's title, so the title passes through its ancestors' templates outward, nearest first. - Conflicts. Siblings that set one single slot, such as the title, to
the same value pass it up once. When they set different values, neither
wins: the slot is left unset until an ancestor's
reducesets it. A conflict no ancestor settles is reported as aConflictnaming every path that set the slot. The runtime sends it toQuery.Problems.
Example A layout template and a page title
import { Head } from "effect/ui"
const head = Head.merge({ entries: [Head.titleTemplate((title) => `${title} ยท App`)], children: [{ entries: [Head.titleTemplate((title) => `${title} ยท People`), Head.title("People")], children: [{ entries: [Head.title("Ada")], children: [] }] }]})
head.title // => "Ada ยท People ยท App"Combinators
Folds a host-neutral tree into its title, applying each node's reduce
entries bottom up.
Details
Entries of other vocabularies are ignored, HtmlHead.reduce included.
Siblings that pass different titles up leave the title unset, and are
reported to onConflict unless a reduce above them sets it.
Signature
declare const merge: <E extends { readonly _tag: string;} = Entry>(tree: Tree<E>, options?: MergeOptions) => HeadConstructors
Makes the Conflict for a slot that the components at paths set to
different values.
Signature
declare function conflict(slot: string, paths: readonly Array<string>): ConflictMaps the folded head of the instance and everything its view placed.
Only the title of f's result is used: every other field of the full
value is kept, so under the HTML vocabulary the meta, links and
<html> and <body> attributes stay as they were.
Details
A node's reduce entries run after its own entries and its children's
values are merged, in written order wherever they sit among the other
entries, each given the previous one's result. A reduce that throws fails
its instance, as a throwing head does. A merge called directly reports
it to the node's onError and gives the next reduce the value the
thrower was given. Use HtmlHead.reduce to read or set the HTML fields.
Signature
declare function reduce(f: (below: Head) => Head): ReduceExample Marking a preview
import { Head } from "effect/ui"
const preview = Head.reduce((below) => ({ title: `[preview] ${below.title}` }))
const head = Head.merge({ entries: [Head.titleTemplate((title) => `${title} ยท App`), preview], children: [{ entries: [Head.title("Ada")], children: [] }]})
head.title // => "[preview] Ada ยท App"Sets the title.
Signature
declare function title(text: string): TitletitleTemplate
unstableWraps the title a descendant sets, such as (title) => \${title} ยท App``.
Details
It never applies to a title set by its own instance. Templates chain outward: the nearest ancestor's applies first. When an instance has several, the last one counts.
Signature
declare function titleTemplate(apply: (title: string) => string): TitleTemplateModels
A host-neutral head entry: a title, a title template or a reduce.
Signature
type Entry = Title | TitleTemplate | ReduceThe folded head of a host-neutral tree.
Signature
interface Head { readonly title: string | undefined;}Options of a merge: where it reports a Conflict.
Signature
interface MergeOptions { readonly onConflict?: (conflict: Conflict) => void;}Maps the folded head of its node: its own entries merged with its children's values.
Signature
interface Reduce { readonly _tag: "Reduce"; readonly apply: <H extends Head>(below: H) => Omit<H, "title"> & Head;}The title: the document's, a window's or a screen's, as the host gives it a meaning. The Router's navigation announcement reads it.
Signature
interface Title { readonly _tag: "Title"; readonly text: string;}Wraps the title set by a descendant.
Signature
interface TitleTemplate { readonly _tag: "TitleTemplate"; readonly apply: (title: string) => string;}A tree of head entries: each node's own entries and its children, in document order. The runtime builds one node per instance.
Details
path names the node in a Conflict, and orders it among its siblings.
Siblings with entries in their subtree need distinct paths: a merge throws
when two have none or share one, whatever their order. onError is called
when one of the node's own title templates or reduce entries throws: the
merge then skips that entry. Without onError the error is rethrown.
Signature
interface Tree<E = Entry> { readonly children: readonly Array<Tree<E>>; readonly entries: readonly Array<E>; readonly onError?: (error: unknown) => void; readonly path?: string;}Problems
Siblings set the same single slot, such as the title, to different values,
and no ancestor's reduce set it. The slot is left unset. paths
holds every path that set it, sorted.
Signature
declare class Conflict extends Readonly<{ readonly message: string; readonly paths: readonly Array<string>; readonly slot: string;}> & { readonly _tag: "HeadConflict";} & Pipeable { constructor(args: { readonly message: string; readonly paths: readonly Array<string>; readonly slot: string; });}Utils
resolveTitle
unstableThe template rule: the title a node passes up. That's the title its children passed up, through the node's own template, or else the node's own title.
Details
entries may hold any vocabulary's entries: only Title and
TitleTemplate are read, and the last of each counts. A template that
throws is reported to onError and skipped, or rethrown without it.
Signature
declare function resolveTitle(node: { readonly entries: readonly Array<{ readonly _tag: string; }>; readonly onError?: (error: unknown) => void;}, below: string | undefined): string | undefined