Skip to content

Commit 116359a

Browse files
committed
Scope dashboard animations to local root
1 parent ad1f0aa commit 116359a

3 files changed

Lines changed: 70 additions & 25 deletions

File tree

assets/js/app.js

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,73 @@ Hooks.LazyLoadQueuePreviews = {
127127
}
128128
}
129129

130+
Hooks.DashboardAnimations = {
131+
mounted() {
132+
this.readyClass = "dashboard-animations-ready"
133+
134+
this.onWindowLoad = () => this.scheduleReady()
135+
this.onPageLoadingStart = () => this.clearReady()
136+
this.onPageLoadingStop = () => this.scheduleReady()
137+
138+
if (document.readyState === "complete") {
139+
this.scheduleReady()
140+
} else {
141+
window.addEventListener("load", this.onWindowLoad, {once: true})
142+
}
143+
144+
window.addEventListener("phx:page-loading-start", this.onPageLoadingStart)
145+
window.addEventListener("phx:page-loading-stop", this.onPageLoadingStop)
146+
},
147+
148+
scheduleReady() {
149+
if (this.idleCallback && "cancelIdleCallback" in window) {
150+
window.cancelIdleCallback(this.idleCallback)
151+
}
152+
153+
if (this.readyTimeout) {
154+
clearTimeout(this.readyTimeout)
155+
}
156+
157+
const markReady = () => {
158+
window.requestAnimationFrame(() => {
159+
window.requestAnimationFrame(() => {
160+
if (this.el.isConnected) {
161+
this.el.classList.add(this.readyClass)
162+
}
163+
})
164+
})
165+
}
166+
167+
if ("requestIdleCallback" in window) {
168+
this.idleCallback = window.requestIdleCallback(markReady, {timeout: 1500})
169+
} else {
170+
this.readyTimeout = setTimeout(markReady, 150)
171+
}
172+
},
173+
174+
clearReady() {
175+
this.el.classList.remove(this.readyClass)
176+
},
177+
178+
destroyed() {
179+
this.clearReady()
180+
181+
window.removeEventListener("phx:page-loading-start", this.onPageLoadingStart)
182+
window.removeEventListener("phx:page-loading-stop", this.onPageLoadingStop)
183+
window.removeEventListener("load", this.onWindowLoad)
184+
185+
if (this.idleCallback && "cancelIdleCallback" in window) {
186+
window.cancelIdleCallback(this.idleCallback)
187+
this.idleCallback = null
188+
}
189+
190+
if (this.readyTimeout) {
191+
clearTimeout(this.readyTimeout)
192+
this.readyTimeout = null
193+
}
194+
}
195+
}
196+
130197
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
131198
// Use embedded socket for iframe pages
132199
let socketUrl = window.location.pathname.startsWith("/embed/") ? "/embed/live" : "/live"
@@ -141,30 +208,6 @@ topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
141208
window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
142209
window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
143210

144-
const dashboardAnimationsReadyClass = "dashboard-animations-ready"
145-
146-
const scheduleDashboardAnimationsReady = () => {
147-
window.requestAnimationFrame(() => {
148-
window.requestAnimationFrame(() => {
149-
document.documentElement.classList.add(dashboardAnimationsReadyClass)
150-
})
151-
})
152-
}
153-
154-
if (document.readyState === "complete") {
155-
scheduleDashboardAnimationsReady()
156-
} else {
157-
window.addEventListener("load", scheduleDashboardAnimationsReady, {once: true})
158-
}
159-
160-
window.addEventListener("phx:page-loading-start", () => {
161-
document.documentElement.classList.remove(dashboardAnimationsReadyClass)
162-
})
163-
164-
window.addEventListener("phx:page-loading-stop", () => {
165-
scheduleDashboardAnimationsReady()
166-
})
167-
168211
// connect if there are any LiveViews on the page
169212
liveSocket.connect()
170213

lib/reencodarr_web/live/dashboard_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ defmodule ReencodarrWeb.DashboardLive do
11191119
@impl true
11201120
def render(assigns) do
11211121
~H"""
1122-
<div class="min-h-screen bg-gray-950 p-6">
1122+
<div id="dashboard-root" phx-hook="DashboardAnimations" class="min-h-screen bg-gray-950 p-6">
11231123
<div class="max-w-7xl mx-auto space-y-4">
11241124
<!-- Row 1: Stats Bar -->
11251125
<.stats_bar

test/reencodarr_web/live/dashboard_v2_live_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ defmodule ReencodarrWeb.DashboardLiveTest do
2424
assert html =~ "Media Library Sync"
2525
assert html =~ "Sonarr"
2626
assert html =~ "Radarr"
27+
assert html =~ ~s(id="dashboard-root")
28+
assert html =~ ~s(phx-hook="DashboardAnimations")
2729
assert html =~ ~s(id="dashboard-active-work")
2830
assert html =~ ~s(phx-hook="LazyLoadQueuePreviews")
2931
assert html =~ ~s(data-loaded="false")

0 commit comments

Comments
 (0)