Skip to content

Instantly share code, notes, and snippets.

@julianrubisch
Last active May 8, 2021 02:39
Show Gist options
  • Save julianrubisch/cb673551de74a844dc8f869df7baa18f to your computer and use it in GitHub Desktop.
Save julianrubisch/cb673551de74a844dc8f869df7baa18f to your computer and use it in GitHub Desktop.
Tabbed navigation with turbo
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));
}
}
}
}
}
@julianrubisch
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment