Smooth Anchors

Pro

Links to a section of the same page glide there instead of jumping. You set how long the glide takes and how it lands, and the hash, the back button and focus keep working.

700ms

Installation#

# One-time setup (tokens + lib helpers)npx koalaui-cli@latest init# Activate your Pro license (once per machine)npx koalaui-cli@latest login <key># Add this component (its dependencies come along)npx koalaui-cli@latest add smooth-anchors

Usage#

This example is part of Koala UI Pro

The preview is yours to explore. Its code comes with a Pro license, paid once and used in every project.

Have a license? Unlock it here

This example is part of Koala UI Pro

The preview is yours to explore. Its code comes with a Pro license, paid once and used in every project.

Have a license? Unlock it here

API reference#

SmoothAnchors

Mount it once, in your root layout. Every click on a link to #id or to the current page plus a hash glides to the target instead of jumping, including next/link. Renders nothing. useSmoothAnchors(options) is the same thing as a hook.

PropTypeDefaultDescription
durationnumber | (distance: number) => number700Time of the glide in milliseconds, or a function of the distance in pixels. A link can override it with data-scroll-duration.
easing"out" | "inOut" | "sine" | "drawer" | [x1, y1, x2, y2]"inOut"The arrival curve: a curve from lib/motion or your own cubic-bezier points. inOut eases away from where you are and settles into the section.
offsetnumber0Extra pixels kept above the target, on top of its scroll-margin-top and the scroller's scroll-padding-top.
containerRefObject<HTMLElement>nearest scrollerThe element that scrolls. By default the nearest scrolling ancestor of the target, else the page.
history"push" | "replace" | false"push"How the hash reaches the URL: a new history entry, the current one replaced, or left alone.
focusbooleantrueMoves focus to the target on arrival, without scrolling again, so Tab continues from the section.
onArrive(target: Element | null) => void-Called when a glide lands. Not called when the reader interrupts it.

smoothScrollTo

The glide on its own, for code: smoothScrollTo(target, options) takes an element, a #hash, a pixel offset or null for the top, and resolves true on arrival or false when interrupted.

PropTypeDefaultDescription
targetElement | string | number | null-Where to go.
options{ duration, easing, offset, container, focus }-Same meaning as the component props; container is the element itself.

FAQ#

The CSS property glides, but the browser picks the time and the curve and you cannot change them. SmoothAnchors runs the glide itself, so you choose both.