2023-01-14 19:38:39 +00:00
|
|
|
<script>
|
|
|
|
import { createEventDispatcher } from 'svelte';
|
|
|
|
|
|
|
|
export let items = undefined;
|
|
|
|
export let position = undefined;
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
2023-01-17 08:18:27 +00:00
|
|
|
const buttons = [];
|
2023-01-22 20:12:56 +00:00
|
|
|
let selected = -1;
|
|
|
|
|
|
|
|
$: if (items && position) {
|
|
|
|
selected = 0;
|
|
|
|
}
|
2023-01-14 19:38:39 +00:00
|
|
|
|
|
|
|
function close() {
|
|
|
|
dispatch('close');
|
2023-01-22 20:12:56 +00:00
|
|
|
selected = -1;
|
2023-01-14 19:38:39 +00:00
|
|
|
}
|
2023-01-14 20:09:21 +00:00
|
|
|
|
|
|
|
function click(fn) {
|
2023-01-15 15:49:08 +00:00
|
|
|
fn?.();
|
2023-01-14 20:09:21 +00:00
|
|
|
close();
|
|
|
|
}
|
2023-01-17 08:18:27 +00:00
|
|
|
|
|
|
|
function keydown(evt) {
|
|
|
|
if (evt.key === 'Escape') {
|
|
|
|
close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (!items?.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let delta = 0;
|
|
|
|
(evt.key === 'ArrowDown') && delta++;
|
|
|
|
(evt.key === 'ArrowUp') && delta--;
|
|
|
|
|
|
|
|
selected += delta;
|
|
|
|
if (selected >= items.length) {
|
|
|
|
selected = 0;
|
|
|
|
}
|
|
|
|
else if (items[selected].separator) {
|
|
|
|
selected += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
buttons[selected]?.focus?.();
|
|
|
|
}
|
2023-01-14 19:38:39 +00:00
|
|
|
</script>
|
|
|
|
|
2023-01-17 08:18:27 +00:00
|
|
|
<svelte:window on:keydown={keydown} />
|
|
|
|
|
2023-01-14 19:38:39 +00:00
|
|
|
{#if items && position}
|
|
|
|
<div class="backdrop" on:pointerdown={close}></div>
|
2023-01-17 08:18:27 +00:00
|
|
|
<nav>
|
|
|
|
<ul class="contextmenu" role="" style:left="{position[0]}px" style:top="{position[1]}px">
|
|
|
|
{#each items as item, index}
|
|
|
|
{#if item.separator}
|
|
|
|
<hr />
|
|
|
|
{:else}
|
|
|
|
<li>
|
|
|
|
<button
|
|
|
|
class="item"
|
|
|
|
class:selected={selected === index}
|
|
|
|
bind:this={buttons[index]}
|
|
|
|
on:mouseenter={() => selected = index}
|
|
|
|
on:mouseleave={() => selected = -1}
|
|
|
|
on:click={() => click(item.fn)}
|
|
|
|
>
|
|
|
|
{item.label}
|
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
{/if}
|
|
|
|
{/each}
|
|
|
|
</ul>
|
|
|
|
</nav>
|
2023-01-14 19:38:39 +00:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.backdrop {
|
|
|
|
position: fixed;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
bottom: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.contextmenu {
|
|
|
|
position: fixed;
|
|
|
|
background-color: rgba(230, 230, 230, 0.7);
|
2023-01-16 15:56:16 +00:00
|
|
|
-webkit-backdrop-filter: blur(30px);
|
2023-01-14 19:38:39 +00:00
|
|
|
backdrop-filter: blur(30px);
|
|
|
|
border-radius: 10px;
|
|
|
|
padding: 5px;
|
|
|
|
box-shadow: 0px 3px 5px rgba(0, 0, 0, 0.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
button {
|
|
|
|
padding: 5px;
|
|
|
|
border-radius: 5px;
|
2023-01-15 15:49:08 +00:00
|
|
|
width: 100%;
|
|
|
|
text-align: left;
|
2023-01-14 19:38:39 +00:00
|
|
|
}
|
2023-01-17 08:18:27 +00:00
|
|
|
button.selected {
|
2023-01-14 19:38:39 +00:00
|
|
|
background-color: #00008b;
|
|
|
|
color: #fff;
|
|
|
|
}
|
2023-01-16 19:03:56 +00:00
|
|
|
|
|
|
|
hr {
|
|
|
|
border: none;
|
|
|
|
border-top: 1px solid #aaa;
|
|
|
|
margin: 5px;
|
|
|
|
}
|
2023-01-14 19:38:39 +00:00
|
|
|
</style>
|