Skip to content

A self-hosted media server and file management system

fix: context submenu has no overflow handling

aminnausin5a0e0b7c74f1
diff --git a/resources/js/components/cedar-ui/context-menu/ContextMenuItem.vue b/resources/js/components/cedar-ui/context-menu/ContextMenuItem.vue
index dc08db49..e892e176 100644
--- a/resources/js/components/cedar-ui/context-menu/ContextMenuItem.vue
+++ b/resources/js/components/cedar-ui/context-menu/ContextMenuItem.vue
@@ -1,8 +1,9 @@
<script setup lang="ts">
import type { ContextMenuItem } from '@aminnausin/cedar-ui';
+import { computed, ref, useTemplateRef } from 'vue';
+import { useMutationObserver } from '@vueuse/core';
import { ButtonBase } from '@/components/cedar-ui/button';
-import { computed } from 'vue';
import { cn } from '@aminnausin/cedar-ui';
import ProiconsChevronRight from '~icons/proicons/chevron-right';
@@ -11,13 +12,20 @@ const props = withDefaults(defineProps<ContextMenuItem & { divider?: boolean; ch
selectedStyle: 'text-primary font-bold',
});
+const isSubMenuOpen = ref(false);
+const isFloating = ref(true);
+
+const subMenu = useTemplateRef('sub-menu');
+
+const hasChildren = computed(() => (props.children?.length ?? 0) > 0);
+
const wrapperProps = computed(() => {
if (!props.url) return {};
if (props.external) return { href: props.url, target: props.target ?? '_blank' };
return { to: props.url, target: props.target };
});
-const hasChildren = computed(() => (props.children?.length ?? 0) > 0);
+useMutationObserver(subMenu, () => (isFloating.value = subMenu.value?.dataset.floating !== 'false'), { attributeFilter: ['data-floating'] });
</script>
<template>
<div v-if="divider" class="bg-hr dark:bg-hr/30 -mx-1 my-1 h-px" />
@@ -25,9 +33,9 @@ const hasChildren = computed(() => (props.children?.length ?? 0) > 0);
<div v-else class="relative" :class="{ 'group/submenu': hasChildren }">
<ButtonBase
v-bind="wrapperProps"
- :class="cn({ [selectedStyle]: selected }, 'hocus:bg-overlay-accent h-7 w-full justify-start rounded-sm px-2 py-1.5 select-none', style)"
+ :class="cn({ [selectedStyle]: selected }, 'hocus:bg-overlay-accent h-7 w-full justify-start rounded-sm px-2 py-1.5 select-none focus:outline-none', style)"
:disabled="disabled"
- @click.stop="if (!hasChildren) action?.();"
+ @click.stop="hasChildren ? (isSubMenuOpen = !isSubMenuOpen) : action?.()"
>
<slot name="icon">
<component v-if="icon" :is="icon" class="size-4 shrink-0" />
@@ -40,30 +48,26 @@ const hasChildren = computed(() => (props.children?.length ?? 0) > 0);
<div
v-if="hasChildren"
+ ref="sub-menu"
data-submenu
data-floating="true"
:class="
cn(
- 'floating-menu peer max-h-0 px-2',
- 'opacity-0',
- 'group-hover/submenu:max-h-none group-hover/submenu:opacity-100',
- 'group-focus-within/submenu:pointer-events-auto group-focus-within/submenu:opacity-100',
+ 'opacity-0 group-focus-within/submenu:opacity-100 group-hover/submenu:opacity-100',
+ 'pointer-events-none group-focus-within/submenu:pointer-events-auto group-hover/submenu:pointer-events-auto',
+ 'transition-opacity duration-300 ease-in group-hover/submenu:ease-out',
+ {
+ 'bg-overlay-2-t border-overlay-border/10 absolute top-0 max-h-none w-48 rounded-md border p-1 shadow-xs backdrop-blur-xs duration-100': isFloating,
+ 'scrollbar-minimal -mx-1 flex flex-col gap-1 overflow-y-auto transition-[opacity,max-height]': !isFloating,
+ 'max-h-0 group-focus-within/submenu:max-h-32 group-hover/submenu:max-h-32': !isFloating && !isSubMenuOpen,
+ 'pointer-events-auto max-h-32 opacity-100': !isFloating && isSubMenuOpen,
+ },
submenuStyle,
)
"
>
- <div class="submenu-divider bg-hr dark:bg-hr/30 -mx-3 my-1 h-px" />
- <ContextMenuItem v-for="(child, index) in children" :key="index" v-bind="child" />
+ <div :class="cn('bg-hr dark:bg-hr/30 top-0 h-0', { 'sticky min-h-px': !isFloating })" />
+ <ContextMenuItem v-for="(child, index) in children" :key="index" v-bind="child" :class="cn({ 'ps-2 pe-1': !isFloating })" />
</div>
</div>
</template>
-<style lang="css" scoped>
-@reference '@css/app.css';
-.floating-menu {
- @apply bg-overlay-2-t border-overlay-border/10 absolute top-0 max-h-none w-48 rounded-md border p-1 px-1! shadow-xs backdrop-blur-xs transition-opacity duration-100 ease-in group-hover/submenu:ease-out;
-}
-
-[data-submenu][data-floating='true'] > .submenu-divider {
- display: none;
-}
-</style>