Getting started
vangle turns a static HTML page into a reactive document. Readers drag numbers in the text or inside formulas, and everything that depends on them updates as they drag.
It's a modern take on Bret Victor's Tangle.
You describe the document with a handful of custom HTML elements. You don't write an
update() function, and there's no build step and no dependencies.
Install
Copy tangle.js and tangle.css into your site, then add two lines to
the page's <head>, or to your blog's layout template:
<link rel="stylesheet" href="/tangle.css">
<script type="module" src="/tangle.js"></script>
Or load both from jsDelivr instead of copying them:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/koaning/vangle@v0.1.0/tangle.min.css">
<script type="module" src="https://cdn.jsdelivr.net/gh/koaning/vangle@v0.1.0/tangle.min.js"></script>
That's all. KaTeX is only downloaded on pages that contain a <t-math> formula.
Your first reactive paragraph
When you eat
Here's what each element does:
<t-num name="cookies">declares a variable calledcookiesand shows it as a draggable number. Its text,3, is the starting value.<t-let name="calories" expr="cookies * 50">defines a computed variable. It's invisible.<t-out expr="…">shows the result of an expression and keeps it up to date.
Try dragging the number. You can also click it to type a value, or focus it and press the arrow keys.
How it works
Variables
Every value in the document is a named variable. A variable is declared by
<t-num>,
<t-var>,
<t-choice>,
<t-select> or
<t-text> (values the reader can change), or by
<t-let> and <t-call> (values computed from other variables).
Names follow JavaScript identifier rules: rate, x2, total_cost.
Reactivity
Expressions are plain JavaScript (see Expressions & formats). While an expression runs, vangle records which variables it reads. When one of them changes, only the outputs, computed values and formulas that depend on it update. Changes are batched, so a drag that moves several values redraws each output once.
Order doesn't matter
You can use a variable before the element that declares it: in the example above,
calories is used before the <t-let> that defines it. This lets you
put declarations wherever reads best, for example next to the formula that uses them.
Scopes
By default, every element on the page shares one set of variables. Wrap part of the page in
<t-scope> to give it its own. Each example in these docs
sits in its own scope, so they don't interfere with each other.
Without JavaScript
The markup degrades gracefully. A <t-num> shows its starting value, and a
<t-out> shows whatever fallback text you put inside it (the 150 above).
RSS readers and no-JS browsers still get a readable sentence.
The elements
| Element | Use it for |
|---|---|
<t-num> | A number the reader can drag, type or step with the keyboard. |
<t-var> | Declaring a variable without showing it, for example one used only in a formula. |
<t-let> | A named value computed from other variables. |
<t-out> | Showing the live result of an expression in the text. |
<t-choice> | A word the reader clicks to cycle through options, or a toggle. |
<t-select> | A word the reader clicks to pick from a searchable menu of options. |
<t-text> | A piece of text the reader clicks to rewrite. |
<t-call> | A function call whose arguments the reader can change, with the result as a variable. |
<t-tag> | An HTML tag whose attributes the reader can change, with the live element below it. |
<t-math> | A KaTeX formula with draggable parameters and live results. |
<t-graph> | A network of nodes and edges that changes with the variables, with the picked and hovered node as variables. |
<t-paint> | A canvas the reader draws on, with the drawing as a variable. |
<t-sketch> | An infinite whiteboard of vector strokes, with the board as a JSON variable. |
<t-scope> | Giving part of the page its own variables. |
For charts and other custom logic, the same variables are available from JavaScript. See Updating a chart for a walkthrough.