@inglorious/web 6.21.0 → 6.23.0

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
@@ -1586,7 +1586,7 @@ watchAppLinks(App, store)
1586
1586
 
1587
1587
  `{ id }` as a third argument notifies a router with another id; it returns a function that stops listening.
1588
1588
 
1589
- The operating system opens the app only when the domain vouches for it: iOS reads `/.well-known/apple-app-site-association` and Android `/.well-known/assetlinks.json`, and the app declares the domain (Associated Domains on iOS, an intent filter on Android). `@inglorious/create-app --mobile` generates the iOS file and documents the rest.
1589
+ The operating system opens the app only when the domain vouches for it: iOS reads `/.well-known/apple-app-site-association` and Android `/.well-known/assetlinks.json`, and the app declares the domain (Associated Domains on iOS, an intent filter on Android). [`@inglorious/capacitor`](../capacitor) generates all of it from `native.appLinks` in `package.json`, and `@inglorious/create-app --mobile` sets it up.
1590
1590
 
1591
1591
  ---
1592
1592
 
@@ -1605,6 +1605,63 @@ watchBackButton(App, { rootPaths: ["/", "/search", "/profile"] })
1605
1605
 
1606
1606
  ---
1607
1607
 
1608
+ ## System Bars
1609
+
1610
+ The status bar and Android's navigation bar follow the device's light or dark mode, so a light app on a phone in dark mode gets white icons on a white page. `@inglorious/web/system-bars` sets them from the app's own `color-scheme`, which every Inglorious UI theme sets, and follows it when the theme class changes. An app whose `color-scheme` is `light dark` keeps the device's style. It does nothing outside a native app.
1611
+
1612
+ ```javascript
1613
+ import { SystemBars } from "@capacitor/core"
1614
+ import { watchSystemBars } from "@inglorious/web/system-bars"
1615
+
1616
+ watchSystemBars(SystemBars)
1617
+ ```
1618
+
1619
+ It returns a function that stops following.
1620
+
1621
+ ---
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, the height stays `0px`.
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
+
1608
1665
  ## Forms
1609
1666
 
1610
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.21.0",
3
+ "version": "6.23.0",
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": [
@@ -45,6 +45,18 @@
45
45
  "types": "./types/back-button.d.ts",
46
46
  "import": "./src/back-button/index.js"
47
47
  },
