Skip to content

Anchor

A heading paired with an in-page anchor link that copies the URL fragment when clicked.

  • stable
  • since v0.1.0
  • 0.9 kB
  • navigation
  • anchor
  • accessible

Renders a semantic heading with a visually-hidden anchor link revealed on hover/focus; respects prefers-reduced-motion when scrolling.

Preview
Open
tsx
import { Anchor } from "@arshad-shah/cynosure-react";
<Anchor id="introduction" level={2}>
Introduction
</Anchor>

The id is required — it is applied to the heading element so URL fragments can target it directly. Clicking the icon scrolls to the heading and copies the absolute URL (origin + path + #id) to the clipboard.

Use the level prop to render h1 through h6. Visual styling stays consistent; only the underlying tag changes for document outline.

Preview
Open
tsx

Pass offsetTop (a number in pixels or any CSS length string) when a fixed header would otherwise overlap the scrolled-into-view heading. The value is exposed as --cynosure-anchor-offset on the wrapper.

Preview
Open
tsx

The onCopy handler fires after the URL fragment is written to the clipboard — useful for a toast confirmation.

Preview
Open
tsx
PropTypeDefaultDescription
id*
string
Required — used as the heading `id` and the URL fragment.
level
1|2|3|4|5|6
2
Which heading tag is rendered (`h1`–`h6`). Pick to match the section's place in the document outline.
offsetTop
string|number
Sticky-header offset applied via `scroll-margin-top` when scrolling the heading into view. Accepts any CSS length (`number` is treated as px). Set this when a fixed top bar would otherwise cover the heading.
onCopy
(() => void)
Fired after the URL has been (best-effort) copied to the clipboard.
label
string
Copy link to section
Accessible label for the copy-link icon button.
children
ReactNode
The visible heading text.
  • The wrapping element is a real <h1><h6> so it appears in the document outline.
  • The reveal link carries a descriptive aria-label ("Copy link to section" by default); override via the label prop for localisation.
  • The link is keyboard-focusable; tabbing onto it reveals it just like a hover.
  • When prefers-reduced-motion: reduce is set the scroll behaviour falls back from smooth to auto.
  • Clipboard failures are swallowed silently — the hash still updates so the URL remains shareable.
  • Pair with a top-of-page BackToTop button so long-form pages get both forward (jump-to-section) and backward (return-to-top) affordances.
  • Use the onCopy callback to surface a Toast confirming the URL was copied.
  • Set offsetTop to the height of any sticky page header so the scrolled heading clears it.
  • Wrap inline <Code> inside the heading children to render code-decorated section titles.