Last active
May 8, 2021 02:39
-
-
Save julianrubisch/cb673551de74a844dc8f869df7baa18f to your computer and use it in GitHub Desktop.
Tabbed navigation with turbo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { navigator } from "@hotwired/turbo"; | |
import { Controller } from "stimulus"; | |
import { useMutation } from "stimulus-use"; | |
export default class extends Controller { | |
connect() { | |
useMutation(this, { attributes: true, childList: true, subtree: true }); | |
} | |
mutate(entries) { | |
for (const mutation of entries) { | |
if (mutation.type === "childList") { | |
// re-initialize third party, e.g. jquery, plugins | |
} | |
if (mutation.type === "attributes" && mutation.attributeName === "src") { | |
const src = this.element.getAttribute("src"); | |
if (src != null) { | |
navigator.history.push(new URL(src)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
adapted from https://gist.github.com/Intrepidd/ac68cb7dfd17d422374807efb6bf2f42