Skip to content

defineComponent

The API for defineComponent(); the primary authoring API for an Ornata component.

import { defineComponent } from 'ornata';
const { defineComponent } = window.Ornata;
const Counter = defineComponent({
name: 'Counter',
state: {
count: { default: 0 },
},
methods: {
increment() {
this.state.count += 1;
},
},
});

It returns a component constructor:

const Counter = defineComponent({ ... });
const instance = Counter.mount(root);
const sameInstance = Counter.getInstance(root);
const maybeInstance = Counter.findInstance(root);
Counter.unmount(root);
Counter.unmountAll();

For the full constructor API, see Component Constructor.

Optional display name used in debugging and reporting.

Root validation options. Currently supports:

PropertyDescription
matchesValidates that the provided root matches an expected CSS selector.

Within component callbacks, the resolved root is available as this.root on the internal instance.

For conceptual guidance, see Component Anatomy.

Reactive state configuration keyed by property name.

Each property supports:

PropertyDescription
defaultProvides the fallback value when no other state source supplies one.
typeDeclares the expected runtime type for validation and HTML parsing.
parseConverts raw HTML-derived string values into the desired shape.
privateHides the property from external reads and writes on the mounted public state.
readonlyAllows external code to read the property but prevents external writes.

Within component callbacks, the resolved reactive state is available as this.state on the internal instance.

For conceptual guidance and examples, see State.

DOM resolution options keyed by property name.

Each property supports:

PropertyDescription
queryResolves the first matching element inside the root with a CSS selector.
queryAllResolves every matching element inside the root with a CSS selector.
createCreates a new element by tag name instead of querying existing markup.
resolveUses custom logic to return an element or array of elements manually.
minReports an error when fewer than the expected number of elements are resolved.
maxReports an error when more than the expected number of elements are resolved.

Each element entry should use a single resolution strategy; query, queryAll, create, and resolve are mutually exclusive.

Resolved element references are available as this.elements on the internal instance.

For conceptual guidance and examples, see Elements.

Lifecycle hooks:

HookDescription
mount()Runs after the component instance has been created and initialized.
unmount()Runs when the component instance is being disposed and cleaned up.

Lifecycle hooks run with this bound to the internal instance, so they can derive values from the component’s current state and other internal data.

For conceptual guidance and examples, see Lifecycle.

Watch callbacks keyed by state property name.

Each callback receives a context object with these properties:

PropertyDescription
typeIdentifies the callback context as a watch callback.
newValueThe incoming value for the watched state property.
oldValueThe outgoing value for the watched state property.
isInitialIndicates whether this is the initialization-time watch run.

Watch callbacks run with this bound to the internal instance, so they can derive values from the component’s current state and other internal data.

For conceptual guidance and examples, see Watchers and State Listeners.

Internal reusable actions bound to the component instance.

Methods run with this bound to the internal instance, so they can derive values from the component’s current state and other internal data.

For conceptual guidance and examples, see Methods.

Derived values keyed by property name.

Each callback receives a context object with these properties:

PropertyDescription
typeIdentifies the callback context as a computed callback.
currentValueThe current computed value before recomputation.
changedPropertyThe state property that changed and triggered the recomputation.

Computed callbacks run with this bound to the internal instance, so they can derive values from the component’s current state and other internal data.

For conceptual guidance and examples, see Computed.

Additional user-defined internal data that does not trigger reactive updates.

For conceptual guidance and examples, see Data.

Render callbacks keyed by resolved element name.

Each callback receives a context object with these properties:

PropertyDescription
typeIdentifies the callback context as a render callback.
indexThe current element index when rendering an element array.

Each callback returns RenderOptions, which may include:

PropertyDescription
styleSets or removes inline style properties on the rendered element.
classesAdds or removes CSS classes by boolean flag.
attributesSets, toggles, or removes HTML attributes on the element.
datasetSets or removes data-* values through the element dataset.
eventsAttaches DOM event handlers to the element.
htmlSets innerHTML directly.
textSets textContent directly.

Render callbacks run with this bound to the internal instance, so they can derive values from the component’s current state and other internal data.

For conceptual guidance and examples, see Render Options.