-
-
Notifications
You must be signed in to change notification settings - Fork 389
feat: persistent sonner functionality via localStorage #674
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
base: main
Are you sure you want to change the base?
feat: persistent sonner functionality via localStorage #674
Conversation
- Add `persistent` option to toast configuration - Implement localStorage-based persistence with SSR safety - Add smart ID management with counter synchronization moved into class - staggered animation for toast restoration - customizable `storageKey` prop to Toaster component
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
This is nice, but I think this type of behavior should be coded by the consumer rather than be included in the library by default. What do you think? |
| i++; | ||
| }); | ||
| // Set the toast count to the number of persistent toasts | ||
| ToastState.setToastCount(numPersistentToasts); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setToastCount runs before any addToast timeouts finish
|
Ya, having it integrated in the library reduces a good amount of boilerplate code and ensures a consistent experience across applications using the library. But notably, there is one reliability aspect that this PR ensures. On loading persistent toasts we animate only the number of visibleToasts on load so it seems consistent with how the library animates new toasts. While persistent toasts load in we have already set the toastCounter so any new toasts do not overwrite persisted toasts. Since toastCounter cannot be set from outside the library logic, consumer code trying to implement this logic would not be able to guarantee a unique toast ID. In summary: Both, animating persistent toasts in, and ensuring unique toast ID's is pretty non-obvious for consumer code to handle. |
|
Very nice. I see what Emil is saying. It is quite comprehensive and feels like a really well-scoped integration for certain niches. Nonetheless, having toast persist across routing, or whatever, is a very good addition. I would definitely consider it! Maybe think the API/prop usage through a bit, keeping it as simple as possible. But I’d say +1. |
|
IMO this should be included in the lib, not implemented by the user. |
|
I think this could be implemented by the developer, but it would be nice if the library had this too. I believe it's missing an important feature:
Security problem:
|
Problem
Some web-apps have critical messages that need to be seen, or a message that may be toasted during a page navigation or refresh. This PR adds message durability so certain toasts can be restored after a page refresh.
Summary
persistentoption to toast configurationstorageKeyprop to Toaster componentUsage
Basic:
Works with evolving toasts, will update persistence if the toast updates:
Restoration Compatibility
All types of toasts can be used with the persistent property. But, Each toast is stringified and then parsed when restored so obviously any functions or timeouts become disconnected.
Demo
persistent-sonners.mp4