Skip to content

Save toggle

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();

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

PropTypeRequiredDoes
storageKeystringYesThe localStorage key the site keeps its saved ids under ("example-saved").
idstringYesThis item's id (a post's slug).
labelstringThe one label; aria-pressed and the filled bookmark carry whether it is saved.

Functions

FunctionSignatureDoes
savedRaw(key: string): stringThe stored list, or "[]" when nothing is stored.
savedIds(raw: string): string[]
isSaved(raw: string, id: string): boolean
toggleSaved(key: string, id: string): booleanAdds or removes id, and tells every listener. Returns whether it is now saved.
subscribeSaved(onChange: () => void): () => voidFor useSyncExternalStore: this tab's changes, and other tabs' through storage.
paint(button: HTMLElement, saved: boolean): voidThe 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): () => voidA page with no React: every button.cap-save[data-cap="save-toggle"] with data-key and data-id.

Parts and data attributes

ClassIs
.cap-saveits own class
.cap-save-iconthe root
StateRead from
[aria-pressed="true"]an attribute

Accessibility

KeyDoes
TabMoves to the button
Enter, SpaceSaves 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.