Jadis Class
The Jadis class is the abstract base class for all components. Extend it to create custom elements with built-in templating, lifecycle management, and DOM helpers.
Import
import { Jadis, css } from '@jadis/core';Static properties
| Property | Type | Default | Description |
|---|---|---|---|
selector | string | (required) | The custom element tag name. It must contain a hyphen. |
template | string | '' | Reserved for future use. |
useShadowDom | boolean | true | Whether to attach a shadow root. Set to false for light-DOM rendering. |
Attributes are observed through useAttributes({ ...callbacks }); do not declare observedAttributes for that API.
Static methods
register()
Registers the component as a custom element. Calling it again does nothing; a different class whose selector is already registered is ignored too, without an error, so keep selectors unique.
MyComponent.register();toTemplate(options?, slotted?)
Creates and returns a component instance with optional properties, attributes, and slotted content.
const slotted = document.createDocumentFragment();
slotted.append(document.createElement('slot-content'));
const instance = MyComponent.toTemplate(
{ props: { title: 'Hello' }, attrs: { theme: 'dark' } },
slotted
);See toTemplate() for details.
settled()
Resolves once no component has an onConnect left to call, those an onConnect connected included, and the router has given the page it showed its route: one wait after drawing a page, mainly in tests.
document.body.append(page);
await Jadis.settled();Instance properties
| Property | Type | Description |
|---|---|---|
isConnected | boolean | Whether the component is currently in the DOM. |
connected | Promise<void> | Resolves once the onConnect of the current connection has returned, or of the next one when the component is not connected. See Waiting for onConnect(). |
shadowRoot | ShadowRoot | null | The shadow root, or null if useShadowDom is false. |
killSignal | AbortSignal | Aborted when the component disconnects; a new one is created on reconnection. See killSignal. |
Lifecycle methods
Override these methods to respond to lifecycle events:
| Method | Timing |
|---|---|
onConnect() | On the task after the component is connected and its template is rendered; again after each reconnection. |
onDisconnect() | When the component is removed from the DOM. |
See Lifecycle for details.
Template methods
| Method | Return type | Description |
|---|---|---|
templateHtml() | Node | Returns the component's DOM structure. |
templateCss() | string | Returns CSS rules for the component. |
DOM helpers
| Method | Description |
|---|---|
getElement<T>(query) | Find an element by CSS selector. Supports >>> for shadow traversal. |
useRefs(mapFn) | Create typed references to multiple elements. |
toggleClass(className, condition) | Add or remove a class based on a boolean. |
useAttributes(callbacks) | Create attribute getters and change callbacks. |
State and events
| Method | Description |
|---|---|
useChange(initial, onChange, options?) | State with getter/setter and change callback, optionally set from an attribute. |
useEvents<T>() | Type-safe event emitter with register() and emit(). |
onBus(bus, eventName, callback) | Listen to events on a shared Bus. |
useTranslation(translator, draw) | Draw the component's texts at every connection, before the paint, and at each language change. Call it in the constructor. See Translations. |
this.on(target, event, callback) | Auto-cleaned event listener on an element, the window or the document. |
Example
import { Jadis, css } from '@jadis/core';
class MyComponent extends Jadis {
static readonly selector = 'my-component';
private readonly _refs = this.useRefs((ref) => ({
display: ref<HTMLSpanElement>('span'),
button: ref<HTMLButtonElement>('button'),
}));
private readonly count = this.useChange(0, (value) => {
this._refs.display.textContent = value.toString();
}, { immediate: true });
templateHtml(): Node {
return <p>Count: <span></span> <button type="button">+</button></p>;
}
templateCss(): string {
return css`:host { display: block; }`;
}
onConnect(): void {
this.on(this._refs.button, 'click', () => this.count.set((value) => value + 1));
}
}
MyComponent.register();