11import { html , css , CSSResultGroup , TemplateResult , nothing , PropertyValues } from 'lit' ;
22import { customElement , property , query } from 'lit/decorators.js' ;
3+ import { ifDefined } from 'lit/directives/if-defined.js' ;
34import { styleMap } from 'lit/directives/style-map.js' ;
45
5- import { COMPONENT } from '../../../constants/const' ;
66import './vsc-btn-card' ;
77import './vsc-state-item' ;
88import './vsc-btn-badge' ;
9+ import { COMPONENT } from '../../../constants/const' ;
910import { handleAction } from '../../../ha/panels/common/handle-actions' ;
1011import { hasAction } from '../../../types/config' ;
1112import { 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