agentleFS
indrazmindrazm/personal-skills/skills/optimistic-ui/SKILL.md

optimistic-ui

Skill15 starsChanged 5 days agoMITView on GitHub

Implements optimistic UI updates for user mutations (messages, edits, toggles, image uploads) using stable client IDs and content hashes, flicker-free image swaps, and restrained enter-only micro-animations. Use when building interactive mutations, chat/messaging flows, forms, file/image uploads, or fixing UI flicker during network reconciliation.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 1 is distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Read references/patterns.md when implementing optimistic state stores, flicker-free image uploads, or WebSocket/SSE deduplication.

    line 10
  2. Very specific:

    Generate a stable clientId: Create a UUID (crypto.randomUUID()) or idempotency key client-side before issuing the request.

    line 24
  3. Very specific:

    Reconcile in place: When the server responds, merge serverId and canonical fields into the existing record matched by clientId without …

    line 26
  4. Distinctive:

    Never allow reconciliation to unmount and remount DOM nodes:

    line 31
  5. Very specific:

    When the remote URL returns, preload and decode it off-screen (img.decode()) before swapping src, and only revoke the object URL …

    line 39
  6. Very specific:

    Zero re-animation on reconciliation: Keep the element mounted when status transitions from pending to synced.

    line 46
  7. Very specific:

    No layout-thrashing animations: Never animate height, width, margin, or top/left for status badges or retry banners.

    line 47
  8. Very specific:

    Always call URL.revokeObjectURL after remote image handoff or component unmount to prevent memory leaks.

    line 61

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Optimistic UI

Make every user mutation feel instantaneous by updating UI state immediately, reconciling server responses in place via stable client IDs and hashes, and ensuring reconciliation never unmounts nodes, flickers images, or replays animations.

Read references/patterns.md when implementing optimistic state stores, flicker-free image uploads, or WebSocket/SSE deduplication.

When to Use

  • Sending chat messages, comments, posts, or form submissions
  • Updating, toggling, reordering, or deleting UI records
  • Uploading and previewing optimistic images, attachments, or avatars
  • Fixing UI flashes, lost focus/scroll jumps, or re-triggered animations on API confirmation

Core Optimistic Pattern

Apply this lifecycle to every user-initiated mutation:

  1. Snapshot prior state: Capture rollback state before mutating so failures can roll back or surface inline retry cleanly.
  2. Generate a stable clientId: Create a UUID (crypto.randomUUID()) or idempotency key client-side before issuing the request.
  3. Mutate state immediately: Insert or update the item with status: "pending" and clientId.
  4. Reconcile in place: When the server responds, merge serverId and canonical fields into the existing record matched by clientId without changing its UI key.
  5. Handle failures gracefully: On error, set status: "failed" with inline Retry/Discard controls (for user-authored content) or roll back cleanly (for toggles/reactions).

Stable IDs, Hashes & Image Uploads

Never allow reconciliation to unmount and remount DOM nodes:

  • Keep UI list keys immutable: Key rendered items by clientId (never swap key={tempId} to key={serverId}, and never key mutable lists by array index). Swapping keys destroys and recreates DOM nodes, causing flashes, lost scroll/focus, and replayed enter animations.
  • Deduplicate with idempotency keys: Send clientId in the mutation payload and echo it back in both REST responses and WebSocket/SSE events so retries and real-time updates merge in place instead of appending duplicates.
  • Flicker-free optimistic image uploads:
    • Compute a stable attachment clientId and content hash (SHA-256 of file bytes or name + size + lastModified paired with clientId) before starting upload.
    • Render immediately via URL.createObjectURL(file) inside a container with locked dimensions (aspect-ratio or explicit width/height) to eliminate layout shift.
    • Render upload progress as a lightweight overlay (opacity / transform) without resizing the media frame.
    • When the remote URL returns, preload and decode it off-screen (img.decode()) before swapping src, and only revoke the object URL (URL.revokeObjectURL) after the remote image is verified ready (or on unmount).

Animation Discipline

Pair optimistic updates with restrained, composite-only motion:

  • Animate only on initial insertion: Play one ease-out enter transition (150ms to 220ms, transform + opacity) when the optimistic item is inserted.
  • Zero re-animation on reconciliation: Keep the element mounted when status transitions from pending to synced; transition only subtle indicators (such as opacity: 0.7 -> 1.0 over 120ms ease-out).
  • No layout-thrashing animations: Never animate height, width, margin, or top/left for status badges or retry banners; reserve space or animate transform and opacity.
  • Honor prefers-reduced-motion: Fall back to instant updates or subtle opacity fades when reduced motion is active.

Red Flags

  • Using serverId or array index as the UI key and swapping keys after API confirmation
  • Blocking UI updates until the network request finishes
  • Swapping a local blob preview src to an uncached remote URL before decoding completes (causes image blink)
  • Replaying enter animations when REST or WebSocket confirmation arrives
  • Dropping user input on network failure instead of providing Retry

Gotchas

  • Propagate clientId through both REST responses and real-time streams (WebSockets/SSE) so real-time echoes reconcile the optimistic item in place.
  • Always call URL.revokeObjectURL after remote image handoff or component unmount to prevent memory leaks.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.