diff --git a/app/assets/tailwind/application.css b/app/assets/tailwind/application.css index f1d8c73..962de3c 100644 --- a/app/assets/tailwind/application.css +++ b/app/assets/tailwind/application.css @@ -1 +1,16 @@ @import "tailwindcss"; + +@theme { + /* 1. Die CSS-Variable für die Animation definieren */ + --animate-shrink: shrink 5s linear forwards; + + /* 2. Die dazugehörigen Keyframes festlegen */ + @keyframes shrink { + 0% { + transform: scaleX(1); + } + 100% { + transform: scaleX(0); + } + } +} diff --git a/app/javascript/controllers/flash_controller.js b/app/javascript/controllers/flash_controller.js new file mode 100644 index 0000000..efc2381 --- /dev/null +++ b/app/javascript/controllers/flash_controller.js @@ -0,0 +1,40 @@ +import { Controller } from "@hotwired/stimulus"; + +export default class extends Controller { + static targets = ["notification"]; + + connect() { + // Startet den Timer: Ruft nach 5000 Millisekunden (5 Sekunden) die dismiss-Methode auf + this.timeout = setTimeout(() => { + this.dismiss(); + }, 5000); + } + + disconnect() { + // Wichtig: Löscht den Timer, falls der Nutzer die Nachricht vor Ablauf der 5 Sekunden manuell schließt + clearTimeout(this.timeout); + } + + dismiss() { + // 1. Tailwind-Klassen für das Ausblenden hinzufügen + // this.notificationTarget.classList.add("opacity-0","translate-y-2","scale-95"); + + // Verschiebt das Element auf der X-Achse weit nach rechts (translate-x-full) + // und blendet es leicht aus, damit es weicher wirkt + // 1. Alten Startpunkt entfernen (wichtig für Tailwind v4/v3) + this.notificationTarget.classList.remove("translate-x-0", "opacity-100"); + // 2. Neuen Endpunkt hinzufügen (löst die Animation aus) + this.notificationTarget.classList.add("translate-x-full", "opacity-20"); + + // Fügt Klassen für Deckkraft (0) und minimale Skalierung hinzu + // 1. Den Startwert (voll sichtbar) entfernen + //this.notificationTarget.classList.remove("opacity-100"); + // 2. Den Endwert (vollständig transparent) hinzufügen -> startet das Verblassen + //this.notificationTarget.classList.add("opacity-0", "scale-95"); + + // 2. Nach dem Ende der CSS-Animation (300ms) das Element komplett aus dem DOM löschen + setTimeout(() => { + this.element.remove(); + }, 300); + } +} diff --git a/app/views/layouts/_flash.html.erb b/app/views/layouts/_flash.html.erb new file mode 100644 index 0000000..4dcf178 --- /dev/null +++ b/app/views/layouts/_flash.html.erb @@ -0,0 +1,37 @@ +<% flash.each do |type, message| %> + <% + bg_color = type == "notice" ? "bg-green-50 border-green-200 text-green-800" : "bg-red-50 border-red-200 text-red-800" + icon_color = type == "notice" ? "text-green-500" : "text-red-500" + # Die Farbe des Balkens passend zum Typ festlegen + bar_color = type == "notice" ? "bg-green-500" : "bg-red-500" + %> + +