Skip to content

Commit

Permalink
fix: do not set focus-ring on mousedown focus (#2963)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan committed Nov 1, 2021
1 parent f5d556c commit c76f6e2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/accordion/test/accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ describe('vaadin-accordion', () => {
accordion.items[1].disabled = true;
accordion.items[2].disabled = true;
heading = getHeading(0);
arrowDownKeyDown(heading);
expect(accordion.items[0].hasAttribute('focus-ring')).to.be.true;
});

Expand Down
2 changes: 2 additions & 0 deletions packages/component-base/src/focus-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface FocusMixinConstructor {
}

interface FocusMixin {
readonly _keyboardActive: boolean;

/**
* Override to change how focused and focus-ring attributes are set.
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/component-base/src/focus-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ window.addEventListener(
export const FocusMixin = dedupingMixin(
(superclass) =>
class FocusMixinClass extends superclass {
/**
* @protected
* @return {boolean}
*/
get _keyboardActive() {
return keyboardActive;
}

/** @protected */
ready() {
this.addEventListener('focusin', (e) => {
Expand Down Expand Up @@ -78,7 +86,7 @@ export const FocusMixin = dedupingMixin(

// focus-ring is true when the element was focused from the keyboard.
// Focus Ring [A11ycasts]: https://youtu.be/ilj2P5-5CjI
this.toggleAttribute('focus-ring', focused && keyboardActive);
this.toggleAttribute('focus-ring', focused && this._keyboardActive);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/password-field/src/vaadin-password-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ export class PasswordField extends SlotStylesMixin(TextField) {
);
}

/** @protected */
/**
* Override method inherited from `FocusMixin` to toggle password visibility.
* @param {boolean} focused
* @protected
* @override
*/
_setFocused(focused) {
super._setFocused(focused);

Expand All @@ -227,7 +232,7 @@ export class PasswordField extends SlotStylesMixin(TextField) {
} else {
const isButtonFocused = this.getRootNode().activeElement === this._revealNode;
// Remove focus-ring from the field when the reveal button gets focused
this.toggleAttribute('focus-ring', !isButtonFocused);
this.toggleAttribute('focus-ring', this._keyboardActive && !isButtonFocused);
}
}

Expand Down
11 changes: 10 additions & 1 deletion packages/password-field/test/password-field.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from '@esm-bundle/chai';
import { sendKeys } from '@web/test-runner-commands';
import sinon from 'sinon';
import { fixtureSync, focusout } from '@vaadin/testing-helpers';
import { fixtureSync, focusout, mousedown } from '@vaadin/testing-helpers';
import '../src/vaadin-password-field.js';

describe('password-field', () => {
Expand Down Expand Up @@ -145,6 +145,15 @@ describe('password-field', () => {
expect(passwordField.hasAttribute('focus-ring')).to.be.true;
});
});

describe('mousedown', () => {
it('should not set focus-ring attribute when focusing on mousedown', () => {
// reset FocusMixin flag
mousedown(passwordField);
passwordField.focus();
expect(passwordField.hasAttribute('focus-ring')).to.be.false;
});
});
});

describe('revealButtonHidden', () => {
Expand Down

0 comments on commit c76f6e2

Please sign in to comment.