JavaScript
One script, no framework, nothing to start by hand. You write HTML with data-momo-… attributes; Momo finds them, also in content you add later, and tells you what happened through events.
Add the script
<script src="https://cdn.jsdelivr.net/npm/momo-neobrut-ui@0.9.1/dist/momo.js" defer></script>It has no dependencies and works with plain pages, server-rendered pages and any framework. There is a smaller site build without the app-only parts.
Attributes switch things on
A component starts because of an attribute in the HTML, never because of a line of script.
<button class="btn" data-momo-dialog="invite">Invite</button>
<dialog class="dialog" id="invite">…</dialog>
<div class="tabs" data-momo-tabs>…</div>
<input class="form-control" data-momo-tags name="skills">Options are attributes too, written beside it: data-momo-placement="top", data-persist="name". Each component's page lists its own.
Content added later
Momo watches the page. Markup you add after loading (a row from your server, a dialog built in script, a framework re-render) wakes up by itself. To start something at once, without waiting for the next moment the browser is free:
list.insertAdjacentHTML('beforeend', html);
Momo.init(list); // safe to call as often as you likeEvents tell you what happened
Every component speaks through events named momo:something. They bubble, so you can listen on the component, on a parent, or on document. What changed is in event.detail.
tabs.addEventListener('momo:tab', (e) => console.log(e.detail.index));
// anywhere on the page
document.addEventListener('momo:toggle', (e) => save(e.target.id, e.detail.on));Some can be stopped before they happen: call event.preventDefault() and nothing changes. The page of each component says which.
star.addEventListener('momo:toggle', (e) => {
if (!signedIn) e.preventDefault();
});Work that takes time uses waitUntil: hand over a promise and the spinner stays until it settles.
feed.addEventListener('momo:load', (e) => {
e.detail.waitUntil(fetch('/api/items?page=' + e.detail.page).then(addItems));
});Form fields that Momo dresses up (the dropdown, the tag input, the date picker, the slider) keep the real field in the form and fire the normal input and change events, so your form code and your framework's bindings keep working.
Doing things from code
Each component has a small set of functions under Momo. They take the element or a selector.
Momo.dialog.open('#invite');
Momo.toast({ title: 'Saved', color: 'green' });
Momo.tabs.select(tabRow, 2);
Momo.filter('#shop', { tags: ['bags'], sort: 'price' });
if (await Momo.confirm('Discard this draft?')) discard();Common events
| Event | From | Detail |
|---|---|---|
momo:tab | Tabs | { tab, panel, index } |
momo:select | Dropdown menu, Split view, Tree view | { item } |
momo:toggle | Toggler | { on, targets } (can be stopped) |
momo:collapse | Collapse | { open } |
momo:chips | Chips | { values, chip } |
momo:filter | Filter & sort | { shown, total, tags, query, sort, dir } |
momo:sort, momo:move | Sortable, Kanban | What moved and where |
momo:load | Infinite scroll | { page, waitUntil, done } |
momo:refresh | Pull to refresh | { waitUntil } |
momo:date, momo:range | Date picker, Date range | The chosen date or dates |
momo:tags | Tag input | { tags, added, removed } |
momo:invalid | Validation | { fields } |
momo:page | Data table | The page that is shown |
momo:overflow | Nav overflow | { hidden } |
momo:theme | Theme toggle | The mode that was chosen |
With a framework
- It just works for most things: render the markup with the attributes, and Momo wakes it up after the framework has put it on the page.
- Listen with the framework's own event syntax where it supports custom events, or with
addEventListeneron a ref. - Let one side own each piece of state. If your framework decides which tab is open, call
Momo.tabs.select()when its state changes; if Momo decides, read it frommomo:tab. - Keep keys stable in lists, so the framework moves elements and does not rebuild them.
Good to know
- Without the script the page still reads and links still work: tabs scroll, collapsed text is visible, the browser's own select and date field show.
- One broken component does not stop the others: an error is written to the console and the rest carries on.
- Nothing is added to the page's global names except
Momo.