diff --git a/.changeset/modal-keyboard-inset.md b/.changeset/modal-keyboard-inset.md deleted file mode 100644 index 86f03cdb2..000000000 --- a/.changeset/modal-keyboard-inset.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"reshaped": patch ---- - -`Modal` with `bottom`, `start`, `end` and `full-screen` positions ends above the on-screen keyboard when the browser doesn't resize the layout for it, like iOS Safari. These modals used to stay under the keyboard, so the end of their content could only be reached by panning the page diff --git a/.changeset/modal-keyboard-safe-area.md b/.changeset/modal-keyboard-safe-area.md deleted file mode 100644 index 9d35e7f38..000000000 --- a/.changeset/modal-keyboard-safe-area.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"reshaped": patch ---- - -`Modal` doesn't keep the bottom safe area inset while the on-screen keyboard covers it. iOS keeps reporting the home indicator inset with the keyboard open, which left a gap between the keyboard and the modals with `start` and `end` positions, and an extra bottom padding in the modals with `bottom` position diff --git a/.changeset/native-keyboard-reveal.md b/.changeset/native-keyboard-reveal.md new file mode 100644 index 000000000..73fc3850d --- /dev/null +++ b/.changeset/native-keyboard-reveal.md @@ -0,0 +1,6 @@ +--- +"reshaped": patch +"@reshaped/utilities": patch +--- + +Page scroll lock on iOS and `Modal` leave revealing the focused field above the on-screen keyboard to the browser. Scroll lock no longer takes over focusing the pressed fields or puts back the page Safari panned for the keyboard, and `Modal` no longer ends above the keyboard. Both raced Safari's own panning, which moved the modal content around while focusing fields. `useVirtualKeyboard` stays available for positioning custom content above the keyboard diff --git a/.changeset/pre.json b/.changeset/pre.json index 0b4a76733..bb23f1ad6 100644 --- a/.changeset/pre.json +++ b/.changeset/pre.json @@ -14,15 +14,12 @@ "flyout-content-measuring", "flyout-nested-outside-click", "keyboard-mode-autofill", - "modal-keyboard-inset", - "modal-keyboard-safe-area", "modal-swipe-gesture", "overlay-ios-backdrop-scroll", "overlay-ios-backdrop", "overlay-open-latency", "quiet-apples-wait", "reviewed-prop-docs", - "scroll-lock-keyboard-pan", "toast-provider-container-ref", "touch-cancel-gestures", "use-on-click-outside-capture", diff --git a/.changeset/scroll-lock-keyboard-pan.md b/.changeset/scroll-lock-keyboard-pan.md deleted file mode 100644 index c7ca6a9a8..000000000 --- a/.changeset/scroll-lock-keyboard-pan.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"reshaped": patch -"@reshaped/utilities": patch ---- - -Page scroll lock on iOS keeps Safari from panning the page for the on-screen keyboard. Safari pans the whole page to reveal a focused field even while the page scroll is locked, which moved the fixed content rendered above it outside of the screen: a `Modal` ending above the keyboard was pushed up by the keyboard height, leaving a gap below it with its top unreachable. While `Overlay`, `Modal` and other components locking the page scroll are open, the pressed fields are focused without letting Safari pan for them, the focused field is revealed inside its own scrollable container instead, and a page panned anyway is put back diff --git a/packages/reshaped/src/components/Modal/Modal.module.css b/packages/reshaped/src/components/Modal/Modal.module.css index f73fa4ccc..8e2eb1e63 100644 --- a/packages/reshaped/src/components/Modal/Modal.module.css +++ b/packages/reshaped/src/components/Modal/Modal.module.css @@ -21,27 +21,14 @@ --rs-modal-container-height: 100vh; --rs-modal-padding: calc(var(--rs-modal-p) * var(--rs-unit-x1)); --rs-modal-env-top: env(safe-area-inset-top, 0px); - - /* iOS keeps reporting the bottom safe area while the on-screen keyboard covers it */ - --rs-modal-env-bottom: max( - 0px, - env(safe-area-inset-bottom, 0px) - var(--rs-modal-keyboard-height, 0px) - ); + --rs-modal-env-bottom: env(safe-area-inset-bottom, 0px); --rs-modal-env-start: env(safe-area-inset-left, 0px); --rs-modal-env-end: env(safe-area-inset-right, 0px); - /** - * On-screen keyboard that doesn't resize the layout covers the bottom of the viewport, - * so the modals attached to the bottom edge end above it to keep their content reachable - */ - --rs-modal-keyboard: var(--rs-modal-keyboard-height, 0px); - /* Distance between the side modals and the screen edges, increased to the safe area insets when they're larger */ --rs-modal-offset: var(--rs-unit-x2); --rs-modal-inset-top: max(var(--rs-modal-offset), var(--rs-modal-env-top)); - --rs-modal-inset-bottom: calc( - max(var(--rs-modal-offset), var(--rs-modal-env-bottom)) + var(--rs-modal-keyboard) - ); + --rs-modal-inset-bottom: max(var(--rs-modal-offset), var(--rs-modal-env-bottom)); --rs-modal-inset-start: max(var(--rs-modal-offset), var(--rs-modal-env-start)); --rs-modal-inset-end: max(var(--rs-modal-offset), var(--rs-modal-env-end)); @@ -96,7 +83,6 @@ --rs-modal-env-bottom: 0px; --rs-modal-env-start: 0px; --rs-modal-env-end: 0px; - --rs-modal-keyboard: 0px; } } @@ -148,13 +134,11 @@ width: 100%; height: var(--rs-modal-size); max-width: 100%; - max-height: calc( - var(--rs-modal-container-height) - var(--rs-unit-x4) - var(--rs-modal-keyboard) - ); + max-height: calc(var(--rs-modal-container-height) - var(--rs-unit-x4)); margin: 0; margin-top: var(--rs-unit-x4); inset: 0; - inset-block: auto var(--rs-modal-keyboard); + inset-block-start: initial; transform: translate(0, 100%); position: absolute; @@ -245,12 +229,12 @@ box-shadow: var(--rs-modal-full-screen-shadow); width: 100%; - height: calc(100% - var(--rs-modal-keyboard)); + height: 100%; max-width: var(--rs-modal-container-width); max-height: 100%; margin: 0; inset: 0; - inset-block: auto var(--rs-modal-keyboard); + inset-block-start: initial; transform: translate(0, var(--rs-unit-x4)); position: absolute; border-width: 0; diff --git a/packages/reshaped/src/components/Modal/Modal.tsx b/packages/reshaped/src/components/Modal/Modal.tsx index 2607cfcff..c8af49750 100644 --- a/packages/reshaped/src/components/Modal/Modal.tsx +++ b/packages/reshaped/src/components/Modal/Modal.tsx @@ -7,7 +7,6 @@ import Overlay, { type OverlayInstance } from "@/components/Overlay"; import Text from "@/components/Text"; import useElementId from "@/hooks/useElementId"; import useResponsiveClientValue from "@/hooks/useResponsiveClientValue"; -import useVirtualKeyboard from "@/hooks/useVirtualKeyboard"; import { responsiveClassNames, responsiveVariables } from "@/utilities/props"; import type * as T from "./Modal.types"; import useModalDrag from "./useModalDrag"; @@ -79,7 +78,6 @@ const Modal: FC = (props) => { } = props; const id = useElementId(); const clientPosition = useResponsiveClientValue(position)!; - const keyboard = useVirtualKeyboard(); const [titleMounted, setTitleMounted] = useState(false); const [subtitleMounted, setSubtitleMounted] = useState(false); const internalRootRef = useRef(null); @@ -162,7 +160,6 @@ const Modal: FC = (props) => { ...responsiveVariables("--rs-modal-size", size), ...responsiveVariables("--rs-modal-p", padding), "--rs-modal-drag": "0px", - "--rs-modal-keyboard-height": `${keyboard.height}px`, } as React.CSSProperties } aria-labelledby={titleMounted ? `${id}-title` : undefined} diff --git a/packages/reshaped/src/components/Modal/tests/Modal.test.tsx b/packages/reshaped/src/components/Modal/tests/Modal.test.tsx index 6615db0b8..c1b5b632a 100644 --- a/packages/reshaped/src/components/Modal/tests/Modal.test.tsx +++ b/packages/reshaped/src/components/Modal/tests/Modal.test.tsx @@ -215,7 +215,7 @@ describe("Components/Modal", () => { await expect.element(page.getByTestId("test-id")).toHaveClass("test-classname"); }); - test("test: virtual keyboard", async () => { + test("stays in place when the on-screen keyboard covers the viewport", async () => { const Component = () => { const { active, activate, deactivate } = useToggle(); @@ -237,36 +237,24 @@ describe("Components/Modal", () => { const root = page.getByRole("dialog"); await expect.element(root).toBeInTheDocument(); - const getKeyboardHeight = () => - root.element().style.getPropertyValue("--rs-modal-keyboard-height"); - - const getBottomOffset = () => - window.innerHeight - root.element().getBoundingClientRect().bottom; - - expect(getKeyboardHeight()).toBe("0px"); - const bottomOffset = getBottomOffset(); + const getBottom = () => root.element().getBoundingClientRect().bottom; + const bottom = getBottom(); try { - // On-screen keyboard shrinks the visual viewport without changing the layout + // On-screen keyboard shrinks the visual viewport without changing the layout, + // and revealing the focused field above it is left to the browser Object.defineProperty(viewport, "height", { configurable: true, value: window.innerHeight - 300, }); viewport.dispatchEvent(new Event("resize")); + await new Promise(requestAnimationFrame); - // Bottom safe area inset is reduced by the keyboard height covering it - await expect.poll(getKeyboardHeight).toBe("300px"); - // Modal ends above the keyboard with the same offset it has from the screen edge - expect(getBottomOffset()).toBe(bottomOffset + 300); + expect(getBottom()).toBe(bottom); } finally { // @ts-expect-error Removes the stubbed value from the viewport itself delete viewport.height; } - - viewport.dispatchEvent(new Event("resize")); - - await expect.poll(getKeyboardHeight).toBe("0px"); - expect(getBottomOffset()).toBe(bottomOffset); }); describe("swipe gesture", () => { diff --git a/packages/utilities/src/scroll/keyboardPan.ts b/packages/utilities/src/scroll/keyboardPan.ts deleted file mode 100644 index 32914021a..000000000 --- a/packages/utilities/src/scroll/keyboardPan.ts +++ /dev/null @@ -1,191 +0,0 @@ -import { findClosestScrollableContainer } from "@/dom"; - -/** Input types that don't open the on-screen keyboard */ -const NON_TEXT_INPUT_TYPES = new Set([ - "checkbox", - "radio", - "range", - "color", - "file", - "image", - "button", - "submit", - "reset", -]); - -/** Distance in px kept between the revealed field and the edges of the area visible above the keyboard */ -const REVEAL_MARGIN = 16; - -/** Field that opens the on-screen keyboard when it's focused */ -const isKeyboardField = (el: Element | null): el is HTMLElement => { - if (el instanceof HTMLTextAreaElement) return !el.disabled && !el.readOnly; - if (el instanceof HTMLInputElement) { - return !el.disabled && !el.readOnly && !NON_TEXT_INPUT_TYPES.has(el.type); - } - - return el instanceof HTMLElement && el.isContentEditable; -}; - -/** - * Field a press is about to focus. - * Controls rendered inside an editable area handle their presses themselves, so they don't resolve to the area around them - */ -const getPressedField = (target: Element) => { - const el = target.closest( - "input, textarea, select, button, a[href], [contenteditable]" - ); - - if (!el || el.matches("select, button, a[href]") || !isKeyboardField(el)) return null; - if (!el.isContentEditable) return el; - - // Editable area takes the focus as a whole, with its outermost editable element - let host = el; - while (host.parentElement?.isContentEditable) host = host.parentElement; - return host; -}; - -/** - * Safari resolves where to pan the page from the geometry the field has while it's getting focused, - * and pans it for the fields inside the transformed containers even when the focus prevents scrolling. - * The field is moved above the screen only for the synchronous focus call, so it's never painted there - */ -const focusWithoutPan = (field: HTMLElement) => { - const { opacity, transform, transition } = field.style; - - field.style.transition = "none"; - field.style.opacity = "0"; - field.style.transform = "translateY(-2000px)"; - - try { - field.focus({ preventScroll: true }); - } finally { - field.style.opacity = opacity; - field.style.transform = transform; - field.style.transition = transition; - } -}; - -/** Safari keeps the caret where the field last had it when the field is focused before its own tap handling */ -const placeCaret = (field: HTMLElement, point: { x: number; y: number }) => { - const position = document.caretPositionFromPoint?.(point.x, point.y); - if (!position) return; - - const node = position.offsetNode; - - if (field instanceof HTMLInputElement || field instanceof HTMLTextAreaElement) { - // Fields without a text selection, like an email input, don't have a caret to place - if (node !== field || field.selectionStart === null) return; - field.setSelectionRange(position.offset, position.offset); - return; - } - - const el = node instanceof HTMLElement ? node : node.parentElement; - if (!field.contains(node) || !el?.isContentEditable) return; - - window.getSelection()?.collapse(node, position.offset); -}; - -/** Area the focused field should be visible in, the caret for the editable areas that can be taller than the screen */ -const getRevealRect = (field: HTMLElement) => { - const selection = window.getSelection(); - - if (field.isContentEditable && selection?.rangeCount && field.contains(selection.focusNode)) { - const range = selection.getRangeAt(0).cloneRange(); - range.collapse(false); - const rect = range.getBoundingClientRect(); - - // Collapsed range can have no size, e.g. at the start of an empty line - if (rect.height) return rect; - } - - return field.getBoundingClientRect(); -}; - -/** - * Focusing without scrolling also skips scrolling the field into view inside its own scrollable container, - * so it's revealed there without moving the page - */ -const revealFocusedField = () => { - const field = document.activeElement; - const viewport = window.visualViewport; - if (!viewport || !isKeyboardField(field) || !field.parentElement) return; - - // Field scrolling its own content can't move itself from under the keyboard - const scrollEl = findClosestScrollableContainer({ el: field.parentElement }); - if (!scrollEl) return; - - const rect = getRevealRect(field); - const scrollRect = scrollEl.getBoundingClientRect(); - const top = Math.max(scrollRect.top, viewport.offsetTop) + REVEAL_MARGIN; - const bottom = Math.min(scrollRect.bottom, viewport.offsetTop + viewport.height) - REVEAL_MARGIN; - - if (rect.bottom > bottom) { - // Fields taller than the visible area keep their start in view - scrollEl.scrollTop += Math.min(rect.bottom - bottom, rect.top - top); - } else if (rect.top < top) { - scrollEl.scrollTop -= top - rect.top; - } -}; - -/** - * iOS covers the page with the on-screen keyboard instead of resizing it, - * and pans the whole page to reveal the focused field even while the page scroll is locked with overflow. - * That moves the fixed content rendered above the locked page, like modals, outside of the screen. - * While the page scroll is locked: - * - Fields are focused without letting Safari pan the page for them - * - Page panned anyway, e.g. by moving between the fields with the keyboard arrows, is put back - * - Focused field is revealed inside its own scrollable container instead - */ -export const preventKeyboardPan = () => { - const viewport = window.visualViewport; - const baseX = window.scrollX; - const baseY = window.scrollY; - let frame: number | undefined; - - const scheduleReveal = () => { - if (frame !== undefined) cancelAnimationFrame(frame); - // Waits for the layout reacting to the keyboard, e.g. modals ending above it - frame = requestAnimationFrame(revealFocusedField); - }; - - const handleMouseDown = (e: MouseEvent) => { - if (e.button !== 0 || !(e.target instanceof Element)) return; - - const field = getPressedField(e.target); - // Focused field handles the press natively to move its caret - if (!field || field === document.activeElement) return; - - focusWithoutPan(field); - placeCaret(field, { x: e.clientX, y: e.clientY }); - // Keyboard that is already open doesn't resize the viewport for the next field - scheduleReveal(); - }; - - const handleScroll = () => { - // Zoomed page is panned by the user - if (viewport && viewport.scale !== 1) return; - if (!isKeyboardField(document.activeElement)) return; - - const scrolled = Math.abs(window.scrollX - baseX) >= 1 || Math.abs(window.scrollY - baseY) >= 1; - const panned = Boolean(viewport && viewport.offsetTop >= 1); - if (!scrolled && !panned) return; - - // Instant behavior ignores the smooth scrolling that could be enabled for the page - window.scrollTo({ left: baseX, top: baseY, behavior: "instant" }); - }; - - // Captured to focus the field before the handlers of the page do it without the geometry override - document.addEventListener("mousedown", handleMouseDown, true); - window.addEventListener("scroll", handleScroll, { passive: true }); - viewport?.addEventListener("scroll", handleScroll); - viewport?.addEventListener("resize", scheduleReveal); - - return () => { - if (frame !== undefined) cancelAnimationFrame(frame); - - document.removeEventListener("mousedown", handleMouseDown, true); - window.removeEventListener("scroll", handleScroll); - viewport?.removeEventListener("scroll", handleScroll); - viewport?.removeEventListener("resize", scheduleReveal); - }; -}; diff --git a/packages/utilities/src/scroll/lockSafari.ts b/packages/utilities/src/scroll/lockSafari.ts index 9f3ed6377..6752aab8c 100644 --- a/packages/utilities/src/scroll/lockSafari.ts +++ b/packages/utilities/src/scroll/lockSafari.ts @@ -1,5 +1,4 @@ import { StyleCache } from "@/css"; -import { preventKeyboardPan } from "./keyboardPan"; const styleCache = new StyleCache(); @@ -19,7 +18,8 @@ const getViewportScroller = () => { * iOS uses overlay scrollbars, so the document scroll is locked with overflow only. * Body is not moved to a fixed position since that collapses the document height, * which makes Safari lose its translucent toolbars and resets the scroll position on unlock. - * Overflow doesn't stop Safari from panning the page for the on-screen keyboard, so that is prevented separately + * Revealing the focused fields above the on-screen keyboard is left to Safari: + * taking over its focus handling and panning makes the page jump between the positions both of them resolve */ const lockSafariScroll = () => { const el = getViewportScroller(); @@ -27,12 +27,7 @@ const lockSafariScroll = () => { // Longhands keep the original value of each axis when unlocking, e.g. overflow-x: hidden on body styleCache.set(el, { overflowX: "hidden", overflowY: "hidden" }); - const allowKeyboardPan = preventKeyboardPan(); - - return () => { - styleCache.reset(el); - allowKeyboardPan(); - }; + return () => styleCache.reset(el); }; export default lockSafariScroll; diff --git a/packages/utilities/src/scroll/tests/keyboardPan.test.ts b/packages/utilities/src/scroll/tests/keyboardPan.test.ts deleted file mode 100644 index e51b73ec6..000000000 --- a/packages/utilities/src/scroll/tests/keyboardPan.test.ts +++ /dev/null @@ -1,193 +0,0 @@ -import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; - -import { preventKeyboardPan } from "../keyboardPan"; - -const original = Object.getOwnPropertyDescriptor(window, "visualViewport")!; -let viewport: EventTarget & { height: number; offsetTop: number; scale: number }; -let release: (() => void) | undefined; - -const press = (target: Element, point?: { x: number; y: number }) => { - const rect = target.getBoundingClientRect(); - - target.dispatchEvent( - new MouseEvent("mousedown", { - bubbles: true, - cancelable: true, - clientX: point?.x ?? rect.left + rect.width / 2, - clientY: point?.y ?? rect.top + rect.height / 2, - }) - ); -}; - -const render = (html: string) => { - const root = document.createElement("div"); - root.innerHTML = html; - document.body.appendChild(root); - return root; -}; - -describe("scroll/preventKeyboardPan", () => { - beforeEach(() => { - document.body.innerHTML = ""; - viewport = Object.assign(new EventTarget(), { - height: window.innerHeight, - offsetTop: 0, - scale: 1, - }); - Object.defineProperty(window, "visualViewport", { configurable: true, value: viewport }); - }); - - afterEach(() => { - release?.(); - release = undefined; - document.body.innerHTML = ""; - window.scrollTo(0, 0); - Object.defineProperty(window, "visualViewport", original); - }); - - test("focuses the pressed field while it's moved above the screen, without scrolling to it", () => { - const root = render(` -
-
- -
- `); - const scrollEl = root.querySelector("[data-scroll]")!; - const input = root.querySelector("input")!; - // Geometry Safari resolves the pan from - let focusedTransform = ""; - const focus = vi - .spyOn(input, "focus") - .mockImplementation(function (this: HTMLInputElement, options) { - focusedTransform = this.style.transform; - HTMLElement.prototype.focus.call(this, options); - }); - release = preventKeyboardPan(); - - press(input); - - expect(focus).toHaveBeenCalledExactlyOnceWith({ preventScroll: true }); - expect(focusedTransform).toBe("translateY(-2000px)"); - expect(document.activeElement).toBe(input); - expect(scrollEl.scrollTop).toBe(0); - expect(input.style.transform).toBe("scale(1)"); - expect(input.style.opacity).toBe("0.5"); - expect(input.style.transition).toBe(""); - }); - - test("puts the caret where the field is pressed", () => { - const root = render(``); - const textarea = root.querySelector("textarea")!; - const rect = textarea.getBoundingClientRect(); - textarea.setSelectionRange(0, 0); - release = preventKeyboardPan(); - - press(textarea, { x: rect.right - 4, y: rect.top + rect.height / 2 }); - - expect(document.activeElement).toBe(textarea); - expect(textarea.selectionStart).toBe(5); - }); - - test("focuses the editable area as a whole and leaves the controls inside it to their own presses", () => { - const root = render(` -
-

Text

- -
Island
-
- `); - const host = root.querySelector("[data-host]")!; - release = preventKeyboardPan(); - - press(root.querySelector("button")!); - press(root.querySelector("[data-island]")!); - expect(document.activeElement).toBe(document.body); - - press(root.querySelector("span")!); - expect(document.activeElement).toBe(host); - expect(host.contains(window.getSelection()!.focusNode)).toBe(true); - }); - - test("leaves the focused field and the fields without a keyboard to the browser", () => { - const root = render(``); - const input = root.querySelector("[data-text]")!; - release = preventKeyboardPan(); - - press(root.querySelector("[type=checkbox]")!); - press(root.querySelector("[readonly]")!); - expect(document.activeElement).toBe(document.body); - - input.focus(); - const focus = vi.spyOn(input, "focus"); - press(input); - expect(focus).not.toHaveBeenCalled(); - }); - - test("puts the page back when it's panned while a field is focused", async () => { - const root = render(`
`); - const input = root.querySelector("input")!; - release = preventKeyboardPan(); - - // Page without a focused field is not affected, e.g. when the user scrolls a contained overlay - window.scrollTo(0, 300); - await new Promise((resolve) => setTimeout(resolve, 100)); - expect(window.scrollY).toBe(300); - window.scrollTo(0, 0); - - press(input); - window.scrollTo(0, 300); - await expect.poll(() => window.scrollY).toBe(0); - - // Zoomed page is panned by the user - viewport.scale = 2; - window.scrollTo(0, 300); - await new Promise((resolve) => setTimeout(resolve, 100)); - expect(window.scrollY).toBe(300); - }); - - test("keeps the scroll position the page was locked at", async () => { - const root = render(`
`); - const input = root.querySelector("input")!; - window.scrollTo(0, 200); - release = preventKeyboardPan(); - - input.focus({ preventScroll: true }); - window.scrollTo(0, 500); - - await expect.poll(() => window.scrollY).toBe(200); - }); - - test("reveals the focused field inside its scrollable container once the keyboard covers it", async () => { - const root = render(` -
-
- -
-
- `); - const scrollEl = root.querySelector("[data-scroll]")!; - const input = root.querySelector("input")!; - release = preventKeyboardPan(); - - press(input); - // Field visible without the keyboard stays where it is - await new Promise((resolve) => requestAnimationFrame(resolve)); - expect(scrollEl.scrollTop).toBe(0); - - viewport.height = 300; - viewport.dispatchEvent(new Event("resize")); - - await expect.poll(() => Math.round(input.getBoundingClientRect().bottom)).toBe(300 - 16); - expect(window.scrollY).toBe(0); - }); - - test("stops handling the fields once released", () => { - const root = render(``); - const input = root.querySelector("input")!; - - preventKeyboardPan()(); - press(input); - - expect(document.activeElement).toBe(document.body); - }); -}); diff --git a/packages/utilities/src/scroll/tests/lockSafari.test.ts b/packages/utilities/src/scroll/tests/lockSafari.test.ts index e67049062..6861f5104 100644 --- a/packages/utilities/src/scroll/tests/lockSafari.test.ts +++ b/packages/utilities/src/scroll/tests/lockSafari.test.ts @@ -59,23 +59,15 @@ describe("scroll/lockSafariScroll", () => { expect(window.scrollY).toBe(300); }); - test("keeps the keyboard from panning the page until unlocked", () => { - document.body.innerHTML = ``; + test("leaves focusing the pressed fields to the browser", () => { + document.body.innerHTML = ``; const input = document.body.querySelector("input")!; - const textarea = document.body.querySelector("textarea")!; - const press = (target: Element) => { - target.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true })); - }; const unlock = lockSafariScroll(); + input.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true })); - // Fields are focused by the lock itself, before the browser pans the page for them - press(input); - expect(document.activeElement).toBe(input); + expect(document.activeElement).not.toBe(input); unlock(); - - press(textarea); - expect(document.activeElement).toBe(input); }); });