Skip to content

Instantly share code, notes, and snippets.

@Intrepidd
Last active September 25, 2023 21:06
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Intrepidd/bb1ffc5944a5c1ec3a9f5582753c4b67 to your computer and use it in GitHub Desktop.
Save Intrepidd/bb1ffc5944a5c1ec3a9f5582753c4b67 to your computer and use it in GitHub Desktop.
import { navigator } from '@hotwired/turbo'
import { Controller } from '@hotwired/stimulus'
import { useMutation } from 'stimulus-use'
export default class extends Controller {
connect (): void {
useMutation(this, { attributes: true })
}
mutate (entries: MutationRecord[]): void {
entries.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
const src = this.element.getAttribute('src')
if (src != null) {
const url = new URL(src)
navigator.view.lastRenderedLocation = url
navigator.history.push(url)
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment