-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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(button): focus styles not applied to programmatically focused buttons #5966
fix(button): focus styles not applied to programmatically focused buttons #5966
Conversation
So it sounds like the motivation for this change was a specific issue with the dialog closing. In general for most cases do you think programmatic focus should default to looking like keyboard or like mouse? If keyboard, we should make this change. Otherwise we should leave the styles the same and change dialog to inject |
IMO all programmatic focus should look like the keyboard one, otherwise the user won't have any indication that something is focused. It's different for the mouse focus since the user is keeping track of their cursor and know what they clicked on so the indication isn't necessary. |
I think the reason I originally made it act like mouse is because of the situation where you click a button to open a sidenav and then the sidenav opens and programmatically focuses its first element. We didn't want the focus styles on that first element. But maybe we can consider that more of the exception case and add some code to do Also if we're going to do this for button let's be consistent, can you search our CSS for |
Sure. I'll update this PR and ping you. |
Updated the 3 other cases we were using it @mmalerba. |
Can we change the sidenav to use |
2f38ff4
to
32ad54c
Compare
Done @mmalerba. |
src/lib/sidenav/sidenav.ts
Outdated
@@ -161,7 +162,7 @@ export class MdSidenav implements AfterContentInit, OnDestroy { | |||
let activeEl = this._doc && this._doc.activeElement; | |||
if (activeEl && this._elementRef.nativeElement.contains(activeEl)) { | |||
if (this._elementFocusedBeforeSidenavWasOpened instanceof HTMLElement) { | |||
this._elementFocusedBeforeSidenavWasOpened.focus(); | |||
this._focusOriginMonitor.focusVia(this._elementFocusedBeforeSidenavWasOpened, 'program'); |
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 is going to do the same thing that just calling .focus()
was doing. I think we want logic like this:
- If the sidenav close is triggered by keyboard interaction (e.g. pressing escape) set it to
keyboard
focused - If the sidenav close is triggered by mouse interaction (e.g. clicking on backdrop) set it to
mouse
focused - If the sidenav close is triggered by directly calling
close
ortoggle
method allow the user to optionally supply the focus origin and default toprogram
if they don't specify.
I think opening the sidenav needs similar logic but it's a little trickier because the sidenav isn't directly responsible for setting the focus on open, it goes through the focus trap.
You can test if this is working properly by opening and closing the sidenav in the demo app via different means and seeing what the resulting focus effect looks like
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.
I'm not sure how that would work, considering that we don't have the equivalent of mdMenuTriggerFor for the sidenav. Alternatively, I can fix this only for the dialog where we have more control over the closing logic.
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.
I think the closing logic I described is fairly straightforward to implement. For open you can just allow the user to pass an origin to the open
method and it will default to program
if they don't specify. That way if people really care about getting rid of the focus indicator they can add logic for it manually. It might also be nice to make a mdSidenavTrigger
directive that does the focus origin logic for them.
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.
That makes sense. I was trying to think of something that would work automagically.
32ad54c
to
063bc9a
Compare
063bc9a
to
7ae7e0a
Compare
Reworked this one based on what we discussed a while ago @mmalerba, can you take another look? |
can you update the demo app's sidenav as well, to avoid this bug:
|
7ae7e0a
to
5cc2417
Compare
Done @mmalerba. |
…tons Currently the tint that is added on top of focused buttons won't show up if the button is focused programmatically, which means that the button won't appear focused in cases like the dialog closing and restoring focus to its trigger. This seems to have been introduced by 5d6920d. Fixes angular#7510.
5cc2417
to
bd52b35
Compare
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Currently the tint that is added on top of focused buttons won't show up if the button is focused programmatically, which means that the button won't appear focused in cases like the dialog closing and restoring focus to its trigger. This seems to have been introduced by 5d6920d.
Fixes #7510.