<t-num>

A number the reader can drag, type or step with the keyboard. This is the classic Tangle control.

A 4 by 3 metre room has m² of floor.

The element's text is the starting value, and also what readers see without JavaScript. You can use the value attribute instead, which is handy when a format adds symbols, as in <t-num name="price" value="1000" format="$%,d"></t-num>.

Attributes

AttributeDefaultDescription
namerequiredThe variable this number shows and changes.
valuetext contentThe starting value. Without a value or text, the element is just another view of a variable declared elsewhere (see below).
min, maxunboundedThe range the reader can drag or type within.
step1The increment for dragging and the arrow keys. It also sets the default number of decimals shown.
formatfrom stepHow the number is displayed. See formats.
coloraccent colorA CSS color, or two ("#246bce #75a7ff") for light and dark mode. Every place the variable appears uses it, formulas included.
labelthe nameThe accessible name screen readers announce.
pixels-per-step5How far the pointer must travel to change the value by one step. Raise it for finer control.
previousA variable to hold the value from before the last drag or edit (starts at the initial value, and updates when a gesture finishes with a new value).

Try them out: change the attributes in the code, then drag the number below it. This uses <t-tag>.

0 20 1 %s 5 4

Interaction

InputEffect
Drag left or rightChanges the value by one step every pixels-per-step pixels. Works with touch too.
Click without draggingOpens a small field to type a value. The value is snapped to the step and clamped to the range.
→ ↑ / ← ↓One step up or down. Hold Shift for ten steps.
Home / EndJumps to min or max, if set.
Enter or SpaceOpens the field to type a value.

In the typing field, Enter confirms and Esc cancels; clicking away also confirms. The field accepts expressions, so readers can type w + 1 or 2 * Math.PI.

Hovering a number highlights every place its variable appears on the page, in text and in formulas.

Several views of one variable

Only the first element that declares a variable sets its starting value. Any other <t-num> with the same name, and with no value of its own, is another handle on the same variable:

Set the budget to .

… three paragraphs later …

With your budget, you can buy books.

Drag sensitivity

A drag changes the value by one step every pixels-per-step pixels. If a range has many steps, lower pixels-per-step so a comfortable drag still covers it. Raise it when you want precise, slow adjustments.

A learning rate of 0.01 over 100 steps moves the weights by .

Previous value

Give <t-num> a previous attribute to keep the value from before the last drag or typed edit. While you drag, previous stays put; once you let go, it updates to where the drag started:

Drag 80: was ().

Notes