@inglorious/web 6.20.0 → 6.21.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 +15 -0
- package/package.json +5 -1
- package/src/back-button/index.js +35 -0
- package/types/back-button.d.ts +24 -0
package/README.md
CHANGED
|
@@ -1590,6 +1590,21 @@ The operating system opens the app only when the domain vouches for it: iOS read
|
|
|
1590
1590
|
|
|
1591
1591
|
---
|
|
1592
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
|
+
|
|
1593
1608
|
## Forms
|
|
1594
1609
|
|
|
1595
1610
|
`@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.
|
|
3
|
+
"version": "6.21.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,10 @@
|
|
|
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
|
+
},
|
|
44
48
|
"./decorators/with-debounce": {
|
|
45
49
|
"types": "./types/decorators/with-debounce.d.ts",
|
|
46
50
|
"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,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>
|