Files
outline/app/hooks/useOnScreen.ts
T
Tom MoorandClaude Opus 5 4b4c7d3281 chore: React 19 upgrade (#13562)
* React 19 upgrade

Bumps react and react-dom to 19.2.8, with the dependency changes React 19
requires: react-is 19, @types/react 19, @types/react-dom 19,
react-helmet-async 3 and react-avatar-editor 15 (which now ships its own
types). framer-motion moves to 13 — its v6 types reference React.ReactHTML,
removed from @types/react 19.

The type changes fall into a few classes:

- The global JSX namespace is gone, so JSX.Element becomes React.JSX.Element
  and the custom element declarations move into a module augmentation.
- useRef() now requires an initial value.
- A ref created with a null initial value is typed nullable, so consumers
  that accept one are widened to RefObject<T | null>.
- React.ReactChild was removed; tooltips take React.ReactNode.
- Ref callbacks may only return a cleanup function. react-dnd connectors
  return the connected node, so useDragRef and useDropRef wrap useDrag and
  useDrop and hand back a ref callback.

react-i18next stays on v12: i18next 23 drops compatibilityJSON "v3", which
would rewrite all 64 _plural keys across every locale.

outline-icons, oy-vey and react-waypoint have no release that lists React 19
as a peer. All three render correctly under it; the install reports them as
unmet peers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Remove react-helmet and forwardRef

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-18 22:22:50 -04:00

82 lines
2.3 KiB
TypeScript

import * as React from "react";
const isSupported = "IntersectionObserver" in window;
// Parses a rootMargin string ("10px 20px" / "10px" / "10px 20px 30px 40px")
// into [top, right, bottom, left] in pixels. Percentages are not supported in
// the synchronous fast path and fall back to 0.
function parseRootMargin(
rootMargin: string | undefined
): [number, number, number, number] {
if (!rootMargin) {
return [0, 0, 0, 0];
}
const parts = rootMargin
.split(/\s+/)
.map((p) => (p.endsWith("px") ? parseFloat(p) : 0));
const [t = 0, r = t, b = t, l = r] = parts;
return [t, r, b, l];
}
/**
* Hook to return if a given ref is visible on screen.
*
* @returns boolean if the node is visible
*/
export default function useOnScreen(
ref: React.RefObject<HTMLElement | null>,
options?: IntersectionObserverInit
) {
const root = options?.root;
const rootMargin = options?.rootMargin;
const threshold = Array.isArray(options?.threshold)
? options?.threshold.join(",")
: options?.threshold;
const [isIntersecting, setIntersecting] = React.useState(!isSupported);
React.useLayoutEffect(() => {
const element = ref.current;
if (!element) {
return undefined;
}
// Synchronous initial check so the first paint is correct.
const [mt, mr, mb, ml] = parseRootMargin(rootMargin);
const rect = element.getBoundingClientRect();
const rootRect =
root instanceof Element
? root.getBoundingClientRect()
: {
top: 0,
left: 0,
bottom: window.innerHeight,
right: window.innerWidth,
};
const initialVisible =
rect.bottom >= rootRect.top - mt &&
rect.top <= rootRect.bottom + mb &&
rect.right >= rootRect.left - ml &&
rect.left <= rootRect.right + mr;
setIntersecting(initialVisible);
if (!isSupported) {
return undefined;
}
const observer = new IntersectionObserver(([entry]) => {
setIntersecting(entry.isIntersecting);
}, options);
observer.observe(element);
return () => {
observer.unobserve(element);
};
// Re-create when option primitives change; options object identity ignored
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ref, root, rootMargin, threshold]);
return isIntersecting;
}