Every component class extends Monogatari.Component, Monogatari.ShadowComponent, Monogatari.ScreenComponent or Monogatari.MenuComponent (see the base classes). This page lists what they all have; the methods you write to hook into the life cycle are on Component Life Cycle.

Static properties

Property Type Default What it’s for
tag string made from the class name The element’s name. Set it with static tag = 'my-tag';
engine engine The engine, set by monogatari.registerComponent() (the built-in components have it from the start). Inside a static method it’s this.engine.
_configuration object {} Settings for the component, read and written with configuration(). Declare static _configuration = {}; in your class so it doesn’t share the base class’s object.
_experimental boolean false When true, the engine leaves the component out of monogatari.components(), and so never defines it, unless the ExperimentalFeatures setting is true.
_priority number 0 Declared on the base class and never read: components are set up in the order they were registered. Deprecated.

Static methods

Method What it does
configuration(object) Merges object into the component’s settings. configuration('key', value) sets one, configuration('key') returns one, configuration() returns all of them.
template(template) Sets the component’s template: a string, or a function that receives the element and returns a string. Elements already on the page render again.
template() Returns the template’s result, or undefined when there’s none.
instances(callback) Calls callback with every element of the component on the page. Without a callback, returns them as an Artemis DOM object.
all() Every element of the component on the page, as an Artemis DOM object.
get('id') The elements of the component whose data-instance attribute is id, as an Artemis DOM object.

The game hooks (setup(), shouldProceed(), onReset() and the others) are on Component Life Cycle.

<main-menu> and <quick-menu> extend MenuComponent, which keeps their buttons in the engine’s configuration, under the menu’s tag, and has these static methods. They work anywhere in your game’s files; the buttons the engine adds or removes when the game starts (Credits, Gallery, Log, and Back and Skip when they’re turned off) are in the list only inside monogatari.init()’s .then(…):

Method What it does
addButton(button) Adds a button at the end.
addButtonAfter('Settings', button) Adds a button after the one whose string is 'Settings'. Nothing happens when there’s none.
addButtonBefore('Settings', button) Adds a button before that one.
removeButton('Settings') Removes that button.
buttons() Returns the list of buttons.
button('Settings') Returns that button.
configuration('buttons', list) Replaces the whole list. configuration({ buttons: list }) does the same.
configuration('buttons') The list, like buttons(). configuration() returns the menu’s settings with the list as buttons.

Each change renders every element of the menu again, through its template when it has one. A button is an object:

Key What it is
string The key of the button’s text in the interface translations. It also identifies the button for the methods above.
icon Optional. Font Awesome classes for the button’s icon, like 'fas fa-save'. Without one, the button has only its label.
data The button’s data- attributes: { action: 'open-screen', open: 'about' } becomes data-action="open-screen" data-open="about".
element The tag of the element, 'button' when left out.

Instance properties

Property What it holds
this.engine The engine.
this.props The props. Read-only: change them with setProps(). A prop that was never set falls back to the attribute of the same name, with "5" read as 5 and "true" and "false" as booleans.
this.state The state. Read-only once the element is on the page: change it with setState().
this.slotContent The HTML that was written between the element’s tags, kept the first time it went on the page.
this.isReady true once the element has rendered and until it’s taken off the page.
this.isFirstMount true until the element has been on the page once.
this.width, this.height The element’s size in pixels, as the browser computes it. Assigning a number or a CSS length sets its style.
this.static The element’s class.

Instance methods

The page

Method What it does
element() Returns the element wrapped in an Artemis DOM object.
content('name') Returns the elements with data-content="name" inside it, as an Artemis DOM object.
instance('id') The same as the static get('id'): the elements of this component whose data-instance is id.
query(selector), queryAll(selector) The element’s querySelector() and querySelectorAll(). In a ShadowComponent they search the shadow root.
remove() Takes the element off the page.

Rendering

Method What it does
render() Returns the markup. You write it.
forceRender() Renders the element again. Returns a promise.
setState(object), setProps(object) Merge object into the state or the props and run the update methods. They don’t render again.
setStyle(css, replace) Adds CSS for the component, given as text. In a ShadowComponent it applies inside the shadow root. true as the second argument replaces what was set before.
ready(callback) Calls callback once the element has rendered, or right away when it already has.
template(template) The same as the static template().

Events

Method What it does
on(event, callback) Listens to an event on the element. Returns the element.
off(event, callback) Stops listening.
once(event, callback) Listens to the next event only.
emit('name', detail) Dispatches a CustomEvent from the element, which bubbles up the page and out of a shadow root.

Attributes on the element

Attribute Added by When
data-component="affection-meter" Pandora When the element goes on the page.
class="animated" Monogatari’s Component When the element goes on the page.
data-screen="about" ScreenComponent When the element goes on the page: the tag without -screen.
class="active" ScreenComponent While the screen is open.
One per text, number or boolean prop Pandora When the element goes on the page, and on every setProps().

Talking to the engine

Inside a component, this.engine is the engine, with the whole Engine API. The calls components use most:

Call What it does
this.engine.storage('affection') Reads one of the game’s variables.
this.engine.state('label') Reads the game’s state: the label, the step, what is on screen.
this.engine.string('About') The interface string for that key, in the player’s language.
this.engine.preference('Language') One of the player’s preferences.
this.engine.setting('Name') One of the game’s settings from js/options.js.
this.engine.on('didUpdateState', callback) Listens to an engine event.
this.engine.element() The game’s <visual-novel> element, as an Artemis DOM object.