2 changed files with 46 additions and 2 deletions
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
// assets/controllers/progress_bar_controller.js
|
||||
import { Controller } from "@hotwired/stimulus"; |
||||
|
||||
export default class extends Controller { |
||||
static targets = ["bar"]; |
||||
|
||||
connect() { |
||||
// Listen for clicks on the entire document instead of just the controller element
|
||||
document.addEventListener("click", this.handleClick.bind(this)); |
||||
} |
||||
|
||||
disconnect() { |
||||
// Clean up event listener when controller disconnects
|
||||
document.removeEventListener("click", this.handleClick.bind(this)); |
||||
} |
||||
|
||||
handleClick(event) { |
||||
const link = event.target.closest("a"); |
||||
if (link && !link.hasAttribute("data-no-progress") && |
||||
!event.ctrlKey && !event.metaKey && !event.shiftKey) { |
||||
this.start(); |
||||
} |
||||
} |
||||
|
||||
start() { |
||||
this.barTarget.style.width = "0"; |
||||
this.barTarget.style.transition = "none"; |
||||
setTimeout(() => { |
||||
this.barTarget.style.transition = "width 5s ease-in-out"; |
||||
this.barTarget.style.width = "100%"; |
||||
}, 10); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue