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

bug(DatePicker): Focus Handling does not work with Shadow DOM #21785

Closed
ghost opened this issue Feb 3, 2021 · 1 comment · Fixed by #21796, cayacdev/cashbox#88, ToWipf/jasmarty#155 or CarlosPavajeau/Kaizen#251
Assignees
Labels
area: material/datepicker P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@ghost
Copy link

ghost commented Feb 3, 2021

Reproduction

Stackblitz Demo: https://stackblitz.com/edit/components-issue-mlorwc?file=src/app/example-component.html
GitHub Repo: https://github.com/zie-rts/mat-datepicker-shadow-dom

The Stackblitz demo does not use custom elements (it did not work) and does not have any styling.
The GitHub repo uses custom elements, has some styling and also shows that the element is focused.

Steps to reproduce:

  1. Open the stackblitz demo or clone the github repo (and run npm i + npm start)
  2. Click on the datepicker button to open the calendar
  3. Click on a date (the calender will be closed)

Step 2/3: Instead of clicking the datepicker button you can also focus the input element and press Alt+Arrow Down to open the calender and use the keyboard to select a date.

Expected Behavior

The previous focused element should be focused again.
That's either the datepicker button or the input element.

Actual Behavior

Angular Material sets the focus to the element that was previously focused.
If the datepicker field is used inside Shadow DOM the shadow root element will be focused. As this will not be focusable most of the time the body element will receive the focus. If we set tabindex="-1" on the element we can see that the element gets the focus (see GitHub repo).

Environment

  • Angular: 11.1.1
  • CDK/Material: 11.1.1
  • Browser(s): Chrome
  • Operating System: macOS
@ghost ghost added the needs triage This issue needs to be triaged by the team label Feb 3, 2021
@crisbeto crisbeto self-assigned this Feb 3, 2021
@crisbeto crisbeto added area: material/datepicker has pr P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed needs triage This issue needs to be triaged by the team labels Feb 3, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Feb 3, 2021
… dom

Our focus restoration works by checking `document.activeElement` before the
panel is opened and restoring to that element on close. The problem is that
`activeElement` will return the shadow root, if the focused element is inside one.

These changes add some extra logic to account for it.

Fixes angular#21785.
annieyw pushed a commit that referenced this issue Feb 9, 2021
… dom (#21796)

Our focus restoration works by checking `document.activeElement` before the
panel is opened and restoring to that element on close. The problem is that
`activeElement` will return the shadow root, if the focused element is inside one.

These changes add some extra logic to account for it.

Fixes #21785.
@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 Mar 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.