'use client' import * as React from 'react' import { useVisualViewportInset } from '@admin/hooks/use-visual-viewport-inset' import { isTextEntryElement } from '@admin/utils/shared/is-text-entry-element' const KEYBOARD_MIN_HEIGHT_PX = 150 // A prediction stands until the viewport reports the keyboard's real size, or this long. const PREDICTION_TIMEOUT_MS = 1000 // How long a blur waits for focus to land in another text field before it predicts a close. const CLOSE_GRACE_MS = 50 // A focus this soon after a press on the same field is that tap's focus. const TAP_WINDOW_MS = 800 // Locks the page while the phone focus view is open (only the editor content scrolls, user // decision 2026-11-12) and keeps the view and the fixed `PageHeader` inside the visible area while // the keyboard is up. iOS pans the visual viewport down to keep the caret clear of the keyboard, // which slides every `fixed` surface up and off the top; the view offsets itself by the returned // inset and the header follows through `--focus-view-viewport-top` (its `top` in `page-header.tsx`; // never a transform, which would make the header the containing block of the form's `fixed` phone // action bar inside it), cleared when the view closes. Only a keyboard-sized inset (150px or more) // counts: Safari's address bar growing and shrinking on scroll also resizes the visual viewport. // iOS reports the keyboard's size only once its animation ends, so focus predicts it: leaving text // entry drops the inset and a tap into a field restores the last keyboard height (a focus made from // code, like a mode switch, never predicts, since it raises no keyboard), and the view's // padding transition then moves in step with the keyboard instead of jumping after it. export function useContentEditorFocusViewport() { const viewportInset = useVisualViewportInset() const isKeyboardMeasured = viewportInset.bottom >= KEYBOARD_MIN_HEIGHT_PX const [lastKeyboardInset, setLastKeyboardInset] = React.useState(0) const [prediction, setPrediction] = React.useState<'open' | 'closed' | null>(null) React.useEffect(() => { if (isKeyboardMeasured) setLastKeyboardInset(viewportInset.bottom) setPrediction(null) }, [isKeyboardMeasured, viewportInset.bottom]) React.useEffect(() => { let timeout: number | undefined let pendingClose: number | undefined let lastTap: { target: EventTarget | null; time: number } | null = null const predict = (next: 'open' | 'closed') => { window.clearTimeout(timeout) setPrediction(next) timeout = window.setTimeout(() => setPrediction(null), PREDICTION_TIMEOUT_MS) } const handlePointerDown = (event: PointerEvent) => { lastTap = { target: event.target, time: performance.now() } } const handleFocusOut = (event: FocusEvent) => { if (!isTextEntryElement(event.target) || isTextEntryElement(event.relatedTarget)) return // Wait a beat: a mode switch blurs one editor and focuses the other right after, and the // keyboard stays up through that hand-over. window.clearTimeout(pendingClose) pendingClose = window.setTimeout(() => predict('closed'), CLOSE_GRACE_MS) } const handleFocusIn = (event: FocusEvent) => { if (!isTextEntryElement(event.target)) return window.clearTimeout(pendingClose) const isTap = lastTap !== null && performance.now() - lastTap.time < TAP_WINDOW_MS && event.target instanceof Node && lastTap.target instanceof Node && event.target.contains(lastTap.target) // Only a tap on the field itself raises the keyboard; a focus made from code (switching // modes, a sheet closing) does not, so it falls back to what the viewport measures. if (isTap) { predict('open') } else { window.clearTimeout(timeout) setPrediction(null) } } document.addEventListener('pointerdown', handlePointerDown, { capture: true }) document.addEventListener('focusout', handleFocusOut) document.addEventListener('focusin', handleFocusIn) return () => { window.clearTimeout(timeout) window.clearTimeout(pendingClose) document.removeEventListener('pointerdown', handlePointerDown, { capture: true }) document.removeEventListener('focusout', handleFocusOut) document.removeEventListener('focusin', handleFocusIn) } }, []) const inset = prediction === 'closed' ? { top: 0, bottom: 0 } : prediction === 'open' && !isKeyboardMeasured && lastKeyboardInset > 0 ? { top: viewportInset.top, bottom: lastKeyboardInset } : isKeyboardMeasured ? viewportInset : { top: 0, bottom: 0 } React.useEffect(() => { const { body, documentElement: root } = document const previousOverflow = [root.style.overflow, body.style.overflow] root.style.overflow = 'hidden' body.style.overflow = 'hidden' return () => { root.style.overflow = previousOverflow[0] body.style.overflow = previousOverflow[1] } }, []) React.useEffect(() => { const root = document.documentElement root.style.setProperty('--focus-view-viewport-top', `${inset.top}px`) return () => { root.style.removeProperty('--focus-view-viewport-top') } }, [inset.top]) return inset }