Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

DisplayReact

Skip Nav Link

A link that stays out of sight until the keyboard reaches it, and moves the focus past the navigation to the main content when it is activated.

Rendered by React 19.3.1

import type { MouseEvent } from 'react'
import { SkipNavLink } from '@hamur/react/skip-nav-link'

const stay = (event: MouseEvent) => event.preventDefault()

export default function SkipNavLinkExample() {
  return (
    <div className="stack">
      <div className="sn-frame">
        <SkipNavLink className="sn-link" targetId="skip-demo-main">
          Skip to main content
        </SkipNavLink>
        <header className="sn-header">
          <strong>Acme</strong>
          <nav aria-label="Example site" className="sn-nav">
            <a href="#" onClick={stay}>
              Home
            </a>
            <a href="#" onClick={stay}>
              Pricing
            </a>
            <a href="#" onClick={stay}>
              Docs
            </a>
          </nav>
        </header>
        <section id="skip-demo-main" className="sn-main" aria-labelledby="skip-demo-title">
          <h2 id="skip-demo-title">Dashboard</h2>
          <p className="muted">The first Tab shows the link; Enter moves the focus here.</p>
          <a href="#" onClick={stay}>
            Open a report
          </a>
        </section>
      </div>
    </div>
  )
}
Not measured yetTests passing in React
3.5 KiBSize
Not measured yetMemory
Not measured yetSpeed
[N] commits

Installation

npm i @hamur/react

Not on npm yet. The package goes out under the chosen name.

Anatomy

Import the component and assemble its parts:

<SkipNavLink />

Examples

The same link in a scrolling frame with a sticky header; scroll-margin-top on the target keeps its heading below the header after the jump.

SkipNavLinkSticky.tsx
import type { MouseEvent } from 'react'
import { SkipNavLink } from '@hamur/react/skip-nav-link'

const stay = (event: MouseEvent) => event.preventDefault()

export default function SkipNavLinkStickyExample() {
  return (
    <div className="stack">
      <div className="sn-frame sn-scroll">
        <SkipNavLink className="sn-link" targetId="skip-sticky-main">
          Skip to main content
        </SkipNavLink>
        <header className="sn-header sn-sticky">
          <strong>Acme</strong>
          <nav aria-label="Example site" className="sn-nav">
            <a href="#" onClick={stay}>
              Home
            </a>
            <a href="#" onClick={stay}>
              Pricing
            </a>
            <a href="#" onClick={stay}>
              Docs
            </a>
          </nav>
        </header>
        <div className="sn-filler">
          <p className="muted">Announcements, a banner and a long list sit before the content.</p>
          <p className="muted">The header stays on screen while this part scrolls.</p>
          <p className="muted">Without a scroll margin the target would slide under it.</p>
        </div>
        <section
          id="skip-sticky-main"
          className="sn-main sn-offset"
          aria-labelledby="skip-sticky-title"
        >
          <h2 id="skip-sticky-title">Dashboard</h2>
          <p className="muted">
            scroll-margin-top: 48px keeps this heading below the sticky header.
          </p>
          <a href="#" onClick={stay}>
            Open a report
          </a>
        </section>
      </div>
    </div>
  )
}

API reference

1 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.

A link hidden until it is focused that moves the focus to the main content when activated. Renders an <a>.

SkipNavLink props
PropTypeDefault
targetIdThe id of the element that receives focus when it is activated.string'main-content'

Shared props

Every part that renders an element takes these.

Shared props
PropType
classNameCSS class applied to the element, or a function that returns a class based on the component's state.string | ((state: State) => string | undefined)
styleStyle applied to the element, or a function that returns a style object based on the component's state.React.CSSProperties | ((state: State) => React.CSSProperties | undefined)
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State>

Keyboard and accessibility

Keyboard

Keyboard
KeysDoesPart
TabReaches the link as the first stop of the page and reveals itSkipNavLink
EnterMoves the focus to the target, scrolls it to the start and leaves the URLSkipNavLink

Accessibility

What the parts set
PartSetsSo that
SkipNavLinkhref, the hidden style until revealed, data-revealed while revealed; no role and no ARIAthe link is a link in the tab order that a keyboard user sees when it has focus

Focus order

  1. The link is the first stop; it is the first focusable element in body.
  2. After Enter, the target holds the focus, and the next Tab goes to the first control inside it, not to the header again.
  3. A focusable control inside the target keeps its own focus ring; only a target made focusable for the jump hides it.