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

[LiveComponent] Re-initialize upon visibility #1847

Open
gerritwitkamp opened this issue May 14, 2024 · 6 comments
Open

[LiveComponent] Re-initialize upon visibility #1847

gerritwitkamp opened this issue May 14, 2024 · 6 comments

Comments

@gerritwitkamp
Copy link

Hi,

Right now we're using a lazy Live Component in a modal, which gets initialized when the modal becomes visible; that's working great. I was wondering if it's possible to 're-initialize' a Live Component when become visible again, so not only the first time.
I only managed by adding a custom Stimulus controller that communicates with the Live Component by calling a 'init'-method.
Thanks in advance.

@WebMamba
Copy link
Collaborator

I don't think we want it in the core but this is a great think you can still share your code here! 😁

@gerritwitkamp
Copy link
Author

gerritwitkamp commented May 22, 2024

@WebMamba
Well, I think it's a great addition. Imagine the following use case: I have an overview with entities and in that overview I have an add-button which opens a modal with that Live Component. I do want to reset the underlying form everytime the modal get's openend. Right now, with lazy, it's only 'reset' upon first init.

@smnandre
Copy link
Collaborator

You can look how i did it for the icon modals on this page: https://ux.symfony.com/icons
(source code is in the repository)

I agree with you this is a frequent pattern, but ... the implementation is really different from a project to another.. so maybe we should write a short post in the cookbook ?

@gerritwitkamp
Copy link
Author

gerritwitkamp commented May 27, 2024

@smnandre Thanks, I will have look. Adding it to the cookbook would be great.

@gerritwitkamp
Copy link
Author

gerritwitkamp commented May 27, 2024

@smnandre just checked the repo, your implementation is slightly different from mine. Your trigger is the Click + Change event. I have added a create and edit (with livearg id) method in the live component, which is called from the records in the overview. When editing, the entity is fetched and populates the form. When creating, resetForm is called. The modal is shown using dispatchBrowserEvent.

@gerritwitkamp
Copy link
Author

gerritwitkamp commented May 31, 2024

@smnandre I end up using a single turbo-frame in my base-template. Using a Stimulus-controller I can change the src-attribute,
the route returns a complete modal-HTML. With the frame-events I can show a backdrop + loader during fetching. When fetch completes the Stimulus controller creates a Bootstrap modal. Turbo doesn't intercept Live Component, so far it's looking good.
Because the turbo-frame refetched the src again and again, it's the same 'effect' as re-initialize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants