petalframework / petal_components

Phoenix + Live View HEEX Components
https://petal.build/components
MIT License
767 stars 84 forks source link

Update modal component to fix duplicate `close_modal` event #283

Closed axelclark closed 5 months ago

axelclark commented 6 months ago

Problem: With the current implementation, the close_modal event gets sent twice to the LiveView if you push_patch from the close_modal handle_event in the LiveView. First when phx-click calls hide_modal/2 and then again when phx-remove is executed after the push_patch removes the modal from the DOM.

Solution: Calls JS.push/2 before executing the function in phx-remove and removes the JS.push/2 from the phx-remove function. This update fixes the bug where close_modal event fires twice with server push_patch.

Also, adds on_cancel attr with default to existing JS.push/2 functionality so library users have the options of passing JS.patch/1 to avoid the round trip through the handle_event or %JS{} if they want to simply close the modal and remain on the current page without sending the close_modal event.

This PR borrows patterns from the latest modal component generated in a new Phoenix project, see here.

mplatts commented 5 months ago

Many thanks!