55 */
66import { PolymerElement , html } from '@polymer/polymer/polymer-element.js' ;
77import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js' ;
8+ import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js' ;
89import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js' ;
910import './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