Skip to content

Instantly share code, notes, and snippets.

@diegohaz
Last active June 2, 2023 16:16
Show Gist options
  • Save diegohaz/341ed774ebac3b4dd820ae48d8bc822c to your computer and use it in GitHub Desktop.
Save diegohaz/341ed774ebac3b4dd820ae48d8bc822c to your computer and use it in GitHub Desktop.
import { Notifications, push } from "./notifications.jsx";
function Example() {
return (
<>
<button
onClick={() => {
push("Hello");
push("Hello", { role: "status" });
push("Hello", { onUndo: () => {}, timeout: 10000 });
push("Hello", { visuallyHidden: true });
}}
>
Push notification
</button>
</>
);
}
export default function App() {
return (
<>
<Example />
<Notifications />
</>
);
}
import type {
NotificationStoreItem
} from "@ariakit/react/notification";
import {
createNotificationStore,
Notification,
NotificationContainer,
NotificationDismiss,
} from "@ariakit/react/notification";
interface Item extends NotificationStoreItem {
onUndo?: () => void;
}
const notification = createNotificationStore<Item>();
export const push = notification.push;
export function Notifications() {
const items = notification.useState("items");
return (
<NotificationContainer className="notification-container">
{items.map(({ onUndo, ...props }) => (
<Notification {...props} key={props.id} className="notification">
{props.children}
<NotificationDismiss />
{onUndo && <button onClick={onUndo}>Undo</button>}
</Notification>
))}
</NotificationContainer>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment