Tucano

Slider

One value, or a range with a minimum and a maximum. Both are the native <input type="range"> with a new look — in the range, two of them on the same track. The name, the POST, the reset, the arrow keys and the screen reader announcement stay the browser's; the script paints the chosen stretch, keeps the minimum from passing the maximum and writes the value out.

Examples

The same <input>, with different attributes.

Decimal step

The decimals in the text come from the step: with 0.5, one.

Range without labels on the ends

With no <label> or aria-label, the ends get "Minimum" and "Maximum".

Age

Next to a field

The box is as tall as a field, with the track in the middle: the row stays aligned.

Disabled

disabled on the native input; in the range, on both.

Usage

Single slider: the class and the attribute on the <input> itself. Range: a container with two, each posting under its own name.

<input type="range" class="tuc-slider" data-tuc-slider
       name="discount" min="0" max="30" value="10">

<div class="tuc-slider is-range" data-tuc-slider data-currency="USD" data-output="#price">
  <input type="range" name="price_min" min="0" max="5000" step="50" value="800">
  <input type="range" name="price_max" min="0" max="5000" step="50" value="3200">
</div>
<output id="price"></output>

Both ends need the same min, max and step. The form receives price_min=800&price_max=3200 — in a GET filter, the URL comes back filled in. data-output points to where the value is written; data-currency writes it as money, and the decimals come from the step: a step of 1 writes "$800", not "$800.00".

Why the class comes from the template

That way the slider is born with the right height and track on the first frame, with no jump when JavaScript arrives. Without the script it works and posts; only the chosen stretch misses the accent color.

With Django

The single slider's widget is a NumberInput with another type. The range is two fields, rendered inside the same container.

class RangeInput(forms.NumberInput):
    input_type = "range"

class ProductFilter(forms.Form):
    attrs = {"min": 0, "max": 5000, "step": 50}
    price_min = forms.IntegerField(initial=0, widget=RangeInput(attrs=attrs))
    price_max = forms.IntegerField(initial=5000, widget=RangeInput(attrs=attrs))
<div class="tuc-slider is-range" data-tuc-slider data-currency="USD" data-output="#price">
  {{ form.price_min }}
  {{ form.price_max }}
</div>
<output id="price"></output>

In JavaScript

const price = new Tucano.Slider('#price-range', {
  currency: 'USD',
  output: '#price',
  onChange: ([min, max]) => console.log(min, max),
});

price.getValue();                        // [800, 3200]
price.setValue([1000, 2000]);            // sorts the pair and emits once
price.setValue([0, 5000], { silent: true });

new Tucano.Slider('#rating', {
  format: (value) => `${value} stars`,   // wins over currency
});

While dragging, as in the color picker, tucano:change fires on every movement — which is what a live preview needs — and the native change only once, on release: that is the one hx-trigger and a form filter should listen to. From the keyboard, both fire on each key.

document.addEventListener('tucano:change', (e) => {
  if (!e.target.matches('.tuc-slider')) return;
  e.detail.value;      // a number on the single slider, [minimum, maximum] on the range
  e.detail.instance;   // the Slider
});

Keyboard and accessibility

The keyboard is the native <input type="range">'s, on each end. In the range, each end is a Tab stop.

KeyAction
← → ↑ ↓One step either way
PageUp PageDownA bigger step, the browser's
Home EndThe minimum and the maximum — in the range, up to the other end

Each end gets aria-valuetext with the written value, so the screen reader says "$800" instead of "800". In the range, an end with no label gets a name of its own — "Mínimo" and "Máximo" by default, changed with Tucano.setTexts({ slider: … }), as this site does. In the range the pointer is handled by the component: a click on the track moves the nearest end, and with both in the same place the first movement decides which one moves.

API

Generated from the code on every build — if something is not here, it does not exist.

Markup
[data-tuc-slider]
In JS
new Tucano.Slider(alvo, opcoes)
Attributes
data-currency data-locale data-output
Methods
getValue setValue destroy
Events
tucano:change

Options

The notes in this table come from comments in the source code, which are written in Portuguese.

OptionDefaultWhat for
outputnullelemento ou seletor onde o valor aparece escrito; no intervalo, "min – max"
currencynullcodigo ISO 4217 ('BRL'): escreve o valor como dinheiro
localeundefineddefault: lang do documento ou do navegador
formatnull(value) => texto; vence currency. Vale para o output e para o leitor de tela
onChangenull(value, detail) a cada valor novo: no arrasto, a cada movimento; o `change` nativo sai so ao soltar