Skip to content

Commit

Permalink
Remove the toast 1s after adding it
Browse files Browse the repository at this point in the history
We also make sure we remove the entire div and not just fade the toast
itself, this ensures that the overlay doesn't interfere with other UI
elements.
  • Loading branch information
chrislo committed Dec 20, 2024
1 parent 9dc2148 commit 6ca63b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/components/toast_component/toast_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div data-controller="toasts" class="flex flex-col items-center fixed z-10 sm:z-0 sm:absolute bottom-3 sm:top-3 w-full" role="alert">
<div class="transition-opacity duration-200 flex flex-row items-center bg-slate-50 gap-3 max-w-xs p-4 bg-white sm:shadow shadow-lg border-l-4 <%= type_class %>" data-toasts-target="toast">
<div data-controller="toasts" class="flex flex-col items-center fixed z-10 sm:z-0 sm:absolute bottom-3 sm:top-3 w-full" role="alert" data-toasts-target="toast">
<div class="flex flex-row items-center bg-slate-50 gap-3 max-w-xs p-4 bg-white sm:shadow shadow-lg border-l-4 <%= type_class %>">
<%= @message %>

<button type="button" data-action="toasts#close" class="-mx-1.5 -my-1.5 bg-slate-50 text-gray-400 hover:text-gray-900 focus:ring-2 focus:ring-gray-300 p-1.5 hover:bg-gray-100 inline-flex h-8 w-8 dark:text-gray-500 dark:hover:text-white dark:bg-gray-800 dark:hover:bg-gray-700" aria-label="Close">
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/controllers/toasts_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["toast"]

connect() {
setTimeout(() => {
this.toastTarget.remove()
}, 1000)
}

close() {
this.toastTarget.classList.add("opacity-0")
this.toastTarget.remove();
}
}

0 comments on commit 6ca63b7

Please sign in to comment.