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.
Vote (<pebble-vote>)
An up/down vote over the server's two forms. The forms are the vote without JS (each posts its side; the server answers with a page); the element makes the round trip feel instant: it paints the press at once, posts it with fetch and reconciles with the server's tally. Self-registering, light DOM, no words of its own.
<link rel="stylesheet" href="/static/pebble/vote.css"> <!-- the look before (or without) JS -->
<script type="module" src="/static/pebble/vote.js"></script>
<pebble-vote error="Le vote n’a pas pu être enregistré.">
<form method="post" action="/suggestions/42/vote">
<input type="hidden" name="valeur" value="1">
<input type="hidden" name="csrf" value="…">
<button aria-pressed="false" data-count="12" data-label="Cadeau" aria-label="Cadeau (12)">
🎁 <span data-count-label>12</span>
</button>
</form>
<form method="post" action="/suggestions/42/vote">
<input type="hidden" name="valeur" value="-1">
<input type="hidden" name="csrf" value="…">
<button aria-pressed="false" data-count="3" data-label="Charbon" aria-label="Charbon (3)">
<svg aria-hidden="true">…</svg> <span data-count-label>3</span>
</button>
</form>
</pebble-vote>
<pebble-toaster></pebble-toaster> <!-- once per page: where errors are shown -->
Load the script as a module (deferred): the element reads its forms when it upgrades.
The markup
| where | what | notes |
|---|---|---|
host field | the name of the input carrying the side | default valeur |
host error | the toast when the server gives no message | default Your vote could not be saved.: set it in the page's language |
each <form> | action (posted to), an input named field, one <button> | the whole form is posted: CSRF fields travel as they would natively |
input field | 1 (or any positive number) for the up side, -1 (negative) for the down side | exactly one of each; otherwise the element leaves the forms alone (dev warning) |
<button> aria-pressed | "true" on the reader's current side | read at upgrade; the element keeps it |
<button> data-count | the side's count | read at upgrade; the element keeps it |
<button> data-label | the side's words: aria-label becomes "<data-label> (n)" | falls back to the server's aria-label minus its (n) |
[data-count-label] inside the button | the visible count | optional; its text follows the count |
The endpoint
The same POST the form makes, with the same body: URL-encoded (application/x-www-form-urlencoded), or multipart/form-data only when the form's enctype says so (0.1.1; 0.1.0 always sent multipart, which servers reading ordinary form fields did not see), plus Accept: application/json (and the page's cookies: credentials: "same-origin"). The server answers:
| answer | the element |
|---|---|
200 + {"up": n, "down": n, "mine": 1 | -1 | 0} | paints the server's truth over its guess |
any other 2xx (no JSON, other JSON) | keeps its guess |
403, or any answer with {"captcha": true} | undoes its guess and calls form.submit(): the native post, so the server's page can ask for its captcha |
any other status, optionally {"error": "…"} | undoes its guess and shows an error toast: the server's error, else the host's error |
| no answer (network) | undoes its guess, toast with the host's error |
mine is the reader's side after the vote. The server is the judge of toggling: the element's guess (pressing your side takes the vote back, pressing the other moves it) is replaced by whatever the server answers.
Behavior
- A press is painted before the request leaves: counts,
aria-pressed,aria-label,[data-count-label], and the host'sdata-mine. - One vote in flight at a time: while it runs the host has
data-busyandaria-busy="true"; further presses are cancelled and ignored (a double tap never posts twice). - The button just pressed gets the class
bouncefor 400 ms (BOUNCE_MS); the sheet animates it, and nothing moves underprefers-reduced-motion. - Errors go to
<pebble-toaster>as its inboundtoastwindow event ({message, type: "error"}), dispatched directly rather than through@pebble/toaster's helper, which would bundle and register the whole toaster here. The page provides one<pebble-toaster>. - Other forms inside the host are left alone.
State hooks
[data-side="up" | "down"] on each button (written by the element; the server may write it too so the side's colour shows without JS), [aria-pressed="true"] on the chosen one, .bounce on the one just pressed; [data-mine="1" | "-1" | "0"] and [data-busy] on the host.
CSS hooks
The sheet ships as vote.css (raw: dist/vote.css, in the published package @pebble/vote/vote.css). The element adopts the same rules when it upgrades; link it as well for the no-JS look. Every value is a custom property with a fallback; dark defaults come from prefers-color-scheme.
--pebble-vote-ink · --pebble-vote-surface · --pebble-vote-border · --pebble-vote-up (the up side's pressed colour) · --pebble-vote-up-wash (its pressed background) · --pebble-vote-down · --pebble-vote-down-wash · --pebble-vote-focus (falls back to --accent) · --pebble-vote-gap (0.5rem) · --pebble-vote-size (44px: the minimum target) · --pebble-vote-padding (0.875rem) · --pebble-vote-radius (999px) · --pebble-vote-font-size (1rem)
An <svg> or <img> in a button is sized to 1.25em.
Exports
press(tally, side) and fromAnswer(answer) (the arithmetic, pure), the Tally and Mine types, BOUNCE_MS.