fix: refactor Modal rendering and clickOutside handling #4035
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
svelte-outclick
in favor of our ownclickOutside
utilNotes
Based on what we discussed:
onMount(() => document.appendChild(...))
seems to just change where in the DOM these<dialog />
elements are appended. When using theonMount
strategy they're appended to the end of thebody
, otherwise they're just in line in the dom wherever they the<Modal />
component is being used.<dialog />
element to the DOM initially and none of its children, (b) when the user opens the modal, we render/mount the rest of the body of the<dialog />
and it gets rendered into thetop-layer
(making it's position in the dom more or less irrelevant).<dialog />
with an{#if open ..}
, because that<dialog />
element at the very least needs to be in the DOM. The components consuming the<Modal />
freak out if it's not there because of thethis
binding. If the<dialog />
is not created then theshowModal()
orclose()
, etc. methods aren't available.Todo
Still need to setTheenable: !!open
for the click outsideuse:
action. This will avoid adding a bunch of unnecessary event handlers onwindow
to listen for clicks to check for Modal's that aren't even open.use:clickOutside
action is on the<form />
element, which is a child of the<dialog />
, meaning it's not created and added to the dom until the user opens the modal anyway. And the event listener is removed from thedocument
when the modal is closed 🥳