nercone.devaboutlinkscontactsitemap
aboutlinkssitemap82ffc6419

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');
MethodDescription
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.

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

HelperDescription
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.

NameClassDoesOptions (default)
themeThemeChanges the theme. See ThemesbaseURL, themes, theme ('auto-stark'), storageKey ('nercone-theme'), rootAttribute ('data-theme'), triggerAttribute ('data-theme-trigger'), linkAttributes
chartChartDraws the charts with data-chart-values. Chart.draw() draws any other. See Data Displayselector ('.chart'), valuesAttribute ('data-chart-values'), maxAttribute ('data-chart-max'), lengthAttribute ('data-chart-length')
cursorCursorDraws the cursorselector ('#cursor'), linkSelector, systemSelector ('.cursor-system'), activeClass ('cursor-active'), padding (6), linkGap (4), textGap (4), layer (1)
heatmapProgressSets --heatmap-value. See Data DisplayThose of progress, with selector ('.heatmap-cell'), property ('--heatmap-value'), max (1), and data-value, data-min, and data-max as the attributes
loading-overlayLoadingOverlayPlays and removes the loading overlayselector ('#loading-overlay'), easing, layer (2), duration ({ in: 3000, wait: 1000, out: 1000 }, in milliseconds)
popupPopupFills in for invoker commands and closedby. See Overlaysselector ('.popup, dialog'), openAttribute ('data-popup-open'), closeAttribute ('data-popup-close')
progressProgressSets --progress-value and the level. See Progressselector, property ('--progress-value'), min (0), max (100), valueAttribute ('aria-valuenow'), minAttribute ('aria-valuemin'), maxAttribute ('aria-valuemax'), thresholdsAttribute ('data-progress-thresholds'), levelAttribute ('data-progress-level')
sidebarSidebarMarks the current page and closes the popover. See Layoutselector ('.sidebar'), media ('(max-width: 768px)')
sliderSliderSets --slider-value. See Formsselector ('.slider, input[type="range"]'), property ('--slider-value')
stack-barProgressSets --stack-bar-value. See Data DisplayThose of progress, with selector ('.stack-bar-segment'), property ('--stack-bar-value'), max (1), and data-value, data-min, and data-max as the attributes
swatchSwatchCopies colors. See Colorsattribute ('data-swatch'), formatAttribute ('data-swatch-format'), format ('variable'), toast (true), copiedMessage, failedMessage
toastToastShows toasts. See Overlaysposition ('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.

Before the swap, window.nercone.cleanup() is called; after it, window.nercone.reinit(doc). To move from a script, call window.nercone.navigate(href).