Release RuneStone 0.1.7 with plugin documents and shared menus
This commit is contained in:
@@ -1,174 +1,45 @@
|
||||
<script lang="ts">
|
||||
import type { MenuAction, MenuItem } from "./menuTypes";
|
||||
import Icon from "$lib/ui/Icon.svelte";
|
||||
import { untrack } from "svelte";
|
||||
import { actionMenu, type ActionMenuItem } from "$lib/ui/actionMenu";
|
||||
import type { MenuItem } from "./menuTypes";
|
||||
|
||||
type Props = {
|
||||
x: number;
|
||||
y: number;
|
||||
items: MenuItem[];
|
||||
onClose: () => void;
|
||||
label?: string;
|
||||
};
|
||||
|
||||
let { x, y, items, onClose }: Props = $props();
|
||||
|
||||
let root: HTMLElement | undefined = $state();
|
||||
let left = $state(0);
|
||||
let top = $state(0);
|
||||
let { x, y, items, onClose, label = "Context actions" }: Props = $props();
|
||||
|
||||
$effect(() => {
|
||||
left = x;
|
||||
top = y;
|
||||
void items;
|
||||
queueMicrotask(() => {
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
const rect = root.getBoundingClientRect();
|
||||
left = Math.min(x, window.innerWidth - rect.width - 8);
|
||||
top = Math.min(y, window.innerHeight - rect.height - 8);
|
||||
});
|
||||
const menuX = x, menuY = y, entries = items, name = label;
|
||||
const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
function close(restoreFocus = false) {
|
||||
root.remove();
|
||||
if (restoreFocus) previous?.focus({ preventScroll: true });
|
||||
onClose();
|
||||
}
|
||||
function convert(entries: MenuItem[]): ActionMenuItem[] {
|
||||
return entries.map(item => {
|
||||
if (item.kind === "sep") return { separator: true };
|
||||
if (item.kind === "group") return { label: item.label, icon: item.icon, children: convert(item.items) };
|
||||
return { label: item.label, icon: item.icon, disabled: item.disabled, run: () => {
|
||||
close(true);
|
||||
item.run();
|
||||
} };
|
||||
});
|
||||
}
|
||||
const root = untrack(() => actionMenu(menuX, menuY, convert(entries), () => close(true), name));
|
||||
const outside = (event: PointerEvent) => { if (!root.contains(event.target as Node)) close(); };
|
||||
const resize = () => close();
|
||||
window.addEventListener("pointerdown", outside);
|
||||
window.addEventListener("resize", resize);
|
||||
return () => {
|
||||
root.remove();
|
||||
window.removeEventListener("pointerdown", outside);
|
||||
window.removeEventListener("resize", resize);
|
||||
};
|
||||
});
|
||||
|
||||
function onWindowDown(event: MouseEvent) {
|
||||
if (root && !root.contains(event.target as Node)) {
|
||||
onClose();
|
||||
}
|
||||
}
|
||||
|
||||
function onKey(event: KeyboardEvent) {
|
||||
if (event.key === "Escape") {
|
||||
onClose();
|
||||
}
|
||||
}
|
||||
|
||||
function runAction(item: MenuAction) {
|
||||
if (item.disabled) {
|
||||
return;
|
||||
}
|
||||
item.run();
|
||||
onClose();
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:window onmousedown={onWindowDown} onkeydown={onKey} />
|
||||
|
||||
<div
|
||||
bind:this={root}
|
||||
class="menu"
|
||||
style:left="{left}px"
|
||||
style:top="{top}px"
|
||||
role="menu"
|
||||
>
|
||||
{#each items as item, i (i)}
|
||||
{#if item.kind === "sep"}
|
||||
<div class="sep"></div>
|
||||
{:else if item.kind === "group"}
|
||||
<div class="group">
|
||||
<div class="group-label" role="none">{item.label}</div>
|
||||
<div class="flyout" role="menu">
|
||||
{#each item.items as sub, j (`${i}-${j}`)}
|
||||
{#if sub.kind === "sep"}
|
||||
<div class="sep"></div>
|
||||
{:else if sub.kind === "action"}
|
||||
<button
|
||||
type="button"
|
||||
role="menuitem"
|
||||
disabled={sub.disabled}
|
||||
onclick={() => runAction(sub)}
|
||||
>
|
||||
{#if sub.icon}<Icon icon={sub.icon} />{/if}
|
||||
{sub.label}
|
||||
</button>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<button
|
||||
type="button"
|
||||
role="menuitem"
|
||||
disabled={item.disabled}
|
||||
onclick={() => runAction(item)}
|
||||
>
|
||||
{#if item.icon}<Icon icon={item.icon} />{/if}
|
||||
{item.label}
|
||||
</button>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.menu,
|
||||
.flyout {
|
||||
min-width: 200px;
|
||||
padding: 4px 0;
|
||||
background: var(--rs-bg-elevated);
|
||||
border: 1px solid var(--rs-border);
|
||||
border-radius: var(--rs-control-radius);
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
|
||||
}
|
||||
|
||||
.menu {
|
||||
position: fixed;
|
||||
z-index: 40;
|
||||
}
|
||||
|
||||
.group {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.group-label {
|
||||
padding: 6px 12px;
|
||||
color: var(--rs-text);
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.group-label::after {
|
||||
content: "▸";
|
||||
float: right;
|
||||
color: var(--rs-text-dim);
|
||||
}
|
||||
|
||||
.flyout {
|
||||
display: none;
|
||||
position: absolute;
|
||||
left: 100%;
|
||||
top: 0;
|
||||
z-index: 41;
|
||||
}
|
||||
|
||||
.group:hover > .flyout,
|
||||
.group:focus-within > .flyout {
|
||||
display: block;
|
||||
}
|
||||
|
||||
button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
width: 100%;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: var(--rs-text-strong);
|
||||
text-align: left;
|
||||
padding: 6px 12px;
|
||||
border-radius: var(--rs-control-radius);
|
||||
}
|
||||
|
||||
button:hover,
|
||||
button:focus-visible {
|
||||
background: var(--rs-bg-active);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
color: var(--rs-text-dim);
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.sep {
|
||||
height: 1px;
|
||||
margin: 4px 0;
|
||||
background: var(--rs-border);
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user