Skip to content

Like button

A toggle button with a heart and its count, pressed when the reader has liked the thing.

<div class="cap-specimen-row">
  <div class="cap-like" id="lk-off"><button type="button" class="cap-like-button" aria-pressed="false"><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 20s-7.5-4.6-7.5-10A4.5 4.5 0 0 1 12 7.5 4.5 4.5 0 0 1 19.5 10c0 5.4-7.5 10-7.5 10z"/></svg><span class="cap-sr-only">Like Field notes, </span><span>12</span></button></div>
  <div class="cap-like" id="lk-on"><button type="button" class="cap-like-button" aria-pressed="true"><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 20s-7.5-4.6-7.5-10A4.5 4.5 0 0 1 12 7.5 4.5 4.5 0 0 1 19.5 10c0 5.4-7.5 10-7.5 10z"/></svg><span class="cap-sr-only">Like Field notes, </span><span>13</span></button></div>
  <div class="cap-like" id="lk-hover"><button type="button" class="cap-like-button" aria-pressed="false"><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 20s-7.5-4.6-7.5-10A4.5 4.5 0 0 1 12 7.5 4.5 4.5 0 0 1 19.5 10c0 5.4-7.5 10-7.5 10z"/></svg><span class="cap-sr-only">Like Field notes, </span><span>12</span></button></div>
  <div class="cap-like" id="lk-signed-out"><button type="button" class="cap-like-button" aria-pressed="false" disabled><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 20s-7.5-4.6-7.5-10A4.5 4.5 0 0 1 12 7.5 4.5 4.5 0 0 1 19.5 10c0 5.4-7.5 10-7.5 10z"/></svg><span class="cap-sr-only">Like Field notes, </span><span>12</span></button></div>
  <div class="cap-like" id="lk-saving"><button type="button" class="cap-like-button" aria-pressed="true" aria-busy="true" aria-disabled="true"><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 20s-7.5-4.6-7.5-10A4.5 4.5 0 0 1 12 7.5 4.5 4.5 0 0 1 19.5 10c0 5.4-7.5 10-7.5 10z"/></svg><span class="cap-sr-only">Like Field notes, </span><span>13</span></button></div>
  <div class="cap-like" id="lk-failed"><button type="button" class="cap-like-button" aria-pressed="false"><svg class="cap-icon" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="M12 20s-7.5-4.6-7.5-10A4.5 4.5 0 0 1 12 7.5 4.5 4.5 0 0 1 19.5 10c0 5.4-7.5 10-7.5 10z"/></svg><span class="cap-sr-only">Like Field notes, </span><span>12</span></button><p class="cap-like-error" role="alert">That did not save. Try again.</p></div>
</div>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/like-button.css";

Usage

<div class="cap-like">
  <button type="button" class="cap-like-button" aria-pressed="true">
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="..."/></svg>
    <span class="cap-sr-only">Like Field notes, </span><span>12</span>
  </button>
  <p class="cap-like-error" role="alert">That did not save. Try again.</p>   <!-- on failure -->
</div>

API reference

LikeButton

PropTypeRequiredDoes
likedbooleanYes
countnumberYes
onChange(liked: boolean) => voidCalled with the new state. The page does the write and passes the answer back as liked and count.
labelstringThe thing liked, for the name ("Like Field notes"). The name stays the same pressed or not.
pendingboolean
disabledbooleanSigned out: the button shows the count and cannot be pressed.
errorReactNode

Parts and data attributes

ClassIs
.cap-likeits own class
.cap-like-buttonthe root
.cap-like-errorits own class
StateRead from
:disabledthe platform
:hoverthe platform
[aria-busy="true"]an attribute
[aria-disabled="true"]an attribute
[aria-pressed="true"]an attribute

Accessibility

KeyDoes
Enter, SpaceLikes or unlikes
  • A 24px target at least (--target).
  • Pressed: --accent-text and a filled heart; forced colors: a Highlight edge.