A quiet banner that offers to turn on notifications. The browser dialog opens only after a click on “Allow”, because browsers ignore or block a request without a user gesture. “Not now” hides the banner for 7 days. When the permission is already granted, or the browser has no Notification API (iPhone Safari outside an installed app, for example), nothing is shown. JavaScript here only reads the permission and remembers the dismissal; the banner slides in with @starting-style and slides out with a discrete display transition.
Browser Support
Shows the permission dialog and system messages
Animates the initial appearance
Animates closing
Preview
The demo uses forceShow, so the banner is visible even when this browser has already answered. The button asks the browser for real, and a granted permission sends a test message.
Browser permission: unsupported
Installation
The banner is built on the useNotificationPermission hook, which is installed along with it.
Usage
The banner decides on its own. It shows up after a second, only while the permission is undecided and the user has not declined recently. To show it on an event instead of a timer, render it conditionally.
import { NotificationPrompt } from "@/components/ui/notification-prompt"
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<>
{children}
<NotificationPrompt
floating
onGranted={() => new Notification("Notifications are on")}
/>
</>
)
}Examples
Hint when blocked
By default a blocked permission hides the banner, since the page cannot ask again. With whenDenied=“hint” the banner explains where to change it.
// forceShow="denied" is only for the demo; in a real app the banner
// switches to the hint on its own once the permission is blocked
<NotificationPrompt whenDenied="hint" forceShow="denied" />Compact
compact turns the banner into one row. For toolbars, sidebars and narrow places.
<NotificationPrompt
compact
delay={3000}
dismissDays={30}
labels={{ title: "Get a ping when your build finishes" }}
/>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| delay | number | 1000 | Milliseconds to wait before the banner appears |
| whenDenied | "hide" | "hint" | "hide" | What to do when the permission is already blocked |
| compact | boolean | false | One row, no description |
| floating | boolean | false | Pin to the bottom of the viewport |
| icon | ReactNode | bell icon | Icon on the left |
| labels | Partial<NotificationPromptLabels> | - | All texts, including the hint for a blocked permission |
| dismissDays | number | 7 | Days to stay hidden after “Not now” |
| storageKey | string | "notification-prompt-dismissed-at" | localStorage key for the dismissal time |
| forceShow | "default" | "denied" | - | Show the banner whatever the permission is. For demos and design reviews, nothing is saved |
| onGranted | () => void | - | Called when the user allows notifications |
| onDenied | () => void | - | Called when the user blocks notifications |
| onDismiss | () => void | - | Called when the user closes the banner |
| className | string | - | Additional CSS classes |