lion/packages/field/src/FocusMixin.js
Thomas Allmer ec8da8f12c feat: release inital public lion version
Co-authored-by: Mikhail Bashkirov <mikhail.bashkirov@ing.com>
Co-authored-by: Thijs Louisse <thijs.louisse@ing.com>
Co-authored-by: Joren Broekema <joren.broekema@ing.com>
Co-authored-by: Gerjan van Geest <gerjan.van.geest@ing.com>
Co-authored-by: Erik Kroes <erik.kroes@ing.com>
Co-authored-by: Lars den Bakker <lars.den.bakker@ing.com>
2019-04-26 10:37:57 +02:00

49 lines
1.5 KiB
JavaScript

/* eslint-disable no-underscore-dangle */
import { dedupeMixin, DelegateMixin } from '@lion/core';
export const FocusMixin = dedupeMixin(
superclass =>
// eslint-disable-next-line no-unused-vars, max-len, no-shadow
class FocusMixin extends DelegateMixin(superclass) {
get delegations() {
return {
...super.delegations,
target: () => this.inputElement,
events: [...super.delegations.events, 'focus', 'blur'], // since these events don't bubble
methods: [...super.delegations.methods, 'focus', 'blur'],
properties: [...super.delegations.properties, 'onfocus', 'onblur', 'autofocus'],
attributes: [...super.delegations.attributes, 'onfocus', 'onblur', 'autofocus'],
};
}
get events() {
return {
...super.events,
// Listen to focusin instead of focus, because it blurs
_onFocus: [() => this.inputElement, 'focusin'],
_onBlur: [() => this.inputElement, 'focusout'],
};
}
/**
* Helper Function to easily check if the element is being focused
*
* TODO: performance comparision vs
* return this.inputElement === document.activeElement;
*/
get focused() {
return this.classList.contains('state-focused');
}
_onFocus() {
if (super._onFocus) super._onFocus();
this.classList.add('state-focused');
}
_onBlur() {
if (super._onBlur) super._onBlur();
this.classList.remove('state-focused');
}
},
);