@inglorious/web 6.23.0 → 6.23.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.
package/README.md CHANGED
@@ -1622,7 +1622,7 @@ It returns a function that stops following.
1622
1622
 
1623
1623
  ## Keyboard
1624
1624
 
1625
- Phones don't shrink the page for the on-screen keyboard: iOS never does, nor does Chrome on Android. Whatever sits at the bottom of the screen, such as a bottom sheet with an input, ends up behind it. `@inglorious/web/keyboard` reads the height the keyboard covers from the visual viewport and sets it as `--iw-keyboard-height` on `<html>`, with the `iw-keyboard-open` class while the keyboard is up. It needs no plugin and works in browsers and native apps alike. Where the page does shrink, the height stays `0px`.
1625
+ Phones don't shrink the page for the on-screen keyboard: iOS never does, nor does Chrome on Android. Whatever sits at the bottom of the screen, such as a bottom sheet with an input, ends up behind it. `@inglorious/web/keyboard` reads the height the keyboard covers from the visual viewport and sets it as `--iw-keyboard-height` on `<html>`, with the `iw-keyboard-open` class while the keyboard is up. It needs no plugin and works in browsers and native apps alike. Where the page does shrink for the keyboard, as in an Android app, the class is still set but the height stays `0px`, since nothing is covered.
1626
1626
 
1627
1627
  ```javascript
1628
1628
  import { watchKeyboard } from "@inglorious/web/keyboard"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.23.0",
3
+ "version": "6.23.1",
4
4
  "description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "contributors": [
@@ -18,14 +18,16 @@ const TEXT_INPUTS = new Set([
18
18
  ])
19
19
 
20
20
  /**
21
- * Tells the page how much of it the on-screen keyboard covers, so what sits at
22
- * the bottom of the screen can move above it. Phones don't shrink the page for
23
- * the keyboard (iOS, and Chrome on Android): a bottom sheet or a snackbar ends
24
- * up behind it. This sets `--iw-keyboard-height` on `<html>`, and the
25
- * `iw-keyboard-open` class while the keyboard is up, from the visual viewport:
26
- * no plugin needed, in a browser or a native app. Where the page does shrink,
27
- * the height stays 0. Inglorious UI's sheets, snackbars and toasts follow it,
28
- * and its bottom navigation hides.
21
+ * Tells the page when the on-screen keyboard is up, and how much of it the
22
+ * keyboard covers, so what sits at the bottom of the screen can move above it
23
+ * or step aside. Most phones don't shrink the page for the keyboard (iOS, and
24
+ * Chrome on Android): a bottom sheet or a snackbar ends up behind it. This sets
25
+ * `--iw-keyboard-height` on `<html>` from the visual viewport, and the
26
+ * `iw-keyboard-open` class while the keyboard is up: no plugin needed, in a
27
+ * browser or a native app. Where the page does shrink, as in an Android app,
28
+ * the class is still set but the height stays 0, since nothing is covered.
29
+ * Inglorious UI's sheets, snackbars and toasts follow it, and its bottom
30
+ * navigation hides.
29
31
  *
30
32
  * @returns {() => void} Stops following.
31
33
  *
@@ -37,22 +39,33 @@ export function watchKeyboard() {
37
39
  if (!viewport) return () => {}
38
40
 
39
41
  const root = document.documentElement
40
- let current = NONE
42
+ let isOpen = false
43
+ let height = NONE
44
+ let fullWidth = window.innerWidth
45
+ let fullHeight = window.innerHeight
41
46
 
42
47
  const update = () => {
48
+ if (window.innerWidth !== fullWidth) {
49
+ fullWidth = window.innerWidth
50
+ fullHeight = window.innerHeight
51
+ }
52
+ if (!isOpen) fullHeight = Math.max(fullHeight, window.innerHeight)
53
+
43
54
  const covered = Math.round(
44
55
  window.innerHeight - viewport.height - viewport.offsetTop,
45
56
  )
57
+ const shrunk = fullHeight - window.innerHeight
46
58
  const isKeyboard =
47
- covered >= MIN_KEYBOARD_PX &&
48
- (Boolean(current) || isEditable(document.activeElement))
49
- const height = isKeyboard ? covered : NONE
50
- if (height === current) return
59
+ (covered >= MIN_KEYBOARD_PX || shrunk >= MIN_KEYBOARD_PX) &&
60
+ (isOpen || isEditable(document.activeElement))
61
+ const nextHeight = isKeyboard && covered >= MIN_KEYBOARD_PX ? covered : NONE
62
+ if (isKeyboard === isOpen && nextHeight === height) return
51
63
 
52
- const isOpening = !current && height
53
- current = height
64
+ const isOpening = isKeyboard && !isOpen
65
+ isOpen = isKeyboard
66
+ height = nextHeight
54
67
  root.style.setProperty(HEIGHT, `${height}px`)
55
- root.classList.toggle(OPEN, Boolean(height))
68
+ root.classList.toggle(OPEN, isOpen)
56
69
  if (isOpening) {
57
70
  requestAnimationFrame(() =>
58
71
  document.activeElement?.scrollIntoView?.({ block: "nearest" }),
@@ -63,10 +76,12 @@ export function watchKeyboard() {
63
76
  // Not on focus changes: a tap on a button would move it before its click.
64
77
  viewport.addEventListener("resize", update)
65
78
  viewport.addEventListener("scroll", update)
79
+ window.addEventListener("resize", update)
66
80
 
67
81
  return function stop() {
68
82
  viewport.removeEventListener("resize", update)
69
83
  viewport.removeEventListener("scroll", update)
84
+ window.removeEventListener("resize", update)
70
85
  root.style.removeProperty(HEIGHT)
71
86
  root.classList.remove(OPEN)
72
87
  }
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Sets `--iw-keyboard-height` on `<html>` to the height the on-screen keyboard
3
3
  * covers, and the `iw-keyboard-open` class while it is up, so what sits at the
4
- * bottom of the screen can move above it.
4
+ * bottom of the screen can move above it or step aside. Where the page shrinks
5
+ * for the keyboard, the class is set and the height stays 0.
5
6
  * @returns A function that stops following.
6
7
  */
7
8
  export declare function watchKeyboard(): () => void