Skip to content
Merged
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions src/components/ReadingProgress.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div id="reading-progress" class="fixed top-14 z-20 h-[3px] pointer-events-none" style="left:0;right:0">
<div
id="reading-progress-bar"
class="h-full bg-primary dark:bg-primary-dark w-0 transition-[width] duration-100 ease-out"
>
</div>
</div>

<script>
import { onHydration } from '../utils/hydration'

function initReadingProgress() {
const barEl = document.getElementById('reading-progress-bar')
const container = document.getElementById('reading-progress')
if (!barEl || !container) return

const contentCol = container.closest('.flex-1.min-w-0')
const articleEl = contentCol?.querySelector('article')
if (!(articleEl instanceof HTMLElement) || !(contentCol instanceof HTMLElement)) return

const bar = barEl
const article = articleEl

function updateLayout() {
const rect = contentCol.getBoundingClientRect()
container.style.left = rect.left + 'px'
container.style.width = rect.width + 'px'
}

function updateProgress() {
const rect = article.getBoundingClientRect()
const articleTop = rect.top + window.scrollY
const articleHeight = rect.height
const windowHeight = window.innerHeight
const scrolled = window.scrollY - articleTop + windowHeight
const total = articleHeight

let progress = (scrolled / total) * 100
progress = Math.max(0, Math.min(100, progress))

bar.style.width = `${progress}%`
}

updateLayout()
updateProgress()

window.addEventListener('scroll', updateProgress, { passive: true })
window.addEventListener('resize', updateLayout, { passive: true })
}

onHydration(initReadingProgress)
</script>
2 changes: 2 additions & 0 deletions src/pages/articles/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import DocsLayout from '../../layouts/DocsLayout.astro'
import ShareButtons from '../../components/ShareButtons.astro'
import GitHubUserCard from '../../components/GitHubUserCard.astro'
import ReadingProgress from '../../components/ReadingProgress.astro'
import { getCollection, render } from 'astro:content'
import { SITE } from '../../config.mjs'
import { getLangFromUrl, t } from '../../i18n/index.ts'
Expand Down Expand Up @@ -82,6 +83,7 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
<script type="application/ld+json" set:html={JSON.stringify(jsonLdArticle)} slot="head" />

<div class="flex">
<ReadingProgress />
<div class="flex-1 px-8 py-12 max-w-4xl mx-auto min-w-0">
<nav
aria-label="Breadcrumb"
Expand Down
2 changes: 2 additions & 0 deletions src/pages/en/articles/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import DocsLayout from '../../../layouts/DocsLayout.astro'
import ShareButtons from '../../../components/ShareButtons.astro'
import GitHubUserCard from '../../../components/GitHubUserCard.astro'
import ReadingProgress from '../../../components/ReadingProgress.astro'
import { getCollection, render } from 'astro:content'
import { SITE } from '../../../config.mjs'
import { t } from '../../../i18n/index.ts'
Expand Down Expand Up @@ -83,6 +84,7 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
<script type="application/ld+json" set:html={JSON.stringify(jsonLdArticle)} slot="head" />

<div class="flex">
<ReadingProgress />
<div class="flex-1 px-8 py-12 max-w-4xl mx-auto min-w-0">
<nav
aria-label="Breadcrumb"
Expand Down
Loading