@reshaped/utilities 4.3.1-canary.0 → 4.4.0-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,3 +10,4 @@ export { findParent, getShadowRoot } from "./dom/index.js";
10
10
  export { rafThrottle } from "./helpers/index.js";
11
11
  export { disableScroll, enableScroll } from "./scroll/index.js";
12
12
  export type { Coordinates } from "./types/global.js";
13
+ export { getVirtualKeyboardHeight, subscribeVirtualKeyboard } from "./viewport/index.js";
package/dist/internal.js CHANGED
@@ -8,3 +8,4 @@ export { activateKeyboardMode, checkKeyboardMode, deactivateKeyboardMode, focusa
8
8
  export { findParent, getShadowRoot } from "./dom/index.js";
9
9
  export { rafThrottle } from "./helpers/index.js";
10
10
  export { disableScroll, enableScroll } from "./scroll/index.js";
11
+ export { getVirtualKeyboardHeight, subscribeVirtualKeyboard } from "./viewport/index.js";
@@ -0,0 +1 @@
1
+ export { getVirtualKeyboardHeight, subscribeVirtualKeyboard } from "./virtualKeyboard.js";
@@ -0,0 +1,2 @@
1
+ // Internal
2
+ export { getVirtualKeyboardHeight, subscribeVirtualKeyboard } from "./virtualKeyboard.js";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
2
+ import { getVirtualKeyboardHeight, subscribeVirtualKeyboard } from "../virtualKeyboard.js";
3
+ const original = Object.getOwnPropertyDescriptor(window, "visualViewport");
4
+ let viewport;
5
+ let unsubscribe;
6
+ const resize = (args) => {
7
+ viewport.height = args.height;
8
+ viewport.scale = args.scale ?? 1;
9
+ viewport.dispatchEvent(new Event("resize"));
10
+ };
11
+ describe("viewport/virtualKeyboard", () => {
12
+ beforeEach(() => {
13
+ viewport = Object.assign(new EventTarget(), { height: window.innerHeight, scale: 1 });
14
+ Object.defineProperty(window, "visualViewport", { configurable: true, value: viewport });
15
+ });
16
+ afterEach(() => {
17
+ unsubscribe?.();
18
+ unsubscribe = undefined;
19
+ // @ts-expect-error Removes the stubbed value from the element itself
20
+ delete document.documentElement.clientHeight;
21
+ Object.defineProperty(window, "visualViewport", original);
22
+ });
23
+ test("returns the height covered by the keyboard", () => {
24
+ const listener = vi.fn();
25
+ unsubscribe = subscribeVirtualKeyboard(listener);
26
+ expect(getVirtualKeyboardHeight()).toBe(0);
27
+ resize({ height: window.innerHeight - 300 });
28
+ expect(getVirtualKeyboardHeight()).toBe(300);
29
+ expect(listener).toHaveBeenCalledTimes(1);
30
+ resize({ height: window.innerHeight });
31
+ expect(getVirtualKeyboardHeight()).toBe(0);
32
+ expect(listener).toHaveBeenCalledTimes(2);
33
+ });
34
+ test("measures the keyboard opened before subscribing", () => {
35
+ viewport.height = window.innerHeight - 300;
36
+ unsubscribe = subscribeVirtualKeyboard(() => { });
37
+ expect(getVirtualKeyboardHeight()).toBe(300);
38
+ });
39
+ test("ignores the pinch zoom", () => {
40
+ const listener = vi.fn();
41
+ unsubscribe = subscribeVirtualKeyboard(listener);
42
+ resize({ height: window.innerHeight / 2, scale: 2 });
43
+ expect(getVirtualKeyboardHeight()).toBe(0);
44
+ expect(listener).not.toHaveBeenCalled();
45
+ });
46
+ test("ignores the document scrollbar", () => {
47
+ // Headless browsers don't render scrollbars, so the height it takes from the document is stubbed
48
+ Object.defineProperty(document.documentElement, "clientHeight", {
49
+ configurable: true,
50
+ value: window.innerHeight - 15,
51
+ });
52
+ viewport.height = window.innerHeight - 15;
53
+ unsubscribe = subscribeVirtualKeyboard(() => { });
54
+ expect(getVirtualKeyboardHeight()).toBe(0);
55
+ });
56
+ test("stops updating without subscribers", () => {
57
+ const listener = vi.fn();
58
+ subscribeVirtualKeyboard(listener)();
59
+ resize({ height: window.innerHeight - 300 });
60
+ expect(getVirtualKeyboardHeight()).toBe(0);
61
+ expect(listener).not.toHaveBeenCalled();
62
+ });
63
+ });
@@ -0,0 +1,9 @@
1
+ type Listener = () => void;
2
+ /**
3
+ * Height of the area the on-screen keyboard covers at the bottom of the viewport,
4
+ * 0 when it's closed or when the browser resizes the layout to fit above the keyboard.
5
+ * Updated while there is at least one subscriber
6
+ */
7
+ export declare const getVirtualKeyboardHeight: () => number;
8
+ export declare const subscribeVirtualKeyboard: (listener: Listener) => () => void;
9
+ export {};
@@ -0,0 +1,51 @@
1
+ const listeners = new Set();
2
+ let height = 0;
3
+ /**
4
+ * iOS, and Android by default, keep the layout as it is when the on-screen keyboard opens and only shrink the visual viewport,
5
+ * so the keyboard height is the part of the dynamic viewport the visual viewport doesn't reach.
6
+ * The dynamic viewport is measured with an element since there is no other way to resolve its units
7
+ */
8
+ const measure = () => {
9
+ const viewport = window.visualViewport;
10
+ if (!viewport)
11
+ return 0;
12
+ const probeEl = document.createElement("div");
13
+ probeEl.style.cssText =
14
+ "position:fixed;top:0;left:0;width:0;height:100dvh;visibility:hidden;pointer-events:none";
15
+ document.body.appendChild(probeEl);
16
+ const dynamicHeight = probeEl.getBoundingClientRect().height;
17
+ probeEl.style.height = "100svh";
18
+ const smallHeight = probeEl.getBoundingClientRect().height;
19
+ probeEl.remove();
20
+ // Viewport units include the classic scrollbar of the document, which the visual viewport leaves out
21
+ const scrollbarHeight = Math.max(0, smallHeight - document.documentElement.clientHeight);
22
+ // Visual viewport gets smaller with the pinch zoom, so it's scaled back to the layout size
23
+ const visibleHeight = viewport.height * viewport.scale;
24
+ // Rounded down to keep the subpixel difference of a zoomed page from being treated as a keyboard
25
+ return Math.max(0, Math.floor(dynamicHeight - scrollbarHeight - visibleHeight));
26
+ };
27
+ const update = () => {
28
+ const nextHeight = measure();
29
+ if (nextHeight === height)
30
+ return;
31
+ height = nextHeight;
32
+ listeners.forEach((listener) => listener());
33
+ };
34
+ /**
35
+ * Height of the area the on-screen keyboard covers at the bottom of the viewport,
36
+ * 0 when it's closed or when the browser resizes the layout to fit above the keyboard.
37
+ * Updated while there is at least one subscriber
38
+ */
39
+ export const getVirtualKeyboardHeight = () => height;
40
+ export const subscribeVirtualKeyboard = (listener) => {
41
+ if (!listeners.size) {
42
+ window.visualViewport?.addEventListener("resize", update);
43
+ update();
44
+ }
45
+ listeners.add(listener);
46
+ return () => {
47
+ listeners.delete(listener);
48
+ if (!listeners.size)
49
+ window.visualViewport?.removeEventListener("resize", update);
50
+ };
51
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reshaped/utilities",
3
- "version": "4.3.1-canary.0",
3
+ "version": "4.4.0-canary.2",
4
4
  "description": "Vanilla JS utilities for implementing common UI patterns",
5
5
  "homepage": "https://reshaped.so",
6
6
  "bugs": {