Summit.directive
Summit.directive adds a new s- attribute to Summit. Your directive runs when
Summit initializes an element that carries it, with the same utilities the
built-in directives use, so a directive you write is a first-class citizen.
Registering a directive
Call Summit.directive(name, handler, priority?). The name is used without the
s- prefix. This registers s-tooltip:
Summit.directive("tooltip", (el, meta, utils) => {
utils.effect(() => {
el.setAttribute("title", String(utils.evaluate(meta.expression)));
});
});<button s-tooltip="'Copied ' + count + ' times'">Copy</button>Summit.directive returns the Summit global, so registrations chain.
The handler signature
The handler receives three arguments: (el, meta, utils).
el is the element the directive is on.
meta describes how the attribute was written:
name, the directive name withouts-(here,"tooltip").value, the part after a colon, ornullif absent ("click"ins-on:click).modifiers, the dot-separated modifiers as an array (["prevent", "stop"]).expression, the attribute's value string, ready to pass toevaluate.raw, the original attribute name as written in the DOM.
utils is the directive author's toolkit:
evaluate(expression)runs a value expression in the element's scope.evaluateAction(expression, locals?)runs statements (like ans-onhandler), optionally with extra local variables.effect(fn)creates a reactive effect that re-runs when its dependencies change and is torn down automatically with the element.cleanup(fn)registers a teardown callback.initTree(el, scopes?)anddestroyTree(el)initialize or tear down a subtree, used by structural directives that manage their own children.scopesis the scope stack visible at this element.makeEnv(el, locals?)builds a fresh evaluation environment for an element.Summitis the global itself.
Use effect for anything that should react to state, and cleanup for anything
that must be undone (a listener, a timer). Errors thrown in a handler are caught
and logged, so one broken directive will not stop the rest of the page.
Priority
The optional third argument sets ordering. When an element has several
directives, lower numbers run earlier. The default is 100, so pass a smaller
number to run before most directives and a larger one to run after:
Summit.directive("setup", handler, 0); // runs early, before default directivesTiming
Registration is timing-safe: register before or after Summit.start. Built-in directives are registered when Summit loads, and registering a custom directive with the same name overrides the built-in.
For a fuller walkthrough, see Writing directives and plugins.