@inglorious/web 6.22.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
@@ -1620,6 +1620,48 @@ It returns a function that stops following.
1620
1620
 
1621
1621
  ---
1622
1622
 
1623
+ ## Keyboard
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 for the keyboard, as in an Android app, the class is still set but the height stays `0px`, since nothing is covered.
1626
+
1627
+ ```javascript
1628
+ import { watchKeyboard } from "@inglorious/web/keyboard"
1629
+
1630
+ watchKeyboard()
1631
+ ```
1632
+
1633
+ Inglorious UI's bottom sheets, snackbars and toasts move above the keyboard, and its bottom navigation hides while the keyboard is up. Your own fixed elements can do the same:
1634
+
1635
+ ```css
1636
+ .composer {
1637
+ position: fixed;
1638
+ bottom: var(--iw-keyboard-height, 0px);
1639
+ }
1640
+ ```
1641
+
1642
+ It returns a function that stops following.
1643
+
1644
+ ---
1645
+
1646
+ ## Haptics
1647
+
1648
+ `@inglorious/web/haptics` plays taps the user can feel. In a native app it uses the system's haptics through [`@capacitor/haptics`](https://capacitorjs.com/docs/apis/haptics), once you hand the plugin over. Elsewhere it vibrates where the browser can (Chrome on Android), once the user has tapped the page; iOS browsers stay silent. Every call returns whether it played.
1649
+
1650
+ ```javascript
1651
+ import { Haptics } from "@capacitor/haptics"
1652
+ import { haptics } from "@inglorious/web/haptics"
1653
+
1654
+ haptics.use(Haptics)
1655
+
1656
+ haptics.selection() // a toggle flipped, a threshold crossed
1657
+ haptics.impact("heavy") // "light", "medium" (default) or "heavy"
1658
+ haptics.notification("error") // "success" (default), "warning" or "error"
1659
+ ```
1660
+
1661
+ Inglorious UI plays them too: `PullToRefresh` gives a tick when letting go would refresh.
1662
+
1663
+ ---
1664
+
1623
1665
  ## Forms
1624
1666
 
1625
1667
  `@inglorious/web` includes a small but powerful `form` type for managing form state inside your entity store. It offers:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.22.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": [
@@ -49,6 +49,14 @@
49
49
  "types": "./types/system-bars.d.ts",
50
50
  "import": "./src/system-bars/index.js"
51
51
  },
