86 lines
3.2 KiB
Vue
86 lines
3.2 KiB
Vue
<script setup lang="ts">
|
|
// One level of the context menu; entries with children open the next level beside it.
|
|
import type { MenuEntry, MenuItem } from '~/composables/useMenu'
|
|
|
|
defineProps<{ entries: MenuEntry[]; flip?: boolean }>()
|
|
const emit = defineEmits<{ done: [] }>()
|
|
|
|
const open = ref<number | null>(null)
|
|
const text = ref('')
|
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
|
|
function enter(i: number, item: MenuItem) {
|
|
clearTimeout(timer)
|
|
timer = setTimeout(() => (open.value = item.children ? i : null), 90)
|
|
item.hover?.(true)
|
|
}
|
|
|
|
function run(i: number, item: MenuItem) {
|
|
if (item.disabled) return
|
|
if (item.children) {
|
|
open.value = i
|
|
return
|
|
}
|
|
item.hover?.(false)
|
|
item.run?.()
|
|
emit('done')
|
|
}
|
|
|
|
const isItem = (e: MenuEntry): e is MenuItem => e !== 'sep' && !('head' in e)
|
|
|
|
/** A submenu that would run off the bottom of the window moves up. */
|
|
function keepOnScreen(el: Element | ComponentPublicInstance | null) {
|
|
if (!(el instanceof HTMLElement)) return
|
|
el.style.top = ''
|
|
const over = el.getBoundingClientRect().bottom - (window.innerHeight - 8)
|
|
if (over > 0) el.style.top = `${-6 - over}px`
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<ul class="ctx-list" role="menu">
|
|
<template v-for="(e, i) in entries" :key="i">
|
|
<li v-if="e === 'sep'" class="ctx-sep" role="separator" />
|
|
<li v-else-if="!isItem(e)" class="ctx-head">{{ e.head }}</li>
|
|
<li v-else class="ctx-li" :class="{ open: open === i }" @pointerenter="enter(i, e)" @pointerleave="e.hover?.(false)">
|
|
<div v-if="e.swatches" class="ctx-swatches">
|
|
<span class="ctx-label">{{ e.label }}</span>
|
|
<button
|
|
v-for="s in e.swatches"
|
|
:key="s.title"
|
|
class="ctx-dot"
|
|
:class="{ active: s.active }"
|
|
:style="{ background: s.color }"
|
|
:title="s.title"
|
|
@click="s.run(), emit('done')"
|
|
/>
|
|
</div>
|
|
<form v-else-if="e.input" class="ctx-input" @submit.prevent="e.input.submit(text.trim()), emit('done')">
|
|
<Icon v-if="e.icon" :name="e.icon" class="ctx-icon size-4" />
|
|
<input v-model="text" :placeholder="e.input.placeholder" :title="`${e.label}: type a name, then Enter`" @keydown.stop />
|
|
</form>
|
|
<button
|
|
v-else
|
|
class="ctx-item"
|
|
role="menuitem"
|
|
:class="{ danger: e.danger, sub: !!e.children }"
|
|
:disabled="e.disabled"
|
|
:aria-haspopup="e.children ? 'menu' : undefined"
|
|
@click="run(i, e)"
|
|
>
|
|
<Icon v-if="e.checked" name="lucide:check" class="ctx-icon size-4 checked" />
|
|
<Icon v-else-if="e.icon" :name="e.icon" class="ctx-icon size-4" />
|
|
<span v-else class="ctx-icon" />
|
|
<span class="ctx-label">{{ e.label }}</span>
|
|
<span v-if="e.note" class="ctx-note">{{ e.note }}</span>
|
|
<kbd v-if="e.keys" class="ctx-keys">{{ e.keys }}</kbd>
|
|
<Icon v-if="e.children" name="lucide:chevron-right" class="ctx-chevron size-3.5" />
|
|
</button>
|
|
<div v-if="e.children && open === i" :ref="keepOnScreen" class="ctx-sub" :class="{ flip }">
|
|
<EditorMenuList :entries="e.children" :flip="flip" @done="emit('done')" />
|
|
</div>
|
|
</li>
|
|
</template>
|
|
</ul>
|
|
</template>
|