Skip to content

Commit

Permalink
turbo history frame
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Nov 16, 2023
1 parent 6473e54 commit a23c264
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
1 change: 0 additions & 1 deletion app/controllers/apps/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def show

@conversation = @app.conversations.find_by(key: params[:id])


if request.headers["Turbo-Frame"].present?
render "show"
else
Expand Down
36 changes: 19 additions & 17 deletions app/javascript/controllers/turbo_frame_history_controller.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import { Controller } from '@hotwired/stimulus'
import { useMutation } from 'stimulus-use'
import { Controller } from '@hotwired/stimulus';
import { useMutation } from 'stimulus-use';

export default class extends Controller {
connect () {

useMutation(this, { attributes: true })
this.popStateListener = this.popStateListener.bind(this)
window.addEventListener("popstate", this.popStateListener)
connect() {
useMutation(this, { attributes: true });
this.popStateListener = this.popStateListener.bind(this);
window.addEventListener('popstate', this.popStateListener);
// Make Turbo ignore popstate events for the initial state
history.replaceState(this.historyState(), "", window.location.href)
history.replaceState(this.historyState(), '', window.location.href);
}

disconnect() {
window.removeEventListener("popstate", this.popStateListener)
window.removeEventListener('popstate', this.popStateListener);
}

mutate (entries) {
mutate(entries) {
entries.forEach((mutation) => {
if (mutation.type === 'attributes' && mutation.attributeName === 'src') {
const src = this.element.getAttribute('src')
const src = this.element.getAttribute('src');
if (src != null && src !== window.location.href) {
history.pushState(this.historyState(), "", src)
history.pushState(this.historyState(), '', src);
}
}
})
});
}

popStateListener(event) {
if (event.state.turbo_frame_history && event.state.turbo_frame === this.element.id) {
this.element.src = window.location.href
if (
event.state.turbo_frame_history &&
event.state.turbo_frame === this.element.id
) {
this.element.src = window.location.href;
}
}

historyState() {
return {
turbo_frame_history: true,
turbo_frame: this.element.id,
}
};
}
}
}
1 change: 0 additions & 1 deletion app/javascript/new_application.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'rc-tooltip/assets/bootstrap.css';
import './controllers';
import '../frontend/components/index';


/*
// hack on preserve scroll https://github.com/hotwired/turbo/issues/37#issuecomment-1581864979
if (!window.scrollPositions) {
Expand Down

0 comments on commit a23c264

Please sign in to comment.