Skip to content
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

fix(sidenav): unable to close by pressing escape in side mode #17967

Closed
wants to merge 1 commit into from

Conversation

crisbeto
Copy link
Member

We currently listen for keydown events on the sidenav so that we know when to close it, however in side mode we don't trap focus inside the sidenav which means that unless the user goes into the sidenav manually, the escape listener won't work. These changes fix the issue by listening to keyboard events at the document level.

Fixes #17965.

We currently listen for `keydown` events on the sidenav so that we know when to close it, however in `side` mode we don't trap focus inside the sidenav which means that unless the user goes into the sidenav manually, the escape listener won't work. These changes fix the issue by listening to keyboard events at the `document` level.

Fixes angular#17965.
@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release labels Dec 14, 2019
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Dec 14, 2019
@mmalerba
Copy link
Contributor

Is this actually the behavior we want? I'm not sure it makes sense to close the sidenav on escape if the focus isn't somewhere in the sidenav

@crisbeto
Copy link
Member Author

I suppose that it depends on how we treat the sidenav in side mode: is it something that's supposed to be open temporarily, or it should always be open? If it's the latter, I can see it conflicting with other components, otherwise I think that it makes sense to handle events that come from the outside.

@mmalerba
Copy link
Contributor

I think it might depend on individual apps, I could see wanting to do it either way. With the other modes they're modal so its a little more clear. I think we should just leave it as is, and let users add an extra listener if they want it to close on escape in a different subtree

@crisbeto
Copy link
Member Author

Up to you. I submitted the fix, because the behavior felt inconsistent.

@mmalerba
Copy link
Contributor

Ok, I would prefer to leave it as is

@alexbjorlig
Copy link

alexbjorlig commented Dec 16, 2019

Is it somehow possible to capture focus, when opening the sidenav in "side" mode?

I agree that it should only close on escape, if focus is on the sidenav. But is that not the case with the autoFocus property? I'm asking because using autoFocus and escape in side mode, does not seem to fix the problem.

@crisbeto
Copy link
Member Author

crisbeto commented Dec 16, 2019

Good point that autoFocus is ignored in side mode on purpose (see https://github.com/angular/components/blob/master/src/material/sidenav/drawer.ts#L258). You could move focus yourself inside the closed event, but that's also not very clean. @mmalerba maybe we should have it always capture focus if autoFocus is set explicitly?

@mmalerba
Copy link
Contributor

I'm not sure about turning on focus trapping because they specified autoFocus, that seems kind of unexpected. But I could see still auto-focusing the first element even though trapping isn't enabled

@crisbeto
Copy link
Member Author

crisbeto commented Dec 16, 2019

Makes sense. I can do a separate PR tomorrow which adds that functionality.

crisbeto added a commit to crisbeto/material2 that referenced this pull request Dec 18, 2019
…explicitly

This PR is the result of the discussions in angular#17967. Currently we don't move focus into the sidenav if it's in `side` mode, because we don't know the context that the component is used in, however in some cases it makes sense to move focus anyway. These changes make it so that if the consumer explicitly opted into `autoFocus`, we always move focus into the sidenav, no matter what mode it's in.
crisbeto added a commit to crisbeto/material2 that referenced this pull request Dec 18, 2019
…explicitly

This PR is the result of the discussions in angular#17967. Currently we don't move focus into the sidenav if it's in `side` mode, because we don't know the context that the component is used in, however in some cases it makes sense to move focus anyway. These changes make it so that if the consumer explicitly opted into `autoFocus`, we always move focus into the sidenav, no matter what mode it's in.
mmalerba pushed a commit that referenced this pull request Dec 29, 2019
This PR is the result of the discussions in #17967. Currently we don't move focus into the sidenav if it's in `side` mode, because we don't know the context that the component is used in, however in some cases it makes sense to move focus anyway. These changes make it so that if the consumer explicitly opted into `autoFocus`, we always move focus into the sidenav, no matter what mode it's in.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jan 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has agreed to Google's Contributor License Agreement P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Closing sidenav with escape does not work when mode is "side"
4 participants