Save status

Slant / Feedback

Saved, saving, unsaved, offline, changed elsewhere or failed, without moving the layout.

Demo

Usage

@tuttn/slant
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

PropTypeDefaultDescription
stateSaveState—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`.
savedAtDate | 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.
actionReactNode—A control of yours after the line, shown in every state: a Review button for a conflict, say.
childrenReactNode—Replaces the state's line ("Draft: incomplete"). The mark still follows `state`.
size'sm' | 'md''md'The text size: `sm` for a dense toolbar.
announcebooleantrueSpeaks 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`.
…restHTMLAttributes<HTMLDivElement>—className, style, the ref and other attributes go on the root, which carries `data-state`.

Keyboard

Tabreaches 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 / Spaceon 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 your children.
  • 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 (from saving only), offline, conflict or error speaks the line through the root’s polite announcer. Nothing is said on mount, or for saving and unsaved, which change with every keystroke; announce={false} turns it off.
  • The labels of the other states are stacked under the shown one with visibility: hidden and aria-hidden, so they hold the width and are never read. The hidden Retry is inert and aria-hidden too.
  • Pressing Retry usually moves the state to saving, which hides it; focus then moves to the status itself (tabIndex={-1} with onRetry) 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 an action to review it.
  • error: a danger icon; the last save failed. Retry shows.