-
Notifications
You must be signed in to change notification settings - Fork 151
new: notification packages upgrade for Solid 2.0 #893
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
10
commits into
solidjs-community:next
Choose a base branch
from
davedbase:update/v2/notification
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.
+1,325
−45
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
88833b5
update: permission adapted to Solid-2.0
atk cf498ee
Initial commit
davedbase ef5857a
new: notification primitive + permission beta.10 migration
davedbase a8480f4
merge: upstream/update/v2/permission into notification branch
davedbase dbecbbb
Merge branch 'next' into update/v2/notification
davedbase 151fab1
Adapting action/optimstic
davedbase 20f84b0
Beta beta 14
davedbase 9cbcb90
Merge branch 'next' into update/v2/notification
davedbase e7f52d3
Lock update
davedbase c136eba
Documentation fixes caught by CR
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,14 @@ | ||
| --- | ||
| "@solid-primitives/notification": minor | ||
| --- | ||
|
|
||
| Add `@solid-primitives/notification` package (Stage 0) | ||
|
|
||
| New primitives for the browser [Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API). | ||
|
|
||
| - **`isNotificationSupported()`** — SSR-safe runtime check for Notifications API availability. | ||
| - **`makeNotification(title, options?)`** — Non-reactive helper returning `[show, close]`. `show()` creates and returns a `Notification` instance (or `null` when permission is not `"granted"`); calling it again replaces the previous notification. No Solid lifecycle dependency. | ||
| - **`createNotification(title, options?)`** — Reactive primitive returning `{ show, close, notification, supported }`. Accepts reactive accessors for `title` and `options` — their current values are read at `show()` time. The `notification` accessor tracks the live instance, updating to `null` when it is dismissed by the OS or closed programmatically. Cleans up automatically on owner disposal. | ||
| - **`createNotificationPermission()`** — Reactive permission manager returning `{ permission, requestPermission }`. The `permission` accessor reflects `Notification.permission` and updates after each `requestPermission()` call. Degrades gracefully to `"unknown"` on the server or when the Notifications API is unsupported. | ||
|
|
||
| Peer dependencies: `solid-js@^2.0.0-beta.14` and `@solidjs/web@^2.0.0-beta.14`. |
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,5 @@ | ||
| --- | ||
| "@solid-primitives/permission": major | ||
| --- | ||
|
|
||
| updated to Solid-2.0 |
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,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2021 Solid Primitives Working Group | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
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,188 @@ | ||
| <p> | ||
| <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=notification" alt="Solid Primitives notification"> | ||
| </p> | ||
|
|
||
| # @solid-primitives/notification | ||
|
|
||
| [](https://bundlephobia.com/package/@solid-primitives/notification) | ||
| [](https://www.npmjs.com/package/@solid-primitives/notification) | ||
| [](https://github.com/solidjs-community/solid-primitives#contribution-process) | ||
|
|
||
| Primitives for the browser [Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API) with reactive permission management. | ||
|
|
||
| - **`isNotificationSupported`** — SSR-safe check for Notifications API availability. | ||
| - **`makeNotification`** — Non-reactive helper returning `[show, close]`. No Solid lifecycle dependency. | ||
| - **`createNotification`** — Reactive primitive that tracks the live `Notification` instance and cleans up on owner disposal. | ||
| - **`createNotificationPermission`** — Reactive permission manager that exposes a live permission signal and a `requestPermission` function. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @solid-primitives/notification | ||
| # or | ||
| yarn add @solid-primitives/notification | ||
| # or | ||
| pnpm add @solid-primitives/notification | ||
| ``` | ||
|
|
||
| ## How to use it | ||
|
|
||
| ### `isNotificationSupported` | ||
|
|
||
| Returns `true` when the Notifications API is available. Always `false` on the server. | ||
|
|
||
| ```ts | ||
| import { isNotificationSupported } from "@solid-primitives/notification"; | ||
|
|
||
| if (isNotificationSupported()) { | ||
| console.log("notifications available"); | ||
| } | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### `makeNotification` | ||
|
|
||
| Non-reactive helper with no Solid lifecycle dependency. Both returned functions are no-ops when the API is unavailable. | ||
|
|
||
| `show()` returns `null` when `Notification.permission` is not `"granted"` — use `createNotificationPermission` to request permission first. | ||
|
|
||
| Because `makeNotification` has no reactive owner, **cleanup is the caller's responsibility**. Inside a reactive scope, register `close` with `onCleanup`: | ||
|
|
||
| ```ts | ||
| import { onCleanup } from "solid-js"; | ||
| import { makeNotification } from "@solid-primitives/notification"; | ||
|
|
||
| const [show, close] = makeNotification("New message", { body: "Hello!" }); | ||
|
|
||
| // Register cleanup with the current reactive owner | ||
| onCleanup(close); | ||
|
|
||
| button.addEventListener("click", () => show()); | ||
|
|
||
| // Or close programmatically at any time | ||
| close(); | ||
| ``` | ||
|
|
||
| Outside a reactive scope (e.g. in plain event handlers), call `close()` directly when done. | ||
|
|
||
| --- | ||
|
|
||
| ### `createNotification` | ||
|
|
||
| Reactive primitive tied to the current reactive owner. | ||
|
|
||
| - `title` and `options` can be plain values **or** reactive accessors — their current values are read each time `show()` is called. | ||
| - `notification` is a reactive `Accessor<Notification | null>` that reflects the live instance, updating to `null` when the notification is dismissed (either programmatically or by the OS). | ||
| - The notification is automatically closed when the reactive owner is disposed. | ||
| - Pass an optional `handlers` object to respond to notification events. | ||
|
|
||
| ```ts | ||
| import { createEffect } from "solid-js"; | ||
| import { createNotification } from "@solid-primitives/notification"; | ||
|
|
||
| const { show, close, notification, supported } = createNotification( | ||
| () => `You have ${unread()} messages`, | ||
| { icon: "/icon.png" }, | ||
| { | ||
| onClick: n => { window.focus(); }, | ||
| onClose: n => { console.log("dismissed"); }, | ||
| onError: n => { console.error("notification failed"); }, | ||
| }, | ||
| ); | ||
|
|
||
| // Show a notification (reads reactive title at call time) | ||
| show(); | ||
|
|
||
| // React to visibility changes | ||
| createEffect(() => { | ||
| if (notification()) console.log("notification visible"); | ||
| else console.log("notification gone"); | ||
| }); | ||
|
|
||
| // Close programmatically | ||
| close(); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### `createNotificationPermission` | ||
|
|
||
| Reactive permission manager built on the browser [Permissions API](https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API). | ||
|
|
||
| The `permission` accessor reflects the **live** permission state and updates automatically whenever it changes — including after `requestPermission()` resolves or the user edits their browser settings directly. | ||
|
|
||
| Permission values follow Permissions API vocabulary: `"granted"`, `"denied"`, `"prompt"` (not yet asked), or `"unknown"` while the initial async query is still resolving. Note that the Notifications API uses `"default"` for the same concept that the Permissions API calls `"prompt"`. | ||
|
|
||
| On the server or when the API is unavailable, `permission` always returns `"unknown"` and `requestPermission` resolves immediately without effect. | ||
|
|
||
| ```ts | ||
| import { Show } from "solid-js"; | ||
| import { createNotificationPermission } from "@solid-primitives/notification"; | ||
|
|
||
| const { permission, requestPermission } = createNotificationPermission(); | ||
|
|
||
| // Gate UI on permission state | ||
| <Show when={permission() !== "granted"}> | ||
| <button onClick={requestPermission}>Enable notifications</button> | ||
| </Show> | ||
|
|
||
| // Call without expecting a return value — permission() updates reactively after it resolves | ||
| requestPermission(); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### Full example | ||
|
|
||
| ```tsx | ||
| import { Component, Show } from "solid-js"; | ||
| import { | ||
| createNotification, | ||
| createNotificationPermission, | ||
| isNotificationSupported, | ||
| } from "@solid-primitives/notification"; | ||
|
|
||
| const NotificationDemo: Component = () => { | ||
| const { permission, requestPermission } = createNotificationPermission(); | ||
| const { show, close, notification } = createNotification( | ||
| "Solid Primitives", | ||
| { body: "Hello from SolidJS!" }, | ||
| { onClick: () => window.focus() }, | ||
| ); | ||
|
|
||
| return ( | ||
| <Show when={isNotificationSupported()} fallback={<p>Not supported</p>}> | ||
| <p>Permission: {permission()}</p> | ||
| <p>Active: {notification() ? "yes" : "no"}</p> | ||
| <Show when={permission() !== "granted"}> | ||
| <button onClick={requestPermission}>Request permission</button> | ||
| </Show> | ||
| <button onClick={() => show()}>Show</button> | ||
| <button onClick={close}>Close</button> | ||
| </Show> | ||
| ); | ||
| }; | ||
| ``` | ||
|
|
||
| ## Types | ||
|
|
||
| ```ts | ||
| /** Event handler callbacks for `createNotification`. */ | ||
| type NotificationEventHandlers = { | ||
| /** Called when the user clicks the notification. */ | ||
| onClick?: (notification: Notification) => void; | ||
| /** Called when the notification is dismissed, whether by the user, the OS, or `close()`. */ | ||
| onClose?: (notification: Notification) => void; | ||
| /** Called when the notification fails to display. */ | ||
| onError?: (notification: Notification) => void; | ||
| }; | ||
| ``` | ||
|
|
||
| ## Browser Support | ||
|
|
||
| The [Notifications API](https://developer.mozilla.org/en-US/docs/Web/API/Notifications_API#browser_compatibility) is supported in all modern browsers. It is not available in iOS Safari (as of 2025) or on the server. All primitives degrade gracefully — `show()` returns `null`, `close()` is a no-op, and `permission()` returns `"unknown"`. | ||
|
|
||
| ## 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { type Component, createSignal, Show } from "solid-js"; | ||
| import { | ||
| isNotificationSupported, | ||
| createNotification, | ||
| createNotificationPermission, | ||
| } from "../src/index.js"; | ||
|
|
||
| const App: Component = () => { | ||
| const supported = isNotificationSupported(); | ||
| const [body, setBody] = createSignal("Hello from Solid Primitives!"); | ||
| const { permission, requestPermission } = createNotificationPermission(); | ||
| const { show, close, notification } = createNotification( | ||
| () => "Solid Primitives Notification", | ||
| () => ({ body: body() }), | ||
| ); | ||
|
|
||
| return ( | ||
| <div class="box-border flex min-h-screen w-full flex-col items-center justify-center space-y-4 bg-gray-800 p-24 text-white"> | ||
| <div class="wrapper-v"> | ||
| <h4>Notification Primitive</h4> | ||
|
|
||
| <Show | ||
| when={supported} | ||
| fallback={<p class="caption">Notifications API is not supported in this browser.</p>} | ||
| > | ||
| <p class="caption"> | ||
| Permission: <strong>{permission()}</strong> | ||
| </p> | ||
| <p class="caption"> | ||
| Active notification: <strong>{notification() ? "visible" : "none"}</strong> | ||
| </p> | ||
|
|
||
| <label class="caption"> | ||
| Body text: | ||
| <input | ||
| type="text" | ||
| value={body()} | ||
| onInput={e => setBody(e.currentTarget.value)} | ||
| class="ml-2 rounded bg-gray-700 px-2 py-1 text-white" | ||
| /> | ||
| </label> | ||
|
|
||
| <div class="flex gap-2"> | ||
| <Show when={permission() !== "granted"}> | ||
| <button class="btn" onClick={requestPermission}> | ||
| Request permission | ||
| </button> | ||
| </Show> | ||
| <button class="btn" onClick={() => show()}> | ||
| Show notification | ||
| </button> | ||
| <button class="btn" onClick={close}> | ||
| Close notification | ||
| </button> | ||
| </div> | ||
| </Show> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default App; |
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,70 @@ | ||
| { | ||
| "name": "@solid-primitives/notification", | ||
| "version": "0.0.100", | ||
| "description": "Primitives for the browser Notifications API with reactive permission management", | ||
| "author": "David Di Biase <dave@solidjs.com>", | ||
| "contributors": [], | ||
| "license": "MIT", | ||
| "homepage": "https://primitives.solidjs.community/package/notification", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/solidjs-community/solid-primitives.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/solidjs-community/solid-primitives/issues" | ||
| }, | ||
| "primitive": { | ||
| "name": "notification", | ||
| "stage": 0, | ||
| "list": [ | ||
| "isNotificationSupported", | ||
| "makeNotification", | ||
| "createNotification", | ||
| "createNotificationPermission" | ||
| ], | ||
| "category": "Browser APIs" | ||
| }, | ||
| "keywords": [ | ||
| "solid", | ||
| "notification", | ||
| "browser", | ||
| "permission", | ||
| "primitives" | ||
| ], | ||
| "private": false, | ||
| "sideEffects": false, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "type": "module", | ||
| "module": "./dist/index.js", | ||
| "types": "./dist/index.d.ts", | ||
| "browser": {}, | ||
| "exports": { | ||
| "import": { | ||
| "@solid-primitives/source": "./src/index.ts", | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
| }, | ||
| "typesVersions": {}, | ||
| "scripts": { | ||
| "dev": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/dev.ts", | ||
| "build": "node --import=@nothing-but/node-resolve-ts --experimental-transform-types ../../scripts/build.ts", | ||
| "vitest": "vitest -c ../../configs/vitest.config.ts", | ||
| "test": "pnpm run vitest", | ||
| "test:ssr": "pnpm run vitest --mode ssr" | ||
| }, | ||
| "peerDependencies": { | ||
| "@solidjs/web": "^2.0.0-beta.14", | ||
| "solid-js": "^2.0.0-beta.14" | ||
| }, | ||
| "dependencies": { | ||
| "@solid-primitives/permission": "workspace:^", | ||
| "@solid-primitives/utils": "workspace:^" | ||
| }, | ||
| "devDependencies": { | ||
| "@solidjs/web": "2.0.0-beta.14", | ||
| "solid-js": "2.0.0-beta.14" | ||
| } | ||
| } |
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.