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";
import "capsomer/tokens.css";
import "capsomer/like-button.css";
import { LikeButton } from "capsomer/react/like-button";
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
| Prop | Type | Required | Does |
|---|
liked | boolean | Yes | |
|---|
count | number | Yes | |
|---|
onChange | (liked: boolean) => void | | Called with the new state. The page does the write and passes the answer back as liked and count. |
|---|
label | string | | The thing liked, for the name ("Like Field notes"). The name stays the same pressed or not. |
|---|
pending | boolean | | |
|---|
disabled | boolean | | Signed out: the button shows the count and cannot be pressed. |
|---|
error | ReactNode | | |
|---|
Parts and data attributes
| Class | Is |
|---|
.cap-like | its own class |
|---|
.cap-like-button | the root |
|---|
.cap-like-error | its own class |
|---|
| State | Read from |
|---|
:disabled | the platform |
|---|
:hover | the platform |
|---|
[aria-busy="true"] | an attribute |
|---|
[aria-disabled="true"] | an attribute |
|---|
[aria-pressed="true"] | an attribute |
|---|
Accessibility
| Key | Does |
|---|
| Enter, Space | Likes or unlikes |
- A 24px target at least (
--target). - Pressed:
--accent-text and a filled heart; forced colors: a Highlight edge.