ActionsSaveButton
Searchastronomic.dev# SaveButton
To let a reader keep a listing in a list in their own browser, with no account, such as Saved homes.
Interactive: inline script.
## Usage
```astro
import SaveButton from "@astronomic-dev/ui/components/SaveButton.astro";
```
## Examples
### On a listing's photo
The heart on a photo's top corner, as the listing cards place it. A press keeps the listing in a saved list in this browser, with no account; every Save button for the same listing flips with it, on this page and in other open tabs.
```astro
---
import Media from "@astronomic-dev/ui/components/Media.astro";
import SaveButton from "@astronomic-dev/ui/components/SaveButton.astro";
import { listings } from "../_data/vega.ts";
const home = listings[0];
---
<div class="p-4"><div class="relative max-w-sm">
<Media src={home.image} alt={home.image.alt} ratio="3/2" />
<SaveButton id={home.id} title={home.title} class="absolute end-2 top-2" />
</div>
</div>
```
### On the page
tone="page" for a Save beside a title on the page, such as on a listing's own page.
```astro
---
import SaveButton from "@astronomic-dev/ui/components/SaveButton.astro";
import { listings } from "../_data/vega.ts";
const home = listings[1];
---
<div class="flex items-center justify-between gap-4 p-4">
<div class="min-w-0">
<p class="m-0 text-heading font-semibold text-balance [overflow-wrap:anywhere]">{home.title}</p>
<p class="m-0 text-small text-muted">{home.address}</p>
</div>
<SaveButton id={home.id} title={home.title} tone="page" />
</div>
```
## Props
### SaveButton
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `id` | `string` | None | The listing's id: the value kept in the list. Every Save button with the same id and list shows the same state. |
| `title` | `string` | None | What is saved, for the button's name: "Save" plus the title, such as "Save The Tennyson Bungalow". |
| `list` | `string` | `"homes"` | The saved list's name. The list is kept in localStorage under "astronomic:saved:<list>", as a JSON list of ids; readSaved() in @astronomic-dev/ui/lib/saved.ts reads it for a Saved homes page. |
| `tone` | `"image" \| "page"` | `"image"` | The surface it sits on: a listing's photo (a light disc with a ring that shows on any picture) or the page (the page's button colors and focus ring). |
| `class` | `string` | None | More classes, merged with cn(), so yours win on conflicts. Place it with these, such as absolute end-2 top-2 on a photo. |
Any other attribute goes on the <button>.
## Accessibility
| Key | Action |
| --- | --- |
| Enter or Space | Saves the listing, or takes it out of the saved list. |
- A native <button> with aria-pressed: pressed means saved. Its name stays "Save" plus the title, so a screen reader says "Save The Tennyson Bungalow, toggle button, pressed".
- The heart is decoration, hidden from a screen reader; it fills when the listing is saved, and its outline keeps the button's text color, so the state does not rest on color alone.
- 44px round, so it is easy to hit on touch screens.
- Hidden without JavaScript, where it could not work.
- After each change, saved:change fires on the document with { list, ids }, for a page that lists the saved homes. Blocked storage is fine: the button still toggles for the page, and nothing is kept.
SaveButton
FreeTo let a reader keep a listing in a list in their own browser, with no account, such as Saved homes.
- File
- SaveButton.astro
- Group
- Actions
- Kind
- Interactive: inline script
- Examples
- 2
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
On a listing's photo
The heart on a photo's top corner, as the listing cards place it. A press keeps the listing in a saved list in this browser, with no account; every Save button for the same listing flips with it, on this page and in other open tabs.
On the page
tone="page" for a Save beside a title on the page, such as on a listing's own page.
Minimal · Light