Set button role on button card and handle enter and space (#11627)

This commit is contained in:
Bram Kragten 2022-02-09 23:36:16 +01:00 committed by GitHub
parent 9c9bfa2b77
commit dc27871189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,9 +160,13 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
hasHold: hasAction(this._config!.hold_action),
hasDoubleClick: hasAction(this._config!.double_tap_action),
})}
role="button"
aria-label=${this._config.name ||
(stateObj ? computeStateName(stateObj) : "")}
tabindex=${ifDefined(
hasAction(this._config.tap_action) ? "0" : undefined
)}
@keydown=${this._handleKeyDown}
>
${this._config.show_icon
? html`
@ -230,6 +234,12 @@ export class HuiButtonCard extends LitElement implements LovelaceCard {
return this._ripple;
});
private _handleKeyDown(ev: KeyboardEvent) {
if (ev.key === "Enter" || ev.key === " ") {
handleAction(this, this.hass!, this._config!, "tap");
}
}
@eventOptions({ passive: true })
private handleRippleActivate(evt?: Event) {
this._rippleHandlers.startPress(evt);