Skip to content

Handle turbo:morph events in PageObserver#182

Open
ErnaneJ wants to merge 2 commits into
basecamp:mainfrom
ErnaneJ:fix/turbo-morph-support
Open

Handle turbo:morph events in PageObserver#182
ErnaneJ wants to merge 2 commits into
basecamp:mainfrom
ErnaneJ:fix/turbo-morph-support

Conversation

@ErnaneJ

@ErnaneJ ErnaneJ commented Jul 14, 2026

Copy link
Copy Markdown

Problem

When Turbo's page refresh action uses morphing, local_time's DOM changes get reverted. The morph restores elements to their server-rendered state, stripping data-localized and other client-side attributes. PageObserver only watches for inserted nodes via MutationObserver, so elements updated in-place are never re-processed.

Fix

PageObserver#start now listens for turbo:morph and turbo:morph-element. On a full-page morph, the existing processElements callback runs with no arguments, scanning the whole document (same as LocalTime.run()). On a partial morph, only the morphed subtree is scanned via findSignificantElements.

The server never renders data-localized, so after morph the selector time[data-local]:not([data-localized]) re-matches and elements get re-processed.

Closes #165.

Copilot AI review requested due to automatic review settings July 14, 2026 04:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates LocalTime’s DOM observation so that Turbo “page refresh” morphing (which updates elements in place) triggers re-processing of affected <time> elements, preventing localized client-side attributes/text from being reverted after a morph.

Changes:

  • Add Turbo morph event listeners to PageObserver to re-run processing after turbo:morph and turbo:morph-element.
  • Update the compiled ES2017 UMD and ESM bundles to include the new morph handling.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 1 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/assets/javascripts/src/local-time/page_observer.js Adds Turbo morph event observation to re-process localized elements after morphing updates.
app/assets/javascripts/local-time.es2017-umd.js Regenerated UMD bundle reflecting the new Turbo morph handling.
app/assets/javascripts/local-time.es2017-esm.js Regenerated ESM bundle reflecting the new Turbo morph handling.
Files not reviewed (2)
  • app/assets/javascripts/local-time.es2017-esm.js: Generated file
  • app/assets/javascripts/local-time.es2017-umd.js: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +21 to +26
observeWithTurboMorphEvents() {
document.addEventListener("turbo:morph", () => this.callback?.())
document.addEventListener("turbo:morph-element", ({ target }) => {
this.notify(this.findSignificantElements(target))
})
}
Comment thread lib/assets/javascripts/src/local-time/page_observer.js
Copilot AI review requested due to automatic review settings July 15, 2026 04:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 4 changed files in this pull request and generated 1 comment.

Files not reviewed (2)
  • app/assets/javascripts/local-time.es2017-esm.js: Generated file
  • app/assets/javascripts/local-time.es2017-umd.js: Generated file

}

observeWithTurboMorphEvents() {
document.addEventListener("turbo:morph", () => this.callback?.())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Need to watch for turbo:morph events

2 participants