Installation
REALM loads from a CDN, so there is nothing to install or build. You need a text editor and a web browser.
Add REALM from the CDN
Put both tags in the <head> of your HTML file:
<link rel="stylesheet" href="https://realm.js.org/dist/realm.css">
<script type="module" src="https://realm.js.org/dist/realm.js"></script>realm.js starts REALM when it loads. It defines every <custom-element> already in the page, watches for new ones, and registers <import-element>.
realm.css should load before anything renders. It does three things:
- hides
<custom-element>definitions and<import-element>tags, - lays out
<is-visible>,<is-hidden>and<repeat-list>withdisplay: contents, - hides undefined custom elements while REALM is starting or importing, so a page never flashes unstyled component content.
The last rule only applies while the page still has an unread <custom-element>, while <import-element> is not yet defined (REALM has not started), or while an <import-element> is loading (import-element:state(loading)). Once REALM is done, tags it never defines, such as Angular's <app-root> or another library's elements, are visible again:
:root:has(custom-element, import-element:not(:defined), import-element:state(loading)) :not(:defined) {
visibility: hidden;
}A lazy import (loading="lazy") does not count as loading while it waits for its tag, so elements it will define show their light-DOM content until it starts.
Add REALM UI
The @realmorg/ui package is a set of ready-made ui-* components and a theme. Load the theme once per page, after REALM itself. Then import the whole kit with one line:
<link rel="stylesheet" href="https://realm.js.org/dist/ui/theme.css">
<import-element from="https://realm.js.org/dist/ui/ui.html"></import-element>Or import only the components you use. Each one is a file in components/:
<import-element from="https://realm.js.org/dist/ui/components/button.html"></import-element>
<import-element from="https://realm.js.org/dist/ui/components/card.html"></import-element>Each file brings its own dependencies. sheet.html, for example, imports dialog.html for you. See Components.
A complete page
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My REALM page</title>
<link rel="stylesheet" href="https://realm.js.org/dist/realm.css">
<link rel="stylesheet" href="https://realm.js.org/dist/ui/theme.css">
<script type="module" src="https://realm.js.org/dist/realm.js"></script>
</head>
<body>
<import-element from="https://realm.js.org/dist/ui/ui.html"></import-element>
<ui-button>Hello, REALM</ui-button>
</body>
</html>Save it as index.html and open it in your browser.
Pin a version
https://realm.js.org/dist/ always serves the latest build, so it cannot be pinned. In production, load exact versions of the npm packages, for example from jsDelivr:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@realmorg/realm@2.0.0-alpha.6/dist/realm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@realmorg/ui@2.0.0-alpha.5/dist/theme.css">
<script type="module" src="https://cdn.jsdelivr.net/npm/@realmorg/realm@2.0.0-alpha.6/dist/realm.js"></script>
<import-element from="https://cdn.jsdelivr.net/npm/@realmorg/ui@2.0.0-alpha.5/dist/ui.html"></import-element>
<import-element from="https://cdn.jsdelivr.net/npm/@realmorg/unicons@2.0.0-alpha.4/dist/unicons.html"></import-element>These are the current versions. Each release lists its changes in the package's CHANGELOG.md.
Hosting the files yourself
<import-element> fetches component files with fetch(), so a file on another domain needs a CORS header that allows your page. realm.js.org and jsDelivr send Access-Control-Allow-Origin: * on every file, which is why the tags above work from any site.
To host REALM yourself, install the packages from npm and serve their dist/ (and, for REALM UI, components/) folders from your own domain, or from another domain that sends that header.
npm install @realmorg/realm@alpha @realmorg/ui@alphaSharing JavaScript between components
Shared JavaScript, such as a store, is a plain module file. Import it from a component script with a path the browser can load:
<custom-element name="cart-badge">
<script type="module">
import { cart } from "/state/cart.js";
</script>
<span><slot name="#cart.items.length">0</slot></span>
</custom-element>To write import { defineStore } from "@realmorg/realm" in a module the browser loads directly, add an import map that points at the same file the page loads, so there is one runtime:
<script type="importmap">
{ "imports": { "@realmorg/realm": "https://realm.js.org/dist/realm.js" } }
</script>Serving pages
A page with inline definitions (a <custom-element> written directly in the page) works when you open the file straight from disk.
<import-element> loads component files with fetch, so pages that import your own component files must be served over HTTP rather than opened from file://. Any static file server works: your editor's live preview, python3 -m http.server, or the static hosting you deploy to.
Optional: Vite plugin
Load REALM from the CDN in a Vite project too. Component files are plain HTML that REALM fetches at runtime, so serve them as static files from public/.
The Vite plugin is an optional dev tool. It pre-assembles components at build time for pages under a strict Content Security Policy, and writes a custom-elements.json manifest for editors.
Next
Continue with the Quick start.
Updated · Edit this page on GitHub · View as Markdown