-
Notifications
You must be signed in to change notification settings - Fork 0
Runtime
Sad Gabi edited this page Jul 23, 2026
·
3 revisions
Components can export a script function that runs once the component is rendered. This is where you add interactivity or logic tied to the component.
// MyButton.js
import body from "path/to/my-button.html";
function RUNTIME() {
console.log("Component rendered");
}
export default function Button() {
return {
body,
script: RUNTIME
}
}The script function receives context to safely access component data and DOM elements:
-
self— the root DOM element of the component. -
ctx— contains props and other dynamic values passed to the component. You can also define default values.
// Counter.js
import body from "./html/counter.html";
function RUNTIME(self, ctx = { count: 0 }) {
const numDisplay = self.querySelector("#number");
self.addEventListener("click", () => {
if (numDisplay) {
numDisplay.textContent = parseInt(numDisplay.textContent) + 1;
}
});
}
export default function Button() {
return {
body,
script: RUNTIME
}
}- Always manipulate elements inside
selfto prevent conflicts when multiple instances of a component are rendered. - Use
ctxto pass dynamic data or props from the parent component or HTML attributes. - Avoid manipulating the global
documentdirectly inside component scripts.