π₯οΈ Rendering HelpersΒΆ
Clera provides three DOM helpers for updating page content from action functions. All three are scoped to the current page and warn if the target element is not found.
π― context.render(selector, html)ΒΆ
Replaces the inner HTML of the matched element.
function loadTasks(context) {
context.render("#taskList", tasks.map(task =>
`<li>${task.title}</li>`
).join(""));
}
Use this when you want to fully replace a list, a cardβs content, or any container.
reserveHeight optionΒΆ
When replacing content that already has height, pass { reserveHeight: true } to prevent a layout jump during the swap:
context.render("#feedList", html, { reserveHeight: true });
How it works: Clera reads the containerβs current height before the swap, pins it as a minHeight, replaces the content, then removes the minHeight. The browser handles the transition to the new natural height. No canvas, no measurement engine: just one offsetHeight read.
Use reserveHeight for content-heavy containers like feeds and chat lists where visible height changes are noticeable.
π― context.append(selector, html)ΒΆ
Adds HTML to the end of the matched element without clearing existing content.
function addMessage(context) {
const title = context.values.title.trim();
if (!title) return;
context.append("#taskList", `<li>${title}</li>`);
context.form.reset();
}
Use this when you are adding single items to an existing list rather than re-rendering the whole thing.
π― context.clear(selector)ΒΆ
Empties the matched element. Equivalent to render(selector, "").
function resetList(context) {
context.clear("#taskList");
}
Use this to clear a container before populating it, or to show an empty state.
All three are page-scopedΒΆ
All three helpers query within the current pageβs root element only. They cannot accidentally reach elements on other pages or outside the current page.
If the selector matches nothing, Clera logs a warning and does nothing (no error thrown):
[CLERA:DOM_MISSING] render("#taskList") found no element in page "home".
π‘ When to use eachΒΆ
Situation |
Helper |
|---|---|
Re-render a full list from data |
|
Add a single item to an existing list |
|
Empty a container |
|
Target a specific element for text or value |
|