diff --git a/src/components/CollapsibleToolMenu.vue b/src/components/CollapsibleToolMenu.vue index f2ffb0af..ca21e257 100644 --- a/src/components/CollapsibleToolMenu.vue +++ b/src/components/CollapsibleToolMenu.vue @@ -10,9 +10,9 @@ const props = withDefaults(defineProps<{ toolsByCategory?: ToolCategory[] }>(), const { toolsByCategory } = toRefs(props); const route = useRoute(); -// Memoize component creation functions -const makeLabel = (tool: Tool) => () => h(MenuItemWithTooltip, { tool, key: tool.path }); -const makeIcon = (tool: Tool) => () => h(MenuIconItem, { tool, key: tool.path }); +// Memoize component creation functions - wrap in markRaw to prevent reactivity +const makeLabel = (tool: Tool) => () => markRaw(h(MenuItemWithTooltip, { tool, key: tool.path })); +const makeIcon = (tool: Tool) => () => markRaw(h(MenuIconItem, { tool, key: tool.path })); const collapsedCategories = useStorage>( 'menu-tool-option:collapsed-categories', @@ -45,6 +45,7 @@ watchEffect(() => { }); const isToggling = ref(false); +const menuContainerRefs = ref>({}); function toggleCategoryCollapse({ name }: { name: string }) { collapsedCategories.value[name] = !collapsedCategories.value[name]; @@ -84,10 +85,16 @@ function getAnimationDuration(itemCount: number): number { return baseDuration + (Math.min(itemCount, 30) * durationIncrement); } +// Function to check if any tool in the category is active +function isCategoryActive(components: Tool[]): boolean { + return components.some(tool => tool.path === route.path); +} + const menuOptions = computed(() => toolsByCategory.value.map(({ name, components }) => ({ name, isCollapsed: collapsedCategories.value[name], + isActive: isCategoryActive(components), animationDuration: getAnimationDuration(components.length), tools: components.map(tool => ({ label: makeLabel(tool), @@ -97,6 +104,37 @@ const menuOptions = computed(() => })), ); +async function scrollToActiveItem() { + const activeCategory = toolsByCategory.value.find(({ components }) => + isCategoryActive(components), + ); + + if (activeCategory) { + // Expand the active category + collapsedCategories.value[activeCategory.name] = false; + + // Wait for the entire animation to complete + await new Promise(resolve => setTimeout(resolve, getAnimationDuration(activeCategory.components.length) + 50)); + + // Scroll to the active menu item + const menuContainer = menuContainerRefs.value[activeCategory.name]; + if (menuContainer) { + const activeItem = menuContainer.querySelector('.router-link-exact-active, .router-link-active'); + if (activeItem) { + activeItem.scrollIntoView({ behavior: 'smooth', block: 'center' }); + } + } + } +} + +onMounted(() => { + scrollToActiveItem(); +}); + +watch(() => route.path, () => { + scrollToActiveItem(); +}); + const themeVars = useThemeVars(); @@ -112,9 +150,10 @@ const themeVars = useThemeVars(); -
+