Components
← All components

Optimistic

stimeo--optimistic

Optimistic UI for Turbo forms: apply instantly, keep on success, roll back precisely on failure.

  • Core
  • Rails / Turbo

The stimeo--optimistic controller wraps a Turbo form and applies a DECLARED optimistic state the instant submission starts: show targets are revealed, hide targets are hidden, and the form gets data-optimistic="true" plus aria-busy="true". On a successful turbo:submit-end the toggle is kept and commit fires — the final DOM belongs to the server's Turbo Stream response or redirect. On failure exactly the changes THIS controller made are restored and rollback fires: every target it actually toggled is marked with data-optimistic-toggled, so an element the author already showed is never clobbered. It rides Turbo's own submit lifecycle events with two delegated listeners — no Action Cable, no fetch patching — which is why it lives in the zero-dependency core rather than stimeo-ui/cable. Pairs naturally with submit-once (double-submit guard) and live-counter (optimistic numbers). Declarations beyond show/hide (classes, text swaps) are the consumer's, via the commit/rollback events; user-facing error display on rollback belongs to the form's error UI.

running

Both buttons flip to "Liked" the instant you click (each button submits a real Turbo form). The first one succeeds, so the flip is kept (and the server's Turbo Stream fills the status line). The second posts fail=1 which the server rejects with 422 — watch the flip roll back.