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

Fix automation picker overflow menu for keyboard #21048

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 4 additions & 1 deletion src/components/ha-menu-item.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { MdMenuItem } from "@material/web/menu/menu-item";
import type { CloseMenuEvent } from "@material/web/menu/menu";
import { css } from "lit";
import { customElement } from "lit/decorators";
import { customElement, property } from "lit/decorators";

@customElement("ha-menu-item")
export class HaMenuItem extends MdMenuItem {
@property() closeAction?: (ev: CloseMenuEvent) => void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions to improve the API:

  1. Turn off the attribute. I don't know why lit-analyzer doesn't flag that function props shouldn't be attributes.
  2. Make the parameter the triggering item instead of the event, and make it optional as many handlers won't need it.
  3. To make the purpose much clearer where used in instances, maybe rename to clickAction or just action. @bramkragten any strong opinion here?
Suggested change
@property() closeAction?: (ev: CloseMenuEvent) => void;
@property({ attribute: false }) clickAction?: (item?: HTMLElement) => void;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should be passed as the item here, the ev.detail.initiator?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes


static override styles = [
...super.styles,
css`
Expand Down
24 changes: 24 additions & 0 deletions src/components/ha-menu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { MdMenu } from "@material/web/menu/menu";
karwosts marked this conversation as resolved.
Show resolved Hide resolved
import type { CloseMenuEvent } from "@material/web/menu/menu";
import {
CloseReason,
KeydownCloseKey,
} from "@material/web/menu/internal/controllers/shared";
karwosts marked this conversation as resolved.
Show resolved Hide resolved
import { css } from "lit";
import { customElement } from "lit/decorators";
import type { HaMenuItem } from "./ha-menu-item";

@customElement("ha-menu")
export class HaMenu extends MdMenu {
connectedCallback(): void {
super.connectedCallback();
this.addEventListener("close-menu", this._handleCloseMenu);
}
steverep marked this conversation as resolved.
Show resolved Hide resolved

private _handleCloseMenu(ev: CloseMenuEvent) {
if (
ev.detail.reason.kind === CloseReason.KEYDOWN &&
ev.detail.reason.key === KeydownCloseKey.ESCAPE
)
return;
karwosts marked this conversation as resolved.
Show resolved Hide resolved
(ev.detail.initiator as HaMenuItem).closeAction?.(ev);
}
karwosts marked this conversation as resolved.
Show resolved Hide resolved

static override styles = [
...super.styles,
css`
Expand All @@ -18,4 +38,8 @@ declare global {
interface HTMLElementTagNameMap {
"ha-menu": HaMenu;
}

interface HTMLElementEventMap {
"close-menu": CloseMenuEvent;
steverep marked this conversation as resolved.
Show resolved Hide resolved
}
}
64 changes: 32 additions & 32 deletions src/panels/config/automation/ha-automation-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
</ha-fab>
</hass-tabs-subpage-data-table>
<ha-menu id="overflow-menu" positioning="fixed">
<ha-menu-item @click=${this._showInfo}>
<ha-menu-item .closeAction=${this._showInfo}>
<ha-svg-icon
.path=${mdiInformationOutline}
slot="start"
Expand All @@ -832,29 +832,29 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
</div>
</ha-menu-item>

<ha-menu-item @click=${this._showSettings}>
<ha-menu-item .closeAction=${this._showSettings}>
<ha-svg-icon .path=${mdiCog} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize(
"ui.panel.config.automation.picker.show_settings"
)}
</div>
</ha-menu-item>
<ha-menu-item @click=${this._editCategory}>
<ha-menu-item .closeAction=${this._editCategory}>
<ha-svg-icon .path=${mdiTag} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize(
`ui.panel.config.automation.picker.${this._overflowAutomation?.category ? "edit_category" : "assign_category"}`
)}
</div>
</ha-menu-item>
<ha-menu-item @click=${this._runActions}>
<ha-menu-item .closeAction=${this._runActions}>
<ha-svg-icon .path=${mdiPlay} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize("ui.panel.config.automation.editor.run")}
</div>
</ha-menu-item>
<ha-menu-item @click=${this._showTrace}>
<ha-menu-item .closeAction=${this._showTrace}>
<ha-svg-icon .path=${mdiTransitConnection} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize(
Expand All @@ -863,13 +863,13 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
</div>
</ha-menu-item>
<md-divider role="separator" tabindex="-1"></md-divider>
<ha-menu-item @click=${this._duplicate}>
<ha-menu-item .closeAction=${this._duplicate}>
<ha-svg-icon .path=${mdiContentDuplicate} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize("ui.panel.config.automation.picker.duplicate")}
</div>
</ha-menu-item>
<ha-menu-item @click=${this._toggle}>
<ha-menu-item .closeAction=${this._toggle}>
<ha-svg-icon
.path=${
this._overflowAutomation?.state === "off"
Expand All @@ -888,7 +888,7 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
}
</div>
</ha-menu-item>
<ha-menu-item @click=${this._deleteConfirm} class="warning">
<ha-menu-item .closeAction=${this._deleteConfirm} class="warning">
<ha-svg-icon .path=${mdiDelete} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize("ui.panel.config.automation.picker.delete")}
Expand Down Expand Up @@ -1051,28 +1051,28 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
this._applyFilters();
}

