Skip to content

Commit

Permalink
feat: open current item on load + scroll into view on opening
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelodelain committed Feb 21, 2024
1 parent 71e0fe0 commit 1ad3b03
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/runtime/components/StoriesNavItem.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import type { PropType } from 'vue'
import { computed } from 'vue'
import { computed, ref, watchEffect, watch } from 'vue'
import { NuxtLink } from '#components'
import { useRoute } from 'vue-router'
Expand All @@ -27,7 +27,8 @@ const props = defineProps({
const route = useRoute()
const isOpen = computed(() => props.open)
const link = ref<HTMLAnchorElement | null>(null)
const folder = ref<HTMLDivElement | null>(null)
const isLink = computed(() => typeof props.item.to === 'string')
Expand All @@ -46,16 +47,28 @@ function getLinkValues(obj: Object & { to?: string }) {
}, [])
}
const isActiveParentRoute = computed(() => {
const hasActiveParentRoute = computed(() => {
return getLinkValues(props.item).includes(route.path)
})
const isOpen = ref(props.open || hasActiveParentRoute.value)
watchEffect(() => {
if (hasActiveParentRoute.value) isOpen.value = true
})
watch(isOpen, () => {
if (isOpen.value) {
;(link.value || folder.value)?.scrollIntoView()
}
})
</script>

<template>
<NuxtLink v-if="isLink" :to="item.to" :class="$style.link">
<NuxtLink v-if="isLink" ref="link" :to="item.to" :class="$style.link">
{{ item.label }}
</NuxtLink>
<div v-else :class="[$style.folder, isActiveParentRoute && $style['folder--active']]">
<div v-else ref="folder" :class="[$style.folder, hasActiveParentRoute && $style['folder--active']]">
<button :class="$style.button" @click="isOpen = !isOpen">
<span>{{ label }}</span>
<span :class="$style.icon">{{ isOpen ? '−' : '+' }}</span>
Expand Down

0 comments on commit 1ad3b03

Please sign in to comment.