shadcn.io is not affiliated with official shadcn/ui
Offline Error Block for React
A full-screen offline page that listens to the browser's real online and offline events: a signal figure, a Check connection button, a Switch to pretend, and what stays available without a network.
Installation
Overview
Tell people they can keep working without a signal, with this offline page for React with Base UI. It's the screen Haulmate, a delivery-driver app, shows when the network goes: the page's two side rails run its full height and one band crosses the screen between two rules with a plus mark where each meets a rail. A signal figure of four bars (solid while online, hollow and struck through while offline) sits over the heading and one sentence, then the Shine Button "Check connection", the only coloured control, and a Switch that is a demo control, "Pretend I'm offline", on when the page opens so the error is what you see. The page listens to the browser's real online and offline events and reads its network state in an effect, so with the switch off and the browser really offline it says so too. Under that is a ruled list of what is saved on the phone, with sizes and a total worked out from them, and the changes waiting to sync, typed. It is the page when the connection drops, not a retry loop or a status page. For delivery, field-service, note-taking and any app with an offline mode.
FAQ
What is real here, and what stays in the page?
The online and offline events and navigator.onLine are real, and they are only read. Nothing is requested, written or synced: the list of saved items and the two waiting changes are typed constants (SAVED and CHANGES), and the line under the list says what a real app would do. To wire it for real, keep an outbox of edits in IndexedDB, add them as people work, flush the outbox when the online event fires, and cache the pages and data you list with a service worker.
What is the Pretend I'm offline switch?
A demo control, so a viewer who is online can still see the failing state. It is on when the page opens. Turn it off and the page follows your browser: still offline if the browser really is, back online otherwise. Remove the switch in your app and keep offline = real === false.
Can navigator.onLine be wrong?
Yes, in one direction. false is reliable: the browser has no network. true only means it is connected to some network, which may not reach your server. The button says exactly that ("Your browser says: online") and never claims more. For a true test, send a small request to your own server and treat a failure as offline.
What does Check connection do?
It shows a busy button for about half a second, reads navigator.onLine again and says what the browser says in a polite status line, plus what the switch changes ("The switch is holding this page offline"). Focus moves to that line because a busy button drops it. Nothing is sent to your server.
Why does it show offline before the browser has been read?
The server can't know whether a visitor is online, so the first render is fixed (the switch on, so offline) and the browser is read in an effect after the page loads. That keeps the server and browser markup identical, so there is no hydration mismatch and no flash of the other state.
Related
- Pro
Connection Fallback Error
NetworkA full-screen page for a live connection that dropped and fell back: three signal bars, three transport rows with a shape and a word each, Reconnect now as a short scripted attempt, and a Keep using polling toggle.
- Pro
Request Timeout Error
Server and outagesA full-height 408 page for a report generator: a request waterfall on a fixed time axis with a cut line at the wait you choose, a verdict that says whether the answer would have arrived, and a Retry that names the wait.
- Pro
Cookies Disabled Error
Browser and deviceA full-screen page for a browser with cookies off: a check that reads the browser and writes nothing, what four essential cookies hold, and the steps for three browsers in an accordion.
- Pro
Storage Full Error
Browser and deviceA full-screen page for a device that is out of room: a meter, a Check this browser button that reads the real storage estimate, and five steps for making room, with the shortcut as words for Mac or Windows.
- Pro
Browser Unsupported Error
Browser and deviceA full-screen update-your-browser page with five real feature checks in a table (ES modules, CSS grid, :has(), WebGL, structuredClone), a Switch to pretend, and the browsers that have all five.
- Pro
A sign-in that follows the browser's real online and offline events: offline it asks for an eight-character offline code and counts sessions, online it says no code is needed, in the same space.