Skip to content

Commit 0cc647c

Browse files
authored
fix: improve back-to-top button to adapt layout changes properly (#527)
Update back-to-top button to accommodate properly, even if the max-width of the blog post is updated. Uses `fixed` position on mobile and `sticky` position on large screens.
1 parent 316bdd8 commit 0cc647c

4 files changed

Lines changed: 20 additions & 31 deletions

File tree

Lines changed: 1 addition & 0 deletions
Loading

src/components/BackToTopButton.astro

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
---
22
import IconChevronLeft from "@/assets/icons/IconChevronLeft.svg";
3-
4-
/*
5-
In #btt-btn-container, left position is `left-[calc((var(--container-3xl)/2)+50%+1rem)]`.
6-
This is because we use `max-w-3xl` in `PostDetails.astro`.
7-
If `max-width` in `PostDetails.astro` get updated, make sure to update the `var(--container-3xl)` as well.
8-
max-w-3xl => var(--container-3xl)
9-
max-w-4xl => var(--container-4xl)
10-
max-w-5xl => var(--container-5xl)
11-
etc...
12-
*/
3+
import IconArrowNarrowUp from "@/assets/icons/IconArrowNarrowUp.svg";
134
---
145

156
<div
167
id="btt-btn-container"
178
class:list={[
18-
"fixed right-4 bottom-8 xl:right-auto xl:left-[calc((var(--container-3xl)/2)+50%+1rem)]",
19-
"translate-y-14 opacity-0 transition-all duration-500",
9+
"fixed right-4 bottom-8 z-50",
10+
"md:sticky md:right-auto md:float-end md:mr-1",
11+
"translate-y-14 opacity-0 transition duration-500",
2012
]}
2113
>
2214
<button
2315
data-button="back-to-top"
2416
class:list={[
25-
"group relative",
26-
"size-14 rounded-full px-2 py-1 shadow-xl lg:h-8 lg:w-fit lg:rounded-md lg:shadow-none",
27-
"bg-background lg:bg-transparent",
17+
"group relative bg-background px-2 py-1",
18+
"size-14 rounded-full shadow-xl",
19+
"md:h-8 md:w-fit md:rounded-md md:shadow-none",
20+
"md:bg-background/35 md:bg-clip-padding md:backdrop-blur-lg",
2821
]}
2922
>
3023
<span
3124
id="progress-indicator"
32-
class="absolute inset-0 -z-10 block size-14 scale-110 rounded-full bg-transparent lg:hidden lg:h-8 lg:rounded-md"
25+
class="absolute inset-0 -z-10 block size-14 scale-110 rounded-full bg-transparent md:hidden md:h-8 md:rounded-md"
3326
></span>
34-
<IconChevronLeft class="inline-block rotate-90 lg:hidden" />
35-
<span
36-
class="sr-only underline decoration-dashed decoration-2 underline-offset-4 group-hover:text-accent lg:not-sr-only"
37-
>
38-
<span class="text-xl" tabindex="0">&uarr;</span> Back To Top
27+
<IconChevronLeft class="inline-block rotate-90 md:hidden" />
28+
<span class="sr-only text-sm group-hover:text-accent md:not-sr-only">
29+
<IconArrowNarrowUp class="inline-block size-4" />
30+
Back To Top
3931
</span>
4032
</button>
4133
</div>

src/components/ShareLinks.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const URL = Astro.url;
77

88
{
99
SHARE_LINKS.length > 0 && (
10-
<div class="flex flex-none flex-col items-center justify-center gap-1 sm:items-end">
10+
<div class="flex flex-none flex-col items-center justify-center gap-1 md:items-start">
1111
<span class="italic">Share this post on:</span>
1212
<div class="text-center">
1313
{SHARE_LINKS.map(social => (

src/layouts/PostDetails.astro

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,13 @@ const nextPost =
110110

111111
<EditPost class="sm:hidden" {hideEditPost} {post} />
112112

113-
<BackToTopButton />
113+
<ul class="mt-4 mb-8 sm:my-8">
114+
{tags.map(tag => <Tag tag={slugifyStr(tag)} tagName={tag} />)}
115+
</ul>
114116

115-
<div
116-
class="flex flex-col items-center justify-between gap-6 sm:flex-row sm:items-start sm:gap-8"
117-
>
118-
<ul class="self-start">
119-
{tags.map(tag => <Tag tag={slugifyStr(tag)} tagName={tag} />)}
120-
</ul>
117+
<BackToTopButton />
121118

122-
<ShareLinks />
123-
</div>
119+
<ShareLinks />
124120

125121
<hr class="my-6 border-dashed" />
126122

0 commit comments

Comments
 (0)