ray-elements and raylang
ray-elements is framework-agnostic: the components are standard custom elements with no dependencies, and they work in any page, in React, in Vue, in a browser or in any webview. Nothing in a component requires raylang.
raylang is where ray-elements comes from and where it gets extras. This page says what those extras are: a bridge to the native side (plan 1), and a raylang package that brings the whole design system without npm (plan 2).
Today
- Tooling in raylang. Tokens, contrast checks, docs, the npm packages and
the browser tests are generated and run by
tools/(tools/run.sh build). - Made for the
std/uiwebview. No build step: the elements load as ES modules fromray://app, and form fields are form-associated, so aFormDatareaches the program throughwindow.ray.sendorwindow.ray.requestas it is. - Examples.
examples/desktop-form(plain HTML),examples/react-appandexamples/vue-app(made withray new --frontend react-ts|vue-ts) drive the page in the real webview and check what reaches raylang. - The raylang package (plan 2): the
elementsdependency brings@raylang/elementsto the app's pages by its npm name (import "@raylang/elements/button", as with a bundler), with no npm and no copied files;examples/starteris the app to copy. It is published to its own repository,github.com/ray-language/elements. - The native bridge (plan 1, done): system file dialogs with
real paths (
ray-file-upload native,ray-path-input), native confirmations (confirmDialog()), native context menus (ray-menu native), the app's menu bar in the command palette (native-menus), and the window:ray-app-shellunder a transparent title bar, window colors that follow the theme, and the "unsaved changes" mark (trackEdits). See below, the "In a raylang app" section of each component's page, andexamples/desktop-app.
Using the bridge
In the program, answer the bridge before the app's own events
(elements = "path:…/ray-elements/elements" in [dependencies] inside this
repository; an app elsewhere uses git+https://github.com/ray-language/elements@v0.3.0
once that version is tagged):
import std/ui;
import elements/bridge;
for e in ui.events() {
if (bridge.handle(e)) {
continue; // a ray-elements request, already answered
}
// the app's own events
}
In the page, once:
import { enableRaylang } from "@raylang/elements/core"; // or from "@raylang/elements"
await enableRaylang(); // the host, or null in a browser
<ray-file-upload native multiple accept=".png,.jpg" label="Photos"></ray-file-upload>
<ray-path-input name="out" label="Export to" mode="save" accept=".csv"></ray-path-input>
ray-file-upload native lists the chosen files as HostFiles (name, size,
type and path), gives their paths in paths and submits the paths with its
form; drop and paste still give Files. ray-path-input shows Browse… only
with the host. confirmDialog() asks with the system dialog unless
native: false. In a browser all of them behave as before.
For menus, keep one Native for the whole loop, declare the menus through it
and use route(), which turns an item run from the palette into the same
"menu" event the menu bar sends:
let n = bridge.system();
bridge.menu(n, "File", [ui.item("new", "New", "cmd+n"), ui.item("open", "Open…", "cmd+o")])?;
for raw in ui.events() {
let e = match (bridge.route(n, raw)) {
Option.Some(e) => e, // for the app
Option.None => continue, // answered by the bridge
};
if (e.kind == "menu") { … } // from the menu bar or from the palette
}
<ray-command-palette native-menus></ray-command-palette>
<ray-menu native context>
<div slot="trigger">…</div>
<ray-menu-item value="rename">Rename</ray-menu-item>
</ray-menu>
bridge.menu_at, replace_menu, rename_menu, set_menu_item and
set_menu_title (an item's text: "Undo" → "Undo Split Clip") keep the record
up to date the same way (bridge.record, bridge.record_rename and
bridge.record_title record a change without touching the bar). The items
of a ui.submenu are listed and run like the others, under their path
(File › Open Recent), and a disabled submenu disables them.
The window: open it with o.kind = "full_content" and enableRaylang() sets
--ray-titlebar-height and --ray-titlebar-inset (the room for the macOS
window buttons) on <html>; a ray-app-shell then makes its header the title
bar (taller, indented, dragging the window) or, with layout="alt" or no
header, starts its navbar and main below it. dragRegion(el) does the same
for any element of your own. enableRaylang() also keeps the window's
background and title bar in the theme's background color (windowColors: false to opt out), and trackEdits(form) marks the window as edited while
the form differs from what was saved:
import { enableRaylang, trackEdits } from "@raylang/elements";
await enableRaylang();
const edits = trackEdits(document.querySelector("form")); // edits.dirty, edits.clean()
Principles
- The components stay agnostic. No component imports the bridge. Without it, every component behaves exactly as it does today, in any host.
- Opt-in and detectable. The bridge is one module, enabled by the app, and
only active when
window.rayexists. A page shared between a browser and astd/uiwindow works in both. - One source of truth. The package, its assets and its types are generated
by the same
buildthat writes the npm packages, from the samecomponent.tomlfiles.
Plan 1: the native bridge
A small protocol between the page and the program, with two halves:
- Page:
core/raylang.js(in@raylang/elements, also re-exported from its index).enableRaylang()detectswindow.ray, asks the program for its capabilities once ("hello") and installs a host incore/host.js, the registry the components ask; they never import the bridge. - Program: a raylang module,
elements/bridge(theelements/directory of this repo), with one entry point for the app's event loop:
import std/ui;
import elements/bridge;
for e in ui.events() {
if (bridge.handle(e)) {
continue; // a ray-elements request, already answered
}
// the app's own events
}
Requests travel through window.ray.request as JSON ({"ray-elements": 1, "op": "pick_files", …}) and bridge.handle answers them with ui.reply_json:
{"ok": true, "value": …} or {"ok": false, "error": "…"}. Anything else
passes through untouched, so the bridge coexists with the app's own protocol.
The dialogs sit behind a Native struct (bridge.system()); handle_with(e, n) takes another one, which is how the webview tests answer with scripted
dialogs.
| Operation | Asks for | Answers |
|---|---|---|
hello |
{protocol, version, platform, ops} |
|
pick_files |
title, directory, multiple, filters |
[{path, name, size}], [] if cancelled |
pick_path |
mode (open, folder, save), title, directory, suggested, filters |
the path, or null |
confirm |
title, message, confirmLabel, cancelLabel, style |
true / false |
menu_items |
[{menu, tag, title, shortcut, enabled, checked}] (no separators, no role:*) |
|
run_menu |
tag |
true; route() gives the app the "menu" event |
popup_menu |
id, items (title, enabled, checked, separator) |
true once shown; the choice comes later as the page event ray-elements:menu ({id, index}), nothing when dismissed |
window_colors |
background, titlebar (#rrggbb, "" = system) |
true |
set_edited |
edited |
true |
What the components get
| Component | With the bridge | raylang API |
|---|---|---|
ray-file-upload |
"browse" opens the native dialog with filters built from accept and returns real paths (paths property, read on change); drop and paste keep working with File objects |
ui.pick_files, ui.file_options, ui.filter |
ray-path-input (new) |
a field with a "Browse…" button for a file, a folder or a save target, showing the chosen path; without the bridge it is a plain text field | ui.pick_file_with, ui.pick_folder_with, ui.save_file_with |
ray-command-palette |
lists the app's native menu items next to its own commands, with their shortcuts; running one emits the same menu event as the menu |
ui.menu (recorded through bridge.menu) |
ray-menu |
native attribute: opens as a native context menu at the pointer; the choice arrives as ray-select |
ui.popup_menu |
confirmDialog() |
asks with the native, modal dialog instead of a ray-dialog |
ui.confirm, ui.message_styled |
ray-sidebar, app header |
in a full_content window, reserve window.ray.titlebar_height and mark the header as the drag handle (data-ray-drag), so the page runs under a transparent macOS title bar |
window kind full_content |
| Theme | when the user picks light, dark or a primary color, the window background and title bar follow, so a resize or reopen never flashes the other theme | ui.set_background, ui.set_titlebar_color |
| Forms | optional edited tracking: a dirty form marks the window as edited (the macOS dot) |
ui.set_edited |
| Attachments | "open" and "show in folder" for an attachment with a path | ui.open_path, ui.reveal |
Milestones
- ✅ Protocol and loop.
core/raylang.jsandelements/bridgewith capability negotiation,pick_filesandconfirm. Tests: the raylang tests ofbridge.answer, andtests/bridge.test.js, where the page talks to the real bridge in the webview runner (scripted dialogs). - ✅ Files.
ray-file-upload nativeand the newray-path-input(pick_path), with their React/Vue wrappers and types. - ✅ Menus.
bridge.menu(andmenu_at,replace_menu,set_menu_item),route(), the palette listing native items (native-menus),ray-menu native. - ✅ Window.
full_contentsupport inray-app-shell(header as the title bar,dragRegion), theme to window colors,trackEdits. The drag handle worked around finding #138 (the shell'sdata-ray-dragdid not see shadow DOM; fixed in raylang 1.27.48); it still spares the interactive elements the shell does not know, such as tabs andsummary. - ✅ Docs and example. An "In a raylang app" section on the pages of
file upload, path input, dialog, menu, command palette and app shell (from
each component's
raylang.md), andexamples/desktop-app, which uses all of it:ray run src/main.ray --checkdrives it in a realfull_contentwindow with scripted dialogs (the system's dialogs and menus cannot be clicked by a script; try them withray run).
Each milestone ships on its own and keeps the components working without the bridge.
Plan 2: the raylang package
The elements/ directory of this repo is a raylang package: a raylang app gets
the whole design system from it, with no npm, no Node and no copied files.
[dependencies]
elements = "git+https://github.com/ray-language/elements@v0.3.0"
# or, next to a checkout of this repository: elements = "path:../ray-elements/elements"
import std/ui;
import elements/bridge;
fn main() -> int {
ui.mount_embed_at("", "assets").unwrap(); // the app's page ([native] embed)
let h = ui.open("App", "ray://app/index.html", 960, 640).unwrap();
for e in ui.events() {
if (bridge.handle(e)) {
continue;
}
// the app's own events
}
0
}
<link rel="stylesheet" href="/node_modules/@raylang/elements/tokens.css">
<link rel="stylesheet" href="/node_modules/@raylang/elements/base.css">
<script type="module">
import { enableRaylang } from "@raylang/elements"; // every component, and the API
await enableRaylang();
</script>
The page imports @raylang/elements by its npm name, the same as an app built
with Vite: import "@raylang/elements/button",
import { enableRaylang } from "@raylang/elements/core". The package's
ray.toml declares it ([web] package = "web"), and raylang (the package
needs 1.27.110 or later) does the rest, under ray run and in a native binary:
- it serves the package at
ray://app/node_modules/@raylang/elements/, where npm would put it (so the stylesheets are linked by that path); - it injects an import map built from the package's
exportsinto every HTML page ofray://app/(ui.import_map()returns it; a page with its own import map is left as is); ray runandray fetchlink the package into the app'snode_modules/@raylang/elements, so the editor resolves the same imports with their types (ray-elements.d.ts) and goes to their definitions. Addnode_modules/to the app's.gitignore. Three small files make every editor see everything, the same as in a Vite project (examples/lean-apphas them): apackage.jsonthat declares"dependencies": { "@raylang/elements": "*" }(the Deno language server only resolves a package the project declares), atsconfig.jsonwith"allowJs": true,"moduleResolution": "bundler"and thedomlib (read by TypeScript and by Deno), and.vscode/settings.jsonwith"html.customData": ["./node_modules/@raylang/elements/vscode.html-custom-data.json"]for the<ray-*>tags and their attributes.
examples/starter is that app, ready to copy (ray new --frontend takes
npm create vite templates, so it cannot start one without npm).
examples/desktop-app uses the package and the whole bridge.
Contents
elements/web(generated bybuild): a copy ofpackages/elements, the npm package@raylang/elementsas it is published —index.js(every component, and the API:enableRaylang,notify,confirmDialog…),core.js,core/,components/,tokens.css,base.css, the types and the editor data. A page that uses a few components imports@raylang/elements/<name>instead of@raylang/elements, and@raylang/elements/corefor the API alone (enableRaylang,setIcons…).examples/lean-appdoes it, and loads its settings dialog withimport()on first open; its--checkfails if the page defines any other element.elements/version(generated):version.VERSION, the version ofray-elements.toml; the bridge'shelloanswers with it. The other modules import it aselements/version, the only form that also works from an app (finding #142).elements/bridge(plan 1, hand-written).elements/README.md(hand-written),LICENSEandNOTICE(copies of the repository's): the package is the whole directory.buildkeeps the version ofray.tomland of the README's install line.- No
mod.ray: it would make the other modules internal to the capsule, so the package isimport elements/bridge;(andelements/version).
How the assets travel: decided by the spike
The package must work under ray run, in a native binary and in a bundle,
where the app runs from any directory and only what was baked in exists.
- The package embeds them (its own
[native] embed): does not work. A dependency'sembedis ignored, underray runand in a native binary:std/embedonly has the app's own files, also when the package reads it (finding #139). - Generated module (chosen until 0.3.0):
buildwrote the 86 files (about 690 KB of source) as string constants ofelements/assets.ray, andassets.mount("ray-elements")served them atray://app/ray-elements/…. It worked underray runand in a native binary, but the pages imported./ray-elements/components/button/button.js: a path that only existed in the window, that the editor could not resolve, and that looked nothing like the npm names. - The npm package, served by raylang (chosen): raylang 1.27.43 made a
dependency's
[native] embedreach the app (#139), and 1.27.44 added[web] package, which serves a dependency's npm package by its npm name, with an import map, and links it into the app'snode_modules/for the editor.elements/webis nowpackages/elementsas published; the generated module is gone, and the pages read the same as with a bundler.examples/lean-app,examples/starterandexamples/desktop-apppass their--checkunderray run, andexamples/lean-appalso as a native binary run from another directory.
Milestones
- ✅ Spike on the two asset routes: the generated module.
- ✅ Package in
elements/, generated bybuild, withmountand a raylang test that checks every file. - ✅ Starter example and its
--check. - ✅ Publish (both repositories are public; the first index entry comes
with the v0.3.0 tag). What the spike found about dependencies decided how:
- a
git+dependency puts the root of the repository in.ray-deps/<key>and imports it by the key; there is no way to point at a subdirectory (finding #140), and the packages of the index are one repository each, with the modules at the root (greeting, for instance). Soelements/cannot be installed from this repository as it is:git+https://github.com/ray-language/ray-elementswould give the docs site's repository, not the package. - So the package has a repository of its own,
ray-language/elements, published like the packages of the raylang monorepo:ray registry mirror(raylang 1.27.43), run fromelements/on a maintainer's machine at each tag, snapshots the directory into that repository, tags it and adds the version to the index. No deploy key and no CI secret. An app installs it withray add elements(the index) or pins the tag withgit+https://github.com/ray-language/elements@v0.3.0(checked with a copy ofexamples/starteroutside this repository).
- a
- ✅ By the npm name: the package carries
@raylang/elementsas published ([web] package, raylang 1.27.44); the pages import it as with a bundler and the editor resolves it fromnode_modules/.
Later ideas
- Typed HTML from raylang. From the same
component.toml, generate a raylang module for compiled.ray.htmltemplates with typed attributes (variantas an enum), so a misspelled variant fails at compile time. Since raylang 1.27.39 the compiled templates are documented and report clear errors (findings #126, #127 and #131 are resolved), so this no longer waits on raylang; it is the next candidate after plans 1 and 2. - Tokens in raylang. A generated
tokens.ray, so charts drawn by the program, icons, theInfo.plistcolors or a native shell use the same palette as the UI. - UI tests for apps. The webview harness of
tools/as a utility: an app that uses ray-elements drives its own pages in the real webview fromray test. - System notifications.
ray-notificationcould fall back to an OS notification when the window is not focused, butstd/uihas no notification API today.