48
+ "./system-bars": {
49
+ "types": "./types/system-bars.d.ts",
50
+ "import": "./src/system-bars/index.js"
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
+ },
48
60
  "./decorators/with-debounce": {
49
61
  "types": "./types/decorators/with-debounce.d.ts",
50
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,83 @@
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 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.
29
+ *
30
+ * @returns {() => void} Stops following.
31
+ *
32
+ * @example
33
+ * watchKeyboard()
34
+ */
35
+ export function watchKeyboard() {
36
+ const viewport = window.visualViewport
37
+ if (!viewport) return () => {}
38
+
39
+ const root = document.documentElement
40
+ let current = NONE
41
+
42
+ const update = () => {
43
+ const covered = Math.round(
44
+ window.innerHeight - viewport.height - viewport.offsetTop,
45
+ )
46
+ const isKeyboard =
47
+ covered >= MIN_KEYBOARD_PX &&
48
+ (Boolean(current) || isEditable(document.activeElement))
49
+ const height = isKeyboard ? covered : NONE
50
+ if (height === current) return
51
+
52
+ const isOpening = !current && height
53
+ current = height
54
+ root.style.setProperty(HEIGHT, `${height}px`)
55
+ root.classList.toggle(OPEN, Boolean(height))
56
+ if (isOpening) {
57
+ requestAnimationFrame(() =>
58
+ document.activeElement?.scrollIntoView?.({ block: "nearest" }),
59
+ )
60
+ }
61
+ }
62
+
63
+ // Not on focus changes: a tap on a button would move it before its click.
64
+ viewport.addEventListener("resize", update)
65
+ viewport.addEventListener("scroll", update)
66
+
67
+ return function stop() {
68
+ viewport.removeEventListener("resize", update)
69
+ viewport.removeEventListener("scroll", update)
70
+ root.style.removeProperty(HEIGHT)
71
+ root.classList.remove(OPEN)
72
+ }
73
+ }
74
+
75
+ function isEditable(element) {
76
+ if (!element) return false
77
+ if (element.isContentEditable) return true
78
+ if (element.tagName === "TEXTAREA") return !element.readOnly
79
+ if (element.tagName === "INPUT") {
80
+ return TEXT_INPUTS.has(element.type) && !element.readOnly
81
+ }
82
+ return false
83
+ }
@@ -0,0 +1,74 @@
1
+ import { isNative } from "../mobile/index.js"
2
+
3
+ const LIGHT_CONTENT = "DARK"
4
+ const DARK_CONTENT = "LIGHT"
5
+ const DEVICE = "DEFAULT"
6
+ const DARK_QUERY = "(prefers-color-scheme: dark)"
7
+ const WATCHED_ATTRIBUTES = ["class", "style", "data-theme"]
8
+
9
+ /**
10
+ * Keeps the status bar and navigation bar readable on the app's own colours.
11
+ * The bars follow the device's light or dark mode, so a light-only app on a
12
+ * phone in dark mode gets white icons on a white page. This reads the app's
13
+ * `color-scheme` instead, which every Inglorious UI theme sets, and follows it
14
+ * when a theme class changes. An app that follows the device keeps the
15
+ * device's style. Does nothing outside a native app.
16
+ *
17
+ * @param {Object} systemBars `SystemBars` from `@capacitor/core`.
18
+ * @returns {() => void} Stops following.
19
+ *
20
+ * @example
21
+ * watchSystemBars(SystemBars)
22
+ */
23
+ export function watchSystemBars(systemBars) {
24
+ if (!isNative()) return () => {}
25
+
26
+ let current = null
27
+ const update = () => {
28
+ const style = styleFor(appColorScheme())
29
+ if (style === current) return
30
+ current = style
31
+ systemBars.setStyle({ style })
32
+ }
33
+
34
+ update()
35
+
36
+ const observer = new MutationObserver(update)
37
+ for (const element of [document.documentElement, document.body]) {
38
+ if (element) {
39
+ observer.observe(element, {
40
+ attributes: true,
41
+ attributeFilter: WATCHED_ATTRIBUTES,
42
+ })
43
+ }
44
+ }
45
+ const media = window.matchMedia?.(DARK_QUERY)
46
+ media?.addEventListener("change", update)
47
+
48
+ return function stop() {
49
+ observer.disconnect()
50
+ media?.removeEventListener("change", update)
51
+ }
52
+ }
53
+
54
+ /**
55
+ * The bars' style for a `color-scheme` value: light content on a dark-only
56
+ * page, dark content on a light-only one, the device's otherwise.
57
+ *
58
+ * @param {string} colorScheme
59
+ * @returns {"DARK" | "LIGHT" | "DEFAULT"}
60
+ */
61
+ export function styleFor(colorScheme) {
62
+ const schemes = colorScheme.split(/\s+/)
63
+ const isDark = schemes.includes("dark")
64
+ const isLight = schemes.includes("light")
65
+
66
+ if (isDark && !isLight) return LIGHT_CONTENT
67
+ if (isLight && !isDark) return DARK_CONTENT
68
+ return DEVICE
69
+ }
70
+
71
+ function appColorScheme() {
72
+ const element = document.body ?? document.documentElement
73
+ return getComputedStyle(element).colorScheme ?? ""
74
+ }
@@ -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,7 @@
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.
5
+ * @returns A function that stops following.
6
+ */
7
+ export declare function watchKeyboard(): () => void
@@ -0,0 +1,23 @@
1
+ /** What `watchSystemBars` needs of `SystemBars` from `@capacitor/core`. */
2
+ export interface SystemBarsPlugin {
3
+ setStyle(options: { style: SystemBarsStyle }): Promise<void>
4
+ }
5
+
6
+ /** `SystemBarsStyle` from `@capacitor/core`: the colour of the background. */
7
+ export type SystemBarsStyle = "DARK" | "LIGHT" | "DEFAULT"
8
+
9
+ /**
10
+ * Keeps the status and navigation bars readable on the app's own colours, as
11
+ * its `color-scheme` sets them, and follows theme changes. Does nothing
12
+ * outside a native app.
13
+ * @returns A function that stops following.
14
+ */
15
+ export declare function watchSystemBars(
16
+ systemBars: SystemBarsPlugin,
17
+ ): () => void
18
+
19
+ /**
20
+ * The bars' style for a `color-scheme` value: `"DARK"` on a dark-only page,
21
+ * `"LIGHT"` on a light-only one, `"DEFAULT"` (the device's) otherwise.
22
+ */
23
+ export declare function styleFor(colorScheme: string): SystemBarsStyle