Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplement Turbo support using MutationObserver #807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

daniel-rikowski
Copy link

My attempt to tackle #796.

There are many changed files, but the bulk of them are to allow initializing only a subtree of the DOM instead of the whole document. The actual core of the functionality is in src/dom/observer.ts.

The only Turbo-specific code is the initial load event. After that, all new elements are initialized automatically, regardless how they entered the DOM (Turbo Frame, Turbo Stream, jQuery, innerHTML, insertAdjacentHTML, append, appendChild, Vue, React, ...)

Similiarly, whenever a DOM element gets removed, and it had an ID, and that ID responds to any component instance, that instance is removed and destroyed.

Components from unchanged parts of the DOM are not touched. (This is the deciding difference, compared with the previous event-based "sledgehammer" approach)

I tested this successfully, with a simple app, where a Flowbite modal is created on-the-fly with HTML sent via both Turbo frame and Turbo stream actions.

Unfortunately, I don't have a larger Flowbite-and-Turbo-based app to play around with, so I'm hoping for feedback on how well (or not) this behaves in a larger app with many components and/or heavy Turbo frame or Turbo stream usage.

Copy link

stackblitz bot commented Feb 18, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@bhtabor
Copy link

bhtabor commented Mar 9, 2024

The only Turbo-specific code is the initial load event. After that, all new elements are initialized automatically, regardless how they entered the DOM (Turbo Frame, Turbo Stream, jQuery, innerHTML, insertAdjacentHTML, append, appendChild, Vue, React, ...)

Thank you @daniel-rikowski!

Does this support Turbo Drive form submission errors? https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission

@artinboghosian
Copy link
Contributor

Any updates on if this PR will get merged?

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.

3 participants