Skip to content

Commit 76e2e09

Browse files
committed
fix: icon action handler method (#207)
1 parent 963a62d commit 76e2e09

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/components/shared/button/vsc-btn-state-info.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { css, CSSResultGroup, html, LitElement, nothing, TemplateResult } from 'lit';
1+
import { css, CSSResultGroup, html, LitElement, TemplateResult } from 'lit';
22
import { customElement, property } from 'lit/decorators.js';
33
import { classMap } from 'lit/directives/class-map.js';
44

55
@customElement('vsc-btn-state-info')
66
export class VscBtnStateInfo extends LitElement {
77
@property({ attribute: false }) public primary?: string | TemplateResult<1>;
8-
@property({ attribute: false }) public secondary?: string | TemplateResult<1>;
8+
@property({ attribute: false }) public secondary?: TemplateResult;
99
@property({ type: Boolean }) public multiline_secondary?: boolean = false;
1010

1111
protected render(): TemplateResult {
@@ -16,7 +16,7 @@ export class VscBtnStateInfo extends LitElement {
1616
? html`<span class=${classMap({ secondary: true, multiline_secondary: this.multiline_secondary! })}
1717
>${this.secondary}</span
1818
>`
19-
: nothing}
19+
: html``}
2020
</div>
2121
`;
2222
}
@@ -43,7 +43,7 @@ export class VscBtnStateInfo extends LitElement {
4343
font-weight: var(--card-secondary-font-weight, 400);
4444
font-size: var(--card-secondary-font-size, 12px);
4545
line-height: var(--card-secondary-line-height, 16px);
46-
color: var(--card-secondary-color, var(--secondary-text-color));
46+
color: var(--card-secondary-color, var(--primary-text-color));
4747
letter-spacing: var(--card-secondary-letter-spacing, 0.4px);
4848
text-overflow: ellipsis;
4949
overflow: hidden;

src/components/shared/button/vsc-button-card-item.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { html, css, CSSResultGroup, TemplateResult, nothing, PropertyValues } from 'lit';
22
import { customElement, property, query } from 'lit/decorators.js';
3+
import { ifDefined } from 'lit/directives/if-defined.js';
34
import { styleMap } from 'lit/directives/style-map.js';
45

5-
import { COMPONENT } from '../../../constants/const';
66
import './vsc-btn-card';
77
import './vsc-state-item';
88
import './vsc-btn-badge';
9+
import { COMPONENT } from '../../../constants/const';
910
import { handleAction } from '../../../ha/panels/common/handle-actions';
1011
import { hasAction } from '../../../types/config';
1112
import { BaseButton } from '../../../utils/base-button';
@@ -24,34 +25,36 @@ export class VscButtonCardItem extends BaseButton {
2425

2526
protected firstUpdated(_changedProperties: PropertyValues): void {
2627
super.firstUpdated(_changedProperties);
27-
this._addIconAction();
28-
this._addCardAction();
28+
this._addActions();
2929
}
30-
31-
private _addIconAction(): void {
32-
if (!this._hasIconAction) {
30+
private _addActions(): void {
31+
if (!Boolean(this._hasAction || this._hasIconAction)) {
3332
return;
3433
}
35-
const icon = this.renderRoot.querySelector('vsc-btn-shape-icon') as HTMLElement;
36-
if (!icon) {
37-
return;
38-
}
39-
const config = this._iconActions;
40-
addActions(icon, config);
41-
}
34+
const clickable = this.shadowRoot?.querySelector('#clickable-background') as HTMLElement;
35+
const icon = this.shadowRoot?.querySelector('vsc-btn-shape-icon') as HTMLElement;
4236

43-
private _addCardAction(): void {
44-
const card = this.renderRoot.querySelector('#clickable-background') as HTMLElement;
45-
if (!card) {
46-
return;
47-
}
4837
const handlerOptions: ActionHandleOpts = {
4938
hasHold: true,
5039
hasDoubleClick: true,
5140
hasClick: true,
5241
};
53-
addActionHandler(card, handlerOptions);
54-
card.addEventListener('action', this._handleCardEvent.bind(this) as EventListener);
42+
43+
if (clickable) {
44+
addActionHandler(clickable, handlerOptions);
45+
clickable.addEventListener('action', this._handleCardEvent.bind(this) as EventListener);
46+
}
47+
if (icon) {
48+
// Separate handlers for icon actions
49+
if (this._hasIconAction) {
50+
// If icon has its own actions, use those instead of the main handlers
51+
const iconActionSpec = this._iconActions;
52+
addActions(icon, iconActionSpec);
53+
} else {
54+
addActionHandler(icon, handlerOptions);
55+
icon.addEventListener('action', this._handleCardEvent.bind(this) as EventListener);
56+
}
57+
}
5558
}
5659

5760
protected render(): TemplateResult | typeof nothing {
@@ -73,14 +76,16 @@ export class VscButtonCardItem extends BaseButton {
7376
const notifyColor = this._getTemplateValue('notify_color');
7477

7578
const _hasAction = this._hasAction;
79+
const _hasIconAction = this._hasIconAction;
80+
const _btnHasAction = Boolean(_hasAction || _hasIconAction);
7681

7782
return html`
7883
<ha-card ?transparent=${btnShowConfig.transparent} style=${styleMap(iconStyle)}>
7984
<div
8085
id="clickable-background"
8186
class="background"
82-
role=${_hasAction ? 'button' : undefined}
83-
tabindex=${_hasAction ? '0' : undefined}
87+
role=${ifDefined(_hasAction ? 'button' : undefined)}
88+
tabindex=${ifDefined(_hasAction ? '0' : undefined)}
8489
>
8590
<ha-ripple .disabled=${!_hasAction}></ha-ripple>
8691
</div>
@@ -90,10 +95,10 @@ export class VscButtonCardItem extends BaseButton {
9095
>
9196
<vsc-btn-shape-icon
9297
slot="icon"
93-
.interactive=${this._hasIconAction}
98+
.interactive=${_hasIconAction}
9499
.imageSrc=${imageUrl}
95-
role=${this._hasIconAction ? 'button' : undefined}
96-
tabindex=${this._hasIconAction ? '0' : undefined}
100+
role=${ifDefined(_btnHasAction ? 'button' : undefined)}
101+
tabindex=${ifDefined(_btnHasAction ? '0' : undefined)}
97102
>
98103
<ha-state-icon
99104
slot="icon"
@@ -111,7 +116,7 @@ export class VscButtonCardItem extends BaseButton {
111116
>
112117
${notifyText
113118
? html`<span>${notifyText}</span>`
114-
: html`<ha-icon .icon=${notifyIcon || 'mdi:circle'}></ha-icon>`}
119+
: html`<ha-icon .icon=${notifyIcon || 'mdi:alert-circle-outline'}></ha-icon>`}
115120
</vsc-btn-badge>`
116121
: nothing
117122
}

0 commit comments

Comments
 (0)