@inglorious/web 6.20.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,37 @@ 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
+
1591
+ ---
1592
+
1593
+ ## Back Button
1594
+
1595
+ On Android, the back button and the back gesture go back in history. `@inglorious/web/back-button` closes the app instead on a root path, a top-level page like a tab bar's, and when there is no history left, as other Android apps do. It does nothing outside a native app, and iOS has no back button.
1596
+
1597
+ ```javascript
1598
+ import { App } from "@capacitor/app"
1599
+ import { watchBackButton } from "@inglorious/web/back-button"
1600
+
1601
+ watchBackButton(App, { rootPaths: ["/", "/search", "/profile"] })
1602
+ ```
1603
+
1604
+ `rootPaths` defaults to `["/"]`; it returns a function that stops listening. Navigate between root paths with `{ to, replace: true }`, so switching tabs does not pile up history.
1605
+
1606
+ ---
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.
1590
1620
 
1591
1621
  ---
1592
1622
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.20.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": [
@@ -41,6 +41,14 @@
41
41
  "types": "./types/app-links.d.ts",
42
42
  "import": "./src/app-links/index.js"
43
43
  },
44
+ "./back-button": {
45
+ "types": "./types/back-button.d.ts",
46
+ "import": "./src/back-button/index.js"
47
+ },
48
+ "./system-bars": {
49
+ "types": "./types/system-bars.d.ts",
50
+ "import": "./src/system-bars/index.js"
51
+ },
44
52
  "./decorators/with-debounce": {
45
53
  "types": "./types/decorators/with-debounce.d.ts",
46
54
  "import": "./src/decorators/with-debounce/index.js"
@@ -0,0 +1,35 @@
1
+ import { isNative } from "../mobile/index.js"
2
+
3
+ const DEFAULT_ROOT_PATHS = ["/"]
4
+ const BACK_BUTTON_EVENT = "backButton"
5
+
6
+ /**
7
+ * Android's back button and back gesture, as other Android apps have them:
8
+ * they go back in history, and close the app on a root path or when there is
9
+ * no history left. Root paths are the top-level pages, like a tab bar's: going
10
+ * back from one leaves the app at once. Does nothing outside a native app, and
11
+ * iOS sends no back button.
12
+ *
13
+ * @param {Object} app `App` from `@capacitor/app`.
14
+ * @param {{ rootPaths?: string[] }} [options] `["/"]` by default.
15
+ * @returns {() => Promise<void>} Stops listening.
16
+ *
17
+ * @example
18
+ * watchBackButton(App, { rootPaths: ["/", "/search", "/profile"] })
19
+ */
20
+ export function watchBackButton(app, { rootPaths = DEFAULT_ROOT_PATHS } = {}) {
21
+ if (!isNative()) return async () => {}
22
+
23
+ const listener = app.addListener(BACK_BUTTON_EVENT, ({ canGoBack }) => {
24
+ if (canGoBack && !rootPaths.includes(window.location.pathname)) {
25
+ window.history.back()
26
+ } else {
27
+ app.exitApp()
28
+ }
29
+ })
30
+
31
+ return async function stop() {
32
+ const handle = await listener
33
+ await handle.remove()
34
+ }
35
+ }
@@ -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,24 @@
1
+ /** What `watchBackButton` needs of `App` from `@capacitor/app`. */
2
+ export interface BackButtonApp {
3
+ exitApp(): Promise<void>
4
+ addListener(
5
+ event: "backButton",
6
+ listener: (event: { canGoBack: boolean }) => void,
7
+ ): Promise<{ remove(): Promise<void> }>
8
+ }
9
+
10
+ export interface WatchBackButtonOptions {
11
+ /** The top-level paths, where back closes the app. Defaults to `["/"]`. */
12
+ rootPaths?: string[]
13
+ }
14
+
15
+ /**
16
+ * Android's back button and back gesture: back in history, or closes the app
17
+ * on a root path or when there is no history left. Does nothing outside a
18
+ * native app.
19
+ * @returns A function that stops listening.
20
+ */
21
+ export declare function watchBackButton(
22
+ app: BackButtonApp,
23
+ options?: WatchBackButtonOptions,
24
+ ): () => Promise<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