clay.js
  1. ClayJS
  2. Reference
  3. Plugins
  4. like

like

A like button on any element, counted by the host, with nothing the plugin draws ever saved.

A like button for any element. The host keeps the counts, so every visitor sees the same number, and the page saves none of it.

Load it#

Ask for the plugin by name:

HTML
<script src="https://clayjs.com/v1/clay.js?plugins=like"></script>

The button#

Give an element a like attribute and it becomes a like button. The value names the item, and an empty value falls back to the element’s id.

HTML
<button like="post-1">♥</button>
<button like id="post-2">♥</button>

The host finds its items by reading the like attributes in the saved file, so an element starts counting once the page is saved with it. A new element added to the page is painted on the next animation frame when the host already lists its item; otherwise the plugin reads the totals again, at most every few seconds, and the item counts once the page is saved with it. There is no rules tag and no script to write.

The plugin keeps a random visitor id and the list of items that visitor liked in localStorage, so the button starts out in the right state on the next visit. When the browser blocks storage, the visitor id and the liked items stay in memory for the tab: the buttons keep working, and the next visit starts over.

The count and the liked word#

After the totals arrive, the plugin appends one span.like-count to the element, marked clay="editor-ui", and writes the number into it. It is the only thing the plugin draws, and it is never saved.

A liked element also carries the word liked in its ephemeral attribute, which is per tab state and is never saved either:

HTML
<button like="post-1" ephemeral="liked">♥<span class="like-count" clay="editor-ui">3</span></button>

Style it with the attribute selector, or with the Tailwind variant ephemeral-liked::

CSS
[like] .like-count { margin-left: 0.25rem; font-size: 0.875em; }
[ephemeral~="liked"] { color: crimson; }
HTML
<button like="post-1" class="text-zinc-500 ephemeral-liked:text-red-600">♥</button>

The plugin never draws the heart or the word. Both come from your markup and your CSS.

Events#

Both events fire on the element that was clicked, and bubble.

  • clay:like fires after a toggle the host accepted, with { item, on, count }.
  • clay:like-failed fires when a toggle fails with a message (a network error, a refusal, a host error), with { item, error }. A click the plugin ignores, because the host has no likes or the element names no item, sends nothing and fires neither event. Cancel clay:like-failed to show your own message: the plugin otherwise passes the error to clay.toast when a toast is available.

The API#

clay.like is there for your own buttons and readouts:

  • await clay.like.toggle(item) toggles this visitor’s like on one item and resolves { ok, on, count, error }. It never rejects, matching clay.message.send and clay.upload.
  • await clay.like.refresh() reads the totals from the host again and repaints every element.
  • clay.like.counts() returns the last totals as { item: { like: number } }, or null before the first read.

On Hyperclay, and elsewhere#

Counts live on the host, so the plugin needs one that offers likes, such as Hyperclay. There the totals survive renaming or moving the page, and a private page has none.

Everywhere else the plugin stays inert: a file opened from disk, or a page in HTML Clay, answers the counts request with no counts, the elements get no span, and a click sends nothing. Your markup and CSS still work, so the page looks the same without the numbers.