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
66 lignes · 3741 octetsBrut · Historique

More (<pebble-more>)

A feed's "load more" link. Without JS it is a link to the next page; with JS the next page loads into the list as the link nears the viewport, or when it is activated. Self-registering, light DOM, no words of its own.

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

<ol id="fil" data-feed-items>
  <li>…</li>
  …
</ol>
<pebble-more for="fil" announce="{n} de plus">
  <a href="/fil?apres=CURSOR" data-more>Voir plus</a>
</pebble-more>

The markup

wherewhatnotes
host forthe id of the list to load intowithout it: the previous sibling if it is [data-feed-items], else the [data-feed-items] inside the previous sibling
host announcethe live-region text after a load, {n} = items addeddefault {n} more: set it in the page's language ({n} de plus)
a[data-more]the link to the next pageits href is fetched

The endpoint

GET <href> with Accept: text/html and X-Fragment: 1 (cookies: credentials: "same-origin"). The answer is HTML: a fragment or a whole page, either works. The element reads two things from it:

  • [data-feed-items]: its children are appended to the page's list, in order;
  • pebble-more a[data-more]: the next link, which replaces the current one. No such link means the end of the feed.

So the cheapest server answer for X-Fragment: 1 is:

<ol data-feed-items><li>…</li>…</ol>
<pebble-more><a href="/fil?apres=NEXT" data-more>Voir plus</a></pebble-more>   <!-- omitted on the last page -->

A non-2xx answer, or one without [data-feed-items], is a failure.

Behavior

  • An IntersectionObserver watches the link with a 600 px root margin (ROOT_MARGIN); when it comes within reach, the next page loads. A page short enough to keep the new link within reach loads the one after at once.
  • Activating the link (click, tap, Enter) loads too, and cancels the navigation. A modified click (new tab) is the browser's.
  • One fetch at a time. While it runs the link is hidden and the host has data-loading (the sheet shows a spinner).
  • A failure shows the link again and marks the host data-error; the observer stops loading by itself until the link is activated by hand, so a failing server is not hammered.
  • The end of the feed removes the link and marks the host data-done. The host stays, empty but for its live region, so the last announcement is heard.
  • Each load writes the announce text into a polite live region (role="status", visually hidden, part status); the same text twice in a row is still announced.
  • When the link had focus (a keyboard user), focus moves to the first new item (given tabindex="-1" if it has none).
  • The URL is never changed.
  • Scripts in the answer do not run (DOMParser documents never run them). Custom elements in it (a <pebble-vote> per item, say) upgrade as they are appended.

State hooks

On the host: [data-loading], [data-error], [data-done]. The link carries hidden while loading.

CSS hooks

The sheet ships as more.css (raw: dist/more.css, published as @pebble/more/more.css); the element adopts it when it upgrades. Dark defaults from prefers-color-scheme.

--pebble-more-ink · --pebble-more-surface · --pebble-more-border · --pebble-more-error (the link's border after a failure) · --pebble-more-focus (falls back to --accent; also the spinner's arc) · --pebble-more-padding (1rem, above and below) · --pebble-more-size (44px: the link's minimum height) · --pebble-more-max-width (24rem) · --pebble-more-radius (999px) · --pebble-more-spinner-size (1.75rem)

Exports

ROOT_MARGIN.