Skip to content

Commit 04640eb

Browse files
committed
fix: pin the sidebar only when necessary
1 parent 704987b commit 04640eb

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

app/pages/package/[...package].vue

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,44 @@ onMounted(() => {
5252
checkHeaderPosition()
5353
})
5454
55+
const windowSize = useWindowSize()
56+
const sidebar = useTemplateRef<HTMLDivElement>('sidebar')
57+
const sidebarBounds = useElementBounding(sidebar)
58+
const scrollState = useWindowScroll()
59+
const scrollDirection = shallowRef<'up' | 'down'>('down')
60+
61+
const sidebarOverflow = computed(() => {
62+
return sidebarBounds.height.value > windowSize.height.value - 120
63+
})
64+
65+
const sidebarStyles = computed(() => {
66+
if (!sidebarOverflow.value) {
67+
return {
68+
alignSelf: 'flex-start',
69+
top: '80px',
70+
}
71+
}
72+
73+
if (scrollDirection.value === 'down') {
74+
return {
75+
alignSelf: 'flex-end',
76+
bottom: '40px',
77+
}
78+
}
79+
80+
if (scrollDirection.value === 'up') {
81+
return {
82+
alignSelf: 'flex-start',
83+
top: '80px',
84+
}
85+
}
86+
})
87+
88+
watch(scrollState.directions, ({ bottom, top }) => {
89+
if (bottom) scrollDirection.value = 'down'
90+
if (top) scrollDirection.value = 'up'
91+
})
92+
5593
const { packageName, requestedVersion, orgName } = usePackageRoute()
5694
const selectedPM = useSelectedPackageManager()
5795
const activePmId = computed(() => selectedPM.value ?? 'npm')
@@ -1165,10 +1203,12 @@ onKeyStroke(
11651203
</div>
11661204
</section>
11671205
</section>
1168-
<div class="area-sidebar">
1206+
<div class="area-sidebar flex">
11691207
<!-- Sidebar -->
11701208
<div
1171-
class="sidebar-scroll sticky top-34 space-y-6 sm:space-y-8 min-w-0 overflow-y-auto pr-2.5 hover:pr-0.5 lg:(max-h-[calc(100dvh-8.5rem)] overscroll-contain) xl:(top-22 pt-2 max-h-[calc(100dvh-6rem)])"
1209+
ref="sidebar"
1210+
class="sticky space-y-6 sm:space-y-8 min-w-0 xl:(pt-2)"
1211+
:style="sidebarStyles"
11721212
>
11731213
<!-- Maintainers (with admin actions when connected) -->
11741214
<PackageMaintainers :package-name="pkg.name" :maintainers="pkg.maintainers" />

0 commit comments

Comments
 (0)