BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/shadcn-htmx/optimistic-toggle

Optimistic Toggle

shadcn-htmx/optimistic-toggle
FreeComponent

A server-backed action toggle (like, star, follow, pin) that flips appearance instantly via a native <template> of the toggled state, then reconciles with the server's HTML response — rolling back automatically on error. Built on a real <button> with aria-pressed and the htmx v4 hx-optimistic extension. Ships in five flavours: Hono JSX (TypeScript), Jinja2 macro, Go html/template, Phoenix function component, and a raw HTML snippet. Follows the WAI-ARIA APG button (toggle) pattern.

Live preview

live · rendered by the registry
Rendered by shadcn-htmx on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://shadcn-htmx.productdevbook.com/r/optimistic-toggle.json

Source

registry/ui/optimistic-toggle.tsxshadcn-htmx.productdevbook.com/r/optimistic-toggle.json · first 6 KB
1/** @jsxImportSource hono/jsx */
2import type { Child } from "hono/jsx"
3import { cn, type ClassValue } from "@/registry/lib/cn"
4
5// Optimistic Toggle — shadcn-htmx, htmx v4 + Tailwind v4.
6//
7// A server-backed action toggle (like / star / follow / pin). Clicking flips
8// the appearance INSTANTLY to the toggled state, then reconciles with the
9// server's HTML response — rolling back automatically if the request fails.
10//
11// Built on:
12// - Real htmx v4 events, NOT an extension. htmx v4 does not ship a working
13// "optimistic" attribute (the bundled src/ext/hx-optimistic.js is an
14// unfinished stub — "TODO: this needs to be updated to use the new internal
15// API" — and it does not flip aria-pressed nor cancel the error swap). So
16// the behaviour is a tiny self-contained script (OPTIMISTIC_TOGGLE_JS,
17// emitted once below) keyed on data-slot="optimistic-toggle":
18// • htmx:before:request — save the button's innerHTML + aria-pressed,
19// then flip aria-pressed and paint the <template>'s optimistic markup
20// in (the instant pre-network flip).
21// • htmx:before:swap — if the response is 4xx/5xx (ctx.response.status),
22// preventDefault() to CANCEL the swap (htmx v4 swaps error bodies by
23// default — see repos/htmx/src/htmx.js:1224) and restore the saved
24// markup + aria-pressed (rollback).
25// • on success htmx's hx-swap="outerHTML" replaces the button with the
26// server's authoritative version — no rollback code needed.
27// htmx v4 fires htmx:response:error for 4xx/5xx and lets a
28// htmx:before:swap listener veto the swap via preventDefault:
29// repos/htmx/CHANGELOG.md (htmx:response:error) and htmx.js:1224.
30// - A native <template> holds the optimistic markup. <template> content is
31// inert/not rendered until cloned, so it never shows until the script pulls
32// its innerHTML. repos/mdn/files/en-us/web/html/reference/elements/template/index.md
33// - A real <button> with aria-pressed: the platform gives us role=button and
34// Space/Enter activation for free, and aria-pressed carries the toggle
35// state. APG: Button (toggle) pattern — the accessible NAME must stay
36// constant across states; only aria-pressed flips.
37// repos/aria-practices/content/patterns/button/examples/button.html
38// repos/mdn/files/en-us/web/accessibility/aria/reference/attributes/aria-pressed/index.md
39//
40// … truncated

Facts

Registry
shadcn-htmx
Type
registry:ui
Access
Free
Files written
0
Licence
MIT
In the index
at or before 2026-08-13
Last confirmed
today

Go to the source

Docs on shadcn-htmx shadcn-htmx.productdevbook.com productdevbook/shadcn-htmx on GitHub Raw registry item This item as JSON

More from shadcn-htmx

All 83 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
Accordionaccordionshadcn-htmxComponentsFreeno deps
Active Searchactive-searchshadcn-htmxComponentsFreeno deps
Alertalertshadcn-htmxComponentsFreeno deps
Alert Dialogalert-dialogshadcn-htmxComponentsFreeno deps
Aspect Ratioaspect-ratioshadcn-htmxComponentsFreeno deps
Auto Gridauto-gridshadcn-htmxComponentsFreeno deps
Autocompleteautocompleteshadcn-htmxComponentsFreeno deps
Autosize Textareaautosize-textareashadcn-htmxComponentsFreeno deps

Also from Kynth Studios

Built for the same person as BlockDex

SkillWorks

Claude Code skills and plugins, scored

skillworks.kynth.studio

RuleStack

AGENTS.md gallery and agent-config comparison

rulestack.kynth.studio

KitGrade

SaaS starter kits, scored from measured facts

kitgrade.kynth.studio

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and KitGrade

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and KitGrade

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and KitGrade

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex