Save status
Slant / Feedback
Saved, saving, unsaved, offline, changed elsewhere or failed, without moving the layout.
Demo
Usage
import { Button, SaveStatus } from '@tuttn/slant'
// An autosave's state, when it last saved, and a retry for a failed or held save.
<SaveStatus state={autosave.state} savedAt={entry.savedAt} onRetry={autosave.flush} />
// Changed elsewhere: your own action beside the line.
<SaveStatus state="conflict" savedAt={entry.savedAt}
action={<Button size="sm" variant="ghost" onClick={review}>Review</Button>} />
// Your own line in place of the state's; the mark still follows the state.
<SaveStatus state="unsaved">Draft: incomplete</SaveStatus>
// A dense toolbar, and no announcements (another part of the page speaks).
<SaveStatus state={state} size="sm" announce={false} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
state | SaveState | — | Where the saves stand: `saved`, `saving`, `unsaved` (changes not saved yet), `offline` (held until the connection is back), `conflict` (changed elsewhere) or `error` (the last save failed). Rendered as `data-state`. |
savedAt | Date | number | string | — | When it last saved: a Date, a timestamp in milliseconds, or a string `new Date()` reads. Shown as a RelativeTime after the line in every state, so a change of state never adds or removes it. |
onRetry | () => void | — | Adds a small ghost Retry button (`messages.retry`), shown in `error` and `offline`. It keeps its room in the other states, hidden and inert. |
action | ReactNode | — | A control of yours after the line, shown in every state: a Review button for a conflict, say. |
children | ReactNode | — | Replaces the state's line ("Draft: incomplete"). The mark still follows `state`. |
size | 'sm' | 'md' | 'md' | The text size: `sm` for a dense toolbar. |
announce | boolean | true | Speaks the line through the root's polite live region when the state changes to `saved` (from `saving` only), `offline`, `conflict` or `error`. Nothing is said on mount, or for `saving` and `unsaved`. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, the ref and other attributes go on the root, which carries `data-state`. |
Keyboard
| Tab | reaches the time (with savedAt; focus shows its full date), then Retry in error and offline, and your action; the hidden Retry in other states is no stop |
| Enter / Space | on Retry, calls onRetry |
Accessibility
- The mark is decorative (
aria-hidden); the state is the line’s text,messages.saveState(state)(“Unsaved changes”, “Couldn’t save”), or yourchildren. - The root is not a live region, since a RelativeTime ticking inside it would be read again and again. A change of state into
saved(fromsavingonly),offline,conflictorerrorspeaks the line through the root’s polite announcer. Nothing is said on mount, or forsavingandunsaved, which change with every keystroke;announce={false}turns it off. - The labels of the other states are stacked under the shown one with
visibility: hiddenandaria-hidden, so they hold the width and are never read. The hidden Retry isinertandaria-hiddentoo. - Pressing Retry usually moves the state to
saving, which hides it; focus then moves to the status itself (tabIndex={-1}withonRetry) instead of falling to the page.
No layout shift
Nothing around the status moves when its state changes. The line is one grid cell holding every state’s label stacked, the shown one visible, so it is always as wide as the longest; with children, the cell stacks it too. The mark sits in a fixed square. With savedAt, the time follows the line in every state (“Unsaved changes · 2 minutes ago”: when it last saved stays worth knowing), so a change of state never adds or removes it. With onRetry, the Retry keeps its room in every state and shows only in error and offline. Only the time’s own words change its width, as the minutes pass.
The states
saved: a check in the success tone; the line is muted.saving: the Spinner; muted.unsaved: the accent pixel; changes not saved yet.offline: a warning triangle; saves are held until the connection is back. Retry shows.conflict: a warning triangle; someone changed it elsewhere. Pass anactionto review it.error: a danger icon; the last save failed. Retry shows.