LuxelEspañol

Luxel v0.2 · Guide

Screens (preview)

Status: preview, not frozen. Screen files are Sprint 12.3 work (ADR-061, a draft). Names and shapes can still change before v0.1; a project written with src/views/ + server/contract.ts + server/events.ts keeps working and can live next to screens.

A screen file, src/screens/‹view id>.screen.tsx, holds everything one view needs, declared once: its state, its messages, who may load it and send each event, the handlers, and the view. The build generates the contract from it; nothing is written twice.

import { allow, collection, input, ref, screen, text } from '@luxel-framework/server/screen'; import { Action, Collection, Field, Row, Status, Text, View } from '@luxel-framework/ui'; import { contacts } from '../../server/contacts.mjs'; import login from './login.screen.js'; const s = screen({ state: { rows: collection({ key: 'id', fields: { name: text() } }), summary: text() }, messages: { added: 'Contact {0} added.', nameInvalid: 'Enter a name of 1 to 60 characters.' }, }); export default s.define({ route: { loginView: login }, load: s.load(allow(['user']), async (ctx) => ctx.snapshot(1, await contacts.page(ctx.session.userId))), events: { add: s.event({ policy: allow(['user']), inputs: { name: input({ maxLength: 60, required: true }) } }, async ({ name }, ctx) => { if (name.trim() === '') return ctx.reject('VALIDATION', ctx.message.nameInvalid()); await contacts.add(ctx.session.userId, name.trim()); return ctx.update(await contacts.page(ctx.session.userId)).success(ctx.message.added(name.trim())); }), remove: s.event({ policy: allow(['user']), param: ref('contact') }, async (_input, ctx) => { await contacts.remove(ctx.session.userId, ctx.param); return ctx.update(await contacts.page(ctx.session.userId)); }), }, view: ({ state, events }) => ( ‹View title="Contacts" dataClass="internal"> ‹Row as="form"> ‹Field for={events.add.inputs.name} label="Name" /> ‹Action id="add" event={events.add} status="result">Add‹/Action> ‹/Row> ‹Status id="result" /> ‹Text bind={state.summary} as="p" /> ‹Collection bind={state.rows} maxItems={20} label="Contacts"> {(c) => ‹Action event={events.remove} param={c.id} status="result">{c.name}‹/Action>} ‹/Collection> ‹/View> ), });

What you declare

  • State (screen({ state })): each leaf with a helper of @luxel-framework/server/screen: text(), money(), date(), quantity(), number({ min, max }), numbers({ min, max, length }), status([...]), value(), options({ max }), collection({ key, fields }). A short, fixed list that is never paginated (the 12 months of a year) is declared collection({ key, fields, page: false, maxItems: 12 }): it is exempt from policies.pageSize, maxItems still bounds it, and its ‹Collection> may leave maxItems out (Collections).
  • Messages (screen({ messages })): the texts a handler may show, with {0}, {1}… placeholders; plural({ one, other }) for counts. A handler calls them as functions: ctx.message.added(name).
  • Policies: allow(roles) or anyone(), each with an optional { effect: 'read' }. They are part of the declaration, next to the handler they guard.
  • Events (s.event(spec, handler)): inputs are declared with input({ type, maxLength, required, min, max }) (or choice(kind) for a select whose value is an id); an event sent from a collection item declares its parameter: param: ref('‹kind>') for an id your scope function checks, param: local() for a key that names no record (a page number).
  • Route (route): what views.‹id> holds in luxel.config.ts for a view file (path, loginView, whenAuthenticated, identity, accountLimit, styles, pollMs…). Another screen is named by importing it: loginView: login. path: '/pricing' gives the screen a clean address; without it the screen is served at /‹variant>/‹file name>. The paths of all screens are the app's route table, fixed at build (Routes); a path with a parameter (/contacts/:id) does not build yet.
  • Load: s.load(policy, handler), or s.staticLoad(handler?) for a public page that is the same for everyone.

What the view uses

The view receives { state, events } and names things with them, never with strings: bind={state.summary}, event={events.add}, ‹Field for={events.add.inputs.name}>, onSuccess={{ clear: [events.add.inputs.name], focus: events.add.inputs.name }}. A string where a state path, an event or a field's input belongs is a build error (LUXEL-E061), and so is an event or an input that the view and the declaration do not agree on (LUXEL-E062). One string remains: onSuccess.focus also takes a string, the id of an action or the name of a field; the build checks that it names one of them. An event's name on the wire is its key with each capital letter written as a dot and its lowercase (signOut is sign.out).

What runs where

The handlers import your server modules (a database, a mailer). The view is compiled at build time. So the build treats a screen file in a particular way:

  • Imports. At build time only @luxel-framework/ui, @luxel-framework/server/screen and relative .tsx / .json files inside src/ are loaded. Every other import (a .ts or .mjs module, a package, node: modules) is replaced by an inert stand-in: its file is not read and its code does not run. Declaring a handler captures the stand-in and runs nothing. Using one outside a handler (calling it, constructing it, writing to it, turning it into text or a number, placing it in the view) is LUXEL-E060. Two things report nothing: reading a property of a stand-in gives another stand-in, and a bare test (if (imported)) is true whatever the real value would be. So do not branch on a server import outside a handler.
  • A separate process. The screens are evaluated in another Node process started with Node's permission model and nothing allowed: no file reads or writes, no child processes, no workers; no network where the Node version's permission model covers it (it does not on Node 22). This is a guard against a build that reads server code by mistake. It is not a sandbox: do not build code you do not trust.
  • On the server. The server bundle (.luxel/events.mjs) has the real imports. It is generated: it registers every screen's load and handlers with screenDefinitions from @luxel-framework/server, which an app does not call itself. A server module written in TypeScript is compiled into that bundle, so its text is there (the bundle is never served).
  • A view file (src/views/*.view.tsx) and luxel.config.ts cannot import a screen file.

Everything else is as in views, the server and forms: the browser receives the compiled template and the universal runtime, and no code of the screen.

Edit this page on GitHub

This page: 7.9 KB of JavaScript (Luxel's shared runtime), 0 KB from your app.

Measured when the site is built: the runtime files this page loads, compressed as they are sent.

Made by IntiOps. The Luxel 0.1.0 betas are on npm (0.1.0-beta.1, 0.1.0-beta.2), with an alpha of 0.2 (0.2.0-alpha.1): no external security review yet.