// SPDX-License-Identifier: AGPL-3.0-or-later // : a point-anchored menu, shared by the system shell (engine // context menus) and app pages (the vault's). // // Position: `x`/`y` are the pointer's viewport coordinates. On a fine // pointer the menu anchors there, clamped fully into the viewport once its // size is known. On a coarse (touch) pointer the CSS centers it instead: a // finger can be anywhere, so the menu is an action sheet. // // Consumers set `items` ([{ id, label, icon?, disabled?, checked?, // danger? }]), `x`, `y`, and `open`. `menu-action` fires with // { action, controlId, target } on pick, `menu-cancel` on dismissal, // `menu-closed` on either (MenuBase). import { MenuBase, css, html } from "./menu_base.js"; import { placeInViewport } from "./viewport.js"; export class ContextMenu extends MenuBase { static properties = { ...MenuBase.properties, items: { type: Array }, x: { type: Number }, y: { type: Number }, controlId: { type: String }, target: { attribute: false }, }; constructor() { super(); this.items = []; this.x = 0; this.y = 0; this.controlId = ""; this.target = null; this.handleKeyDown = this.handleKeyDown.bind(this); this.handleScroll = () => this.close(); this._sizeObserver = new ResizeObserver(() => this._clamp()); } disconnectedCallback() { super.disconnectedCallback(); this.removeEventListeners(); this._sizeObserver.disconnect(); } updated(changedProperties) { if (changedProperties.has("open")) { if (this.open) { document.addEventListener("keydown", this.handleKeyDown); // The menu is viewport-fixed but its target may move: close. document.addEventListener("scroll", this.handleScroll, true); this._sizeObserver.observe(this.renderRoot.querySelector("menu")); } else { this.removeEventListeners(); this._sizeObserver.disconnect(); } } if (this.open) { this._clamp(); } } removeEventListeners() { document.removeEventListener("keydown", this.handleKeyDown); document.removeEventListener("scroll", this.handleScroll, true); } /** * Keep every pixel onscreen, repositioning on every size change (the menu * grows after opening: async icon font, late items). On a coarse-pointer * device the CSS centering overrides this with `!important`, so clamping * unconditionally is safe there too. */ _clamp() { const menu = this.renderRoot.querySelector("menu"); if (menu) { placeInViewport(menu, this.x, this.y); } } handleKeyDown(e) { if (e.key === "Escape") { this.cancel(); } } cancel() { this.dispatchEvent( new CustomEvent("menu-cancel", { bubbles: true, composed: true, detail: { controlId: this.controlId }, }), ); this.close(); } handleItemClick(item) { if (item.disabled) { return; } this.dispatchEvent( new CustomEvent("menu-action", { bubbles: true, composed: true, detail: { action: item.id, controlId: this.controlId, target: this.target, }, }), ); this.close(); } static styles = css` @import url(beaver://shared/context_menu.css); `; render() { return html`
${this.items.map((item) => item.header ? html`
  • ${item.label}
  • ` : html`
  • this.handleItemClick(item)} > ${ item.icon ? html`` : "" } ${item.label} ${ item.checked ? html`` : "" }
  • `, )}
    `; } } customElements.define("context-menu", ContextMenu);