Files
outline/app/hooks/useKeyboardStickyOffset.ts
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

95 lines
3.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import * as React from "react";
import { getSafeAreaInsets } from "@shared/utils/browser";
/**
* Pins a `position: fixed; bottom: 0` element directly above the on-screen
* keyboard on mobile browsers.
*
* On iOS Safari the layout viewport (which fixed elements are positioned
* against) does not shrink when the software keyboard opens — only the visual
* viewport does. As a result an element anchored to the bottom of the screen
* ends up hidden behind the keyboard. This hook measures the gap between the
* bottom of the layout viewport and the bottom of the visual viewport and
* translates the element upwards by that amount so it always rests on top of
* the keyboard.
*
* The measurement is repeated every frame while enabled. A keyboard changes
* height without warning – a predictive text strip or an accessory bar comes
* and goes, one keyboard type replaces another – and not every change is
* announced by a viewport event, so an element positioned once from an event
* can be left behind the keyboard. Writes go straight to the node's style, and
* only when the offset changes, so a frame that measures the same keyboard
* costs a handful of property reads.
*
* @param ref A ref to the fixed-position element to keep pinned.
* @param enabled Whether the behavior should be active.
*/
export default function useKeyboardStickyOffset(
ref: React.RefObject<HTMLElement | null>,
enabled: boolean
) {
const applied = React.useRef<number | null>(null);
// Reading the layout viewport height can force a layout, so it is measured
// when it changes rather than on every frame.
const safeAreaBottom = React.useRef(0);
const layoutHeight = React.useRef(0);
const apply = React.useCallback(() => {
const viewport = window.visualViewport;
const node = ref.current;
if (!enabled || !viewport || !node) {
return;
}
// Distance from the bottom of the layout viewport (where the element is
// anchored) up to the bottom of the visible area — i.e. the height of the
// keyboard, if any. On browsers that resize the layout viewport with the
// keyboard this is ~0 and we fall back to the safe area inset so the bar
// clears the home indicator.
const keyboardInset = Math.max(
0,
layoutHeight.current - (viewport.offsetTop + viewport.height)
);
const offset = Math.round(Math.max(keyboardInset, safeAreaBottom.current));
if (offset !== applied.current) {
applied.current = offset;
node.style.transform = `translate3d(0, ${-offset}px, 0)`;
}
}, [ref, enabled]);
// Re-apply after every render, before paint, so the transform survives a
// parent re-render and there is no flash on first mount.
React.useLayoutEffect(() => {
safeAreaBottom.current = getSafeAreaInsets().bottom;
layoutHeight.current = window.innerHeight;
applied.current = null;
apply();
});
React.useEffect(() => {
if (!enabled || !window.visualViewport) {
return;
}
const handleResize = () => {
layoutHeight.current = window.innerHeight;
safeAreaBottom.current = getSafeAreaInsets().bottom;
};
window.addEventListener("resize", handleResize);
window.addEventListener("orientationchange", handleResize);
let frame = requestAnimationFrame(function tick() {
apply();
frame = requestAnimationFrame(tick);
});
return () => {
cancelAnimationFrame(frame);
window.removeEventListener("resize", handleResize);
window.removeEventListener("orientationchange", handleResize);
};
}, [enabled, apply]);
}