Add keydown listener to backdrop to enable static offcanvas to close on Esc#37968
Add keydown listener to backdrop to enable static offcanvas to close on Esc#37968
backdrop to enable static offcanvas to close on Esc#37968Conversation
js/src/util/backdrop.js
Outdated
There was a problem hiding this comment.
Can you explain me, why you set the listener over the parent element?
There was a problem hiding this comment.
Can you explain me, why you set the listener over the parent element?
The backdrop, being a part of another element, is always out of focus (e.g. the focus is on the modal/offcanvas element itself), and therefore a keydown event will not be triggered on it.
I double-checked my code and I agree that using the parent element isn't the best approach, since it doesn't work if the offcanvas is placed in another div.
According to the documentation only one offcanvas/ modal can be opened at once, and these are the only two elements using the backdrop so I think a safe solution is setting the event listener on the document element.
There was a problem hiding this comment.
this being said, I am not sure if we need to change something on offcanvas.
The initial point is to enable esc behavior in case of static backdrop. Another approach that seems fine too, is focusTrap to trap clicks too, in order not to let user get focus from the trapped element. Esc would work in that case too
There was a problem hiding this comment.
The initial point is to enable
escbehavior in case of static backdrop. Another approach that seems fine too, isfocusTrapto trap clicks too, in order not to let user get focus from the trapped element.Escwould work in that case too
I've explored the second approach as well, and it is also a possibility. Personally, I think this should remain a backdrop functionality for now, since this discussion is about the wanted behavior of the backdrop and not of any element in focus. There might be a place in the future for extending the focustrap but I'm not sure it is currently needed.
There was a problem hiding this comment.
@patrickhlauke @twbs/js-review your opinion would help here
|
Hey @Ronid1 . Thanks for the effort here. |
backdrop to enable static offcanvas to close on Escbackdrop to enable static offcanvas to close on Esc
7e34f44 to
7ba6b39
Compare
|
@GeoSot I made changes according to your review and added tests. The pipeline is clear now. |
7ba6b39 to
0f27ab9
Compare
louismaximepiton
left a comment
There was a problem hiding this comment.
The changes look great to me !
I don't know if it is wanted since it's not the case for modals but clicking on the static backdrop sets the focus on the close button. It leads to a display of a box-shadow. Feels a bit weird using this but why not.
It could be tackled in another PR but my only question here is: Should we use this new functionality in the modals as well ? Because it seems like the backdrop escape event is handled differently in the 2 components.
0f27ab9 to
7e32458
Compare
|
Made changes requested by @louismaximepiton. Bundlewatch seems to be failing due to max size. Can you please help? @GeoSot |
|
I have it in my mind, and I apologize for the delay. Team, at this point, has as priority the major css changes. Many many thanks for your patience |
Description
Added keydown event listener to
backdrop.Updated static
offcanvasto close onescwhendata-bs-keyboard = true, which is also the current default setting for this attribute.If
data-bs-keyboard = falsetheoffcanvaswill not close on anyescpress.Motivation & Context
This is a bug fix and provides
offcanvaswith similar behavior onesckeydown tomodal,escpressed on eitherstatic offcanvasor itsbackdropwill close it.Type of changes
Checklist
npm run lint)Live previews
Change can be verified using this code snippet (showcases all scenarios):
Related issues
Closes #37155