Parent component now sets the keys used to close the modal #757
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.
The parent component provides the keysToClose prop with an array of strings specifying which keys should be able to close the modal (Example: ["Escape", "Enter", " "])
I found an edge-case where if the dialog is opened by the parent component by a certain key and the modal is configured to close with that same key, it will immediately close after opening. For example, the parent decides to open the modal when the enter key is pressed down. If the modal is configured to close with the enter key, it will immediately close once that same key that was used to open it is released.
To fix this, I added a boolean to track if a key that is used to close the modal has been pressed down. This boolean is then checked before closing the modal from a key press. This makes it so the modal will not close unless the key configured to close it is pressed down and then released.
One controversial change worth mentioning is backwards compatibility. Any previous usages of this modal that had clickToClose set to false will now close when the Esc key is pressed (unless the parent is changed to pass an empty array to the keysToClose prop).
My justification for this change is to allow for the capability for the dialog to not be closable by clicking, yet still be closable by the keys specified in the parent.
If we wish to have this commit perfectly backwards compatible, adding back the "if clickToClose" statement surrounding the add/remove listener lines will do the trick, but it won't be possible to have the dialog not closable by clicking, yet closable by pressing a key.
One last point to mention is that if we wish for Dialog.vue to also allow the parent to set the keys to close it, it will need a simple change to take the keysToClose prop in-and-out.