Skip to content

Open source personal finance application

fix(ui): UiContextMenuMy wrapper with working pressOpenDelay and cross-menu close

Ilya Komichev31ab9371dcad
diff --git a/app/app/app.config.ts b/app/app/app.config.ts
index 03ff4663..0b9d4b14 100644
--- a/app/app/app.config.ts
+++ b/app/app/app.config.ts
@@ -29,11 +29,6 @@ export default defineAppConfig({
viewport: 'scrollerBlock',
},
},
- contextMenu: {
- props: {
- pressOpenDelay: 400,
- },
- },
modal: {
slots: {
overlay: '!bg-overlay',
diff --git a/app/app/components/categories/Item.vue b/app/app/components/categories/Item.vue
index 79172464..e7b442ca 100644
--- a/app/app/components/categories/Item.vue
+++ b/app/app/components/categories/Item.vue
@@ -29,7 +29,7 @@ const emit = defineEmits<{
</script>
<template>
- <UContextMenu v-if="props.contextMenuItems" :items="props.contextMenuItems">
+ <UiContextMenuMy v-if="props.contextMenuItems" :items="props.contextMenuItems">
<CategoriesItemBody
:activeItemId="props.activeItemId"
:stacked="props.stacked"
@@ -49,7 +49,7 @@ const emit = defineEmits<{
@filter="emit('filter', $event)"
@toggle="emit('toggle')"
/>
- </UContextMenu>
+ </UiContextMenuMy>
<CategoriesItemBody
v-else
diff --git a/app/app/components/categories/RoundLink.vue b/app/app/components/categories/RoundLink.vue
index ac9dcf73..9e077168 100644
--- a/app/app/components/categories/RoundLink.vue
+++ b/app/app/components/categories/RoundLink.vue
@@ -15,7 +15,7 @@ const emit = defineEmits<{
</script>
<template>
- <UContextMenu v-if="props.contextMenuItems" :items="props.contextMenuItems">
+ <UiContextMenuMy v-if="props.contextMenuItems" :items="props.contextMenuItems">
<NuxtLink
:to="props.to"
class="text-inherit no-underline"
@@ -23,7 +23,7 @@ const emit = defineEmits<{
>
<CategoriesRoundPill :categoryId="props.categoryId" :isActive="props.isActive" />
</NuxtLink>
- </UContextMenu>
+ </UiContextMenuMy>
<NuxtLink
v-else
diff --git a/app/app/components/currencies/Item.vue b/app/app/components/currencies/Item.vue
index bb2aadfa..b9686d0c 100644
--- a/app/app/components/currencies/Item.vue
+++ b/app/app/components/currencies/Item.vue
@@ -24,7 +24,7 @@ const contextMenuItems = computed(() => [[{
</script>
<template>
- <UContextMenu :items="contextMenuItems">
+ <UiContextMenuMy :items="contextMenuItems">
<UiElement
:lineWidth="props.isBase ? 6 : 0"
class="group"
@@ -56,5 +56,5 @@ const contextMenuItems = computed(() => [[{
</div>
</div>
</UiElement>
- </UContextMenu>
+ </UiContextMenuMy>
</template>
diff --git a/app/app/components/trnForm/Sidebar.vue b/app/app/components/trnForm/Sidebar.vue
index a48e900d..27424558 100644
--- a/app/app/components/trnForm/Sidebar.vue
+++ b/app/app/components/trnForm/Sidebar.vue
@@ -4,6 +4,7 @@ import { Pagination } from 'swiper/modules'
import { useTrnsFormStore } from '~/components/trnForm/useTrnsFormStore'
import { TrnType } from '~/components/trns/types'
+import { closeAllContextMenus } from '~/composables/useContextMenuRegistry'
import 'swiper/css'
@@ -34,6 +35,7 @@ function init() {
watch(isShow, async (v) => {
if (v) {
+ closeAllContextMenus()
await nextTick()
init()
}
diff --git a/app/app/components/trns/itemWrap.vue b/app/app/components/trns/itemWrap.vue
index f54895f5..66a329ed 100644
--- a/app/app/components/trns/itemWrap.vue
+++ b/app/app/components/trns/itemWrap.vue
@@ -55,14 +55,14 @@ function handleDeleteConfirm() {
</script>
<template>
- <UContextMenu v-bind="$attrs" :items="contextMenuItems">
+ <UiContextMenuMy v-bind="$attrs" :items="contextMenuItems">
<TrnsItem
:compact="props.compact"
:trnItem
:date
@click="click"
/>
- </UContextMenu>
+ </UiContextMenuMy>
<LayoutConfirmModal
v-if="showDeleteConfirm"
diff --git a/app/app/components/ui/ContextMenuMy.vue b/app/app/components/ui/ContextMenuMy.vue
new file mode 100644
index 00000000..3ddbd1ee
--- /dev/null
+++ b/app/app/components/ui/ContextMenuMy.vue
@@ -0,0 +1,75 @@
+<script setup lang="ts">
+import { tv } from '@nuxt/ui/utils/tv'
+import { ContextMenuRoot, ContextMenuTrigger } from 'reka-ui'
+
+import type { ContextMenuItem } from '#ui/components/ContextMenu.vue'
+
+import theme from '#build/ui/context-menu'
+import { useAppConfig } from '#imports'
+import { closeOtherContextMenus, registerContextMenu } from '~/composables/useContextMenuRegistry'
+
+// Workaround: @nuxt/ui 4.6+ declares pressOpenDelay but never forwards it to reka-ui ContextMenuRoot.
+const props = withDefaults(defineProps<{
+ descriptionKey?: string
+ disabled?: boolean
+ items?: ContextMenuItem[][]
+ labelKey?: string
+ modal?: boolean
+ portal?: boolean | string
+ pressOpenDelay?: number
+ size?: 'lg' | 'md' | 'sm' | 'xl' | 'xs'
+}>(), {
+ descriptionKey: 'description',
+ labelKey: 'label',
+ modal: true,
+ portal: true,
+ pressOpenDelay: 400,
+})
+
+const emit = defineEmits<{
+ 'update:open': [open: boolean]
+}>()
+
+const exposer = ref<{ close: () => void } | null>(null)
+
+function close() {
+ exposer.value?.close()
+}
+
+onMounted(() => {
+ const unregister = registerContextMenu(close)
+ onBeforeUnmount(unregister)
+})
+
+function onOpen(v: boolean) {
+ emit('update:open', v)
+ if (v)
+ closeOtherContextMenus(close)
+}
+
+const appConfig = useAppConfig()
+const ui = computed(() => tv({
+ extend: tv(theme as any),
+ ...(appConfig as any).ui?.contextMenu || {},
+})({ size: props.size }))
+</script>
+
+<template>
+ <ContextMenuRoot
+ :modal="props.modal"
+ :pressOpenDelay="props.pressOpenDelay"
+ @update:open="onOpen"
+ >
+ <UiContextMenuStateExposer ref="exposer" />
+ <ContextMenuTrigger asChild :disabled="props.disabled">
+ <slot />
+ </ContextMenuTrigger>
+ <UContextMenuContent
+ :items="props.items"
+ :portal="props.portal"
+ :labelKey="props.labelKey"
+ :descriptionKey="props.descriptionKey"
+ :ui="ui"
+ />
+ </ContextMenuRoot>
+</template>
diff --git a/app/app/components/ui/ContextMenuOptional.vue b/app/app/components/ui/ContextMenuOptional.vue
index fd7eb505..9faa91c4 100644
--- a/app/app/components/ui/ContextMenuOptional.vue
+++ b/app/app/components/ui/ContextMenuOptional.vue
@@ -7,8 +7,8 @@ const props = defineProps<{
</script>
<template>
- <UContextMenu v-if="props.items" :items="props.items">
+ <UiContextMenuMy v-if="props.items" :items="props.items">
<slot />
- </UContextMenu>
+ </UiContextMenuMy>
<slot v-else />
</template>
diff --git a/app/app/components/ui/ContextMenuStateExposer.vue b/app/app/components/ui/ContextMenuStateExposer.vue
new file mode 100644
index 00000000..3fa158ae
--- /dev/null
+++ b/app/app/components/ui/ContextMenuStateExposer.vue
@@ -0,0 +1,13 @@
+<script setup lang="ts">
+import { injectContextMenuRootContext } from 'reka-ui'
+
+const ctx = injectContextMenuRootContext()
+
+defineExpose({
+ close: () => ctx.onOpenChange(false),
+})
+</script>
+
+<template>
+ <div hidden />
+</template>
diff --git a/app/app/components/wallets/Item.vue b/app/app/components/wallets/Item.vue
index cc348cab..d700e22f 100644
--- a/app/app/components/wallets/Item.vue
+++ b/app/app/components/wallets/Item.vue
@@ -24,7 +24,7 @@ const emit = defineEmits<{
</script>
<template>
- <UContextMenu v-if="props.contextMenuItems" :items="props.contextMenuItems">
+ <UiContextMenuMy v-if="props.contextMenuItems" :items="props.contextMenuItems">
<WalletsItemBody
:activeItemId="props.activeItemId"
:compact="props.compact"
@@ -40,7 +40,7 @@ const emit = defineEmits<{
:walletId="props.walletId"
@click="emit('click', $event)"
/>
- </UContextMenu>
+ </UiContextMenuMy>
<WalletsItemBody
v-else
diff --git a/app/app/composables/useContextMenuRegistry.ts b/app/app/composables/useContextMenuRegistry.ts
new file mode 100644
index 00000000..df1092d9
--- /dev/null
+++ b/app/app/composables/useContextMenuRegistry.ts
@@ -0,0 +1,20 @@
+// Shared registry of open context-menu close handlers.
+// Lets UiContextMenuMy auto-dismiss siblings and lets overlays
+// (trnForm Sidebar etc.) force-close any open menu.
+const closers = new Set<() => void>()
+
+export function registerContextMenu(close: () => void): () => void {
+ closers.add(close)
+ return () => closers.delete(close)
+}
+
+export function closeOtherContextMenus(self: () => void): void {
+ for (const close of closers) {
+ if (close !== self)
+ close()
+ }
+}
+
+export function closeAllContextMenus(): void {
+ for (const close of closers) close()
+}