Skip to content

Commit c2d88fd

Browse files
authored
refactor: use FocusMixin instead of custom logic (#2966)
1 parent 6f9cc93 commit c2d88fd

3 files changed

Lines changed: 30 additions & 53 deletions

File tree

‎packages/avatar-group/test/avatar-group.test.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,25 @@ describe('avatar-group', () => {
404404
overflow.click();
405405
});
406406

407-
it('should not restore focus-ring attribute on close if not set', (done) => {
407+
it('should restore focus-ring attribute on close if closed with keyboard', (done) => {
408408
overlay.addEventListener('vaadin-overlay-open', () => {
409409
const list = overlay.content.querySelector('vaadin-avatar-group-list-box');
410410
escKeyDown(list);
411411

412+
afterNextRender(overlay, () => {
413+
expect(overflow.hasAttribute('focus-ring')).to.be.true;
414+
done();
415+
});
416+
});
417+
418+
overflow.click();
419+
});
420+
421+
it('should not restore focus-ring attribute on close if not set', (done) => {
422+
overlay.addEventListener('vaadin-overlay-open', () => {
423+
const items = overlay.content.querySelectorAll('[theme="avatar-group-item"]');
424+
items[0].click();
425+
412426
afterNextRender(overlay, () => {
413427
expect(overflow.hasAttribute('focus-ring')).to.be.false;
414428
done();

‎packages/avatar/src/vaadin-avatar.d.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
7+
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
78
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
89

910
export interface AvatarI18n {
@@ -26,15 +27,17 @@ export interface AvatarI18n {
2627
* `abbr` | The abbreviation element
2728
* `icon` | The icon element
2829
*
29-
* The following attributes are exposed for styling:
30+
* The following state attributes are available for styling:
3031
*
31-
* Attribute | Description
32-
* --------- | -----------
32+
* Attribute | Description
33+
* ------------------|-------------
34+
* `focus-ring` | Set when the avatar is focused using the keyboard.
35+
* `focused` | Set when the avatar is focused.
3336
* `has-color-index` | Set when the avatar has `colorIndex` and the corresponding custom CSS property exists.
3437
*
3538
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
3639
*/
37-
declare class Avatar extends ElementMixin(ThemableMixin(HTMLElement)) {
40+
declare class Avatar extends FocusMixin(ElementMixin(ThemableMixin(HTMLElement))) {
3841
/**
3942
* The path to the image
4043
*/

‎packages/avatar/src/vaadin-avatar.js‎

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,10 @@
55
*/
66
import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
77
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
8+
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
89
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
910
import './vaadin-avatar-icons.js';
1011

11-
// We consider the keyboard to be active if the window has received a keydown
12-
// event since the last mousedown event.
13-
let keyboardActive = false;
14-
15-
// Listen for top-level Tab keydown and mousedown events.
16-
// Use capture phase so we detect events even if they're handled.
17-
window.addEventListener(
18-
'keydown',
19-
(e) => {
20-
keyboardActive = e.keyCode === 9;
21-
},
22-
true
23-
);
24-
25-
window.addEventListener(
26-
'mousedown',
27-
() => {
28-
keyboardActive = false;
29-
},
30-
true
31-
);
32-
3312
/**
3413
* `<vaadin-avatar>` is a Web Component providing avatar displaying functionality.
3514
*
@@ -46,19 +25,22 @@ window.addEventListener(
4625
* `abbr` | The abbreviation element
4726
* `icon` | The icon element
4827
*
49-
* The following attributes are exposed for styling:
28+
* The following state attributes are available for styling:
5029
*
51-
* Attribute | Description
52-
* --------- | -----------
30+
* Attribute | Description
31+
* ------------------|-------------
32+
* `focus-ring` | Set when the avatar is focused using the keyboard.
33+
* `focused` | Set when the avatar is focused.
5334
* `has-color-index` | Set when the avatar has `colorIndex` and the corresponding custom CSS property exists.
5435
*
5536
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
5637
*
5738
* @extends HTMLElement
39+
* @mixes FocusMixin
5840
* @mixes ElementMixin
5941
* @mixes ThemableMixin
6042
*/
61-
class Avatar extends ElementMixin(ThemableMixin(PolymerElement)) {
43+
class Avatar extends FocusMixin(ElementMixin(ThemableMixin(PolymerElement))) {
6244
static get template() {
6345
return html`
6446
<style>
@@ -236,28 +218,6 @@ class Avatar extends ElementMixin(ThemableMixin(PolymerElement)) {
236218
if (!this.hasAttribute('tabindex')) {
237219
this.setAttribute('tabindex', '0');
238220
}
239-
240-
this.addEventListener('focusin', () => {
241-
this.__setFocused(true);
242-
});
243-
244-
this.addEventListener('focusout', () => {
245-
this.__setFocused(false);
246-
});
247-
}
248-
249-
/** @private */
250-
__setFocused(focused) {
251-
if (focused) {
252-
this.setAttribute('focused', '');
253-
254-
if (keyboardActive) {
255-
this.setAttribute('focus-ring', '');
256-
}
257-
} else {
258-
this.removeAttribute('focused');
259-
this.removeAttribute('focus-ring');
260-
}
261221
}
262222

263223
/** @private */

0 commit comments

Comments
 (0)