-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,87 @@ | ||
<script setup lang="ts"> | ||
import type { TocLink } from '@nuxt/content' | ||
import { useScrollspy, useNuxtApp, useRouter } from '#imports' | ||
const { activeHeadings, updateHeadings } = useScrollspy() | ||
type ContentTocProps = { | ||
title?: string | ||
const { links = [], isHover, isMobile } = defineProps<{ | ||
links: TocLink[] | ||
active?: string | ||
} | ||
const { title = 'Table of Contents', links = [] } = defineProps<ContentTocProps>() | ||
isHover: boolean | ||
activeHeadings: string[] | ||
isMobile: boolean | ||
}>() | ||
const nuxtApp = useNuxtApp() | ||
const router = useRouter() | ||
nuxtApp.hooks.hookOnce('page:finish', () => { | ||
updateHeadings([ | ||
...document.querySelectorAll('h2'), | ||
...document.querySelectorAll('h3') | ||
]) | ||
}) | ||
const emit = defineEmits(['move']) | ||
const scrollToHeading = (id: string): void => { | ||
const encodedId = encodeURIComponent(id) | ||
router.push(`#${encodedId}`) | ||
emit('move', id) | ||
if (!isMobile || (isMobile && isHover)) { | ||
const encodedId = encodeURIComponent(id) | ||
router.push(`#${encodedId}`) | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<ul v-if="links?.length"> | ||
<li v-for="link in links" :key="link.text" :class="link.depth === 3 ? 'ml-3' : ''"> | ||
<a | ||
class="block truncate text-xs/6" | ||
:class="activeHeadings.includes(link.id) ? 'text-accent' : 'text-tertiary hover:text-gray-700 dark:hover:text-gray-200'" | ||
:href="`#${link.id}`" | ||
@click.prevent="scrollToHeading(link.id)" | ||
> | ||
{{ link.text }} | ||
</a> | ||
<div> | ||
<Transition | ||
enter-active-class="transition-all duration-100 ease-in-out" | ||
enter-from-class="opacity-0" | ||
enter-to-class="opacity-100" | ||
leave-active-class="transition-all duration-100 ease-in-out" | ||
leave-from-class="opacity-100" | ||
leave-to-class="opacity-0" | ||
mode="out-in" | ||
> | ||
<ul v-if="links?.length && isHover" class="space-y-2"> | ||
<li | ||
v-for="link in links" | ||
:key="link.text" | ||
:class="[ | ||
link.depth === 3 ? 'ml-4' : '' | ||
]" | ||
> | ||
<a | ||
class="block truncate text-xs/6 transition-all duration-500 ease-in-out" | ||
:class="activeHeadings.includes(link.id) ? 'text-accent' : 'text-tertiary hover:text-gray-700 dark:hover:text-gray-200'" | ||
:href="`#${link.id}`" | ||
@click.prevent="scrollToHeading(link.id)" | ||
> | ||
{{ link.text }} | ||
</a> | ||
|
||
<Links | ||
v-if="link.children" | ||
:links="link.children" | ||
:is-hover | ||
:active-headings | ||
:is-mobile | ||
/> | ||
</li> | ||
</ul> | ||
<div v-else-if="links?.length" class="space-y-4"> | ||
<div | ||
v-for="link in links" | ||
:key="link.text" | ||
class="flex flex-col items-end gap-1" | ||
> | ||
<div | ||
class="h-[4px] rounded-full transition-all duration-500 ease-in-out" | ||
:class="[ | ||
activeHeadings.includes(link.id) | ||
? 'w-12 bg-accent' | ||
: 'w-8 bg-secondary/20', | ||
link.depth === 3 ? 'w-6' : '', | ||
isMobile ? 'cursor-default' : 'cursor-pointer' | ||
]" | ||
/> | ||
|
||
<Links v-if="link.children" :links="link.children" /> | ||
</li> | ||
</ul> | ||
<Links | ||
v-if="link.children" | ||
:links="link.children" | ||
:is-hover | ||
:active-headings | ||
:is-mobile | ||
/> | ||
</div> | ||
</div> | ||
</Transition> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters