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: restore focus-ring when closing on click #4341

Merged
merged 2 commits into from
Aug 12, 2022

Conversation

web-padawan
Copy link
Member

@web-padawan web-padawan commented Aug 11, 2022

Description

Fixes #4154

TL;DR: after focus-ring is set after focusing with keyboard, only remove it on blur when the overlay is closed.

Problem

Currently, the logic for restoring focus-ring in the date-picker is broken. The following happens when closing on click:

  1. Focus is lost to the <body> (in case of outside click) or <td> inside of <vaadin-month-calendar> (cell click),
  2. The focusout event is fired on the <input> and _setFocused(false) removes the focus-ring attribute,
  3. The logic trying to set focus-ring is called too early, as the focus hasn't been restored to the <input> yet,
  4. After a timeout, vaadin-overlay restores focus, but not focus-ring because the keyboardActive is false.

Solution

Here is how this behavior will be changed after this PR is merged.

  1. On first focusin event, calling _setFocused(true) sets the flag to indicate if focus-ring should be preserved,
  2. On focusout event, _setFocused(false) is no longer called when opened (the focus will be restored anyways),
  3. On subsequent focusin after restoring focus back to the <input>, there is no extra _setFocused(true) call.
  4. Even though keyboardActive is false when restoring focus, it's not checked and focus-ring is preserved.

Type of change

  • Bugfix

this._overlayContent.addEventListener('focusin', () => {
this._setFocused(true);
if (this._keyboardActive) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is needed to avoid focus-ring from getting removed on date click. Currently, it happens because FocusMixin is also used in vaadin-month-calendar which resets keyboardActive flag.

if (this._openedWithFocusRing && this.hasAttribute('focused')) {
this.setAttribute('focus-ring', '');
} else if (!this.hasAttribute('focused')) {
this.blur();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is removed without replacement because it wasn't working anyways (focus is restored after a timeout). Furthermore, the case of calling input.blur() is covered by focus listener:

_onFocus(event) {
super._onFocus(event);
if (this._noInput) {
event.target.blur();
}

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@web-padawan web-padawan requested a review from tomivirkki August 12, 2022 07:44
@web-padawan web-padawan removed the request for review from vursen August 12, 2022 08:43
@web-padawan web-padawan merged commit 653e1fa into master Aug 12, 2022
@web-padawan web-padawan deleted the fix/date-picker-focus-ring branch August 12, 2022 08:43
@vaadin-bot
Copy link
Collaborator

This ticket/PR has been released with Vaadin 23.2.0.beta2 and is also targeting the upcoming stable 23.2.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[date-picker] Restoring focus-ring attribute does not work on outside click
3 participants