rspace-online/shared/components/rstack-app-switcher.ts

180 lines
5.5 KiB
TypeScript

/**
* <rstack-app-switcher> — Dropdown to switch between rSpace modules.
*
* Attributes:
* current — the active module ID (highlighted)
*
* Methods:
* setModules(list) — provide the list of available modules
*/
export interface AppSwitcherModule {
id: string;
name: string;
icon: string;
description: string;
standaloneDomain?: string;
}
export class RStackAppSwitcher extends HTMLElement {
#shadow: ShadowRoot;
#modules: AppSwitcherModule[] = [];
constructor() {
super();
this.#shadow = this.attachShadow({ mode: "open" });
}
static get observedAttributes() {
return ["current"];
}
get current(): string {
return this.getAttribute("current") || "";
}
connectedCallback() {
this.#render();
}
attributeChangedCallback() {
this.#render();
}
setModules(modules: AppSwitcherModule[]) {
this.#modules = modules;
this.#render();
}
#render() {
const current = this.current;
const currentMod = this.#modules.find((m) => m.id === current);
const label = currentMod ? `${currentMod.icon} ${currentMod.name}` : "🌌 rSpace";
this.#shadow.innerHTML = `
<style>${STYLES}</style>
<div class="switcher">
<button class="trigger" id="trigger">${label} <span class="caret">▾</span></button>
<div class="menu" id="menu">
${this.#modules
.map(
(m) => `
<div class="item-row ${m.id === current ? "active" : ""}">
<a class="item"
href="/${this.#getSpaceSlug()}/${m.id}"
data-id="${m.id}">
<span class="item-icon">${m.icon}</span>
<div class="item-text">
<span class="item-name">${m.name}</span>
<span class="item-desc">${m.description}</span>
</div>
</a>
${m.standaloneDomain ? `<a class="item-ext" href="https://${m.standaloneDomain}" target="_blank" rel="noopener" title="${m.standaloneDomain}">↗</a>` : ""}
</div>
`
)
.join("")}
</div>
</div>
`;
const trigger = this.#shadow.getElementById("trigger")!;
const menu = this.#shadow.getElementById("menu")!;
trigger.addEventListener("click", (e) => {
e.stopPropagation();
menu.classList.toggle("open");
});
// Prevent external links from closing the menu prematurely
this.#shadow.querySelectorAll(".item-ext").forEach((el) => {
el.addEventListener("click", (e) => e.stopPropagation());
});
document.addEventListener("click", () => menu.classList.remove("open"));
}
#getSpaceSlug(): string {
// Read from the space switcher or URL
const spaceSwitcher = document.querySelector("rstack-space-switcher");
if (spaceSwitcher) return spaceSwitcher.getAttribute("current") || "personal";
// Fallback: parse from URL (/:space/:module)
const parts = window.location.pathname.split("/").filter(Boolean);
return parts[0] || "personal";
}
static define(tag = "rstack-app-switcher") {
if (!customElements.get(tag)) customElements.define(tag, RStackAppSwitcher);
}
}
const STYLES = `
:host { display: contents; }
.switcher { position: relative; }
.trigger {
display: flex; align-items: center; gap: 6px;
padding: 6px 14px; border-radius: 8px; border: none;
font-size: 0.9rem; font-weight: 600; cursor: pointer;
transition: background 0.15s;
background: rgba(255,255,255,0.08); color: inherit;
}
:host-context([data-theme="light"]) .trigger {
background: rgba(0,0,0,0.05); color: #0f172a;
}
:host-context([data-theme="dark"]) .trigger {
background: rgba(255,255,255,0.08); color: #e2e8f0;
}
.trigger:hover { background: rgba(255,255,255,0.12); }
:host-context([data-theme="light"]) .trigger:hover { background: rgba(0,0,0,0.08); }
.caret { font-size: 0.7em; opacity: 0.6; }
.menu {
position: absolute; top: 100%; left: 0; margin-top: 6px;
min-width: 260px; border-radius: 12px; overflow: hidden;
box-shadow: 0 8px 30px rgba(0,0,0,0.25); display: none; z-index: 200;
}
.menu.open { display: block; }
:host-context([data-theme="light"]) .menu {
background: white; border: 1px solid rgba(0,0,0,0.1);
}
:host-context([data-theme="dark"]) .menu {
background: #1e293b; border: 1px solid rgba(255,255,255,0.1);
}
.item-row {
display: flex; align-items: center;
transition: background 0.12s;
}
:host-context([data-theme="light"]) .item-row { color: #374151; }
:host-context([data-theme="light"]) .item-row:hover { background: #f1f5f9; }
:host-context([data-theme="light"]) .item-row.active { background: #e0f2fe; }
:host-context([data-theme="dark"]) .item-row { color: #e2e8f0; }
:host-context([data-theme="dark"]) .item-row:hover { background: rgba(255,255,255,0.05); }
:host-context([data-theme="dark"]) .item-row.active { background: rgba(6,182,212,0.1); }
.item {
display: flex; align-items: center; gap: 12px;
padding: 10px 14px; text-decoration: none;
cursor: pointer; flex: 1; min-width: 0; color: inherit;
}
.item-ext {
display: flex; align-items: center; justify-content: center;
width: 32px; height: 100%; flex-shrink: 0;
font-size: 0.8rem; text-decoration: none; opacity: 0;
transition: opacity 0.15s;
}
.item-row:hover .item-ext { opacity: 0.5; }
.item-ext:hover { opacity: 1 !important; }
:host-context([data-theme="light"]) .item-ext { color: #06b6d4; }
:host-context([data-theme="dark"]) .item-ext { color: #22d3ee; }
.item-icon { font-size: 1.3rem; width: 28px; text-align: center; flex-shrink: 0; }
.item-text { display: flex; flex-direction: column; min-width: 0; }
.item-name { font-size: 0.875rem; font-weight: 600; }
.item-desc { font-size: 0.75rem; opacity: 0.6; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
`;