@inglorious/web 6.21.0 → 6.22.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,21 @@ 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
+
1608
1623
  ## Forms
1609
1624
 
1610
1625
  `@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.22.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,10 @@
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
+ },
48
52
  "./decorators/with-debounce": {
49
53
  "types": "./types/decorators/with-debounce.d.ts",
50
54
  "import": "./src/decorators/with-debounce/index.js"
@@ -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 `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