@solid-primitives/page-utilities 3.0.0-next.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/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +95 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Solid Primitives Working Group
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
<p>
|
|
2
|
+
<img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Page%20Utilities" alt="Solid Primitives Page Utilities">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# @solid-primitives/page-utilities
|
|
6
|
+
|
|
7
|
+
[](https://bundlephobia.com/package/@solid-primitives/page-utilities)
|
|
8
|
+
[](https://www.npmjs.com/package/@solid-primitives/page-utilities)
|
|
9
|
+
[](https://github.com/solidjs-community/solid-primitives#contribution-process)
|
|
10
|
+
[](https://vitest.dev)
|
|
11
|
+
|
|
12
|
+
Primitives for tracking page visibility and intercepting navigation away from the page.
|
|
13
|
+
|
|
14
|
+
- [`createPageVisibility`](#createpagevisibility) - Reactive signal tracking whether the page is currently visible
|
|
15
|
+
- [`usePageVisibility`](#usepagevisibility) - Shared [singleton root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) version of `createPageVisibility`
|
|
16
|
+
- [`makePageLeave`](#makepageleave) - Intercepts `beforeunload` to prevent navigation; returns a manual cleanup function
|
|
17
|
+
- [`createPageLeaveBlocker`](#createpageleaveblocker) - Reactive version of `makePageLeave`; accepts a signal to toggle prevention
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @solid-primitives/page-utilities
|
|
23
|
+
# or
|
|
24
|
+
yarn add @solid-primitives/page-utilities
|
|
25
|
+
# or
|
|
26
|
+
pnpm add @solid-primitives/page-utilities
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## `createPageVisibility`
|
|
30
|
+
|
|
31
|
+
Returns a reactive boolean signal reflecting the [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) — `true` when the page is visible, `false` when hidden or in a prerender state. On the server it always returns `true`.
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { createPageVisibility } from "@solid-primitives/page-utilities";
|
|
35
|
+
|
|
36
|
+
const visible = createPageVisibility();
|
|
37
|
+
|
|
38
|
+
createEffect(() => {
|
|
39
|
+
console.log(visible()); // => boolean
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Definition
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
function createPageVisibility(): Accessor<boolean>;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## `usePageVisibility`
|
|
50
|
+
|
|
51
|
+
A [singleton root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) version of `createPageVisibility`. The underlying event listener and signal are shared across all callers, making it more efficient when used in multiple places simultaneously.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { usePageVisibility } from "@solid-primitives/page-utilities";
|
|
55
|
+
|
|
56
|
+
const visible = usePageVisibility();
|
|
57
|
+
|
|
58
|
+
createEffect(() => {
|
|
59
|
+
console.log(visible()); // => boolean
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Definition
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const usePageVisibility: () => Accessor<boolean>;
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## `makePageLeave`
|
|
70
|
+
|
|
71
|
+
Intercepts the browser's [`beforeunload`](https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event) event to show a confirmation dialog when the user attempts to close the tab, refresh, or navigate away. Returns a cleanup function to remove the listener.
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { makePageLeave } from "@solid-primitives/page-utilities";
|
|
75
|
+
|
|
76
|
+
const cleanup = makePageLeave();
|
|
77
|
+
|
|
78
|
+
// remove the listener when done
|
|
79
|
+
cleanup();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Definition
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
function makePageLeave(): VoidFunction;
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## `createPageLeaveBlocker`
|
|
89
|
+
|
|
90
|
+
Reactive version of `makePageLeave`. Accepts an optional `enabled` parameter — a static boolean or a reactive signal — to toggle prevention on and off. Defaults to `true`. Automatically removes the listener when the reactive owner is disposed. No-ops on the server.
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { createPageLeaveBlocker } from "@solid-primitives/page-utilities";
|
|
94
|
+
|
|
95
|
+
// Always block navigation
|
|
96
|
+
createPageLeaveBlocker();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
A common pattern is gating on unsaved state:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const [isDirty, setIsDirty] = createSignal(false);
|
|
103
|
+
|
|
104
|
+
createPageLeaveBlocker(isDirty);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Definition
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
function createPageLeaveBlocker(enabled?: MaybeAccessor<boolean>): void;
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Changelog
|
|
114
|
+
|
|
115
|
+
See [CHANGELOG.md](./CHANGELOG.md)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Accessor } from "solid-js";
|
|
2
|
+
import { type MaybeAccessor } from "@solid-primitives/utils";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a signal with a boolean value identifying the page visibility state.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const visible = createPageVisibility();
|
|
9
|
+
*
|
|
10
|
+
* createEffect(() => {
|
|
11
|
+
* visible() // => boolean
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare const createPageVisibility: () => Accessor<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Returns a signal with a boolean value identifying the page visibility state.
|
|
18
|
+
*
|
|
19
|
+
* This is a [singleton root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) except if during hydration.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const visible = usePageVisibility();
|
|
24
|
+
*
|
|
25
|
+
* createEffect(() => {
|
|
26
|
+
* visible() // => boolean
|
|
27
|
+
* })
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare const usePageVisibility: () => Accessor<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Intercepts the browser's `beforeunload` event to show a confirmation dialog
|
|
33
|
+
* when the user attempts to close the tab, navigate away, or refresh.
|
|
34
|
+
* Returns a cleanup function to remove the listener.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const cleanup = makePageLeave();
|
|
39
|
+
* // later:
|
|
40
|
+
* cleanup();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function makePageLeave(): VoidFunction;
|
|
44
|
+
/**
|
|
45
|
+
* Intercepts the browser's `beforeunload` event to show a confirmation dialog
|
|
46
|
+
* when the user attempts to close the tab, navigate away, or refresh.
|
|
47
|
+
*
|
|
48
|
+
* Pass a reactive signal as {@link enabled} to toggle prevention on and off.
|
|
49
|
+
* Automatically removes the listener when the reactive scope is disposed.
|
|
50
|
+
*
|
|
51
|
+
* @param enabled - whether to block navigation. Defaults to `true`. Accepts a boolean or a reactive accessor.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* // Always block navigation
|
|
56
|
+
* createPageLeaveBlocker();
|
|
57
|
+
*
|
|
58
|
+
* // Only block when there are unsaved changes
|
|
59
|
+
* const [isDirty, setIsDirty] = createSignal(false);
|
|
60
|
+
* createPageLeaveBlocker(isDirty);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function createPageLeaveBlocker(enabled?: MaybeAccessor<boolean>): void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { createEffect, onCleanup } from "solid-js";
|
|
2
|
+
import { isServer } from "@solidjs/web";
|
|
3
|
+
import { createHydratableSingletonRoot } from "@solid-primitives/rootless";
|
|
4
|
+
import { createHydratableSignal, INTERNAL_OPTIONS, trueFn } from "@solid-primitives/utils";
|
|
5
|
+
import { makeEventListener } from "@solid-primitives/event-listener";
|
|
6
|
+
/**
|
|
7
|
+
* Creates a signal with a boolean value identifying the page visibility state.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const visible = createPageVisibility();
|
|
12
|
+
*
|
|
13
|
+
* createEffect(() => {
|
|
14
|
+
* visible() // => boolean
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export const createPageVisibility = () => {
|
|
19
|
+
if (isServer) {
|
|
20
|
+
return trueFn;
|
|
21
|
+
}
|
|
22
|
+
const checkVisibility = () => document.visibilityState === "visible";
|
|
23
|
+
const [isVisible, setVisible] = createHydratableSignal(true, checkVisibility, INTERNAL_OPTIONS);
|
|
24
|
+
makeEventListener(document, "visibilitychange", () => setVisible(checkVisibility));
|
|
25
|
+
return isVisible;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Returns a signal with a boolean value identifying the page visibility state.
|
|
29
|
+
*
|
|
30
|
+
* This is a [singleton root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) except if during hydration.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const visible = usePageVisibility();
|
|
35
|
+
*
|
|
36
|
+
* createEffect(() => {
|
|
37
|
+
* visible() // => boolean
|
|
38
|
+
* })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export const usePageVisibility =
|
|
42
|
+
/*#__PURE__*/ createHydratableSingletonRoot(createPageVisibility);
|
|
43
|
+
/**
|
|
44
|
+
* Intercepts the browser's `beforeunload` event to show a confirmation dialog
|
|
45
|
+
* when the user attempts to close the tab, navigate away, or refresh.
|
|
46
|
+
* Returns a cleanup function to remove the listener.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const cleanup = makePageLeave();
|
|
51
|
+
* // later:
|
|
52
|
+
* cleanup();
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export function makePageLeave() {
|
|
56
|
+
const handler = (e) => {
|
|
57
|
+
e.preventDefault();
|
|
58
|
+
};
|
|
59
|
+
window.addEventListener("beforeunload", handler);
|
|
60
|
+
return () => window.removeEventListener("beforeunload", handler);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Intercepts the browser's `beforeunload` event to show a confirmation dialog
|
|
64
|
+
* when the user attempts to close the tab, navigate away, or refresh.
|
|
65
|
+
*
|
|
66
|
+
* Pass a reactive signal as {@link enabled} to toggle prevention on and off.
|
|
67
|
+
* Automatically removes the listener when the reactive scope is disposed.
|
|
68
|
+
*
|
|
69
|
+
* @param enabled - whether to block navigation. Defaults to `true`. Accepts a boolean or a reactive accessor.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* // Always block navigation
|
|
74
|
+
* createPageLeaveBlocker();
|
|
75
|
+
*
|
|
76
|
+
* // Only block when there are unsaved changes
|
|
77
|
+
* const [isDirty, setIsDirty] = createSignal(false);
|
|
78
|
+
* createPageLeaveBlocker(isDirty);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
export function createPageLeaveBlocker(enabled = true) {
|
|
82
|
+
if (isServer)
|
|
83
|
+
return;
|
|
84
|
+
if (typeof enabled !== "function") {
|
|
85
|
+
if (!enabled)
|
|
86
|
+
return;
|
|
87
|
+
onCleanup(makePageLeave());
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
createEffect(enabled, isEnabled => {
|
|
91
|
+
if (!isEnabled)
|
|
92
|
+
return;
|
|
93
|
+
return makePageLeave();
|
|
94
|
+
});
|
|
95
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@solid-primitives/page-utilities",
|
|
3
|
+
"version": "3.0.0-next.0",
|
|
4
|
+
"description": "Primitives for page visibility and navigation blocking",
|
|
5
|
+
"author": "David Di Biase <dave@solidjs.com>",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Damian Tarnawski <gthetarnav@gmail.com>",
|
|
8
|
+
"Tom Pichaud <dev.tompichaud@icloud.com>"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"homepage": "https://primitives.solidjs.community/package/page-utilities",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/solidjs-community/solid-primitives.git"
|
|
15
|
+
},
|
|
16
|
+
"primitive": {
|
|
17
|
+
"name": "page-utilities",
|
|
18
|
+
"stage": 3,
|
|
19
|
+
"list": [
|
|
20
|
+
"createPageVisibility",
|
|
21
|
+
"usePageVisibility",
|
|
22
|
+
"makePageLeave",
|
|
23
|
+
"createPageLeaveBlocker"
|
|
24
|
+
],
|
|
25
|
+
"category": "Display & Media",
|
|
26
|
+
"gzip": 747
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"private": false,
|
|
32
|
+
"sideEffects": false,
|
|
33
|
+
"type": "module",
|
|
34
|
+
"module": "./dist/index.js",
|
|
35
|
+
"browser": {},
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
|
+
"exports": {
|
|
38
|
+
"import": {
|
|
39
|
+
"@solid-primitives/source": "./src/index.ts",
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"default": "./dist/index.js"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"page",
|
|
46
|
+
"visibility",
|
|
47
|
+
"navigation",
|
|
48
|
+
"solid",
|
|
49
|
+
"primitives"
|
|
50
|
+
],
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@solid-primitives/event-listener": "^3.0.0-next.0",
|
|
53
|
+
"@solid-primitives/utils": "^7.0.0-next.0",
|
|
54
|
+
"@solid-primitives/rootless": "^2.0.0-next.0"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@solidjs/web": "^2.0.0-beta.15",
|
|
58
|
+
"solid-js": "^2.0.0-beta.15"
|
|
59
|
+
},
|
|
60
|
+
"typesVersions": {},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@solidjs/web": "2.0.0-beta.15",
|
|
63
|
+
"solid-js": "2.0.0-beta.15"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"dev": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/dev.ts",
|
|
67
|
+
"build": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/build.ts",
|
|
68
|
+
"vitest": "vitest -c ../../configs/vitest.config.ts",
|
|
69
|
+
"test": "pnpm run vitest",
|
|
70
|
+
"test:ssr": "pnpm run vitest --mode ssr",
|
|
71
|
+
"test:watch": "watchlist src test -- npm test"
|
|
72
|
+
}
|
|
73
|
+
}
|