jsails/view is the small runtime that compiled .jsails templates call, and
the types their generated code names. It is browser-safe and dependency-free
(under 1 KB minified and gzipped), so the same helpers render a page on the
server and an island in the browser. TSX that wants the template rules for
class, style and loops can import them too.
| Export | Kind | What it does |
|---|---|---|
normalizeClass |
function | A :class value (string, record, array) to a class list |
normalizeStyle |
function | A :style value (string, record, array) to a style object |
bindAttributes |
function | A js-bind spread without raw-HTML attributes, merged with an element's own class and style |
renderList |
function | The loop of js-for: numbers, iterables and records |
PageProps<typeof load> |
type | The props a page receives: the awaited result of its load |
ComponentChild |
type | Anything a component may render, and a slot's type |
ClassValue, StyleValue, StyleObject, SpreadAttributes, BoundAttributes |
types | The shapes the helpers accept and return |
Which .jsails files are views
A file's kind comes from its path. Under pages/, a file is a page, except
layout.jsails, error.jsails and not-found.jsails, which are the layout,
the error boundary and the not-found page of their directory. Files under
components/ are live components, under islands/ islands, under mail/
mails, and every other .jsails file is a view: a plain server-rendered
component named after its file (ui/tag-badge.jsails is TagBadge).
A page reads its props from load, and a layout or a view declares them with
defineProps<T>(); a name the template uses that nothing declares is a prop.
A view's <slot>s are its children and named slot props, typed by
defineSlots<T>().
Classes and styles
normalizeClass joins every truthy entry in order and returns undefined
when no class is named, so the element renders no class attribute:
import { normalizeClass } from 'jsails/view';
const active = true;
const className = normalizeClass(['card', { active, hidden: false }, ['wide']]);
// 'card active wide'
void className;
normalizeStyle parses declaration strings (a ; inside parentheses, as in a
url(data:…), or inside quotes stays in its declaration), lets later entries
win, and removes a property set to null, undefined or false:
import { normalizeStyle } from 'jsails/view';
const style = normalizeStyle(['color: red; margin: 0', { color: 'blue', margin: null }]);
// { color: 'blue' }
void style;
A template's static class/style merges with the bound one, and js-show
adds display: none last while its condition is false (while it holds, the
element keeps the display its own style sets). js-bind spreads an object
onto the element through bindAttributes, which drops dangerouslySetInnerHTML
and innerHTML, so an attribute object that comes from data cannot insert
markup: js-html is a template's one trusted-HTML door, and the compiler
rejects those two names as attributes. When the element has a class or
style of its own, the spread's class and style merge after the element's
instead of replacing them:
import { bindAttributes } from 'jsails/view';
const fromData: Record<string, unknown> = { id: 'card', class: 'wide', innerHTML: '<b>x</b>' };
const attributes = bindAttributes(fromData, { class: 'card' });
// { id: 'card', class: 'card wide' }
void attributes;
Loops
renderList is what js-for compiles to. n in 10 counts from 1 to 10, an
array or any iterable passes each item and its index, and a plain object passes
each value, key and index in Object.keys order. null and undefined render
nothing:
import { renderList } from 'jsails/view';
const rows = renderList({ a: 1, b: 2 }, (value, key, index) => `${index}. ${key}=${value}`);
// ['0. a=1', '1. b=2']
void rows;
Page props
PageProps<typeof load> is the type of the props a page's template sees: the
awaited return of its load export. Use it to type a TSX page the same way:
import type { PageProps } from 'jsails/view';
async function load(): Promise<{ title: string }> {
return { title: 'Home' };
}
type HomeProps = PageProps<typeof load>; // { title: string }
const props: HomeProps = { title: 'Home' };
void props;