@dashdot/notifications
v0.0.2
Published
Dashdot notifications
Downloads
101
Keywords
Readme
Built with ❤︎ by Dashdot and contributors
Motivation
This package contains a Notification dispatcher EventListener to dispatch and listen to notifications
Installing
Install package
npm i @dashdot/notifications
Add provider
import { getNotificationDispatcher } from '@dashdot/notifications/NotificationDispatcher'
import NotificationsProvider from '@dashdot/notifications/NotificationsProvider'
export default function AppProviders() {
return (
<NotificationsProvider value={getNotificationDispatcher()}>
{children}
</NotificationsProvider>
)
}
Subscribe and dispatch to notifications
import { useEffect, useState, useContext } from 'react'
import { NotificationsContext } from '@dashdot/notifications/NotificationsProvider'
import {
Notification,
NotificationData,
ENotificationType,
} from '@dashdote/notifications/NotificationDispatcher'
export default function useNotifications() {
const notificationDispatcher = useContext(NotificationsContext)
const [notifications, setNotifications] = useState<[Notification] | []>([])
useEffect(
() =>
notificationDispatcher?.subscribe(
(newNotifications: [Notification]) => {
setNotifications(newNotifications)
},
),
[notificationDispatcher],
)
const success = (data = {}, duration?: undefined | number) => {
notificationDispatcher?.dispatch(
{
type: ENotificationType.SUCCESS,
title: 'Success,
...data,
},
duration,
)
}
const removeNotification = (notification: any) => {
notificationDispatcher?.removeNotification(notification)
}
return {
notifications,
success,
dispatch: (
data: NotificationData,
createdAt?: number,
duration?: number,
) => notificationDispatcher?.dispatch(data, createdAt, duration),
removeNotification,
}
}
Versioning
We use SemVer for versioning. For the versions available, see the tags on this repository.
Authors
- Dries Vandermeulen - Initial work - Kevin Hurts
- Kevin Hurts - Create package - Kevin Hurts
See also the list of contributors who participated in this project.
About us
Dashdot BV We shape, build and grow ambitious digital products.