Skip to content

Commit dc1e29c

Browse files
committed
feat: enhance main menu with event handling for item selection and toast notifications
1 parent bbf1370 commit dc1e29c

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

app/main-menu/main-menu.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<app-header><h2>Main Menu</h2></app-header>
22

3-
<menu-container>
3+
<menu-container id="main-menu">
44
<menu-label>Menu 1</menu-label>
55
<menu-item value="1">Item 1</menu-item>
66
<menu-item value="2">Item 2</menu-item>

app/main-menu/main-menu.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import { EventsManager } from "../../src/system/events-manager.js";
12
import "./../../components/menu/menu-container/menu-container.js";
3+
import "./../../components/toast-notification/toast-notification.js";
24

35
export default class MainMenuView extends HTMLElement {
46
static tag = "main-menu-view";
57

8+
#eventsManager = new EventsManager();
9+
610
constructor() {
711
super();
812
this.attachShadow({ mode: "open" });
@@ -12,9 +16,20 @@ export default class MainMenuView extends HTMLElement {
1216
this.shadowRoot.innerHTML = await api.call("component", "load_html", {
1317
url: import.meta.url,
1418
});
19+
20+
requestAnimationFrame(() => {
21+
const menu = this.shadowRoot.querySelector("#main-menu");
22+
this.#eventsManager.addEvent(menu, "selected", this.#menuSelected.bind(this));
23+
})
24+
}
25+
26+
async disconnectedCallback() {
27+
this.#eventsManager = this.#eventsManager.dispose();
1528
}
1629

17-
load(data) {
30+
#menuSelected(event) {
31+
const { detail } = event;
32+
toastNotification.info(`Selected: ${detail}`);
1833
}
1934
}
2035

components/menu/menu-container/menu-container-input.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ function menuItemCliced(event) {
3535
const parent = element.parentElement;
3636

3737
if (element.tagName === "MENU-ITEM") {
38+
if (element.getAttribute("value") != null) {
39+
this.dispatchEvent(new CustomEvent("selected", { detail: element.getAttribute("value") }));
40+
return closeAllGroups.call(this);
41+
}
42+
3843
if (parent.tagName === "MENU-GROUP") {
3944
const isExpanded = element.getAttribute("aria-expanded") === "true";
4045
element.setAttribute("aria-expanded", !isExpanded);

0 commit comments

Comments
 (0)