Scripts
script.js adds behavior that HTML and CSS cannot provide. Every page must still work without it.
window.nercone
initializer.js defines window.nercone. Each script registers a feature under a name, and the feature is started when the document is ready.
const toast = window.nercone.get('toast'); // the registered feature, or null
toast.show('Hello');| Method | Description |
|---|---|
register(name, feature) | Stops the feature registered under name, if any, registers feature, and calls its init() when the document is ready. Returns feature |
unregister(name) | Stops the feature and removes it |
get(name) | Returns the feature, or null |
cleanup() | Calls cleanup() of every feature. Called before a page transition swaps the page |
reinit(doc) | Calls reinit(doc) of every feature. Called after a page transition, with the new document |
navigate(href) | Moves to href with a page transition when one is available, or loads it normally |
Writing a feature
A feature is any object. It may have these methods, and those it lacks are skipped. Errors thrown by them are logged, so one feature cannot stop the others.
init(): starts the feature. Add listeners todocumentrather than to elements, because a page transition replaces the elements.cleanup(): called before the page is swapped. Close what belongs to the old page.reinit(doc): called after the page is swapped. Update for the new page.destroy(): stops the feature. Called byregister()andunregister(); without it,cleanup()is called instead.
A page transition runs a script only once, even when the page that loaded it is opened again, so page scripts should be features as well.
class Clock {
init() {
this.timer ??= setInterval(() => this.render(), 1000);
this.render();
}
reinit() {
this.render();
}
destroy() {
clearInterval(this.timer);
this.timer = null;
}
render() {
for (const element of document.querySelectorAll('[data-clock]')) element.textContent = new Date().toLocaleTimeString();
}
}
window.nercone.register('clock', new Clock());Changing options
Each feature has a class on window.nercone whose constructor takes options. To change them, register a new instance under the same name. The old one is stopped first.
window.nercone.register('toast', new window.nercone.Toast({ position: 'top-center' }));Helpers
| Helper | Description |
|---|---|
Nercone.whenReady(callback) | Calls callback when the document has been parsed, or at once if it already has |
Nercone.whenLoaded(element) | Resolves to true when the element, such as a <link>, fires load, and to false when it fires error |
Numbers.clamp(value, min, max) | Limits value to the range |
Numbers.parse(value, fallback) | Parses a string, such as an attribute value, as a finite number, or returns fallback |
Numbers.ratio(value, min, max) | Where value is between min and max, from 0 to 1, or null |
topLayer.add(element, order) | Keeps the element in the top layer, above modal popups, as a manual popover. Elements with a larger order are drawn above |
topLayer.remove(element) | Stops keeping the element in the top layer |
The classes are window.nercone.Nercone, window.nercone.Numbers, and window.nercone.TopLayer; window.nercone.topLayer is the shared instance. The toast container (order 0), the cursor (1), and the loading overlay (2) are kept in it.
Features
The features registered by script.js, with their classes and options.
| Name | Class | Does | Options (default) |
|---|---|---|---|
theme | Theme | Changes the theme. See Themes | baseURL, themes, theme ('auto-stark'), storageKey ('nercone-theme'), rootAttribute ('data-theme'), triggerAttribute ('data-theme-trigger'), linkAttributes |
chart | Chart | Draws the charts with data-chart-values. Chart.draw() draws any other. See Data Display | selector ('.chart'), valuesAttribute ('data-chart-values'), maxAttribute ('data-chart-max'), lengthAttribute ('data-chart-length') |
cursor | Cursor | Draws the cursor | selector ('#cursor'), linkSelector, systemSelector ('.cursor-system'), activeClass ('cursor-active'), padding (6), linkGap (4), textGap (4), layer (1) |
heatmap | Progress | Sets --heatmap-value. See Data Display | Those of progress, with selector ('.heatmap-cell'), property ('--heatmap-value'), max (1), and data-value, data-min, and data-max as the attributes |
loading-overlay | LoadingOverlay | Plays and removes the loading overlay | selector ('#loading-overlay'), easing, layer (2), duration ({ in: 3000, wait: 1000, out: 1000 }, in milliseconds) |
popup | Popup | Fills in for invoker commands and closedby. See Overlays | selector ('.popup, dialog'), openAttribute ('data-popup-open'), closeAttribute ('data-popup-close') |
progress | Progress | Sets --progress-value and the level. See Progress | selector, property ('--progress-value'), min (0), max (100), valueAttribute ('aria-valuenow'), minAttribute ('aria-valuemin'), maxAttribute ('aria-valuemax'), thresholdsAttribute ('data-progress-thresholds'), levelAttribute ('data-progress-level') |
sidebar | Sidebar | Marks the current page and closes the popover. See Layout | selector ('.sidebar'), media ('(max-width: 768px)') |
slider | Slider | Sets --slider-value. See Forms | selector ('.slider, input[type="range"]'), property ('--slider-value') |
stack-bar | Progress | Sets --stack-bar-value. See Data Display | Those of progress, with selector ('.stack-bar-segment'), property ('--stack-bar-value'), max (1), and data-value, data-min, and data-max as the attributes |
swatch | Swatch | Copies colors. See Colors | attribute ('data-swatch'), formatAttribute ('data-swatch-format'), format ('variable'), toast (true), copiedMessage, failedMessage |
toast | Toast | Shows toasts. See Overlays | position ('bottom-end'; also top-start, top-center, top-end, bottom-start, bottom-center), duration (5000; 0 never closes), limit (5), dismissible (true), label ('Notifications'), closeLabel ('Close'), triggerAttribute ('data-toast'), variantAttribute ('data-toast-variant'), layer (0), containerAttributes |
Page transitions are not a feature but the navigator of window.nercone; see below. window.nercone.Binding (binding.js) is a class without a feature: it fills elements with data when a script calls it. See Data Display.
Cursor
A round cursor that follows the mouse. Over a link or button it becomes a box around it, and over text it becomes a thin bar. It is shown only for a mouse; touch and pen input hide it and leave the system cursor. Add the element to <body>:
<div id="cursor" data-view-transition-keep></div>Elements with cursor-system show the system cursor instead, for places where the exact position matters, such as a canvas. The elements treated as links are set by the linkSelector option. The colors are --color-cursor and --color-cursor-on-link.
Loading overlay
Covers the page while it loads, and draws the logo before fading out. When the system prefers reduced motion, it only fades out. Add it as the first child of <body>:
<div id="loading-overlay" data-view-transition-keep>
<svg viewBox="96 256 832 512" xmlns="http://www.w3.org/2000/svg">
<polyline points="872 572.75 152 332 152 692 512 692 872 332 872 692 684.5 692" fill="currentColor"/>
</svg>
</div>It is shown only on the first page; page transitions do not show it again. noscript.css hides it when JavaScript is disabled, and it fades out by itself after --loading-overlay-timeout (default 10s) when the scripts fail to load.
Page transitions
In browsers with the View Transition API, view-transition.js moves between pages of the same origin without reloading. It fetches the next page, then swaps the head and the children of <body> while the header and main content cross-fade. When the page cannot be swapped, it is loaded normally.
- Both pages must have
<header>and<main>. - Links with
target(other than_self) ordownload, links to other origins, and clicks with a modifier key are left to the browser. - Requests have the header
X-Requested-With: view-transition. - Elements and stylesheets with
data-view-transition-keepstay as they are, even when the next page does not have them. The cursor, the loading overlay, the toast container, and the stylesheet added bytheme.jshave it. - Stylesheets of the next page are loaded before the swap, and stylesheets the next page does not have are removed. Scripts are added but never removed, and a script that has run once does not run again.
- The scroll position is restored when going back and forward.
Before the swap, window.nercone.cleanup() is called; after it, window.nercone.reinit(doc). To move from a script, call window.nercone.navigate(href).