Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: force scroll position in aside #636

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions theme/components/docs/DocsPageContent.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
const { page, navigation } = useContent()
const route = useRoute()

const fallbackValue = (value: string, fallback = true) => {
if (typeof page.value?.[value] !== 'undefined') {
Expand All @@ -14,17 +15,48 @@ const hasToc = computed(() => page.value?.toc !== false && page.value?.body?.toc

// TODO: get navigation links from aside level
const hasAside = computed(() => page.value?.aside !== false && navigation.value.length > 1)

const bottom = computed(() => fallbackValue('bottom', true))

const isOpen = ref(false)

/*
** This below is a workaround until Nuxt has a proper support for layouts and Suspense
*/
const asideNav = ref(null)

const getParentPath = () => route.path.split('/').slice(0, 2).join('/')
const asideScroll = useState('asideScroll', () => {
return {
parentPath: getParentPath(),
scrollTop: asideNav.value?.scrollTop || 0
}
})

function watchScrollHeight () {
if (asideNav.value.scrollHeight === 0) {
setTimeout(watchScrollHeight, 0)
}
asideNav.value.scrollTop = asideScroll.value.scrollTop
}
onMounted(() => {
if (asideScroll.value.parentPath !== getParentPath()) {
asideScroll.value.parentPath = getParentPath()
asideScroll.value.scrollTop = 0
} else {
watchScrollHeight()
}
})

onBeforeUnmount(() => {
asideScroll.value.scrollTop = asideNav.value.scrollTop
})
</script>

<template>
<AppContainer padded class="relative flex flex-col-reverse lg:grid lg:grid-cols-12 lg:gap-8">
<!-- Aside -->
<aside
v-if="hasAside"
ref="asideNav"
class="lg:top-header hidden overflow-y-auto overflow-x-hidden pb-8 lg:sticky lg:col-span-2 lg:block lg:max-h-[calc(100vh-var(--header-height))] lg:self-start lg:pt-8"
>
<DocsAside />
Expand Down