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:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user