-
Notifications
You must be signed in to change notification settings - Fork 152
upgrade: page-utilities packages migration and adaptation upgrade for Solid 2.0 #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davedbase
wants to merge
5
commits into
solidjs-community:next
Choose a base branch
from
davedbase:update/v2/page-visibility
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5bff218
Migrated to Solid 2.0 and added createPageLeaveBlocker primitive
davedbase dc25a6d
Adjust copy
davedbase 509e581
Merge branch 'next' into update/v2/page-visibility
davedbase c61e964
Merge branch 'next' into update/v2/page-visibility
davedbase cebf743
Clarical cleanup
davedbase File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@solid-primitives/page-utilities": major | ||
| --- | ||
|
|
||
| Migrate to Solid.js v2.0 (beta.13) | ||
|
|
||
| ## Breaking Changes | ||
|
|
||
| **Peer dependencies**: `solid-js@^2.0.0-beta.13` and `@solidjs/web@^2.0.0-beta.13` are now required. | ||
|
|
||
| - `isServer` is now imported from `@solidjs/web` (was `solid-js/web`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/page-visibility/CHANGELOG.md → packages/page-utilities/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # @solid-primitives/page-visibility | ||
| # @solid-primitives/page-utilities | ||
|
|
||
| ## 2.1.5 | ||
|
|
||
|
|
||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <p> | ||
| <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Page%20Utilities" alt="Solid Primitives Page Utilities"> | ||
| </p> | ||
|
|
||
| # @solid-primitives/page-utilities | ||
|
|
||
| [](https://bundlephobia.com/package/@solid-primitives/page-utilities) | ||
| [](https://www.npmjs.com/package/@solid-primitives/page-utilities) | ||
| [](https://github.com/solidjs-community/solid-primitives#contribution-process) | ||
|
|
||
| Primitives for tracking page visibility and intercepting navigation away from the page. | ||
|
|
||
| - [`createPageVisibility`](#createpagevisibility) - Reactive signal tracking whether the page is currently visible | ||
| - [`usePageVisibility`](#usepagevisibility) - Shared [singleton root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) version of `createPageVisibility` | ||
| - [`makePageLeave`](#makepageleave) - Intercepts `beforeunload` to prevent navigation; returns a manual cleanup function | ||
| - [`createPageLeaveBlocker`](#createpageleaveblocker) - Reactive version of `makePageLeave`; accepts a signal to toggle prevention | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @solid-primitives/page-utilities | ||
| # or | ||
| yarn add @solid-primitives/page-utilities | ||
| # or | ||
| pnpm add @solid-primitives/page-utilities | ||
| ``` | ||
|
|
||
| ## `createPageVisibility` | ||
|
|
||
| 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`. | ||
|
|
||
| ```ts | ||
| import { createPageVisibility } from "@solid-primitives/page-utilities"; | ||
|
|
||
| const visible = createPageVisibility(); | ||
|
|
||
| createEffect(() => { | ||
| console.log(visible()); // => boolean | ||
| }); | ||
| ``` | ||
|
|
||
| ### Definition | ||
|
|
||
| ```ts | ||
| function createPageVisibility(): Accessor<boolean>; | ||
| ``` | ||
|
|
||
| ## `usePageVisibility` | ||
|
|
||
| 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. | ||
|
|
||
| ```ts | ||
| import { usePageVisibility } from "@solid-primitives/page-utilities"; | ||
|
|
||
| const visible = usePageVisibility(); | ||
|
|
||
| createEffect(() => { | ||
| console.log(visible()); // => boolean | ||
| }); | ||
| ``` | ||
|
|
||
| ### Definition | ||
|
|
||
| ```ts | ||
| const usePageVisibility: () => Accessor<boolean>; | ||
| ``` | ||
|
|
||
| ## `makePageLeave` | ||
|
|
||
| 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. | ||
|
|
||
| ```ts | ||
| import { makePageLeave } from "@solid-primitives/page-utilities"; | ||
|
|
||
| const cleanup = makePageLeave(); | ||
|
|
||
| // remove the listener when done | ||
| cleanup(); | ||
| ``` | ||
|
|
||
| ### Definition | ||
|
|
||
| ```ts | ||
| function makePageLeave(): VoidFunction; | ||
| ``` | ||
|
|
||
| ## `createPageLeaveBlocker` | ||
|
|
||
| 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. | ||
|
|
||
| ```ts | ||
| import { createPageLeaveBlocker } from "@solid-primitives/page-utilities"; | ||
|
|
||
| // Always block navigation | ||
| createPageLeaveBlocker(); | ||
| ``` | ||
|
|
||
| A common pattern is gating on unsaved state: | ||
|
|
||
| ```ts | ||
| const [isDirty, setIsDirty] = createSignal(false); | ||
|
|
||
| createPageLeaveBlocker(isDirty); | ||
| ``` | ||
|
|
||
| ### Definition | ||
|
|
||
| ```ts | ||
| function createPageLeaveBlocker(enabled?: MaybeAccessor<boolean>): void; | ||
| ``` | ||
|
|
||
| ## Changelog | ||
|
|
||
| See [CHANGELOG.md](./CHANGELOG.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { type Accessor, createEffect, onCleanup } from "solid-js"; | ||
| import { isServer } from "@solidjs/web"; | ||
| import { createHydratableSingletonRoot } from "@solid-primitives/rootless"; | ||
| import { createHydratableSignal, INTERNAL_OPTIONS, trueFn, type MaybeAccessor } from "@solid-primitives/utils"; | ||
| import { makeEventListener } from "@solid-primitives/event-listener"; | ||
|
|
||
| /** | ||
| * Creates a signal with a boolean value identifying the page visibility state. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const visible = createPageVisibility(); | ||
| * | ||
| * createEffect(() => { | ||
| * visible() // => boolean | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export const createPageVisibility = (): Accessor<boolean> => { | ||
| if (isServer) { | ||
| return trueFn; | ||
| } | ||
| const checkVisibility = () => document.visibilityState === "visible"; | ||
| const [isVisible, setVisible] = createHydratableSignal(true, checkVisibility, INTERNAL_OPTIONS); | ||
| makeEventListener(document, "visibilitychange", () => setVisible(checkVisibility)); | ||
| return isVisible; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns a signal with a boolean value identifying the page visibility state. | ||
| * | ||
| * This is a [singleton root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSingletonRoot) except if during hydration. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const visible = usePageVisibility(); | ||
| * | ||
| * createEffect(() => { | ||
| * visible() // => boolean | ||
| * }) | ||
| * ``` | ||
| */ | ||
| export const usePageVisibility: () => Accessor<boolean> = | ||
| /*#__PURE__*/ createHydratableSingletonRoot(createPageVisibility); | ||
|
|
||
| /** | ||
| * Intercepts the browser's `beforeunload` event to show a confirmation dialog | ||
| * when the user attempts to close the tab, navigate away, or refresh. | ||
| * Returns a cleanup function to remove the listener. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const cleanup = makePageLeave(); | ||
| * // later: | ||
| * cleanup(); | ||
| * ``` | ||
| */ | ||
| export function makePageLeave(): VoidFunction { | ||
| const handler = (e: BeforeUnloadEvent) => { | ||
| e.preventDefault(); | ||
| }; | ||
| window.addEventListener("beforeunload", handler); | ||
| return () => window.removeEventListener("beforeunload", handler); | ||
| } | ||
|
|
||
| /** | ||
| * Intercepts the browser's `beforeunload` event to show a confirmation dialog | ||
| * when the user attempts to close the tab, navigate away, or refresh. | ||
| * | ||
| * Pass a reactive signal as {@link enabled} to toggle prevention on and off. | ||
| * Automatically removes the listener when the reactive scope is disposed. | ||
| * | ||
| * @param enabled - whether to block navigation. Defaults to `true`. Accepts a boolean or a reactive accessor. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Always block navigation | ||
| * createPageLeaveBlocker(); | ||
| * | ||
| * // Only block when there are unsaved changes | ||
| * const [isDirty, setIsDirty] = createSignal(false); | ||
| * createPageLeaveBlocker(isDirty); | ||
| * ``` | ||
| */ | ||
| export function createPageLeaveBlocker(enabled: MaybeAccessor<boolean> = true): void { | ||
| if (isServer) return; | ||
|
|
||
| if (typeof enabled !== "function") { | ||
| if (!enabled) return; | ||
| onCleanup(makePageLeave()); | ||
| return; | ||
| } | ||
|
|
||
| createEffect(enabled, isEnabled => { | ||
| if (!isEnabled) return; | ||
| return makePageLeave(); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.