52
+ "./keyboard": {
53
+ "types": "./types/keyboard.d.ts",
54
+ "import": "./src/keyboard/index.js"
55
+ },
56
+ "./haptics": {
57
+ "types": "./types/haptics.d.ts",
58
+ "import": "./src/haptics/index.js"
59
+ },
52
60
  "./decorators/with-debounce": {
53
61
  "types": "./types/decorators/with-debounce.d.ts",
54
62
  "import": "./src/decorators/with-debounce/index.js"
@@ -0,0 +1,78 @@
1
+ const IMPACTS = { light: "LIGHT", medium: "MEDIUM", heavy: "HEAVY" }
2
+ const NOTIFICATIONS = { success: "SUCCESS", warning: "WARNING", error: "ERROR" }
3
+ const LIGHT_MS = 20
4
+ const MEDIUM_MS = 40
5
+ const HEAVY_MS = 70
6
+ const PAUSE_MS = 80
7
+ const VIBRATIONS = {
8
+ light: [LIGHT_MS],
9
+ medium: [MEDIUM_MS],
10
+ heavy: [HEAVY_MS],
11
+ success: [LIGHT_MS, PAUSE_MS, LIGHT_MS],
12
+ warning: [MEDIUM_MS, PAUSE_MS, MEDIUM_MS],
13
+ error: [HEAVY_MS, PAUSE_MS, HEAVY_MS, PAUSE_MS, HEAVY_MS],
14
+ }
15
+
16
+ let plugin = null
17
+
18
+ /**
19
+ * Taps the user can feel, as native apps give on a toggle or a refresh. In a
20
+ * native app it plays the system's own haptics through `Haptics` from
21
+ * `@capacitor/haptics`, once handed over with `haptics.use`. Elsewhere it
22
+ * vibrates where the browser can (Chrome on Android); iOS browsers stay
23
+ * silent, and Chrome only vibrates once the user has tapped the page. Every
24
+ * call returns whether it played. Inglorious UI plays them too, such as
25
+ * `PullToRefresh` when letting go would refresh.
26
+ *
27
+ * @example
28
+ * import { Haptics } from "@capacitor/haptics"
29
+ * import { haptics } from "@inglorious/web/haptics"
30
+ *
31
+ * haptics.use(Haptics)
32
+ * haptics.notification("success")
33
+ */
34
+ export const haptics = {
35
+ /**
36
+ * Plays the system's haptics in a native app.
37
+ * @param {Object | null} haptics `Haptics` from `@capacitor/haptics`.
38
+ */
39
+ use(haptics) {
40
+ plugin = haptics
41
+ },
42
+
43
+ /**
44
+ * A choice changed, such as a toggle flipped or a threshold crossed.
45
+ * @returns {boolean} Whether it played.
46
+ */
47
+ selection() {
48
+ return play("light", () => plugin.impact({ style: IMPACTS.light }))
49
+ },
50
+
51
+ /**
52
+ * Something hit, harder the heavier the style.
53
+ * @param {"light" | "medium" | "heavy"} [style]
54
+ * @returns {boolean} Whether it played.
55
+ */
56
+ impact(style = "medium") {
57
+ return play(style, () => plugin.impact({ style: IMPACTS[style] }))
58
+ },
59
+
60
+ /**
61
+ * The outcome of an action.
62
+ * @param {"success" | "warning" | "error"} [type]
63
+ * @returns {boolean} Whether it played.
64
+ */
65
+ notification(type = "success") {
66
+ return play(type, () => plugin.notification({ type: NOTIFICATIONS[type] }))
67
+ },
68
+ }
69
+
70
+ function play(kind, native) {
71
+ if (plugin) {
72
+ Promise.resolve()
73
+ .then(native)
74
+ .catch(() => {})
75
+ return true
76
+ }
77
+ return globalThis.navigator?.vibrate?.(VIBRATIONS[kind]) ?? false
78
+ }
@@ -0,0 +1,98 @@
1
+ const HEIGHT = "--iw-keyboard-height"
2
+ const OPEN = "iw-keyboard-open"
3
+ const MIN_KEYBOARD_PX = 100
4
+ const NONE = 0
5
+ const TEXT_INPUTS = new Set([
6
+ "text",
7
+ "search",
8
+ "email",
9
+ "url",
10
+ "tel",
11
+ "password",
12
+ "number",
13
+ "date",
14
+ "datetime-local",
15
+ "month",
16
+ "time",
17
+ "week",
18
+ ])
19
+
20
+ /**
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.
31
+ *
32
+ * @returns {() => void} Stops following.
33
+ *
34
+ * @example
35
+ * watchKeyboard()
36
+ */
37
+ export function watchKeyboard() {
38
+ const viewport = window.visualViewport
39
+ if (!viewport) return () => {}
40
+
41
+ const root = document.documentElement
42
+ let isOpen = false
43
+ let height = NONE
44
+ let fullWidth = window.innerWidth
45
+ let fullHeight = window.innerHeight
46
+
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
+
54
+ const covered = Math.round(
55
+ window.innerHeight - viewport.height - viewport.offsetTop,
56
+ )
57
+ const shrunk = fullHeight - window.innerHeight
58
+ const isKeyboard =
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
63
+
64
+ const isOpening = isKeyboard && !isOpen
65
+ isOpen = isKeyboard
66
+ height = nextHeight
67
+ root.style.setProperty(HEIGHT, `${height}px`)
68
+ root.classList.toggle(OPEN, isOpen)
69
+ if (isOpening) {
70
+ requestAnimationFrame(() =>
71
+ document.activeElement?.scrollIntoView?.({ block: "nearest" }),
72
+ )
73
+ }
74
+ }
75
+
76
+ // Not on focus changes: a tap on a button would move it before its click.
77
+ viewport.addEventListener("resize", update)
78
+ viewport.addEventListener("scroll", update)
79
+ window.addEventListener("resize", update)
80
+
81
+ return function stop() {
82
+ viewport.removeEventListener("resize", update)
83
+ viewport.removeEventListener("scroll", update)
84
+ window.removeEventListener("resize", update)
85
+ root.style.removeProperty(HEIGHT)
86
+ root.classList.remove(OPEN)
87
+ }
88
+ }
89
+
90
+ function isEditable(element) {
91
+ if (!element) return false
92
+ if (element.isContentEditable) return true
93
+ if (element.tagName === "TEXTAREA") return !element.readOnly
94
+ if (element.tagName === "INPUT") {
95
+ return TEXT_INPUTS.has(element.type) && !element.readOnly
96
+ }
97
+ return false
98
+ }
@@ -0,0 +1,23 @@
1
+ /** What `haptics` needs of `Haptics` from `@capacitor/haptics`. */
2
+ export interface HapticsPlugin {
3
+ impact(options: { style: "LIGHT" | "MEDIUM" | "HEAVY" }): Promise<void>
4
+ notification(options: {
5
+ type: "SUCCESS" | "WARNING" | "ERROR"
6
+ }): Promise<void>
7
+ }
8
+
9
+ /**
10
+ * Taps the user can feel: the system's haptics in a native app, a vibration
11
+ * where the browser can, nothing elsewhere. Each call returns whether it
12
+ * played.
13
+ */
14
+ export declare const haptics: {
15
+ /** Plays the system's haptics in a native app. */
16
+ use(haptics: HapticsPlugin | null): void
17
+ /** A choice changed, such as a toggle flipped or a threshold crossed. */
18
+ selection(): boolean
19
+ /** Something hit, harder the heavier the style. */
20
+ impact(style?: "light" | "medium" | "heavy"): boolean
21
+ /** The outcome of an action. */
22
+ notification(type?: "success" | "warning" | "error"): boolean
23
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Sets `--iw-keyboard-height` on `<html>` to the height the on-screen keyboard
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 or step aside. Where the page shrinks
5
+ * for the keyboard, the class is set and the height stays 0.
6
+ * @returns A function that stops following.
7
+ */
8
+ export declare function watchKeyboard(): () => void