Skip to content

Commit

Permalink
feat: apply hover effects anywhere on the component (#3796)
Browse files Browse the repository at this point in the history
* feat: apply hover effects anywhere on the component

* prevent styles applying to disabled items

* fix styles

* Apply styles to textarea

* limit changes to d2l-input-text/area

* Move event listeners to `connectedCallback`
  • Loading branch information
GZolla committed Jul 24, 2023
1 parent d8528c6 commit b81e832
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
16 changes: 12 additions & 4 deletions components/inputs/input-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,21 @@ class InputText extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMixin(
if (this.hasAttribute('aria-label')) {
this.labelRequired = false;
}
this.addEventListener('mouseover', this._handleMouseEnter);
this.addEventListener('mouseout', this._handleMouseLeave);
this.addEventListener('click', this._handleClick);
}

disconnectedCallback() {
super.disconnectedCallback();
if (this._intersectionObserver) this._intersectionObserver.disconnect();
const container = this.shadowRoot && this.shadowRoot.querySelector('.d2l-input-text-container');
this.removeEventListener('mouseover', this._handleMouseEnter);
this.removeEventListener('mouseout', this._handleMouseLeave);
this.removeEventListener('click', this._handleClick);
if (!container) return;
container.removeEventListener('blur', this._handleBlur, true);
container.removeEventListener('focus', this._handleFocus, true);
container.removeEventListener('mouseover', this._handleMouseEnter);
container.removeEventListener('mouseout', this._handleMouseLeave);
}

firstUpdated(changedProperties) {
Expand All @@ -352,8 +356,6 @@ class InputText extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMixin(
if (!container) return;
container.addEventListener('blur', this._handleBlur, true);
container.addEventListener('focus', this._handleFocus, true);
container.addEventListener('mouseover', this._handleMouseEnter);
container.addEventListener('mouseout', this._handleMouseLeave);

// if initially hidden then update layout when it becomes visible
if (typeof(IntersectionObserver) === 'function') {
Expand Down Expand Up @@ -542,6 +544,12 @@ class InputText extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMixin(
));
}

_handleClick(e) {
const input = this.shadowRoot?.querySelector('.d2l-input');
if (!input || e.composedPath()[0] !== this) return;
input.focus();
}

_handleFocus() {
this._focused = true;
}
Expand Down
36 changes: 34 additions & 2 deletions components/inputs/input-textarea.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '../tooltip/tooltip.js';
import { css, html, LitElement } from 'lit';
import { classMap } from 'lit-html/directives/class-map.js';
import { FocusMixin } from '../../mixins/focus/focus-mixin.js';
import { formatNumber } from '@brightspace-ui/intl/lib/number.js';
import { FormElementMixin } from '../form/form-element-mixin.js';
Expand Down Expand Up @@ -86,7 +87,8 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
* Value of the input
* @type {string}
*/
value: { type: String }
value: { type: String },
_hovered: { state: true }
};
}

Expand Down Expand Up @@ -157,6 +159,7 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
this.value = '';

this._descriptionId = getUniqueId();
this._hovered = false;
this._textareaId = getUniqueId();
}

Expand Down Expand Up @@ -192,6 +195,16 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
if (this.hasAttribute('aria-label')) {
this.labelRequired = false;
}
this.addEventListener('mouseover', this._handleMouseEnter);
this.addEventListener('mouseout', this._handleMouseLeave);
this.addEventListener('click', this._handleClick);
}

disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('mouseover', this._handleMouseEnter);
this.removeEventListener('mouseout', this._handleMouseLeave);
this.removeEventListener('click', this._handleClick);
}

render() {
Expand All @@ -211,6 +224,11 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
if (this.rows > 0) mirrorStyles.minHeight = `calc(${this.rows + 1}rem + 2px)`;
if (this.maxRows > 0) mirrorStyles.maxHeight = `calc(${this.maxRows + 1}rem + 2px)`;

const inputClasses = {
'd2l-input': true,
'd2l-input-focus': !this.disabled && this._hovered
};

const textarea = html`
<div class="d2l-input-textarea-container d2l-skeletize">
<div class="d2l-input d2l-input-textarea-mirror" style="${styleMap(mirrorStyles)}" aria-invalid="${ifDefined(ariaInvalid)}">
Expand All @@ -222,7 +240,7 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
aria-required="${ifDefined(ariaRequired)}"
@blur="${this._handleBlur}"
@change="${this._handleChange}"
class="d2l-input"
class=${classMap(inputClasses)}
?disabled="${disabled}"
id="${this._textareaId}"
@input="${this._handleInput}"
Expand Down Expand Up @@ -307,6 +325,12 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
));
}

_handleClick(e) {
const input = this.shadowRoot && this.shadowRoot.querySelector('textarea');
if (!input || e.composedPath()[0] !== this) return;
input.focus();
}

_handleInput(e) {
this.value = e.target.value;
return true;
Expand All @@ -316,6 +340,14 @@ class InputTextArea extends FocusMixin(LabelledMixin(FormElementMixin(SkeletonMi
e.preventDefault();
}

_handleMouseEnter() {
this._hovered = true;
}

_handleMouseLeave() {
this._hovered = false;
}

}

customElements.define('d2l-input-textarea', InputTextArea);

0 comments on commit b81e832

Please sign in to comment.