Changing Built-in Components
The built-in components are yours to change. From the lightest change to the heaviest:
| You want to | Do this | Needs JavaScript |
|---|---|---|
| Change how it looks | Restyle it in style/main.css. |
No |
| Add an element to it | Write it inside its tag in index.html. |
No |
| Rewrite its markup | Give it a template. | A little, or none with a <template> tag |
| Add or remove a menu button | Use the menu methods. | Yes |
| Change an element once the game is set up | Change it from js/main.js. |
Yes |
| Change what it does | Replace it with a subclass. | Yes |
To know what a component renders, look at it in your browser’s developer tools, read its page in the Components section, or read its render() method in src/components/<tag>/index.ts in the engine’s code.
Restyle it
Every component is an element you can select in style/main.css by its tag, like text-box or main-menu, and the engine also gives it a data-component attribute with the same name. Style and Design covers styling the game, and each built-in component’s page lists the classes and variables it uses.
Add to its markup
Elements you write inside a component’s tag in index.html are kept and shown after the component’s own markup. This puts the game’s title on the main screen, above the menu, without any JavaScript:
<main-screen>
<h1 class="game-title">After the Bell</h1>
<main-menu></main-menu>
</main-screen>.game-title {
position: absolute;
top: 20%;
width: 100%;
color: var(--text-color);
font-size: 3rem;
text-align: center;
}The <main-screen> renders nothing of its own, so its children are all it shows. Inside a component that does render markup, like <game-screen>, your elements come after it: that’s how <text-box> and <quick-menu> end up in the game screen.
Rewrite its markup
A template replaces a component’s markup: Pandora renders the template instead of calling the component’s render(). Set one from js/main.js with the component’s template() method:
monogatari.component('main-screen').template(() => {
return '<h1 class="game-title">After the Bell</h1>';
});
- Call it above
$_ready(…), so the component renders with it from the start. Called later, it renders every element of that component on the page again. - The function receives the element, also as
thiswhen it isn’t an arrow function, so the template can readelement.propsandelement.state. - The children written in
index.htmlare still added after the template, as they are afterrender(). Don’t repeat them in the template:<main-menu></main-menu>written both inindex.htmland in the template shows two menus.
Or write the template in index.html, without JavaScript: a <template> tag whose id is the component’s tag, anywhere in the page:
<template id="main-screen">
<h1 class="game-title">After the Bell</h1>
</template>Pandora uses it when an element of that component goes on the page for the first time and no template was set with template().
WARNING
The engine finds parts of its components by their data-ui, data-content, data-action and data-string attributes. When you rewrite the markup of a component that has them, like <text-box>, keep those elements and attributes, or the parts of the engine that look for them stop working.
Change a menu’s buttons
<main-menu> and <quick-menu> keep their buttons in the engine’s configuration and have methods to change them: addButton(), addButtonAfter(), addButtonBefore(), removeButton(), buttons(), button() and configuration('buttons', [ … ]). They work anywhere in your game’s files, and inside monogatari.init()’s .then(…) they also see the Credits and Gallery buttons the engine adds when the game starts. A screen of your own adds a button this way, and the Main Menu and Quick Menu pages document the methods.
Each change draws the menu again through its template, when it has one. A menu’s template can draw the buttons with the element’s own render():
monogatari.component('main-menu').template((menu) => {
return `<div class="menu-frame">${menu.render()}</div>`;
});Change it after the game is set up
A component is an element like any other, so once monogatari.init() has run you can change it with the browser’s DOM methods or with Artemis, inside .then(…):
$_ready(() => {
monogatari.init('#monogatari').then(() => {
const gameScreen = document.querySelector('game-screen');
// Add a title before everything in the game screen.
gameScreen.element().prepend('<h1>My Title</h1>');
// Add an element to the game screen's visuals.
gameScreen.content('visuals').append('<div id="someCustomElement"></div>');
});
});element() returns the element wrapped in Artemis, and content('visuals') finds the element with data-content="visuals" inside it. Changes made this way last until the component renders again. The menus do that whenever their buttons change, so change a menu through its methods instead.
Replace a built-in component
To change what a component does, extend its class and register your version with the same tag, before monogatari.init(). The engine then uses yours everywhere the original was. monogatari.component('text-box') gives you the built-in class.
This text box wraps the built-in one’s markup in a frame:
const TextBox = monogatari.component('text-box');
class FramedTextBox extends TextBox {
static tag = 'text-box';
async render() {
const markup = await super.render();
return `<div class="frame">${markup}</div>`;
}
}
monogatari.registerComponent(FramedTextBox);- Registering a tag that’s already registered, before
monogatari.init(), removes the first registration, so the game has one<text-box>component:FramedTextBox. - The built-in
<text-box>has an asynchronousrender(), sosuper.render()returns a promise:awaitit. - Calling
super.render()keeps every element the engine looks for (data-ui="who",data-ui="face", the<type-writer>), so dialog still shows. Writing a component of your own from nothing means writing all of them yourself. - A subclass inherits the built-in’s tag, so
static tag = 'text-box'only says out loud which component it replaces.
Related
- Components: writing and registering a component.
- Component Life Cycle: when templates and
render()run. - Components: every built-in component, its markup and its classes.