-
-
Notifications
You must be signed in to change notification settings - Fork 79.1k
Add keydown listener to backdrop
to enable static offcanvas
to close on Esc
#37968
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
base: main
Are you sure you want to change the base?
Conversation
js/src/util/backdrop.js
Outdated
@@ -138,6 +142,12 @@ class Backdrop extends Config { | |||
execute(this._config.clickCallback) | |||
}) | |||
|
|||
if (this._config.keydownCallback) { | |||
EventHandler.on(element.parentElement, EVENT_KEYDOWN, event => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain me, why you set the listener over the parent element?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial point is to enable
esc
behavior in case of static backdrop. Another approach that seems fine too, isfocusTrap
to trap clicks too, in order not to let user get focus from the trapped element.Esc
would 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickhlauke @twbs/js-review your opinion would help here
Hey @Ronid1 . Thanks for the effort here. |
backdrop
to enable static offcanvas
to close on Esc
backdrop
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
offcanvas
to close onesc
whendata-bs-keyboard = true
, which is also the current default setting for this attribute.If
data-bs-keyboard = false
theoffcanvas
will not close on anyesc
press.Motivation & Context
This is a bug fix and provides
offcanvas
with similar behavior onesc
keydown tomodal
,esc
pressed on eitherstatic offcanvas
or itsbackdrop
will 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