Global Context Menu
Open one context menu for a whole surface, with no trigger element. A contextmenu listener sets openAtPosition, a transitionfinish listener clears it after a close, and anchor-scroll="lock" holds the page still while the menu is open.
Code
The following example assumes that you have imported the components and set up the theme.
<div class="global-context-menu-example">22 collapsed lines
<div class="context-area" tabindex="0"> <strong>Context menu area</strong> <span> Right-click or touch and hold anywhere in this rectangle, or focus it and press Shift+F10 on Windows or Linux. </span> </div>
<sv-menu class="global-context-menu" anchor-scroll="lock" aria-label="Page actions menu" mode="context" > <button type="button" class="context-option" role="menuitem">Refresh</button> <button type="button" class="context-option" role="menuitem">Paste</button> <button type="button" class="context-option" role="menuitem">Select all</button> <div class="divider" role="separator"></div> <button type="button" class="context-option" role="menuitem">Open developer tools</button> </sv-menu>
<p class="context-result" aria-live="polite">Choose an action from the context menu.</p></div>
<script type="application/x-typescript" module>23 collapsed lines
const example = document.querySelector(".global-context-menu-example")!;
const contextArea = example.querySelector<HTMLElement>(".context-area")!; const menu = example.querySelector<Menu>(".global-context-menu")!; const result = example.querySelector<HTMLElement>(".context-result")!; const options = menu.querySelectorAll<HTMLButtonElement>(".context-option");
contextArea.addEventListener("contextmenu", event => { menu.openAtPosition = { left: event.clientX, top: event.clientY }; });
menu.addEventListener("transitionfinish", () => { if (!menu.opened) { menu.openAtPosition = undefined; } });
for (const option of options) { option.addEventListener("click", () => { result.textContent = `${option.textContent} selected.`; menu.close(); }); }</script>
<style>74 collapsed lines
.global-context-menu-example { display: grid; gap: 0.75em; max-inline-size: 40rem; margin: auto;
sv-menu { --sv-flyout-offset: 0;
&::part(content) { min-block-size: 100px; padding: 0.25em; } }
.context-area { display: grid; gap: 0.5em; place-content: center; min-block-size: 18rem; padding: 1.5em; color: var(--sv-gray-2); text-align: center; touch-action: manipulation; cursor: context-menu; user-select: none; outline: none; background: linear-gradient(var(--sv-gray-7), var(--sv-gray-7)) padding-box, repeating-linear-gradient(135deg, var(--sv-gray-5) 0 1px, transparent 1px 10px) border-box; border: var(--sv-bdr-sz-sm) solid transparent; border-radius: var(--sv-bdr-rad-lg); -webkit-touch-callout: none;
&:focus-visible { outline: 2px solid var(--sv-accent); outline-offset: 2px; }
span { color: var(--sv-gray-3); } }
.context-option { padding: 0.5em; font: inherit; line-height: 1.25; color: var(--sv-gray-2); text-align: start; cursor: pointer; background: transparent; border: 0; border-radius: var(--sv-bdr-rad-sm);
@media (hover: hover) and (pointer: fine) { &:hover { background: var(--sv-gray-5); } } }
.divider { block-size: 1px; background: var(--sv-gray-5); }
.context-result { min-block-size: 1.5em; margin: 0; color: var(--sv-gray-3); text-align: center; } }</style>