Skip to content
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

Add infinite duration toasts #31

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions assets/js/live_toast/live_toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ export function createLiveToastHook(duration = 6000, maxItems = 3) {
durationOverride = Number.parseInt(this.el.dataset.duration)
}

window.setTimeout(async () => {
// animate this element sliding down, opacity to 0, with delay time
await animateOut.bind(this)()
// you can set duration to 0 for infinite duration, basically
if (durationOverride !== 0) {
window.setTimeout(async () => {
// animate this element sliding down, opacity to 0, with delay time
await animateOut.bind(this)()

this.pushEventTo('#toast-group', 'clear', { id: this.el.id })
}, durationOverride + removalTime)
this.pushEventTo('#toast-group', 'clear', { id: this.el.id })
}, durationOverride + removalTime)
}
}
}
}
54 changes: 53 additions & 1 deletion demo/lib/demo_web/live/tabs/demo.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@
</div>

<p class="mt-6 mb-4">
You can also use arbitrary severity levels. If you want to add another level like "warning", refer to [the documentation here](https://hexdocs.pm/live_toast/readme.html#custom-severity-levels).
You can also use arbitrary severity levels. If you want to add another level like "warning", refer to <.link
href="https://hexdocs.pm/live_toast/readme.html#custom-severity-levels"
class="text-blue-700 hover:text-blue-500 underline"
>the
documentation here</.link>.
</p>
<div class="flex flex-wrap gap-3">
<.button phx-click="flash" phx-value-kind="warn">
Expand All @@ -84,6 +88,54 @@
</div>
</div>

<div class="mb-12">
<h2 class="text-lg font-semibold mb-3">Duration</h2>
<p class="mt-2 mb-4">
You can set the duration of different toasts. If you set duration to 0, they will stay until dismissed (like
flashes). The default duration is 6000ms.
</p>
<div class="flex flex-wrap gap-3">
<.button phx-click={
JS.push("toast",
value: %{
"kind" => "info",
"title" => "Duration",
"body" => "This toast will stay for 1000ms.",
"duration" => 1000
}
)
}>
Short Toast
</.button>

<.button phx-click={
JS.push("toast",
value: %{
"kind" => "info",
"title" => "Duration",
"body" => "This toast will stay for 8000ms.",
"duration" => 8000
}
)
}>
Long Toast
</.button>

<.button phx-click={
JS.push("toast",
value: %{
"kind" => "info",
"title" => "Duration",
"body" => "This toast will stay until dismissed.",
"duration" => 0
}
)
}>
Infinite Toast
</.button>
</div>
</div>

<div class="mb-12">
<h2 class="text-lg font-semibold mb-3">Position</h2>
<p class="mt-2 mb-4">
Expand Down
Loading