private _showInfo(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _showInfo = (ev) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...then type the parameter as HaMenuItem (trying to cut down on the way too many instances of implicit any types we have).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will say that typing this is getting kind of ugly. I have to cast the parent element, then cast the anchorElement back to any as the automation property does not actually exist on any type, it's just a custom added thing here.

const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!.automation;

Is this going to be acceptable or need to clean it up somehow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine for now. The best goal would be to turn on noImplicitAny in tsconfig. Casting as any is never great, but that's a different rule for another day.

const automation = ev.currentTarget.anchorElement.automation;
fireEvent(this, "hass-more-info", { entityId: automation.entity_id });
}
};
karwosts marked this conversation as resolved.
Show resolved Hide resolved

private _showSettings(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _showSettings = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;

fireEvent(this, "hass-more-info", {
entityId: automation.entity_id,
view: "settings",
});
}
};

private _runActions(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _runActions = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;

triggerAutomationActions(this.hass, automation.entity_id);
}
};

private _editCategory(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _editCategory = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;

const entityReg = this._entityReg.find(
(reg) => reg.entity_id === automation.entity_id
Expand All @@ -1092,10 +1092,10 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
scope: "automation",
entityReg,
});
}
};

private _showTrace(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _showTrace = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;

if (!automation.attributes.id) {
showAlertDialog(this, {
Expand All @@ -1108,19 +1108,19 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
navigate(
`/config/automation/trace/${encodeURIComponent(automation.attributes.id)}`
);
}
};

private async _toggle(ev): Promise<void> {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _toggle = async (ev): Promise<void> => {
const automation = ev.currentTarget.anchorElement.automation;

const service = automation.state === "off" ? "turn_on" : "turn_off";
await this.hass.callService("automation", service, {
entity_id: automation.entity_id,
});
}
};

private async _deleteConfirm(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _deleteConfirm = async (ev) => {
const automation = ev.currentTarget.anchorElement.automation;

showConfirmationDialog(this, {
title: this.hass.localize(
Expand All @@ -1135,7 +1135,7 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
confirm: () => this._delete(automation),
destructive: true,
});
}
};

private async _delete(automation) {
try {
Expand All @@ -1155,8 +1155,8 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
}
}

private async _duplicate(ev) {
const automation = ev.currentTarget.parentElement.anchorElement.automation;
private _duplicate = async (ev) => {
const automation = ev.currentTarget.anchorElement.automation;

try {
const config = await fetchAutomationFileConfig(
Expand All @@ -1180,7 +1180,7 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
),
});
}
}
};

private _showHelp() {
showAlertDialog(this, {
Expand Down
Loading