Skip to content

[WIP][Live] New data-rerender mode for handling custom JavaScript #519

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

Closed
wants to merge 2 commits into from

Conversation

weaverryan
Copy link
Member

Q A
Bug fix? yes
New feature? yes
Tickets Fix #515, fix #490, fix #270, Bug H on #102, hopefully can include fix for #354
License MIT

Hi!

Just the docs to start. This aims to address the biggest pain-point with live components currently: when you have add custom JavaScript to your component HTML (e.g. a date picker). If that JS modifies the markup at all (e.g. adding classes or elements), when the component re-renders, those changes are "removed" and the custom JS stops working.

This adds a very flexible model for re-rendering. However, I think the data-rerender="if(startDate)" will handle 95% of use-cases because it will:

A) Avoid re-rendering the component in most cases (e.g. similar to data-live-ignore currently
B) But if some specific LiveProp changes (e.g. startDate for a date picker), then it will do a complete re-render of that element, where the old element is completely replaced by a new one. That's important because it will re-trigger the connect() method on any Stimulus controllers on (or inside) this element, which will allow them to completely re-initialize the JavaScript.

Please let me know if you think this will fit your use-case. And if, not, please tell me why!

Cheers!

- ``mode`` includes ``diff`` & ``complete`` (default). ``diff`` is the
default mode used by live components where the new element is
"morphed" onto the existing element by applying a diff of changes. However,
as soon as you specify ``data-render``, ``complete`` becomes the default
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
as soon as you specify ``data-render``, ``complete`` becomes the default
as soon as you specify ``data-rerender``, ``complete`` becomes the default


.. code-block:: twig
<div data-render"">
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<div data-render"">
<div data-rerender="">

@jcrombez
Copy link
Contributor

jcrombez commented Nov 4, 2022

Very nice idea, i hope it gets implemented <3

@weaverryan
Copy link
Member Author

Replaced by #728

@weaverryan weaverryan closed this Mar 13, 2023
weaverryan added a commit that referenced this pull request Mar 16, 2023
…xternal JavaScript (weaverryan)

This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[Live] Smart Rendering! Mix/match live components with external JavaScript

| Q             | A
| ------------- | ---
| Bug fix?      | yesish
| New feature?  | yes
| Tickets       | Fixes #702, Fixes #490, Fixes #515, Fixes #270, Fixes Bug H on #102, Fixes #354 Replaces #519
| License       | MIT

Hi!

This fixes the biggest pain-point with LiveComponents: mixing them with custom JavaScript. Before this PR, a lot of things were difficult/weird: keeping modals open, collapsing content (#702), using JS widgets like Autocomplete or a date picker, and more. The best part? Everything works automatically with you needing to do anything.

With this PR, a new philosophy is born: **start with a live component, then mix in any "extras" you want with Stimulus controllers**.

The new "smart rendering" system isn't designed to catch everything (see the docs), but I think it will catch 95%+ of the situations. Here are 2 cool things that now work out-of-the-box:

A) ux-autocomplete + Live components works perfectly now - even if the available options completely change - the "dependent form fields" example has been updated to use autocomplete on both fields 🔥

https://user-images.githubusercontent.com/121003/224790239-3b723fa0-1eb7-4f27-bd71-a04bf32687f1.mp4

B) I added a new demo of Live Components + Autocomplete + Chartjs. What you see is done without needing to write any JavaScript:

https://user-images.githubusercontent.com/121003/224789304-ac1228a8-57eb-4797-8df0-245bd767872b.mp4

### How Does it Work?

If you care about the technical details, we leverage a `MutationObserver` to watch your component for changes made by anything *other* than live components. Those changes are "tracked". Then, during re-render, we use that info to *keep* those custom changes.

Cheers!

Commits
-------

d142431 [Live] Smart Rendering! Mix/match live components with external JavaScript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants