@molecule/app-reading-progress-bar-angular
Feature · reading-progress-bar · App (browser) · v1.1.1 · Apache-2.0
Top-of-page scroll progress bar for article pages
npm install @molecule/app-reading-progress-bar-angularHow it works
@molecule/app-reading-progress-bar-angular is a ready-made reading-progress-bar feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { Component } from '@angular/core'
import { ReadingProgressBar } from '@molecule/app-reading-progress-bar-angular'
@Component({
selector: 'app-article',
standalone: true,
imports: [ReadingProgressBar],
template: `
<div
mol-reading-progress-bar
[containerRef]="articleRef"
color="var(--brand)"
thickness="4"
></div>
<article #articleRef>…long read…</article>
`,
})
export class Article {}Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular
Reference
Angular reading progress bar — the counterpart of
@molecule/app-reading-progress-bar-react: the same export names, the same
props and the same DOM for the same input.
Exports <ReadingProgressBar> — a thin bar pinned to the viewport edge that
fills as the user scrolls through an article. Used by blog and
news-aggregator article pages.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { Component } from '@angular/core'
import { ReadingProgressBar } from '@molecule/app-reading-progress-bar-angular'
@Component({
selector: 'app-article',
standalone: true,
imports: [ReadingProgressBar],
template: `
<div
mol-reading-progress-bar
[containerRef]="articleRef"
color="var(--brand)"
thickness="4"
></div>
<article #articleRef>…long read…</article>
`,
})
export class Article {}
Type
feature
Installation
npm install @molecule/app-reading-progress-bar-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular
API
Interfaces
ReadingProgressBarProps
Props accepted by ReadingProgressBar — the same props
@molecule/app-reading-progress-bar-react accepts.
interface ReadingProgressBarProps {
/**
* Optional ref to the article element. When provided, progress is measured
* against that element's bounding rect (so the bar fills as the user scrolls
* through the article body, not the whole page). When omitted, progress
* falls back to `window` scroll position.
*/
containerRef?: { readonly current?: Element | null }
/** Bar thickness in pixels. Defaults to `3`. */
thickness?: number
/**
* Where the bar pins. Defaults to `'top'`. Both variants render fixed across
* the full viewport width with a high z-index.
*/
position?: 'top' | 'bottom'
/**
* CSS color for the filled portion of the bar. Defaults to `'currentColor'`
* so the bar inherits whatever text color is in scope — letting the
* surrounding theme drive it. Pass a theme-aware color (`'var(--brand)'`,
* etc.) to override.
*/
color?: string
/** Extra classes on the outer wrapper. */
className?: string
/** `data-mol-id` for AI-agent / E2E selectors. */
dataMolId?: string
}
Classes
ReadingProgressBar
ReadingProgressBar component.
Tracks how far the user has scrolled through an article and renders a thin
horizontal bar pinned to the top (or bottom) of the viewport, filling
left-to-right from 0% to 100% as they read. The host IS the root <div>
(div[mol-reading-progress-bar]), matching React's root exactly: role,
the aria values, data-mol-id, data-position, the class and the inline
style all live on it.
Throttles scroll updates with requestAnimationFrame so the work runs at
most once per frame — the handler attaches in passive mode. Listens to both
scroll and resize because progress changes when the viewport height
changes (e.g. mobile toolbar collapse). The aria label comes from
useTranslation() (readingProgressBar.aria.label), read reactively so a
locale switch refreshes it.
@Component({
selector: 'div[mol-reading-progress-bar]',
standalone: true,
inputs: ['containerRef', 'thickness', 'position', 'color', 'className', 'dataMolId'],
host: {
role: 'progressbar',
'[attr.aria-label]': 'label()',
'[attr.aria-valuenow]': 'pct()',
'[attr.aria-valuemin]': '0',
'[attr.aria-valuemax]': '100',
'[attr.data-mol-id]': 'dataMolId ?? "reading-progress-bar"',
'[attr.data-position]': 'position ?? "top"',
'[class]': 'classes()',
'[style.position]': '"fixed"',
'[style.left]': '"0px"',
'[style.right]': '"0px"',
'[style.top]': 'topOffset()',
'[style.bottom]': 'bottomOffset()',
'[style.height]': 'heightPx()',
'[style.z-index]': '"1000"',
'[style.pointer-events]': '"none"',
},
template: `<div
data-mol-id="reading-progress-bar-fill"
[style.width]="widthPct()"
[style.height]="'100%'"
[style.background]="fillColor()"
[style.transition]="'width 80ms linear'"
></div>`,
})
export class ReadingProgressBar implements OnInit, OnChanges, OnDestroy {
/** Optional ref to the article element measured instead of the window. */
containerRef?: ReadingProgressBarProps['containerRef']
/** Bar thickness in pixels (React's default is applied at read time). */
thickness?: number
/** Where the bar pins (React's default is applied at read time). */
position?: 'top' | 'bottom'
/** CSS color for the filled portion of the bar. */
color?: string
/** Extra classes on the outer wrapper. */
className?: string
/** `data-mol-id` for AI-agent / E2E selectors. */
dataMolId?: string
/**
* Reactive translation, read in the injection context. Its `t` reads the
* locale, so the view re-renders the label on a locale switch.
*/
private readonly i18n = useTranslation()
/** The measured progress, 0..1. */
private readonly progress = signal(0)
/** What undoes the current listeners and frame callback. */
private dispose?: () => void
/**
* The current class map.
* @returns The UIClassMap.
*/
cm(): UIClassMap {
return getClassMap()
}
/**
* The wrapper class list (React's `cm.cn(className)`).
* @returns The class string.
*/
classes(): string {
return this.cm().cn(this.className)
}
/**
* The rounded percentage the bar shows (React's
* `Math.round(progress * 1000) / 10`).
* @returns The percentage, to one decimal.
*/
pct(): number {
return Math.round(this.progress() * 1000) / 10
}
/**
* The accessible label (React's `readingProgressBar.aria.label`).
* @returns The label text.
*/
label(): string {
return this.i18n.t(
'readingProgressBar.aria.label',
{},
{
defaultValue: 'Reading progress',
},
)
}
/**
* The inline `top` of the wrapper: `0px` while pinned to the top.
* @returns The value, or null when the bar is pinned to the bottom.
*/
topOffset(): string | null {
return (this.position ?? 'top') === 'top' ? '0px' : null
}
/**
* The inline `bottom` of the wrapper: `0px` while pinned to the bottom.
* @returns The value, or null when the bar is pinned to the top.
*/
bottomOffset(): string | null {
return this.position === 'bottom' ? '0px' : null
}
/**
* The wrapper's inline height.
* @returns The height in pixels.
*/
heightPx(): string {
return `${this.thickness ?? 3}px`
}
/**
* The fill's inline width.
* @returns The percentage to one decimal.
*/
widthPct(): string {
return `${this.pct()}%`
}
/**
* The fill's inline background (React's `color ?? 'currentColor'`).
* @returns The CSS color.
*/
fillColor(): string {
return this.color ?? 'currentColor'
}
/**
* Attaches the scroll/resize listeners and schedules the first measurement,
* exactly as React's effect does; the returned function is its cleanup.
* @returns What undoes this installation.
*/
install(): () => void {
const ref = this.containerRef
let rafId = 0
let pending = false
let cancelled = false
/**
* Recomputes progress on the next animation frame. Coalesces bursts of
* scroll events into a single state update.
*/
const schedule = (): void => {
if (pending) return
pending = true
rafId = requestAnimationFrame(() => {
pending = false
rafId = 0
if (cancelled) return
const el = ref?.current ?? null
this.progress.set(
el ? computeArticleProgress(el, window.innerHeight ?? 0) : computeWindowProgress(),
)
})
}
schedule()
window.addEventListener('scroll', schedule, { passive: true })
window.addEventListener('resize', schedule, { passive: true })
return () => {
cancelled = true
if (rafId !== 0) cancelAnimationFrame(rafId)
window.removeEventListener('scroll', schedule)
window.removeEventListener('resize', schedule)
}
}
/** Installs the listeners once the inputs are bound, as React's mount effect. */
ngOnInit(): void {
this.dispose = this.install()
}
/**
* Re-installs the listeners when the `containerRef` object is replaced, the
* way React's effect depends on it.
*
* @param changes - The changed inputs.
*/
ngOnChanges(changes: SimpleChanges): void {
if (!changes['containerRef'] || !this.dispose) return
this.dispose()
this.dispose = this.install()
}
/** Removes the listeners when the component is destroyed. */
ngOnDestroy(): void {
this.dispose?.()
this.dispose = undefined
}
}
Functions
computeArticleProgress(el, viewportHeight)
Computes the scroll progress (0..1) of an element, measured by how much of the element has scrolled past the top of the viewport.
Returns 0 when the element's top is at or below the viewport top, 1 when
its bottom is at or above the viewport top plus the viewport height (fully
read), and linearly interpolates between those endpoints in the middle. An
element shorter than the viewport counts as fully read once its top reaches
the viewport top.
function computeArticleProgress(el: Element, viewportHeight: number): number
el— The article element whose read-through progress we want.viewportHeight— The viewport height to use as the denominator.
Returns: A number in [0, 1].
computeWindowProgress()
Computes window scroll progress (0..1) using scrollY and the document's
total scrollable height.
function computeWindowProgress(): number
Returns: A number in [0, 1]. Falls back to 0 when there is nothing to scroll (page shorter than the viewport).
Injection Notes
Requirements
Peer dependencies:
@angular/common^22.0.0@angular/core^22.0.0@angular/platform-browser^22.0.0@molecule/app-angular^3.5.0 || ^4.0.0@molecule/app-ui^1.0.1@molecule/app-ui-angular^1.1.0
Runtime Dependencies
@angular/common@angular/core@angular/platform-browser@molecule/app-angular@molecule/app-ui@molecule/app-ui-angular
Companion locale bond: @molecule/app-locales-reading-progress-bar (the
progressbar aria-label). The fill defaults to currentColor — it inherits
the surrounding text color, so pass color (e.g. var(--color-primary))
when the ambient text color is low-contrast against the page edge. The bar
renders position: fixed at z-index: 1000 spanning the viewport width; it
only listens to window scroll — containerRef changes what is measured,
not which scroller is observed (inner scroll containers won't drive it).
Requires the Angular binding's i18n provider and a wired ClassMap bond.
Translations
Translation strings are provided by @molecule/app-locales-reading-progress-bar.