1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
---
import { ScrollArea } from '@/components/ui/scroll-area'
import {
getCombinedReadingTime,
getParentId,
getParentPost,
getPostById,
getPostReadingTime,
getSubpostsForParent,
isSubpost,
} from '@/lib/data-utils'
import { Icon } from 'astro-icon/components'
const { parentId } = Astro.props
const currentPostId = Astro.params.id as string
const isCurrentSubpost = isSubpost(currentPostId)
const rootParentId = isCurrentSubpost ? getParentId(currentPostId) : parentId
const currentPost = !isCurrentSubpost ? await getPostById(currentPostId) : null
const subposts = await getSubpostsForParent(rootParentId)
const parentPost = isCurrentSubpost ? await getParentPost(currentPostId) : null
const activePost = parentPost || currentPost
const isActivePost = activePost?.id === currentPostId
const activePostReadingTime = activePost
? await getPostReadingTime(activePost.id)
: null
const activePostCombinedReadingTime =
activePost && subposts.length > 0
? await getCombinedReadingTime(activePost.id)
: null
const subpostsWithReadingTime = await Promise.all(
subposts.map(async (subpost) => ({
...subpost,
readingTime: await getPostReadingTime(subpost.id),
})),
)
const currentSubpostDetails = isCurrentSubpost
? subpostsWithReadingTime.find((subpost) => subpost.id === currentPostId)
: null
---
{
activePost && subposts.length > 0 && (
<div id="mobile-subposts-container" class="w-full xl:hidden">
<details class="group">
<summary class="flex w-full cursor-pointer items-center justify-between">
<div class="mx-auto flex w-full max-w-3xl items-center px-4 py-3">
<div class="relative mr-2 size-4">
<Icon
name={
currentSubpostDetails
? 'lucide:file-text'
: isActivePost
? 'lucide:book-open-text'
: 'lucide:book-open'
}
class="size-4 flex-shrink-0"
aria-hidden="true"
/>
</div>
<div class="flex flex-grow flex-col truncate text-sm">
<span class="text-muted-foreground truncate">
{currentSubpostDetails
? currentSubpostDetails.data.title
: activePost?.data.title}
</span>
</div>
<span class="text-muted-foreground ml-2">
<Icon
name="lucide:chevron-down"
class="h-4 w-4 transition-transform duration-200 group-open:rotate-180"
/>
</span>
</div>
</summary>
<ScrollArea
client:load
className="mx-auto max-w-3xl"
data-subposts-header-scroll
>
<div class="max-h-[30vh]">
<ul class="flex list-none flex-col gap-y-1 px-4 pb-4">
{activePost && (
<li>
{isActivePost ? (
<div class="text-foreground bg-muted mobile-subposts-active-item flex items-center gap-2 rounded-md px-2 py-1.5 text-sm font-medium">
<Icon
name="lucide:book-open-text"
class="size-4 flex-shrink-0"
aria-hidden="true"
/>
<div class="flex flex-col">
<span class="line-clamp-2">
{activePost.data.title}
</span>
{activePostReadingTime && (
<span class="text-muted-foreground/80 text-xs">
{activePostReadingTime}
{activePostCombinedReadingTime &&
activePostCombinedReadingTime !==
activePostReadingTime && (
<span>
{' '}
({activePostCombinedReadingTime} total)
</span>
)}
</span>
)}
</div>
</div>
) : (
<a
href={`/blog/${activePost.id}`}
class="mobile-subposts-link hover:text-foreground text-muted-foreground hover:bg-muted/50 flex items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors"
>
<Icon
name="lucide:book-open"
class="size-4 flex-shrink-0"
aria-hidden="true"
/>
<div class="flex flex-col">
<span class="line-clamp-2">
{activePost.data.title}
</span>
{activePostReadingTime && (
<span class="text-muted-foreground/80 hover:text-foreground/80 text-xs">
{activePostReadingTime}
{activePostCombinedReadingTime &&
activePostCombinedReadingTime !==
activePostReadingTime && (
<span>
{' '}
({activePostCombinedReadingTime} total)
</span>
)}
</span>
)}
</div>
</a>
)}
</li>
)}
{subpostsWithReadingTime.length > 0 && (
<div class="ml-4 space-y-1">
{subpostsWithReadingTime.map((subpost) =>
currentPostId === subpost.id ? (
<div class="text-foreground bg-muted mobile-subposts-active-item flex items-center gap-2 rounded-md px-2 py-1.5 text-sm font-medium">
<Icon
name="lucide:file-text"
class="size-4 flex-shrink-0"
aria-hidden="true"
/>
<div class="flex flex-col">
<span class="line-clamp-2">{subpost.data.title}</span>
<span class="text-muted-foreground/80 text-xs">
{subpost.readingTime}
</span>
</div>
</div>
) : (
<a
href={`/blog/${subpost.id}`}
class="mobile-subposts-link hover:text-foreground text-muted-foreground hover:bg-muted/50 flex items-center gap-2 rounded-md px-2 py-1.5 text-sm transition-colors"
>
<Icon
name="lucide:file"
class="size-4 flex-shrink-0"
aria-hidden="true"
/>
<div class="flex flex-col">
<span class="line-clamp-2">{subpost.data.title}</span>
<span class="text-muted-foreground/80 hover:text-foreground/80 text-xs">
{subpost.readingTime}
</span>
</div>
</a>
),
)}
</div>
)}
</ul>
</div>
</ScrollArea>
</details>
</div>
)
}
<script>
class SubpostsState {
scrollArea: HTMLElement | null = null
detailsElement: HTMLDetailsElement | null = null
headerScrollArea: HTMLElement | null = null
reset() {
this.scrollArea = null
this.detailsElement = null
this.headerScrollArea = null
}
}
const state = new SubpostsState()
class SubpostsHeaderScrollMask {
static update() {
if (!state.scrollArea || !state.headerScrollArea) return
const { scrollTop, scrollHeight, clientHeight } = state.scrollArea
const threshold = 5
const isAtTop = scrollTop <= threshold
const isAtBottom = scrollTop >= scrollHeight - clientHeight - threshold
state.headerScrollArea.classList.toggle('mask-t-from-80%', !isAtTop)
state.headerScrollArea.classList.toggle('mask-b-from-80%', !isAtBottom)
}
}
class SubpostsScroll {
static toActive() {
if (!state.scrollArea) return
const activeItem = state.scrollArea.querySelector(
'.mobile-subposts-active-item',
)
if (!activeItem) return
const { top: areaTop, height: areaHeight } =
state.scrollArea.getBoundingClientRect()
const { top: itemTop, height: itemHeight } =
activeItem.getBoundingClientRect()
const currentItemTop = itemTop - areaTop + state.scrollArea.scrollTop
const targetScroll = Math.max(
0,
Math.min(
currentItemTop - (areaHeight - itemHeight) / 2,
state.scrollArea.scrollHeight - state.scrollArea.clientHeight,
),
)
state.scrollArea.scrollTop = targetScroll
}
static setupInteraction() {
const container = document.getElementById('mobile-subposts-container')
if (!container) return
state.scrollArea = container.querySelector(
'[data-radix-scroll-area-viewport]',
)
state.detailsElement = container.querySelector('details')
state.headerScrollArea = container.querySelector(
'[data-subposts-header-scroll]',
)
const links = container.querySelectorAll('.mobile-subposts-link')
if (state.scrollArea) {
state.scrollArea.addEventListener(
'scroll',
SubpostsHeaderScrollMask.update,
{ passive: true },
)
}
if (state.detailsElement) {
state.detailsElement.addEventListener('toggle', () => {
if (state.detailsElement?.open) {
requestAnimationFrame(() => {
SubpostsScroll.toActive()
setTimeout(SubpostsHeaderScrollMask.update, 100)
})
}
})
}
links.forEach((link) => {
link.addEventListener('click', () => {
if (state.detailsElement) state.detailsElement.open = false
})
})
}
}
class SubpostsController {
static init() {
state.reset()
SubpostsScroll.setupInteraction()
}
static cleanup() {
if (state.scrollArea) {
state.scrollArea.removeEventListener(
'scroll',
SubpostsHeaderScrollMask.update,
)
}
state.reset()
}
}
document.addEventListener('astro:page-load', () => SubpostsController.init())
document.addEventListener('astro:after-swap', () => {
SubpostsController.cleanup()
SubpostsController.init()
})
document.addEventListener('astro:before-swap', () =>
SubpostsController.cleanup(),
)
</script>
|