A component can be an ordinary value holding its HTML and the stylesheets it needs. A const function constructs that value. Another const function combines several parts into one page.
This is the intended @core/html component API, not yet a shipped package surface. Const functions and combined stylesheet contributions still need their planned implementation.
Compose a documentation page
@core/html HtmlPart, html_page
@components site_navbar, docs_sidebar, docs_topic
@introduction content as introduction
article #= HtmlPart(
body = [introduction]
)
page #= html_page({
site_navbar(),
docs_sidebar("Documentation"),
docs_topic("Functions", article)
})
[$in title:Functions]
[$in description:Function declarations and calls.]
[$in stylesheets:[page.stylesheets]]
[page.body]
The entry owns the final document. Each component returns its body and stylesheet references, while html_page() assembles the parts in order. The page forwards those dependencies to the builder rather than letting a nested template insert into a distant head or stylesheets slot.
A small page can skip html_page() and write its main content directly:
@components sidebar
[sidebar]
[$md:
# Getting started
Write an ordinary Moth program first.
]
Use the abstraction where it saves work. A component system should not make a simple page harder to read.
Build a component value
A sidebar can become a const function in the shared component package. Its return type says what the page receives:
@core/html HtmlPart
docs_sidebar #|heading String| -> HtmlPart:
return HtmlPart(
body = [$html:
<aside class="docs-sidebar">
<h2>[heading]</h2>
</aside>
],
stylesheets = [@sidebar.css]
)
;
A different page can call docs_sidebar("Reference") and receive the same markup and the same stylesheet reference. The CSS dependency is declared once. The returned path stays a tracked resource, not a guessed URL.
Use a named template slot
A smaller helper can stay a template rather than become a new type:
card #= [$html:
<article class="card">
<h2>[$slot title]</h2>
[$slot]
</article>
]
[card:
[$in title:Functions]
Body content
]
The title insert fills the card's own named slot. It never searches ancestor templates or sends a builder message. Body content lands in the default slot.
Keep using slots while a helper needs no typed result and no resource dependency. Reach for a component value when the page needs both.
See Templates for other slot and wrapper forms.
A component abstraction should stay ordinary Moth data. Its author chooses the public type and const functions. A page decides where that data becomes output.
A deliberately small result type
This is an illustrative source-backed @core/html design, not a committed constructor or public signature:
HtmlPart = |
body String = "",
stylesheets String = "",
|
html_page #|parts {HtmlPart}| -> HtmlPart:
body ~= ""
stylesheets ~= ""
loop parts |part|:
~body = [body, part.body]
~stylesheets = [stylesheets, part.stylesheets]
;
return HtmlPart(
body = body,
stylesheets = stylesheets
)
;
A template is the normal way to concatenate Strings in Moth. The composite result carries HTML and a structural String of stylesheet references. It holds no hidden page registration, renderer instance or component lifecycle.
A real library will also need a way to carry independent head contributions. That representation remains an API decision. Concatenating head snippets into one String loses the boundaries the builder needs to deduplicate them individually, so treat HtmlPart as a starting shape rather than a finished contract.
Keep local slots local
The Basic example's card owns its named title slot. An imported or reused card cannot reach an enclosing page's builder slots. That parent-only routing rule keeps deep composition predictable and stops an intermediate component from silently changing where inserts go.
Const functions compose finished HtmlPart values across file and module boundaries. They run under the compiler's constant rules, never call runtime DOM functions and cannot invent new declarations or source code.
Carry resources, not generated URLs
A component's stylesheets field can come from file values such as @sidebar.css. Composition preserves the resource origin inside the String. The builder later resolves URLs for the destination document and deduplicates stylesheets by stable origin, keeping first-use order.
One builder contract still needs implementation. Today's stylesheets slot accepts exactly one CSS resource per direct insert. The composed multi-resource String on the Basic page describes an intended extension under investigation, not what the current slot validator accepts. That validator must keep each CSS origin and refuse stray text, site-root pieces and non-CSS resources.
Don't compare resource strings to test whether two components use the same file. A URL depends on the output context. CSS rules shared by every instance belong in a separate resource. Inline $css suits one instance's small value, such as a CSS custom property.
Types and boundaries keep reuse safe
A nominal HtmlPart gives the library a checked contract. It never converts an arbitrary record automatically. Const construction checks every field and every called function. Reusable source packages publish only selected declarations through $export.
Runtime components and browser handles still obey Moth's shared and exclusive access rules and mandatory lifetime analysis. Returning a String from a const function grants no retained DOM reference and no implicit subscription. Explicit Error! contracts and ordinary recovery handle expected runtime failures. A const function that the compiler cannot evaluate reports a diagnostic instead of quietly becoming runtime work.
When to use the pattern
Reach for a reusable HtmlPart result when a navbar, sidebar, tabbed docs section or pager owns markup plus resource dependencies. A straightforward article can stay a direct top-level [$md: ...] fragment. Keep runtime behaviour in a separately invoked function, covered in Browser behaviour.
See Constants, Resources and @core/html.