2020-10-17 05:12:00 -06:00
|
|
|
<template>
|
2022-07-17 06:06:33 -06:00
|
|
|
<div>
|
|
|
|
<div
|
|
|
|
ref="itemsEl" v-hotkey="keymap"
|
|
|
|
class="rrevdjwt _popup _shadow"
|
|
|
|
:class="{ center: align === 'center', asDrawer }"
|
|
|
|
:style="{ width: (width && !asDrawer) ? width + 'px' : '', maxHeight: maxHeight ? maxHeight + 'px' : '' }"
|
|
|
|
@contextmenu.self="e => e.preventDefault()"
|
|
|
|
>
|
|
|
|
<template v-for="(item, i) in items2">
|
|
|
|
<div v-if="item === null" class="divider"></div>
|
|
|
|
<span v-else-if="item.type === 'label'" class="label item">
|
|
|
|
<span>{{ item.text }}</span>
|
|
|
|
</span>
|
|
|
|
<span v-else-if="item.type === 'pending'" :tabindex="i" class="pending item">
|
|
|
|
<span><MkEllipsis/></span>
|
|
|
|
</span>
|
|
|
|
<MkA v-else-if="item.type === 'link'" :to="item.to" :tabindex="i" class="_button item" @click.passive="close(true)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
2022-11-06 19:49:47 -07:00
|
|
|
<i v-if="item.icon" class="ph-fw ph-lg" :class="item.icon"></i>
|
2022-07-17 06:06:33 -06:00
|
|
|
<MkAvatar v-if="item.avatar" :user="item.avatar" class="avatar"/>
|
|
|
|
<span>{{ item.text }}</span>
|
2022-11-06 19:51:51 -07:00
|
|
|
<span v-if="item.indicate" class="indicator"><i class="ph-circle-fill"></i></span>
|
2022-07-17 06:06:33 -06:00
|
|
|
</MkA>
|
|
|
|
<a v-else-if="item.type === 'a'" :href="item.href" :target="item.target" :download="item.download" :tabindex="i" class="_button item" @click="close(true)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
2022-11-06 19:49:47 -07:00
|
|
|
<i v-if="item.icon" class="ph-fw ph-lg" :class="item.icon"></i>
|
2022-07-17 06:06:33 -06:00
|
|
|
<span>{{ item.text }}</span>
|
2022-11-06 19:51:51 -07:00
|
|
|
<span v-if="item.indicate" class="indicator"><i class="ph-circle-fill"></i></span>
|
2022-07-17 06:06:33 -06:00
|
|
|
</a>
|
|
|
|
<button v-else-if="item.type === 'user'" :tabindex="i" class="_button item" :class="{ active: item.active }" :disabled="item.active" @click="clicked(item.action, $event)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
|
|
|
<MkAvatar :user="item.user" class="avatar"/><MkUserName :user="item.user"/>
|
2022-11-06 19:51:51 -07:00
|
|
|
<span v-if="item.indicate" class="indicator"><i class="ph-circle-fill"></i></span>
|
2022-07-17 06:06:33 -06:00
|
|
|
</button>
|
|
|
|
<span v-else-if="item.type === 'switch'" :tabindex="i" class="item" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
|
|
|
<FormSwitch v-model="item.ref" :disabled="item.disabled" class="form-switch">{{ item.text }}</FormSwitch>
|
|
|
|
</span>
|
|
|
|
<button v-else-if="item.type === 'parent'" :tabindex="i" class="_button item parent" :class="{ childShowing: childShowingItem === item }" @mouseenter="showChildren(item, $event)">
|
2022-11-06 19:49:47 -07:00
|
|
|
<i v-if="item.icon" class="ph-fw ph-lg" :class="item.icon"></i>
|
2022-07-17 06:06:33 -06:00
|
|
|
<span>{{ item.text }}</span>
|
2022-11-06 19:49:47 -07:00
|
|
|
<span class="caret"><i class="ph-caret-right-bold ph-lg ph-fw ph-lg"></i></span>
|
2022-07-17 06:06:33 -06:00
|
|
|
</button>
|
|
|
|
<button v-else :tabindex="i" class="_button item" :class="{ danger: item.danger, active: item.active }" :disabled="item.active" @click="clicked(item.action, $event)" @mouseenter.passive="onItemMouseEnter(item)" @mouseleave.passive="onItemMouseLeave(item)">
|
2022-11-06 19:49:47 -07:00
|
|
|
<i v-if="item.icon" class="ph-fw ph-lg" :class="item.icon"></i>
|
2022-07-17 06:06:33 -06:00
|
|
|
<MkAvatar v-if="item.avatar" :user="item.avatar" class="avatar"/>
|
|
|
|
<span>{{ item.text }}</span>
|
2022-11-06 19:51:51 -07:00
|
|
|
<span v-if="item.indicate" class="indicator"><i class="ph-circle-fill"></i></span>
|
2022-07-17 06:06:33 -06:00
|
|
|
</button>
|
|
|
|
</template>
|
|
|
|
<span v-if="items2.length === 0" class="none item">
|
2022-07-20 07:24:26 -06:00
|
|
|
<span>{{ i18n.ts.none }}</span>
|
2020-10-17 05:12:00 -06:00
|
|
|
</span>
|
2022-07-17 06:06:33 -06:00
|
|
|
</div>
|
|
|
|
<div v-if="childMenu" class="child">
|
2022-07-17 08:18:05 -06:00
|
|
|
<XChild ref="child" :items="childMenu" :target-element="childTarget" :root-element="itemsEl" showing @actioned="childActioned"/>
|
2022-07-17 06:06:33 -06:00
|
|
|
</div>
|
2020-10-17 05:12:00 -06:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-29 22:11:52 -07:00
|
|
|
<script lang="ts" setup>
|
2022-11-06 20:42:01 -07:00
|
|
|
import { computed, menu, defineAsyncComponent, nextTick, onBeforeUnmount, onMounted, onUnmounted, Ref, ref, watch } from 'vue';
|
2021-11-11 10:02:25 -07:00
|
|
|
import { focusPrev, focusNext } from '@/scripts/focus';
|
2022-01-29 22:11:52 -07:00
|
|
|
import FormSwitch from '@/components/form/switch.vue';
|
|
|
|
import { MenuItem, InnerMenuItem, MenuPending, MenuAction } from '@/types/menu';
|
2022-07-17 06:06:33 -06:00
|
|
|
import * as os from '@/os';
|
2022-07-20 07:24:26 -06:00
|
|
|
import { i18n } from '@/i18n';
|
|
|
|
|
2022-09-06 03:21:49 -06:00
|
|
|
const XChild = defineAsyncComponent(() => import('./MkMenu.child.vue'));
|
2022-01-29 22:11:52 -07:00
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
items: MenuItem[];
|
|
|
|
viaKeyboard?: boolean;
|
|
|
|
asDrawer?: boolean;
|
|
|
|
align?: 'center' | string;
|
|
|
|
width?: number;
|
|
|
|
maxHeight?: number;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2022-07-17 06:06:33 -06:00
|
|
|
(ev: 'close', actioned?: boolean): void;
|
2022-01-29 22:11:52 -07:00
|
|
|
}>();
|
|
|
|
|
|
|
|
let itemsEl = $ref<HTMLDivElement>();
|
|
|
|
|
|
|
|
let items2: InnerMenuItem[] = $ref([]);
|
|
|
|
|
2022-07-17 06:06:33 -06:00
|
|
|
let child = $ref<InstanceType<typeof XChild>>();
|
|
|
|
|
2022-11-06 20:42:01 -07:00
|
|
|
let keymap = computed(() => ({
|
2022-01-29 22:11:52 -07:00
|
|
|
'up|k|shift+tab': focusUp,
|
|
|
|
'down|j|tab': focusDown,
|
|
|
|
'esc': close,
|
|
|
|
}));
|
|
|
|
|
2022-07-17 06:06:33 -06:00
|
|
|
let childShowingItem = $ref<MenuItem | null>();
|
|
|
|
|
2022-01-29 22:11:52 -07:00
|
|
|
watch(() => props.items, () => {
|
|
|
|
const items: (MenuItem | MenuPending)[] = [...props.items].filter(item => item !== undefined);
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
const item = items[i];
|
|
|
|
|
|
|
|
if (item && 'then' in item) { // if item is Promise
|
|
|
|
items[i] = { type: 'pending' };
|
|
|
|
item.then(actualItem => {
|
|
|
|
items2[i] = actualItem;
|
2020-10-17 05:12:00 -06:00
|
|
|
});
|
|
|
|
}
|
2022-01-29 22:11:52 -07:00
|
|
|
}
|
2020-10-17 05:12:00 -06:00
|
|
|
|
2022-01-29 22:11:52 -07:00
|
|
|
items2 = items as InnerMenuItem[];
|
|
|
|
}, {
|
|
|
|
immediate: true,
|
|
|
|
});
|
2020-10-17 05:12:00 -06:00
|
|
|
|
2022-07-17 06:06:33 -06:00
|
|
|
let childMenu = $ref<MenuItem[] | null>();
|
|
|
|
let childTarget = $ref<HTMLElement | null>();
|
|
|
|
|
|
|
|
function closeChild() {
|
|
|
|
childMenu = null;
|
|
|
|
childShowingItem = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function childActioned() {
|
|
|
|
closeChild();
|
|
|
|
close(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onGlobalMousedown(event: MouseEvent) {
|
|
|
|
if (childTarget && (event.target === childTarget || childTarget.contains(event.target))) return;
|
|
|
|
if (child && child.checkHit(event)) return;
|
|
|
|
closeChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
let childCloseTimer: null | number = null;
|
|
|
|
function onItemMouseEnter(item) {
|
|
|
|
childCloseTimer = window.setTimeout(() => {
|
|
|
|
closeChild();
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
function onItemMouseLeave(item) {
|
|
|
|
if (childCloseTimer) window.clearTimeout(childCloseTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function showChildren(item: MenuItem, ev: MouseEvent) {
|
|
|
|
if (props.asDrawer) {
|
|
|
|
os.popupMenu(item.children, ev.currentTarget ?? ev.target);
|
|
|
|
close();
|
|
|
|
} else {
|
|
|
|
childTarget = ev.currentTarget ?? ev.target;
|
|
|
|
childMenu = item.children;
|
|
|
|
childShowingItem = item;
|
2020-10-17 05:12:00 -06:00
|
|
|
}
|
2022-07-17 06:06:33 -06:00
|
|
|
}
|
2022-01-29 22:11:52 -07:00
|
|
|
|
|
|
|
function clicked(fn: MenuAction, ev: MouseEvent) {
|
|
|
|
fn(ev);
|
2022-07-17 06:06:33 -06:00
|
|
|
close(true);
|
2022-01-29 22:11:52 -07:00
|
|
|
}
|
|
|
|
|
2022-07-17 06:06:33 -06:00
|
|
|
function close(actioned = false) {
|
|
|
|
emit('close', actioned);
|
2022-01-29 22:11:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function focusUp() {
|
|
|
|
focusPrev(document.activeElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
function focusDown() {
|
|
|
|
focusNext(document.activeElement);
|
|
|
|
}
|
2022-07-17 06:06:33 -06:00
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
if (props.viaKeyboard) {
|
|
|
|
nextTick(() => {
|
|
|
|
focusNext(itemsEl.children[0], true, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('mousedown', onGlobalMousedown, { passive: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
document.removeEventListener('mousedown', onGlobalMousedown);
|
|
|
|
});
|
2020-10-17 05:12:00 -06:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.rrevdjwt {
|
|
|
|
padding: 8px 0;
|
2021-11-28 04:07:37 -07:00
|
|
|
box-sizing: border-box;
|
2021-10-03 00:39:39 -06:00
|
|
|
min-width: 200px;
|
2021-11-04 19:48:37 -06:00
|
|
|
overflow: auto;
|
2021-12-02 20:00:11 -07:00
|
|
|
overscroll-behavior: contain;
|
2020-10-17 05:12:00 -06:00
|
|
|
|
2021-10-03 00:39:39 -06:00
|
|
|
&.center {
|
2020-10-17 05:12:00 -06:00
|
|
|
> .item {
|
2021-10-03 00:39:39 -06:00
|
|
|
text-align: center;
|
2020-10-17 05:12:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .item {
|
|
|
|
display: block;
|
|
|
|
position: relative;
|
2022-07-06 03:20:32 -06:00
|
|
|
padding: 6px 16px;
|
2020-10-17 05:12:00 -06:00
|
|
|
width: 100%;
|
|
|
|
box-sizing: border-box;
|
|
|
|
white-space: nowrap;
|
2022-07-15 02:01:13 -06:00
|
|
|
font-size: 0.9em;
|
2020-10-17 05:12:00 -06:00
|
|
|
line-height: 20px;
|
2021-10-03 00:39:39 -06:00
|
|
|
text-align: left;
|
2021-03-02 06:57:16 -07:00
|
|
|
overflow: hidden;
|
2020-10-17 05:12:00 -06:00
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
2021-10-03 00:39:39 -06:00
|
|
|
&:before {
|
|
|
|
content: "";
|
|
|
|
display: block;
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
margin: auto;
|
2021-10-15 08:33:27 -06:00
|
|
|
width: calc(100% - 16px);
|
2022-07-24 01:00:24 -06:00
|
|
|
margin-bottom: 0.2rem;
|
2021-10-03 00:39:39 -06:00
|
|
|
height: 100%;
|
2021-10-15 08:33:27 -06:00
|
|
|
border-radius: 6px;
|
2021-10-03 00:39:39 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 22:12:41 -06:00
|
|
|
> * {
|
|
|
|
position: relative;
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:42:01 -07:00
|
|
|
> &.icon {
|
|
|
|
transform: translateY(0em);
|
|
|
|
}
|
|
|
|
|
2022-06-04 21:23:57 -06:00
|
|
|
&:not(:disabled):hover {
|
|
|
|
color: var(--accent);
|
|
|
|
text-decoration: none;
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
background: var(--accentedBg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 05:12:00 -06:00
|
|
|
&.danger {
|
2022-07-24 00:46:13 -06:00
|
|
|
color: #eb6f92;
|
2020-10-17 05:12:00 -06:00
|
|
|
|
|
|
|
&:hover {
|
2022-07-24 00:46:13 -06:00
|
|
|
color: #e0def4;
|
2021-10-03 00:39:39 -06:00
|
|
|
|
|
|
|
&:before {
|
2022-07-24 00:46:13 -06:00
|
|
|
background: #eb6f92;
|
2021-10-03 00:39:39 -06:00
|
|
|
}
|
2020-10-17 05:12:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
&:active {
|
2022-07-24 00:46:13 -06:00
|
|
|
color: #e0def4;
|
2021-10-03 00:39:39 -06:00
|
|
|
|
|
|
|
&:before {
|
2022-07-24 00:46:13 -06:00
|
|
|
background: #b4637a;
|
2021-10-03 00:39:39 -06:00
|
|
|
}
|
2020-10-17 05:12:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 22:12:41 -06:00
|
|
|
&.active {
|
|
|
|
color: var(--fgOnAccent);
|
|
|
|
opacity: 1;
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
background: var(--accent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 11:46:58 -06:00
|
|
|
&:not(:active):focus-visible {
|
2020-10-17 05:12:00 -06:00
|
|
|
box-shadow: 0 0 0 2px var(--focus) inset;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.label {
|
|
|
|
pointer-events: none;
|
|
|
|
font-size: 0.7em;
|
|
|
|
padding-bottom: 4px;
|
|
|
|
|
|
|
|
> span {
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.pending {
|
|
|
|
pointer-events: none;
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.none {
|
|
|
|
pointer-events: none;
|
|
|
|
opacity: 0.7;
|
|
|
|
}
|
|
|
|
|
2022-07-17 06:06:33 -06:00
|
|
|
&.parent {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
cursor: default;
|
|
|
|
|
|
|
|
> .caret {
|
|
|
|
margin-left: auto;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.childShowing {
|
|
|
|
color: var(--accent);
|
|
|
|
text-decoration: none;
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
background: var(--accentedBg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:22:59 -06:00
|
|
|
> i {
|
2021-10-15 08:35:28 -06:00
|
|
|
margin-right: 5px;
|
2020-10-17 05:12:00 -06:00
|
|
|
width: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .avatar {
|
2021-10-15 08:35:28 -06:00
|
|
|
margin-right: 5px;
|
2020-10-17 05:12:00 -06:00
|
|
|
width: 20px;
|
|
|
|
height: 20px;
|
|
|
|
}
|
|
|
|
|
2021-04-20 08:22:59 -06:00
|
|
|
> .indicator {
|
2020-10-17 05:12:00 -06:00
|
|
|
position: absolute;
|
|
|
|
top: 5px;
|
|
|
|
left: 13px;
|
|
|
|
color: var(--indicator);
|
|
|
|
font-size: 12px;
|
|
|
|
animation: blink 1s infinite;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .divider {
|
|
|
|
margin: 8px 0;
|
2021-12-17 23:00:29 -07:00
|
|
|
border-top: solid 0.5px var(--divider);
|
2020-10-17 05:12:00 -06:00
|
|
|
}
|
2021-12-16 10:14:40 -07:00
|
|
|
|
|
|
|
&.asDrawer {
|
2021-12-24 07:25:23 -07:00
|
|
|
padding: 12px 0 calc(env(safe-area-inset-bottom, 0px) + 12px) 0;
|
2021-12-16 10:14:40 -07:00
|
|
|
width: 100%;
|
2022-07-21 07:22:13 -06:00
|
|
|
border-radius: 24px;
|
|
|
|
border-bottom-right-radius: 0;
|
|
|
|
border-bottom-left-radius: 0;
|
2021-12-16 10:14:40 -07:00
|
|
|
|
|
|
|
> .item {
|
|
|
|
font-size: 1em;
|
|
|
|
padding: 12px 24px;
|
|
|
|
|
|
|
|
&:before {
|
|
|
|
width: calc(100% - 24px);
|
|
|
|
border-radius: 12px;
|
|
|
|
}
|
|
|
|
|
|
|
|
> i {
|
|
|
|
margin-right: 14px;
|
|
|
|
width: 24px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .divider {
|
|
|
|
margin: 12px 0;
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 05:12:00 -06:00
|
|
|
}
|
|
|
|
</style>
|