ciscoheat / sveltekit-flash-message

Send temporary data after redirect, usually from endpoints. Works with both SSR and client.
https://www.npmjs.com/package/sveltekit-flash-message
MIT License
246 stars 5 forks source link

Usage with Skeleton `Toast` component #8

Closed ohmree closed 1 year ago

ohmree commented 1 year ago

In the readme you explain why using the {@html} syntax when displaying messages from this library is a bad idea but skeleton seems to make heavy use of it, including in the view layer for its global toast system.

Is there a recommended way to make sveltekit-flash-message and skeleton's Toast work together nicely in a superforms app or should I just pick one and stick with it? If I do have to ditch one or the other here are the ideas I was thinking of:

In both approaches I have to learn how something works (a flashMessage.module conformant implementation vs the styling in skeleton's Toast) so neither one is ideal.

ciscoheat commented 1 year ago

Hi, would it work to subscribe to the flash store, triggering the toastStore on updates? In the update function you can verify and clean up the message. Maybe you can even do it like this:

$: if($flash) {
  const message = cleanHtml($flash)
  toastStore.trigger(message)
}
ohmree commented 1 year ago

The approach you suggest did seem to work at first but then I noticed that flash subscribers seem to be notified twice with the same message data, using the snippet from your comment or doing something like this (in my top-level +layout.svelte):

const unsubscribe = flash.subscribe(messageData => {
  if (messageData != null) {
    const message = purifier.sanitize(messageData.message);
    toastStore.trigger({
      message,
      background: backgrounds[messageData.type],
    });
  }
});
onDestroy(unsubscribe);

I'd like to try reproducing this in isolation, do you happen to have a minimal repo I could use for that or should I just manually follow the docs for setting superforms and flash-message up on a fresh starter?

anatoliy-t7 commented 1 year ago

In these examples, I am getting notified twice. Do you have any idea how to fix it?

ciscoheat commented 1 year ago

The double notification problem should be fixed now in 1.0.0-rc.2, just released. Now it should be possible to use the flash store as an event handler:

flash.subscribe($flash => {
  if($flash) sendToast($flash)
})
vladislav-yemelyanov commented 1 year ago

@ciscoheat I tested this on my personal project, now it's fixed for me, thanks!