[Vue] Fix: Prevent modal from closing on Escape key when closeable is false #1510
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.
Summary:
This PR addresses an issue where the modal (dialog element) closes when the Escape key is pressed, even if the
closeable
prop is set tofalse
.Description of the Issue:
Currently, the modal closes when the Escape key is pressed regardless of the
closeable
prop. This behavior occurs because the native behavior of the<dialog>
element allows it to be closed by the Escape key, which is not prevented in the existing implementation. As a result, even whencloseable
isfalse
, the modal's internal event handling processes the Escape key event, leading to unintended closures.From the MDN Web Docs on the
<dialog>
element:Because of this default behavior, the modal closes even when it is not intended to be closeable, and the internal state is not updated correctly. This also leads to issues where the modal cannot be reopened because the
show
state remainstrue
, causing thewatch
cleanup logic not to trigger properly.Change Details:
Updated the
closeOnEscape
function to includee.preventDefault()
to prevent the default action of the Escape key. This ensures that the modal does not close whencloseable
is set tofalse
.Why This Fix is Necessary:
By preventing the default action of the Escape key, we ensure that the modal remains open when
closeable
is set tofalse
. This fix ensures that the native behavior of the<dialog>
element is overridden when necessary, providing a more consistent user experience.Technical Explanation:
e.preventDefault()
method is used to prevent the default behavior of the Escape key, which is to close the<dialog>
element. This ensures that the modal does not close unexpectedly when the Escape key is pressed andcloseable
isfalse
.closeable
is false, the modal closes due to the native behavior, but the internal state (show
) is not updated. This results in the modal being visually closed but logically still open (show
remainstrue
), leading to issues where the modal cannot be reopened because the state does not trigger thewatch
hook.watch
cleanup logic is triggered properly, removing event listeners and resetting styles as expected.Impact:
This change will prevent the modal from closing when the Escape key is pressed and
closeable
isfalse
. It will also ensure proper state management and cleanup, preventing unexpected closures and making the modal behavior consistent with thecloseable
prop.Testing:
closeable
set tofalse
and verified that pressing the Escape key does not close the modal.closeable
set totrue
and verified that pressing the Escape key closes the modal as expected.Please review the changes and let me know if there are any questions or further improvements needed.