browser#
HTML templates, formatting, and browser helpers in one bundle.
Install#
npm i @nodedk/browser
Usage#
Copy the browser bundle into your application's assets:
mkdir -p app/assets/js
cp node_modules/@nodedk/browser/build/browser.js app/assets/js/browser.js
Include /js/browser.js before your application scripts in app/config/bundle.js:
module.exports = {
css: [],
js: ['/js/browser.js', '/js/base.js']
}
Load the bundle in your layout, as in @nodedk/scaffold:
var { html } = require('@nodedk/page')
function Layout(state, slot = []) {
return html`<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
${state.script(['/bundle.js'])}
</head>
<body>
<div class="flash"><span class="message"></span></div>
${slot}
<button onclick="window.flash('Saved')">Save</button>
</body>
</html>`
}
The copied script installs every helper on window. Refresh the copy when upgrading the package. ndk build combines assets listed in bundle.js; it does not resolve CommonJS imports in browser files.
state.script() loads individual scripts in development and the built bundle
in production. Serve the generated dist directory through your production
web server or the server’s static option.
For a separate module-aware bundler, the package also exports the functions:
var browser = require('@nodedk/browser')
Object.assign(window, browser)
API#
The package combines these exports without wrapping their arguments or return values:
Templates and formatting#
html(strings, ...values): template string; joins arrays and omits nullish and boolean values.date(value, type = 'date'): local date/time input formatting; supportsdate,datetime-local,time,month, andweek.esc(value)andraw(value): escape and decode HTML entities.isImage(name): image filename-extension check.num(value, options): locale-aware number formatting;optionsis a locale or{ lang, ...IntlOptions }.time(value, options): date formatting;optionsis a locale or{ lang, format, ...IntlOptions }.truncate(value = '', size = 32): shorten text with an ellipsis suffix.
Browser helpers#
From window, which documents each option, return value, and required HTML structure:
active(options = {}): mark links for the current pathname; optionsselectorandactive.back()andtrack(): navigate to and record the tracked URL.blur(element)andtoggle(name, element): manage open elements and classes.client(path, data = {}, options = {}): JSON request; optionsmethodandheaders; resolves to parsed JSON.cookie(key, value, options): read, write, or delete a cookie; optionsdays,path,domain,sameSite, andsecure.copy(input): copy the value of an input or selector.download(input, filename): download a Blob, URL, or link.file(input): presign and upload selected files from a.fieldcontainer.flash(message, options = {}): show a message; optionsel,name,time,class, andscroll.load(path, into): fetch HTML into an optional element or selector; resolves to text.params(id): query value by name or pathname segment by numeric index.serialize(form): form values as an object, including dotted field names.store(key, value): JSON session storage;nulldeletes, omitted value reads.submit(form, options = {}): submit a form; optionserror,success, andredirect.upload(link, file, options = {}): signed POST or PUT upload; optionsprogressanderror; resolves to{ url }.
Development#
npm run build
Writes build/browser.js and build/browser.min.js from the exported functions.
Packaging runs this build automatically.
Created by Vidar Eldøy