Browser state
Three hooks read the network, fullscreen and the scroll direction. Each returns the server’s value in the hydration render.
On this page
Values on the server
The server has no visitor’s browser to read, so on the server the three hooks return:
useOnline():true- the value of
useFullscreen():false useScrollDirection():null
In the browser, the render that hydrates the server’s HTML returns them too, and the browser’s value arrives in the render right after. Hydration never mismatches.
A component the browser mounts after hydration reads the browser’s value from its first render.
A browser-only subtree
src/components/baseline-notice.tsx'use client';
import { use } from 'react';
import { browser } from 'react-dom';
export function BaselineNotice() {
use(browser('the notice depends on what this browser supports'));
The server stops here
return supportsBaseline() ? null : (
<p>Your browser is out of date.</p>
);
}<Suspense fallback={null}>
<BaselineNotice />
</Suspense>A part only the browser can render renders in the browser alone. This package has no hook that says whether hydration has happened.
Pass browser() from react-dom to use(), and the server writes the nearest <Suspense> fallback into its HTML. The component renders in the browser alone, with no mounted flag.
Rendering the server’s HTML and then something else once hydrated shifts the layout. A value only the browser knows comes from the hook that reads it, which has its own value for the server. The colour scheme’s guess comes from @k8ordo/color-scheme’s hook.
Network
offline-notice.tsxconst online = useOnline();
return online ? null : <p>You are offline.</p>;useOnline() returns navigator.onLine and updates on the online and offline events. The server cannot know the visitor’s connection, so it renders true.
Fullscreen
fullscreen-button.tsxconst [fullscreen, setFullscreen] = useFullscreen();
<button
onClick={() => {
void setFullscreen(!fullscreen);
}}
type="button"
>
{fullscreen ? 'Exit fullscreen' : 'Fullscreen'}
</button>;The value is whether any element is fullscreen. setFullscreen(true) puts the whole page (<html>) fullscreen, and setFullscreen(false) leaves.
setFullscreen returns the browser’s promise, which rejects when the browser refuses, as it does outside a click or a key press. Call it from an event handler, and handle the rejection if you can do anything about it.
To put one element fullscreen, call its requestFullscreen() in the handler. The value follows either way.
Scroll direction
src/lib/hides-on-scroll.tsimport { useScrollDirection } from '@k8ordo/effects';
export const useHidesOnScroll = () =>
useScrollDirection({ threshold: 64 }) === 'down';It returns 'down', 'up', or null until the page has scrolled. Only the page’s vertical scroll (window.scrollY) counts.
Scrolling down within threshold pixels of the top leaves the direction as it was, so a bar that hides on the way down stays at the top of the page. Scrolling up turns it 'up' anywhere.
Every component that asks with the same threshold reads one record, so a header and a dock never disagree, and one mounted later reads the direction the others see. To use one threshold in two places, put the call in a hook of your own, as above.