Add controller to retarget links

So that internal links can skip turbo frames, and external links
open in new pages
This commit is contained in:
Jorge Manrubia
2025-06-02 12:04:43 +02:00
parent e973df97e2
commit d7c06702e3
3 changed files with 17 additions and 2 deletions
@@ -0,0 +1,15 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
connect() {
this.element.querySelectorAll("a").forEach(this.#retargetLink.bind(this))
}
#retargetLink(link) {
link.target = this.#targetsSameDomain(link) ? "_top" : "_blank"
}
#targetsSameDomain(link) {
return link.href.startsWith(window.location.origin)
}
}