diff --git a/assets/index.d.ts b/assets/index.d.ts index 1fcb0eb..740a197 100644 --- a/assets/index.d.ts +++ b/assets/index.d.ts @@ -2,4 +2,10 @@ import type { ViewHook } from '../deps/phoenix_live_view' declare const createLiveToastHook: () => ViewHook +declare global { + interface Window { + addToast: (kind: string, msg: string, options?: Options) => void + } +} + export { createLiveToastHook } diff --git a/assets/js/live_toast/live_toast.ts b/assets/js/live_toast/live_toast.ts index 87efa09..e3f6041 100644 --- a/assets/js/live_toast/live_toast.ts +++ b/assets/js/live_toast/live_toast.ts @@ -198,14 +198,35 @@ async function animateOut(this: ViewHook) { export function createLiveToastHook(duration = 6000, maxItems = 3) { return { destroyed(this: ViewHook) { + if (this.el.dataset.part === 'toast-group') { + return + } + doAnimations.bind(this)(duration, maxItems) }, updated(this: ViewHook) { + if (this.el.dataset.part === 'toast-group') { + return + } + // animate to targetDestination in 0ms const keyframes = { y: [this.el.targetDestination] } animate(this.el, keyframes, { duration: 0 }) }, mounted(this: ViewHook) { + if (this.el.dataset.part === 'toast-group') { + window.addToast = (kind: string, msg: string, options = {}) => { + const payload = { + kind, + msg, + options + } + this.pushEventTo(this.el, 'add_toast', payload) + } + + return + } + // for the special flashes, check if they are visible, and if not, return early out of here. if (['server-error', 'client-error'].includes(this.el.id)) { if (isHidden(document.getElementById(this.el.id))) { diff --git a/demo/assets/js/app.js b/demo/assets/js/app.js index 1338286..c93a5c5 100644 --- a/demo/assets/js/app.js +++ b/demo/assets/js/app.js @@ -1,26 +1,31 @@ -import 'phoenix_html' -import { Socket } from 'phoenix' -import { LiveSocket } from 'phoenix_live_view' -import { createLiveToastHook } from '../../../assets/js/live_toast/live_toast.ts' +import "phoenix_html" +import { Socket } from "phoenix" +import { LiveSocket } from "phoenix_live_view" +import { createLiveToastHook } from "../../../assets/js/live_toast/live_toast.ts" let csrfToken = document .querySelector("meta[name='csrf-token']") - .getAttribute('content') -let liveSocket = new LiveSocket('/live', Socket, { + .getAttribute("content") + +let liveSocket = new LiveSocket("/live", Socket, { longPollFallbackMs: 2500, params: { _csrf_token: csrfToken }, - hooks: { LiveToast: createLiveToastHook() }, + hooks: { + LiveToast: createLiveToastHook() + } }) +console.log(liveSocket) + // connect if there are any LiveViews on the page liveSocket.connect() -window.addEventListener('phx:close-menu', (e) => { - const menu = document.querySelector('aside#main-navigation') - menu.classList.remove('max-md:block') +window.addEventListener("phx:close-menu", e => { + const menu = document.querySelector("aside#main-navigation") + menu.classList.remove("max-md:block") - const backdrop = document.querySelector('#backdrop') - backdrop.classList.remove('max-md:block') + const backdrop = document.querySelector("#backdrop") + backdrop.classList.remove("max-md:block") }) // expose liveSocket on window for web console debug logs and latency simulation: diff --git a/lib/live_toast/live_component.ex b/lib/live_toast/live_component.ex index c85290b..269fec8 100644 --- a/lib/live_toast/live_component.ex +++ b/lib/live_toast/live_component.ex @@ -80,7 +80,12 @@ defmodule LiveToast.LiveComponent do @impl Phoenix.LiveComponent def render(assigns) do ~H""" -