JavaScript
Every component works from its HTML. The API is for when code has to open, read or change one, and the events are how a page hears about it.
The tsunami object
Each script that has an API adds one module to
window.tsunami. Call a method with the
root element of the component, the one that has
the class (.otp, .table,
.stepper), not a child of it.
const otp = document.querySelector(".otp");
tsunami.otp.value(otp); // "123456"
tsunami.otp.set(otp, "654321"); // fills the boxes
tsunami.otp.clear(otp); // empties them and focuses the first
A few modules are a function, such as
tsunami.toast({ title: "Saved" }) and
tsunami.copy("text"); the rest of that module hangs
off the function (tsunami.toast.clear()).
Components added later
Components start on their own when the page loads and when you
add markup later. Where a component also has mount(el)
and destroy(el), use them to start or stop one by
hand, for example with options the markup cannot carry.
Components built on a library (Editor, Code editor, Chart, Date
picker, Sortable) need that library's script on the page. Their
API returns null until the library is ready; wait for
the ready event, tsunami:editorready and its
siblings.
document.addEventListener("tsunami:chartready", (e) => {
const chart = tsunami.chart.get(e.target); // the Chart.js instance
});
Events
Events are named tsunami:<name>, bubble, and
carry their data in event.detail. Listen on the
component, an ancestor or document;
event.target is the component that sent it.
A few can be cancelled with preventDefault(); the
component's page says which.
document.addEventListener("tsunami:otpcomplete", (e) => {
verify(e.detail.value);
});
form.addEventListener("tsunami:tagschange", (e) => {
console.log(e.detail.value); // ["design", "billing"]
});
Form controls keep sending the browser's own
input and change too, so a form, the
Save bar and your own listeners keep working.
Reading a component's API section
Each component page ends with the same few tables. Read them in this order:
- Attributes: what you set in the HTML, with the element it goes on. Start here; most pages need nothing more.
-
Methods: what you call on
tsunami.<module>. The first argument is the component's root element unless the table says otherwise. -
Events: the name, the element it is sent on
and what
event.detailholds. - Keyboard: the keys the component handles, for testing and for accessibility.
All modules
The short version of every API. The component page has the arguments, return values and examples.
| Module | Component | Calls | Events |
|---|---|---|---|
tsunami.theme |
Theme switcher | get, set, toggle |
tsunami:themechange |
tsunami.toast |
Toast | toast(), toast.clear() |
tsunami:toast, tsunami:toastclose |
tsunami.copy |
Copy button | copy() |
tsunami:copy |
tsunami.sidebar |
Sidebar | collapse, expand, toggle, mount |
— |
tsunami.tabs |
Tabs | select, close |
tsunami:closetab |
tsunami.select |
Select | open, close, value, refresh |
tsunami:select |
tsunami.autocomplete |
Autocomplete | mount, destroy |
tsunami:autocomplete |
tsunami.table |
Table | sort, select, selected, sync, refresh, page, filter |
tsunami:sort, tsunami:select, tsunami:tablechange |
tsunami.bulkBar |
Bulk action bar | update, clear |
— |
tsunami.tree |
Tree | filter |
— |
tsunami.journey |
Journey | pick, draw |
tsunami:pick |
tsunami.saveBar |
Save bar | check, markClean |
tsunami:dirty, tsunami:clean |
tsunami.sortable |
Sortable | mount, get, destroy, toArray, sort, disabled |
tsunami:reorder |
tsunami.datePicker |
Date picker | mount, get, destroy |
— |
tsunami.timePicker |
Time picker | mount, destroy, get, set |
tsunami:timechange |
tsunami.phoneInput |
Phone input | get, set |
tsunami:phonechange |
tsunami.otp |
One-time code | mount, value, set, clear |
tsunami:otpcomplete |
tsunami.tagInput |
Tag input | mount, value, set, clear |
tsunami:tagschange |
tsunami.filterBar |
Filter bar | mount, values, clear |
tsunami:chipremove, tsunami:filterchange |
tsunami.stepper |
Stepper | go, next, prev, index |
tsunami:stepchange, tsunami:stepcomplete |
tsunami.editor |
Editor | mount, get, destroy |
tsunami:editorready |
tsunami.codeEditor |
Code editor | mount, get, destroy |
tsunami:codeeditorready |
tsunami.chart |
Chart | chart(), chart.get, chart.destroy |
tsunami:chartready |
tsunami.jsonViewer |
JSON viewer | expand, collapse, view, value |
— |
tsunami.slider |
Field | sync |
— |
The Pressed script has no methods; it sends
tsunami:pressedchange when a toggle button changes.