Save for later, kept in this browser only, as a toggle whose bookmark fills when saved.
<div class="cap-specimen-row">
<button type="button" class="cap-chip cap-save" data-size="sm" id="save-a" data-cap="save-toggle" data-key="cap-specimen-saved" data-id="spring-update" aria-pressed="false"><svg class="cap-save-icon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false"><path d="M4 2.5h8v11L8 10.5l-4 3z"/></svg><span class="cap-save-label">Save</span></button>
<button type="button" class="cap-chip cap-save" data-size="sm" id="save-b" data-cap="save-toggle" data-key="cap-specimen-saved" data-id="spring-update" aria-pressed="false"><svg class="cap-save-icon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false"><path d="M4 2.5h8v11L8 10.5l-4 3z"/></svg><span class="cap-save-label">Save</span></button>
</div>
Installation
// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/save-toggle.css";
import { enhance } from "capsomer/behavior/save-toggle";
enhance();
import "capsomer/tokens.css";
import "capsomer/save-toggle.css";
import { SaveToggle } from "capsomer/react/save-toggle";
Usage
<button type="button" class="cap-chip cap-save" data-size="sm" data-cap="save-toggle" data-key="example-saved" data-id="spring-update" aria-pressed="false">
<svg class="cap-save-icon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false"><path d="M4 2.5h8v11L8 10.5l-4 3z"/></svg>
<span class="cap-save-label">Save</span>
</button>
Examples
Saved, and under the pointer
<div class="cap-specimen-row">
<button type="button" class="cap-chip cap-save" data-size="sm" aria-pressed="true"><svg class="cap-save-icon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false"><path d="M4 2.5h8v11L8 10.5l-4 3z"/></svg><span class="cap-save-label">Save</span></button>
<button type="button" class="cap-chip cap-save" data-size="sm" aria-pressed="false"><svg class="cap-save-icon" viewBox="0 0 16 16" width="16" height="16" aria-hidden="true" focusable="false"><path d="M4 2.5h8v11L8 10.5l-4 3z"/></svg><span class="cap-save-label">Save</span></button>
</div>
API reference
SaveToggle
| Prop | Type | Required | Does |
|---|
storageKey | string | Yes | The localStorage key the site keeps its saved ids under ("example-saved"). |
|---|
id | string | Yes | This item's id (a post's slug). |
|---|
label | string | | The one label; aria-pressed and the filled bookmark carry whether it is saved. |
|---|
Functions
| Function | Signature | Does |
|---|
savedRaw | (key: string): string | The stored list, or "[]" when nothing is stored. |
|---|
savedIds | (raw: string): string[] | |
|---|
isSaved | (raw: string, id: string): boolean | |
|---|
toggleSaved | (key: string, id: string): boolean | Adds or removes id, and tells every listener. Returns whether it is now saved. |
|---|
subscribeSaved | (onChange: () => void): () => void | For useSyncExternalStore: this tab's changes, and other tabs' through storage. |
|---|
paint | (button: HTMLElement, saved: boolean): void | The button's state: aria-pressed, which fills the bookmark. Its label stays the same (APG button: a toggle's name does not change with its state). |
|---|
enhance | (root?: ParentNode): () => void | A page with no React: every button.cap-save[data-cap="save-toggle"] with data-key and data-id. |
|---|
Parts and data attributes
| Class | Is |
|---|
.cap-save | its own class |
|---|
.cap-save-icon | the root |
|---|
| State | Read from |
|---|
[aria-pressed="true"] | an attribute |
|---|
Accessibility
| Key | Does |
|---|
| Tab | Moves to the button |
| Enter, Space | Saves or unsaves |
aria-pressed carries the state; the visible words change with it.- The pill is at least
--target tall. - In forced colors the saved bookmark is filled in ButtonText.