Skip to content

Commit

Permalink
🐛 Fix effect selection and scrolling behavior
Browse files Browse the repository at this point in the history
Improve effect selection and dropdown behavior:

- Move scrolling and highlighting to requestAnimationFrame
- Implement async effect selection
- Add logging for better debugging
- Remove unused import

This change ensures smoother UI updates and more reliable
effect selection.
  • Loading branch information
hyperb1iss committed Aug 14, 2024
1 parent be19a5d commit 7993c13
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions src/hyper-light-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
formatAttributeValue,
} from './utils';
import styles from './hyper-light-card-styles.css';
import { resolve } from 'path';

Check failure on line 11 in src/hyper-light-card.ts

View workflow job for this annotation

GitHub Actions / Build + test

'resolve' is defined but never used

interface Hass {
states: Record<string, HassEntity>;
Expand Down Expand Up @@ -90,11 +91,6 @@ class HyperLightCard extends LitElement {
console.debug('HyperLightCard: firstUpdated called');
this._setupMutationObserver();
this._debouncedExtractColors();

// Scroll to the current effect if the dropdown is already open
if (this._isDropdownOpen) {
this._scrollToCurrentEffect();
}
}

private _extractColors() {
Expand Down Expand Up @@ -199,8 +195,16 @@ class HyperLightCard extends LitElement {
}

updated(changedProperties: Map<string | number | symbol, unknown>) {
console.debug('HyperLightCard: updated called', changedProperties);
super.updated(changedProperties);

console.debug('HyperLightCard: updated called', changedProperties);
console.debug('HyperLightCard: current state:', {
isOn: this._isOn,
currentEffect: this._currentEffect,
brightness: this._brightness,
isDropdownOpen: this._isDropdownOpen,
});

if (changedProperties.has('hass') || changedProperties.has('config')) {
this._debouncedUpdate();
}
Expand All @@ -227,7 +231,12 @@ class HyperLightCard extends LitElement {
}

render() {
console.debug('HyperLightCard: render called');
console.debug('HyperLightCard: render called with state:', {
isOn: this._isOn,
currentEffect: this._currentEffect,
brightness: this._brightness,
isDropdownOpen: this._isDropdownOpen,
});
if (!this.hass || !this.config) {
console.debug('HyperLightCard: hass or config not available');
return html``;
Expand All @@ -253,6 +262,14 @@ class HyperLightCard extends LitElement {
'--slider-color': this._accentColor,
};

// Scroll and highlight
requestAnimationFrame(() => {
if (this._isDropdownOpen) {
this._scrollToCurrentEffect();
this._highlightCurrentEffect();
}
});

return html`
<ha-card>
<div
Expand Down Expand Up @@ -559,6 +576,20 @@ class HyperLightCard extends LitElement {
});
}

private _highlightCurrentEffect() {
const dropdownContent = this.shadowRoot!.querySelector('.dropdown-content');
const items = dropdownContent?.querySelectorAll('.dropdown-item') || [];

items.forEach(item => {
item.classList.remove('selected');
if (item.textContent?.trim() === this._currentEffect) {
item.classList.add('selected');
}
});

console.debug('HyperLightCard: Highlighted current effect');
}

private _toggleDropdown(e: Event) {
console.debug('HyperLightCard: _toggleDropdown called');
e.stopPropagation();
Expand All @@ -575,14 +606,14 @@ class HyperLightCard extends LitElement {
this.requestUpdate();
}

private _selectEffect(effect: string) {
private async _selectEffect(effect: string) {
console.debug('HyperLightCard: _selectEffect called', effect);
this._currentEffect = effect;
this._isDropdownOpen = false;
this.requestUpdate();

if (this._isOn) {
this.hass!.callService('light', 'turn_on', {
await this.hass!.callService('light', 'turn_on', {
entity_id: this.config!.entity,
effect: effect,
});
Expand Down

0 comments on commit 7993c13

Please sign in to comment.