@bunihq/app-bridge
Version:
BuniHQ App Bridge library
108 lines (105 loc) • 3.19 kB
JavaScript
import {
COMPONENT_TYPES
} from "./chunk-KQKSOMYA.mjs";
import {
EVENTS
} from "./chunk-D5JMXCZV.mjs";
import {
getOrigin
} from "./chunk-M7RMDYQK.mjs";
// src/components/sidebar.ts
var SidebarMenuElement = class extends HTMLElement {
constructor() {
super(...arguments);
this.__hostComponent = true;
this.__componentType = COMPONENT_TYPES.SIDEBAR_MENU;
}
// connected to the host -> mount
connectedCallback() {
const items = Array.from(this.children).map((child, index) => {
if (child instanceof HTMLAnchorElement) {
child.style.display = "none";
return {
id: child.id || `menu-item-${index}`,
label: child.textContent?.trim() || null,
rel: child.getAttribute("rel") || null,
href: child.getAttribute("href")
};
}
throw new Error("SidebarMenu can only contain anchor elements");
});
window.parent.postMessage(
{
type: EVENTS.HOST_COMPONENT_CONNECTED,
componentType: this.__componentType,
detail: {
type: this.__componentType,
payload: items
}
},
getOrigin()
);
}
};
var SidebarAreaElement = class extends HTMLElement {
constructor() {
super(...arguments);
this.__hostComponent = true;
this.__componentType = COMPONENT_TYPES.SIDEBAR_AREA;
}
connectedCallback() {
const items = Array.from(this.children).map((child, index) => {
if (child.tagName.toLowerCase() === "sidebar-group") {
return {
id: child.id || `group-${index}`,
label: child.getAttribute("label") || null,
children: Array.from(child.children).map((child2, index2) => {
if (child2 instanceof HTMLAnchorElement) {
child2.style.display = "none";
return {
id: child2.id || `menu-item-${index2}`,
icon: child2.getAttribute("data-icon") || null,
label: child2.textContent?.trim() || null,
href: child2.getAttribute("href"),
rel: child2.getAttribute("rel")
};
}
throw new Error("SidebarGroup can only contain anchor elements");
})
};
}
throw new Error("SidebarArea can only contain sidebar-group elements");
});
window.parent.postMessage(
{
type: EVENTS.HOST_COMPONENT_CONNECTED,
componentType: this.__componentType,
detail: {
type: this.__componentType,
payload: items
}
},
getOrigin()
);
}
};
var SidebarGroupElement = class extends HTMLElement {
constructor() {
super(...arguments);
this.__hostComponent = true;
this.__componentType = COMPONENT_TYPES.SIDEBAR_GROUP;
}
};
if (typeof window !== "undefined") {
window.customElements.define("sidebar-menu", SidebarMenuElement);
window.customElements.define("sidebar-area", SidebarAreaElement);
window.customElements.define("sidebar-group", SidebarGroupElement);
}
var SidebarMenu = "sidebar-menu";
var SidebarArea = "sidebar-area";
var SidebarGroup = "sidebar-group";
export {
SidebarMenu,
SidebarArea,
SidebarGroup
};