Connection state, plus whatever the Network Information API will say about it. It renders nothing: the state is yours to render however this app already reports things.
Usage
Installation
Adds the package and prints the import to use. Append --copy to write the source into your project instead.
enigma add networkInstalls with your project's own package manager - npm, pnpm, yarn or bun - read from its packageManager field or its lockfile.
1 Install the dependencies
2 Use it
Import from @enigmax/primitives/react.
State only - it renders nothing and shows no toast. Binding a toaster inside would make it useless to anyone who already has one.
recovered is the part worth having
“You are back online” has to know the connection had dropped. Every implementation of that
ends up as a stray wasOffline boolean beside the effect, re-derived on every screen - and
wrong on a page that loads while already offline, which announces a comeback that never
happened. It is tracked in the monitor instead, and is false on a first load.
Three details the naive version misses
The Network Information API is still prefixed in some browsers and absent in Safari, so
the connection object is looked up three ways and may be null throughout. Every field it
would provide is nullable, and null means “not reported” rather than zero.
The change event fires for readings that did not change, so the snapshot is
shallow-compared before any subscriber is told. Without that, every subscriber re-renders
for nothing on a connection that merely re-measured itself.
A server render must report online. Assuming offline flashes a warning on every page before hydration corrects it.
online |
navigator.onLine |
recovered |
It dropped earlier and is back. false on a first load |
slow |
2g or slower, or data saver on |
effectiveType |
"slow-2g", "2g", "3g", "4g", or null |
downlink / rtt |
Estimated Mbps and round trip, or null |
saveData |
The visitor asked for reduced data use |
type |
"wifi", "cellular", "ethernet", or null |