-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add backend control for modal open/close #18
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
Conversation
Extend modal component to support closing and opening via push_event
from the backend in frontend (non-async) mode.
- Add handleEvent registration in modal hook for prima:modal:close
and prima:modal:open events
- Integrate push event listener setup/cleanup into initialize/cleanup
lifecycle methods
- Add fixture and Wallaby test demonstrating backend-initiated close
- Backend can now use push_event(socket, "prima:modal:close", %{})
to close frontend modals
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Rename for clarity to distinguish between DOM event listeners (addEventListener) and LiveView push event handlers (handleEvent). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Add test to verify modal can be opened from backend using push_event, in addition to the existing close functionality. - Add test for prima:modal:open push_event - Add backend handler for open-frontend-modal event - Add backend-open-button to fixture 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Allow backend to target specific modals via push_event payload,
enabling precise control when multiple modals are present.
- Add optional `id` field in push_event payload
- Hook checks payload and only responds if ID matches or is unspecified
- Maintains backward compatibility (empty payload targets all modals)
- Add fixture and tests demonstrating multiple modal targeting
Usage:
push_event(socket, "prima:modal:close", %{id: "user-modal"})
push_event(socket, "prima:modal:open", %{id: "settings-modal"})
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Consolidate frontend_modal_push_event and multiple_modals_push_event fixtures and tests into a unified modal_push_event fixture that demonstrates both single modal (no ID) and multi-modal (with ID) scenarios. - Merge two fixtures into modal_push_event_fixture.html.heex - Merge two test files into modal_push_event_test.exs - Update router and template to use single fixture route - All 26 features + 1 test still pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
PR Review: Backend Control for Modal Open/CloseThis is a solid feature addition that enables server-side control of modals via push_event. The implementation is well-thought-out and follows good practices. ✅ Strengths
🔍 Issues and Concerns1. Potential Memory Leak in Event Handler Registration In modal.js:27-40, setupPushEventListeners() is called on every initialize() (which happens on mounted, updated, and reconnected). The cleanup relies on this.pushEventRefs being truthy. Recommendation: Initialize pushEventRefs as an empty array in setupPushEventListeners() before creating refs to ensure cleanup always works. 2. Missing Error Handling Push event handlers in modal.js:29-38 don't validate payload structure. Consider adding basic validation for malformed data, though this might be overkill for an internal API. 3. Element Reference Cleanup ✅ The PR correctly removes this.modalEl and replaces all references with this.el. The old code was incorrectly trying to find the modal element by removing -portal from the ID. This removal fixes a bug. 🎯 Minor Suggestions
🔒 Security and Performance✅ No security concerns - ID parameter uses string comparison not DOM query construction, no XSS risk ✅ Performance looks good - minimal event handlers (2 per modal) with proper cleanup 📝 SummaryThis is a high-quality PR with valuable functionality, excellent documentation, and solid test coverage. Main concern is the potential event handler accumulation (item 1) which should be verified or fixed. Other issues are minor. Recommendation: ✅ Approve with minor suggestions Great work! The separation between Prima.Modal.JS and Prima.Modal.push_* functions is particularly elegant. |
Summary
This PR adds backend control capabilities for opening and closing modals via
push_event, enabling server-side components to control modal visibility without relying on client-side JavaScript commands.Key Features
Prima.Modal.push_open/2andPrima.Modal.push_close/2functions allow backend to control modalsidparameter enables precise control when multiple modals are present on a pageUsage
Single modal (no ID needed):
Multiple modals (specify ID):
Implementation Details
prima:modal:openandprima:modal:closepush eventsFiles Changed
lib/prima/modal.ex- Added documentation and push_open/push_close functionsassets/js/hooks/modal.js- Implemented push_event handlers with ID targetingdemo/- Added fixtures and tests demonstrating the featureTesting