shelving 1.328.0 → 1.328.1

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.
@@ -39331,8 +39331,10 @@ eventLoopFocus({ currentTarget, relatedTarget }: { currentTarget: Element; relat
39331
39331
  - Import: `import { isKeyboardOpen } from "shelving/ui";`
39332
39332
 
39333
39333
  Is the on-screen keyboard open (or still closing)?
39334
- - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window.
39335
- - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window.
39334
+ - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window,
39335
+ or pushes it up (`visualViewport.offsetTop`). When the page cannot scroll, iOS shrinks the window too, so only the push
39336
+ shows the keyboard.
39337
+ - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window and moves.
39336
39338
  - Returns `false` where the keyboard resizes the whole page (Android Chrome by default), and with no on-screen keyboard.
39337
39339
 
39338
39340
  ## Signatures
@@ -39345,7 +39347,7 @@ isKeyboardOpen(): boolean
39345
39347
 
39346
39348
  | Type | Description |
39347
39349
  | --- | --- |
39348
- | `boolean` | `true` when the visual viewport is much shorter than the window. |
39350
+ | `boolean` | `true` when the visual viewport is much shorter than the window, or pushed up. |
39349
39351
 
39350
39352
  ## Examples
39351
39353
 
@@ -39363,8 +39365,8 @@ if (isKeyboardOpen()) await waitForKeyboard();
39363
39365
 
39364
39366
  Lower the on-screen keyboard, and wait until it is down.
39365
39367
  - Blurs the focused text field, if there is one, so the keyboard starts to close.
39366
- - When the keyboard is open (`isKeyboardOpen()`) or a field was just blurred, it waits until the keyboard is down and the
39367
- viewport has stopped changing size, for at most half a second.
39368
+ - While the keyboard is open or closing (`isKeyboardOpen()`), it checks once each frame and goes on when the keyboard is
39369
+ down, or after half a second at most.
39368
39370
  - Returns nothing when there is nothing to wait for, so a caller can go on at once without a promise.
39369
39371
  - Use it before a page slide: a keyboard that closes during a view transition resizes the page in the middle of it.
39370
39372
 
@@ -4,8 +4,10 @@
4
4
  - Import: `import { isKeyboardOpen } from "shelving/ui";`
5
5
 
6
6
  Is the on-screen keyboard open (or still closing)?
7
- - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window.
8
- - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window.
7
+ - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window,
8
+ or pushes it up (`visualViewport.offsetTop`). When the page cannot scroll, iOS shrinks the window too, so only the push
9
+ shows the keyboard.
10
+ - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window and moves.
9
11
  - Returns `false` where the keyboard resizes the whole page (Android Chrome by default), and with no on-screen keyboard.
10
12
 
11
13
  ## Signatures
@@ -18,7 +20,7 @@ isKeyboardOpen(): boolean
18
20
 
19
21
  | Type | Description |
20
22
  | --- | --- |
21
- | `boolean` | `true` when the visual viewport is much shorter than the window. |
23
+ | `boolean` | `true` when the visual viewport is much shorter than the window, or pushed up. |
22
24
 
23
25
  ## Examples
24
26
 
@@ -5,8 +5,8 @@
5
5
 
6
6
  Lower the on-screen keyboard, and wait until it is down.
7
7
  - Blurs the focused text field, if there is one, so the keyboard starts to close.
8
- - When the keyboard is open (`isKeyboardOpen()`) or a field was just blurred, it waits until the keyboard is down and the
9
- viewport has stopped changing size, for at most half a second.
8
+ - While the keyboard is open or closing (`isKeyboardOpen()`), it checks once each frame and goes on when the keyboard is
9
+ down, or after half a second at most.
10
10
  - Returns nothing when there is nothing to wait for, so a caller can go on at once without a promise.
11
11
  - Use it before a page slide: a keyboard that closes during a view transition resizes the page in the middle of it.
12
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shelving",
3
- "version": "1.328.0",
3
+ "version": "1.328.1",
4
4
  "author": "Dave Houlbrooke <dave@shax.com>",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * Is the on-screen keyboard open (or still closing)?
3
- * - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window.
4
- * - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window.
3
+ * - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window,
4
+ * or pushes it up (`visualViewport.offsetTop`). When the page cannot scroll, iOS shrinks the window too, so only the push
5
+ * shows the keyboard.
6
+ * - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window and moves.
5
7
  * - Returns `false` where the keyboard resizes the whole page (Android Chrome by default), and with no on-screen keyboard.
6
8
  *
7
- * @returns `true` when the visual viewport is much shorter than the window.
9
+ * @returns `true` when the visual viewport is much shorter than the window, or pushed up.
8
10
  * @example if (isKeyboardOpen()) await waitForKeyboard();
9
11
  * @see https://shelving.cc/ui/isKeyboardOpen
10
12
  */
@@ -12,8 +14,8 @@ export declare function isKeyboardOpen(): boolean;
12
14
  /**
13
15
  * Lower the on-screen keyboard, and wait until it is down.
14
16
  * - Blurs the focused text field, if there is one, so the keyboard starts to close.
15
- * - When the keyboard is open (`isKeyboardOpen()`) or a field was just blurred, it waits until the keyboard is down and the
16
- * viewport has stopped changing size, for at most half a second.
17
+ * - While the keyboard is open or closing (`isKeyboardOpen()`), it checks once each frame and goes on when the keyboard is
18
+ * down, or after half a second at most.
17
19
  * - Returns nothing when there is nothing to wait for, so a caller can go on at once without a promise.
18
20
  * - Use it before a page slide: a keyboard that closes during a view transition resizes the page in the middle of it.
19
21
  *
@@ -1,16 +1,16 @@
1
- /** How much shorter the visual viewport must be than the window before the on-screen keyboard counts as open, in pixels. */
1
+ /** How far the keyboard must squash or push up the visual viewport before it counts as open, in pixels. */
2
2
  const _GAP = 100;
3
- /** How long the viewport must stay the same size before the keyboard counts as still, in milliseconds. */
4
- const _QUIET = 100;
5
3
  /** The longest `waitForKeyboard()` waits, in milliseconds. */
6
4
  const _MAX = 500;
7
5
  /**
8
6
  * Is the on-screen keyboard open (or still closing)?
9
- * - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window.
10
- * - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window.
7
+ * - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window,
8
+ * or pushes it up (`visualViewport.offsetTop`). When the page cannot scroll, iOS shrinks the window too, so only the push
9
+ * shows the keyboard.
10
+ * - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window and moves.
11
11
  * - Returns `false` where the keyboard resizes the whole page (Android Chrome by default), and with no on-screen keyboard.
12
12
  *
13
- * @returns `true` when the visual viewport is much shorter than the window.
13
+ * @returns `true` when the visual viewport is much shorter than the window, or pushed up.
14
14
  * @example if (isKeyboardOpen()) await waitForKeyboard();
15
15
  * @see https://shelving.cc/ui/isKeyboardOpen
16
16
  */
@@ -20,13 +20,13 @@ export function isKeyboardOpen() {
20
20
  const viewport = window.visualViewport;
21
21
  if (!viewport || Math.abs(viewport.scale - 1) > 0.01)
22
22
  return false;
23
- return window.innerHeight - viewport.height > _GAP;
23
+ return window.innerHeight - viewport.height > _GAP || viewport.offsetTop > _GAP;
24
24
  }
25
25
  /**
26
26
  * Lower the on-screen keyboard, and wait until it is down.
27
27
  * - Blurs the focused text field, if there is one, so the keyboard starts to close.
28
- * - When the keyboard is open (`isKeyboardOpen()`) or a field was just blurred, it waits until the keyboard is down and the
29
- * viewport has stopped changing size, for at most half a second.
28
+ * - While the keyboard is open or closing (`isKeyboardOpen()`), it checks once each frame and goes on when the keyboard is
29
+ * down, or after half a second at most.
30
30
  * - Returns nothing when there is nothing to wait for, so a caller can go on at once without a promise.
31
31
  * - Use it before a page slide: a keyboard that closes during a view transition resizes the page in the middle of it.
32
32
  *
@@ -37,38 +37,24 @@ export function isKeyboardOpen() {
37
37
  export function waitForKeyboard() {
38
38
  if (typeof window === "undefined")
39
39
  return;
40
- const blurred = _blurTextField();
41
- if (!blurred && !isKeyboardOpen())
40
+ _blurTextField();
41
+ if (!isKeyboardOpen())
42
42
  return;
43
43
  return new Promise(resolve => {
44
- const viewport = window.visualViewport;
45
44
  const start = performance.now();
46
- let changed = start;
47
- const onResize = () => {
48
- changed = performance.now();
49
- };
50
- const done = () => {
51
- viewport?.removeEventListener("resize", onResize);
52
- window.removeEventListener("resize", onResize);
53
- resolve();
54
- };
55
45
  const check = (now) => {
56
- if (now - start >= _MAX || (!isKeyboardOpen() && now - changed >= _QUIET))
57
- return done();
46
+ if (now - start >= _MAX || !isKeyboardOpen())
47
+ return resolve();
58
48
  requestAnimationFrame(check);
59
49
  };
60
- viewport?.addEventListener("resize", onResize);
61
- window.addEventListener("resize", onResize);
62
50
  requestAnimationFrame(check);
63
51
  });
64
52
  }
65
- /** Blur the focused element when it takes typed text (and so shows the on-screen keyboard), and say if it did. */
53
+ /** Blur the focused element when it takes typed text (and so shows the on-screen keyboard). */
66
54
  function _blurTextField() {
67
55
  const el = document.activeElement;
68
- if (!(el instanceof HTMLElement) || !_isTextField(el))
69
- return false;
70
- el.blur();
71
- return true;
56
+ if (el instanceof HTMLElement && _isTextField(el))
57
+ el.blur();
72
58
  }
73
59
  /** Does an element take typed text? */
74
60
  function _isTextField(el) {
@@ -1,7 +1,36 @@
1
- import { describe, expect, test } from "bun:test";
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
2
  import { isKeyboardOpen, waitForKeyboard } from "shelving/ui";
3
3
 
4
+ /** Pretend to be a browser window with these sizes (numbers from a real iPhone). */
5
+ function _window(innerHeight: number, height: number, offsetTop: number, scale = 1): void {
6
+ (globalThis as { window?: unknown }).window = { innerHeight, visualViewport: { height, offsetTop, scale } };
7
+ }
8
+
4
9
  describe("keyboard", () => {
10
+ afterEach(() => {
11
+ delete (globalThis as { window?: unknown }).window;
12
+ });
13
+
14
+ test("isKeyboardOpen() is true when the keyboard squashes the visual viewport", () => {
15
+ _window(684, 383, 0);
16
+ expect(isKeyboardOpen()).toBe(true);
17
+ });
18
+
19
+ test("isKeyboardOpen() is true when the keyboard shrinks the window and pushes the visual viewport up", () => {
20
+ _window(383, 383, 301);
21
+ expect(isKeyboardOpen()).toBe(true);
22
+ });
23
+
24
+ test("isKeyboardOpen() is false with the keyboard down", () => {
25
+ _window(684, 684, 0);
26
+ expect(isKeyboardOpen()).toBe(false);
27
+ });
28
+
29
+ test("isKeyboardOpen() is false when the page is zoomed in", () => {
30
+ _window(684, 300, 200, 2);
31
+ expect(isKeyboardOpen()).toBe(false);
32
+ });
33
+
5
34
  test("isKeyboardOpen() is false with no window", () => {
6
35
  expect(isKeyboardOpen()).toBe(false);
7
36
  });
@@ -1,19 +1,18 @@
1
- /** How much shorter the visual viewport must be than the window before the on-screen keyboard counts as open, in pixels. */
1
+ /** How far the keyboard must squash or push up the visual viewport before it counts as open, in pixels. */
2
2
  const _GAP = 100;
3
3
 
4
- /** How long the viewport must stay the same size before the keyboard counts as still, in milliseconds. */
5
- const _QUIET = 100;
6
-
7
4
  /** The longest `waitForKeyboard()` waits, in milliseconds. */
8
5
  const _MAX = 500;
9
6
 
10
7
  /**
11
8
  * Is the on-screen keyboard open (or still closing)?
12
- * - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window.
13
- * - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window.
9
+ * - On iOS (Safari, home-screen web apps and `WKWebView`) the keyboard makes the visual viewport shorter than the window,
10
+ * or pushes it up (`visualViewport.offsetTop`). When the page cannot scroll, iOS shrinks the window too, so only the push
11
+ * shows the keyboard.
12
+ * - Returns `false` when the page is zoomed in, because a zoomed visual viewport is also shorter than the window and moves.
14
13
  * - Returns `false` where the keyboard resizes the whole page (Android Chrome by default), and with no on-screen keyboard.
15
14
  *
16
- * @returns `true` when the visual viewport is much shorter than the window.
15
+ * @returns `true` when the visual viewport is much shorter than the window, or pushed up.
17
16
  * @example if (isKeyboardOpen()) await waitForKeyboard();
18
17
  * @see https://shelving.cc/ui/isKeyboardOpen
19
18
  */
@@ -21,14 +20,14 @@ export function isKeyboardOpen(): boolean {
21
20
  if (typeof window === "undefined") return false;
22
21
  const viewport = window.visualViewport;
23
22
  if (!viewport || Math.abs(viewport.scale - 1) > 0.01) return false;
24
- return window.innerHeight - viewport.height > _GAP;
23
+ return window.innerHeight - viewport.height > _GAP || viewport.offsetTop > _GAP;
25
24
  }
26
25
 
27
26
  /**
28
27
  * Lower the on-screen keyboard, and wait until it is down.
29
28
  * - Blurs the focused text field, if there is one, so the keyboard starts to close.
30
- * - When the keyboard is open (`isKeyboardOpen()`) or a field was just blurred, it waits until the keyboard is down and the
31
- * viewport has stopped changing size, for at most half a second.
29
+ * - While the keyboard is open or closing (`isKeyboardOpen()`), it checks once each frame and goes on when the keyboard is
30
+ * down, or after half a second at most.
32
31
  * - Returns nothing when there is nothing to wait for, so a caller can go on at once without a promise.
33
32
  * - Use it before a page slide: a keyboard that closes during a view transition resizes the page in the middle of it.
34
33
  *
@@ -38,36 +37,22 @@ export function isKeyboardOpen(): boolean {
38
37
  */
39
38
  export function waitForKeyboard(): Promise<void> | void {
40
39
  if (typeof window === "undefined") return;
41
- const blurred = _blurTextField();
42
- if (!blurred && !isKeyboardOpen()) return;
40
+ _blurTextField();
41
+ if (!isKeyboardOpen()) return;
43
42
  return new Promise<void>(resolve => {
44
- const viewport = window.visualViewport;
45
43
  const start = performance.now();
46
- let changed = start;
47
- const onResize = () => {
48
- changed = performance.now();
49
- };
50
- const done = () => {
51
- viewport?.removeEventListener("resize", onResize);
52
- window.removeEventListener("resize", onResize);
53
- resolve();
54
- };
55
44
  const check = (now: number) => {
56
- if (now - start >= _MAX || (!isKeyboardOpen() && now - changed >= _QUIET)) return done();
45
+ if (now - start >= _MAX || !isKeyboardOpen()) return resolve();
57
46
  requestAnimationFrame(check);
58
47
  };
59
- viewport?.addEventListener("resize", onResize);
60
- window.addEventListener("resize", onResize);
61
48
  requestAnimationFrame(check);
62
49
  });
63
50
  }
64
51
 
65
- /** Blur the focused element when it takes typed text (and so shows the on-screen keyboard), and say if it did. */
66
- function _blurTextField(): boolean {
52
+ /** Blur the focused element when it takes typed text (and so shows the on-screen keyboard). */
53
+ function _blurTextField(): void {
67
54
  const el = document.activeElement;
68
- if (!(el instanceof HTMLElement) || !_isTextField(el)) return false;
69
- el.blur();
70
- return true;
55
+ if (el instanceof HTMLElement && _isTextField(el)) el.blur();
71
56
  }
72
57
 
73
58
  /** Does an element take typed text? */