Skip to content

Commit e71a1f3

Browse files
committed
use astro-pure toc in mobile
Signed-off-by: catcodeme <1020082805@qq.com>
1 parent 3c01916 commit e71a1f3

2 files changed

Lines changed: 101 additions & 7 deletions

File tree

src/layouts/BlogPost.astro

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ const primaryColor = data.heroImage?.color ?? 'hsl(var(--primary) / var(--un-tex
5454
highlightColor={primaryColor}
5555
back='/blog'
5656
>
57-
{/* 手风琴目录专用 slot,支持贴边布局 */}
57+
{/* 大屏幕:手风琴目录专用 slot,支持贴边布局 (仅在大屏幕显示) */}
5858
{!!headings.length && headings.length > 4 && (
59-
<AccordionTOC headings={headings} position='right' slot='handletoc' />
59+
<AccordionTOC headings={headings} position='right' slot='handletoc' class='hidden lg:block' />
6060
)}
61-
{/* 右侧 sidebar 只显示普通 TOC */}
62-
{!!headings.length && headings.length <= 4 && (
63-
<TOC {headings} slot='sidebar' />
61+
62+
{/* 右侧 sidebar TOC:小屏幕显示所有目录,大屏幕只显示 <= 4 项的目录 */}
63+
{!!headings.length && (
64+
<TOC {headings} slot='sidebar' class={
65+
headings.length > 4 ? 'lg:hidden' : ''
66+
} />
6467
)}
6568

6669
<Hero {data} {remarkPluginFrontmatter} slot='header' />

src/layouts/ContentLayout.astro

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import BackToTop from '@/custom/components/pages/BackToTop.astro'
33
44
import type { SiteMeta } from 'astro-pure/types'
5-
import { Button } from 'astro-pure/user'
5+
import { Button, Icon } from 'astro-pure/user'
66
import { cn } from 'astro-pure/utils'
77
import PageLayout from '@/layouts/BaseLayout.astro'
88
import { integ } from '@/site-config'
@@ -74,5 +74,96 @@ const { meta, highlightColor, back = '/', ...props } = Astro.props
7474
</div>
7575
</div>
7676

77-
<BackToTop header='content-header' content='content' />
77+
<BackToTop header='content-header' content='content'>
78+
<button
79+
slot='other-icons'
80+
aria-label='Toggle sidebar'
81+
class='size-10 flex items-center justify-center rounded-full border-2 border-transparent bg-muted text-muted-foreground duration-300 hover:border-border/75 md:hidden sm:size-12'
82+
id='sidebar-btn'
83+
>
84+
<Icon name='list' />
85+
</button>
86+
</BackToTop>
7887
</PageLayout>
88+
89+
<script>
90+
// Toggle sidebar visibility on button click
91+
const sidebarButton = document.getElementById('sidebar-btn')
92+
const sidebar = document.getElementById('sidebar')
93+
const sidebarShade = document.getElementById('sidebar-shade') as HTMLElement
94+
95+
if (sidebarShade) {
96+
sidebarShade.addEventListener('click', () => {
97+
toggleSidebar()
98+
})
99+
}
100+
101+
function toggleSidebar() {
102+
if (sidebar && sidebar.classList.contains('show')) {
103+
sidebar.classList.remove('show')
104+
sidebarShade.style.display = 'none'
105+
} else if (sidebar) {
106+
sidebar.classList.add('show')
107+
sidebarShade.style.display = 'block'
108+
} else {
109+
console.error('Sidebar element not found.')
110+
}
111+
}
112+
113+
sidebarButton?.addEventListener('click', () => {
114+
toggleSidebar()
115+
})
116+
</script>
117+
<style>
118+
@media (min-width: 769px) {
119+
#sidebar-shade {
120+
display: none !important;
121+
}
122+
}
123+
@media (max-width: 769px) {
124+
aside.show {
125+
display: block !important;
126+
opacity: 1;
127+
}
128+
129+
@keyframes fade-in-side {
130+
0% {
131+
transform: translateX(50%);
132+
opacity: 0;
133+
}
134+
100% {
135+
transform: translateX(0);
136+
opacity: 1;
137+
}
138+
}
139+
@keyframes fade-in {
140+
0% {
141+
opacity: 0;
142+
}
143+
100% {
144+
opacity: 1;
145+
}
146+
}
147+
aside {
148+
animation: 300ms fade-in-side !important;
149+
animation-fill-mode: forwards;
150+
right: 0;
151+
top: 0 !important;
152+
height: 100vh !important;
153+
min-width: 40vw !important;
154+
max-width: 75vw !important;
155+
border-top-right-radius: 0 !important;
156+
border-bottom-right-radius: 0 !important;
157+
padding-top: 1.5rem !important;
158+
padding-bottom: 1.5rem !important;
159+
}
160+
#sidebar-shade {
161+
animation: 300ms fade-in;
162+
}
163+
}
164+
@media (max-width: 640px) {
165+
aside {
166+
min-width: 55vw !important;
167+
}
168+
}
169+
</style>

0 commit comments

Comments
 (0)