Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: apply hover effects anywhere on the component #3796

Merged
merged 6 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
dlockhart marked this conversation as resolved.
Show resolved Hide resolved
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);