yew-notifications
Pavlo Myroniuk April 17, 2023 #yew #rustNotifications components library for Yew. It's like react-toastify but for Yew and more simpler (so far 😏).
TL;DR: documentation.
Motivation
I was writing my personal project crypto-helper some time ago. I was forced to write awful code ([1], [2]) to add some notifications functionality to my web app. So, I decided to write this library that allows the easy showing of notifications to users.
Inspired by yew-toastrack
.
Core concepts
First of all, simplicity is one of the main purposes. I would like to have a general provider and hook that will spawn notifications. Something like that:
<Provider>
// inner components
</Provider>
... and spawn notifications in any inner component using one simple hook:
let notifications = use_notifications;
notifications.spawn;
I managed to implement it as I wanted using the yew ContextProvider. Basically, all notifications are saved in this context provider. But we need to add new notifications, remove old ones, and so on. So the simple Vec
inside of the context provider is a bad idea. Actually, it holds the UseReducerDispatcher
wrapper into the NotificationsManager
. This NotificationsManager
can spawn new notifications using the inner UseReducerDispatcher
and the context will handle them.
The second goal is customization. I don't want to force users to use only built-in notification components. This is a reason why the NotificationsProvider
component and use_notifications
hook have generic parameters.
For the notifications provider component, we need to specify two generic parameters:
- Notifications type. This is a struct that described notification data like title, text, lifetime, etc. You can create your own such struct and use it here. It must implement the
Notifiable
trait. - Notification Factory type. The purpose of this type is actual notification component creation. It must implement the
NotifiableComponentFactory
trait. When the library will try to render the user's notification, it'll use this factory to create notification yew component from the Notification instance.
< component_creator=>
// some inner components
</>
For the use_notifications
hook, we need to specify only the notification type:
let notifications = ;
notifications.spawn;
Pay attention: if you specify the different notification types in the provider and hook then the code will compile but fail in the runtime. But you can use different notification types if they provider components do not overlap.
For example, with fully customized notifications, we can achieve the following result:
How to use it
- Decide which notification components to use.
yew-notifications
already has implemented standard notifications but you can write your own. Seebasic
andcustom
examples for more information.
# Cargo.toml
# if you want to use standard notification components
= { = "https://github.com/TheBestTvarynka/yew-notifications.git", = ["standard-notification"] }
# if you decide to write and use custom notification components
= { = "https://github.com/TheBestTvarynka/yew-notifications.git" }
- Include
yew-notification
styles into your project:
This one below is needed only when you decide to use components from the yew-notifications
:
Or you can copy scss file into your project and specify the path to it instead of the link. At this point, the scss file is one possible way to import styles.
- Wrap needed components into
NotificationProvider
:
// Notification, NotificationFactory - your notification types.
// They can be imported from this library or written by yourself (see `custom` example).
// component_creator is an instance of the NotificationFactory
< component_creator=>
// some inner components
</>
- Spawn notifications:
use use_notification;
// Notification - your notification type.
// It can be imported from this library or written by yourself (see `custom` example).
let notifications_manager = ;
notifications_manager.spawn;
See the examples
directory for the code examples.
Moving forward
At this point, this library has minimal functionality implemented. I plan to improve it continuously according to my needs and goals. If you have any feature requests, then create an issue with the description. It'll be a priority for me.