Something went wrong. Try again.
Rewild Your Web forked from me.webbeef.org/browser.html
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155// SPDX-License-Identifier: AGPL-3.0-or-later
// <context-menu>: 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` <div class="backdrop" @click=${this.cancel}></div> <menu style="left: ${this.x}px; top: ${this.y}px;"> ${this.items.map((item) => item.header ? html`<li class="group-header">${item.label}</li>` : html` <li class="${item.disabled ? "disabled" : ""} ${item.danger ? "danger" : ""}" @click=${() => this.handleItemClick(item)} > <span class="icon-slot"> ${ item.icon ? html`<lucide-icon name="${item.icon}"></lucide-icon>` : "" } </span> <span>${item.label}</span> ${ item.checked ? html`<lucide-icon name="check"></lucide-icon>` : "" } </li> `, )} </menu> `; }}
customElements.define("context-menu", ContextMenu);