Number 63: Save to a collection
Specification
- Catalogue
- No. 63 of 110
- Section
- Buttons and toggles
- Interaction
- Feedback
- Element
- Button, Overlay
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Save a product into a named collection, move it, or remove it, all from the same button.
Saving is more useful with a place to put things. The button says where the item went, and the same button changes or undoes it.
Collection menu opens 150ms, closes 110ms (the popover recipe). Escape or a click outside closes; focus returns to the button; changes are announced. Kept in memory here; a real build stores it in the account or the browser.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useEffect, useId, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/kit/pop.css";
import "./save-button.css";
function SaveButtonDemo() {
const [lists, setLists] = useState(["Living room", "Bedroom ideas"]);
const [savedTo, setSavedTo] = useState<string | null>(null);
const [open, setOpen] = useState(false);
const [naming, setNaming] = useState(false);
const [name, setName] = useState("");
const [said, setSaid] = useState("");
const btn = useRef<HTMLButtonElement>(null);
const pop = useRef<HTMLDivElement>(null);
const wrap = useRef<HTMLDivElement>(null);
const uid = useId();
useEffect(() => {
if (!open) return;
// Opening focuses the first collection; "New collection" focuses its name field.
pop.current?.querySelector<HTMLElement>(naming ? "input" : "button")?.focus();
const onDown = (e: PointerEvent) => {
if (wrap.current && e.target instanceof Node && !wrap.current.contains(e.target)) setOpen(false);
};
document.addEventListener("pointerdown", onDown);
return () => document.removeEventListener("pointerdown", onDown);
}, [open, naming]);
const close = () => {
setOpen(false);
setNaming(false);
btn.current?.focus();
};
const save = (l: string) => {
setSavedTo(l);
setSaid(`Saved to ${l}`);
close();
};
return (
<div className="dm" style={{ alignContent: "start", paddingTop: 40 }}>
<div className="sv-card">
<div className="sv-img" aria-hidden="true">
<span className="sv-cushion" />
</div>
<div className="sv-info">
<strong>Linen cushion</strong>
<span>Oatmeal, 45cm · £38</span>
<div
className="sv-wrap"
ref={wrap}
onKeyDown={(e) => {
if (e.key === "Escape" && open) {
e.stopPropagation();
close();
}
}}
>
<button ref={btn} type="button" className="sv-btn" data-saved={savedTo ? true : undefined} aria-haspopup="true" aria-expanded={open} onClick={() => setOpen((o) => !o)}>
<svg viewBox="0 0 24 24" width="18" height="18" fill={savedTo ? "currentColor" : "none"} stroke="currentColor" strokeWidth="2" strokeLinejoin="round" aria-hidden="true">
<path d="M6 3.5h12v17l-6-4.2-6 4.2z" />
</svg>
{savedTo ? `Saved to ${savedTo}` : "Save"}
</button>
<div ref={pop} className="sv-pop pop" data-closed={!open || undefined} inert={!open || undefined} role="group" aria-label="Save to a collection">
<p className="sv-pop-title">{savedTo ? "Move to" : "Save to"}</p>
{lists.map((l) => (
<button key={l} type="button" className="sv-opt" aria-pressed={savedTo === l} onClick={() => save(l)}>
{l}
{savedTo === l && <span className="sv-tick">Saved</span>}
</button>
))}
{naming ? (
<form
className="sv-new"
onSubmit={(e) => {
e.preventDefault();
const n = name.trim();
if (!n) return;
if (!lists.includes(n)) setLists((x) => [...x, n]);
setName("");
save(n);
}}
>
<label className="sr-only" htmlFor={`${uid}-n`}>
New collection name
</label>
<input id={`${uid}-n`} value={name} onChange={(e) => setName(e.target.value)} placeholder="Name it" maxLength={30} />
<button type="submit" className="sv-opt sv-add">
Add
</button>
</form>
) : (
<button type="button" className="sv-opt sv-add" onClick={() => setNaming(true)}>
New collection
</button>
)}
{savedTo && (
<button
type="button"
className="sv-opt sv-remove"
onClick={() => {
setSaid(`Removed from ${savedTo}`);
setSavedTo(null);
close();
}}
>
Remove from saved
</button>
)}
</div>
</div>
{savedTo && (
<a href="#" className="sv-link">
View saved items
</a>
)}
</div>
<p className="sr-only" aria-live="polite">
{said}
</p>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ click: ".sv-btn", wait: 1000 },
{ click: ".sv-opt", nth: 0, wait: 1800 },
];
export default function SaveButtonEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Save to a collection" sample tour={TOUR}>
<SaveButtonDemo />
</PieceStage>
);
}