tobogganSe connecter

library

Prebuilt pebble components: drop-in custom elements, no compiler needed

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Dernier runAucun
0Étoiles
0Forks
2Issues
58 lignes · 4272 octetsBrut · Historique

Combobox (<pebble-combobox>)

Suggestions under a search form's field as the reader types, from the server's HTML, with the ARIA combobox pattern (list autocomplete, focus stays in the field). Without JS the form is a plain GET search. Self-registering, light DOM, no words of its own.

<link rel="stylesheet" href="/static/pebble/combobox.css">
<script type="module" src="/static/pebble/combobox.js"></script>

<pebble-combobox src="/proposer/film/suggestions" min="2">
  <form method="get" action="/proposer/film">
    <label for="q">Un film ou une série</label>
    <input id="q" name="q" type="search" autocomplete="off">
    <button>Chercher</button>
  </form>
</pebble-combobox>

The markup

wherewhatnotes
host srcthe suggestions endpointrequired; without it the element leaves the form alone (dev warning)
host mincharacters needed before suggestions are fetcheddefault 2
the first non-hidden <input>the fieldits name is the query parameter (default q); its <label> names the listbox

The endpoint

GET <src>?<name>=<query> (an existing query string on src is kept), Accept: text/html, cookies with credentials: "same-origin". The query is the field's value, trimmed. The answer is an HTML fragment whose first list's <li> items become the options:

<ul>
  <li><a href="/titres/KEY"><img src="/affiches/KEY.jpg" alt=""> Maman, j’ai raté l’avion <small>1990</small></a></li>
  …
</ul>

Each item should hold one link: Enter on the active option, and a click, follow it. An empty list (or a non-2xx answer) closes the popup. Keep the answer small (8–10 items) and the posters tiny: it is fetched as the reader types.

Behavior

  • The request leaves 250 ms (DEBOUNCE_MS) after the last keystroke, once the trimmed value has min characters. Typing on restarts the wait and aborts a request in flight; only the last query's answer is shown. Under min, the popup closes.
  • The field gets role="combobox", aria-autocomplete="list", aria-expanded, aria-controls (the listbox) and aria-activedescendant (the active option), and autocomplete="off". Each item gets role="option", an id and aria-selected; links and controls inside it leave the tab order. The listbox is labelled by the field's <label> (given an id if it has none). All of it is put back as it was when the element disconnects.
  • Keys in the field: ArrowDown / ArrowUp move the active option, wrapping, and open the popup again on results already fetched; Enter follows the active option's link (with none active, the form submits as usual); Escape closes.
  • A press outside the element, focus leaving it, or submitting the form closes the popup. A press inside the popup keeps focus in the field, and the click then follows the option's link.

State hooks

On the host: [data-open] (the popup shows), [data-loading] (a request is in flight). On the active option: [data-active] and aria-selected="true". The popup (part popup) carries hidden when closed; the listbox is part listbox.

CSS hooks

The sheet ships as combobox.css (raw: dist/combobox.css, published as @pebble/combobox/combobox.css); the element adopts it when it upgrades. It positions the popup under the form, full width, scrolling past max-height; lays each option's link out as a 44 px row; sizes an <img> in it as a 2:3 poster and mutes a <small> or <time>. It gives the field and the button a 44 px minimum height and leaves the rest of the form to the page. Dark defaults from prefers-color-scheme.

--pebble-combobox-ink · --pebble-combobox-muted · --pebble-combobox-surface · --pebble-combobox-border · --pebble-combobox-active (the active and hovered option's background) · --pebble-combobox-shadow · --pebble-combobox-focus (the active option's outline; falls back to --accent) · --pebble-combobox-size (44px) · --pebble-combobox-offset (0.25rem under the form) · --pebble-combobox-z-index (50) · --pebble-combobox-max-height (min(60vh, 26rem)) · --pebble-combobox-radius (0.5rem) · --pebble-combobox-poster-width (2.5rem)

Exports

DEBOUNCE_MS, optionsOf(html) (the first list's items of a fragment).