From 121674e379babc2d546fbd6e48293fb27e69fb04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20B=C3=B6hm?= Date: Sun, 15 Sep 2024 01:55:38 +0200 Subject: [PATCH] Fixed turbo_stream animation hook and added some URL source --- app/javascript/controllers/application.js | 45 +++++++++++++++-------- app/views/jobs/_job_tr.html.erb | 2 +- app/views/operator/jobs/_job_tr.html.erb | 2 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/app/javascript/controllers/application.js b/app/javascript/controllers/application.js index 437bb88..b34182c 100644 --- a/app/javascript/controllers/application.js +++ b/app/javascript/controllers/application.js @@ -8,9 +8,23 @@ window.Stimulus = application export { application } +// Transition for turbo_frames +// URL: https://dev.to/nejremeslnici/how-to-use-view-transitions-in-hotwire-turbo-1kdi +// Alternative turbo_stream URL: https://evilmartians.com/chronicles/the-future-of-full-stack-rails-turbo-view-transitions +addEventListener("turbo:before-frame-render", (event) => { + if (document.startViewTransition) { + const originalRender = event.detail.render + event.detail.render = (currentElement, newElement) => { + document.startViewTransition(()=> originalRender(currentElement, newElement)) + } + } +}) + // URL: https://edforshaw.co.uk/hotwire-turbo-stream-animations -// FIXME: data-stream-exit-class not working.. -document.addEventListener("turbo:before-stream-render", function(event) { +// Alternative Hotwire Animate (Wrapper for Animate.css) +// URL: https://railshackathon.com/entries/12 https://yozu.co.uk/tech-blog-turbocharged-animations-introducing-hotwire-animate/ +// Discussion about hotwire animation: https://discuss.hotwired.dev/t/are-transitions-and-animations-on-hotwire-roadmap/1547/10 +document.addEventListener("turbo:before-stream-render", (event) => { // Add a class to an element we are about to add to the page // as defined by its "data-stream-enter-class" if (event.target.firstElementChild instanceof HTMLTemplateElement) { @@ -22,17 +36,18 @@ document.addEventListener("turbo:before-stream-render", function(event) { // Add a class to an element we are about to remove from the page // as defined by its "data-stream-exit-class" -// var elementToRemove = document.getElementById(event.target.target) -// if (elementToRemove) { -// var streamExitClass = elementToRemove.dataset.streamExitClass -// if (streamExitClass) { -// // Intercept the removal of the element -// event.preventDefault() -// elementToRemove.classList.add(streamExitClass) -// // Wait for its animation to end before removing the element -// elementToRemove.addEventListener("animationend", function() { -// event.target.performAction() -// }) -// } -// } + var elementToRemove = document.getElementById(event.target.target) + if (elementToRemove) { + var streamExitClass = elementToRemove.dataset.streamExitClass + if (streamExitClass) { + // Intercept the removal of the element + event.preventDefault() + elementToRemove.classList.remove(elementToRemove.dataset.streamEnterClass) + elementToRemove.classList.add(streamExitClass) + // Wait for its animation to end before removing the element + elementToRemove.addEventListener("animationend", function() { + event.target.performAction() + }) + } + } }) diff --git a/app/views/jobs/_job_tr.html.erb b/app/views/jobs/_job_tr.html.erb index 03074ec..0471d2c 100644 --- a/app/views/jobs/_job_tr.html.erb +++ b/app/views/jobs/_job_tr.html.erb @@ -1,4 +1,4 @@ - + <%= job.id %> diff --git a/app/views/operator/jobs/_job_tr.html.erb b/app/views/operator/jobs/_job_tr.html.erb index 29ba289..b4161a8 100644 --- a/app/views/operator/jobs/_job_tr.html.erb +++ b/app/views/operator/jobs/_job_tr.html.erb @@ -1,4 +1,4 @@ - + <%= link_to operator_job_path(job) do %>