@bunihq/app-bridge
Version:
BuniHQ App Bridge library
150 lines (144 loc) • 4.7 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/components/sidebar.ts
var sidebar_exports = {};
__export(sidebar_exports, {
SidebarArea: () => SidebarArea,
SidebarGroup: () => SidebarGroup,
SidebarMenu: () => SidebarMenu
});
module.exports = __toCommonJS(sidebar_exports);
// src/events/index.ts
var EVENTS = {
HOST_COMPONENT_CONNECTED: "host-component-connected",
REQUEST_SESSION_TOKEN: "request-session-token"
};
// src/utils/origin.ts
function getOrigin() {
const ancestors = window.location.ancestorOrigins;
if (ancestors?.length) {
return ancestors[0];
}
if (document.referrer) {
return new URL(document.referrer).origin;
}
return window.location.origin;
}
// src/components/types.ts
var COMPONENT_TYPES = {
SIDEBAR_AREA: "sidebar-area",
SIDEBAR_GROUP: "sidebar-group",
SIDEBAR_MENU: "sidebar-menu"
};
// 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";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SidebarArea,
SidebarGroup,
SidebarMenu
});