On this page

The <nys-onthispage> component builds a list of links from the headings on a page. Users can jump to a section, and the link for the section in view is highlighted as they scroll. It doesn't render with fewer than two trackable headings.

Apply for a permit

Eligibility

You must be a New York State resident aged 18 or older.

How to apply

Complete the online form and upload your supporting documents.

Required documents

Bring a valid proof of identity and proof of address.

What happens next

We review applications within 10 business days.

Copy Code
<div class="nys-grid-container">
  <div class="nys-grid-row nys-grid-gap">
    <aside class="nys-desktop:nys-grid-col-auto nys-order-first">
      <nys-onthispage></nys-onthispage>
    </aside>

    <main class="nys-desktop:nys-grid-col-fill">
      <h3>Apply for a permit</h3>

      <h2>Eligibility</h2>
      <p>You must be a New York State resident aged 18 or older.</p>

      <h2>How to apply</h2>
      <p>Complete the online form and upload your supporting documents.</p>

      <h2>Required documents</h2>
      <p>Bring a valid proof of identity and proof of address.</p>

      <h2>What happens next</h2>
      <p>We review applications within 10 business days.</p>
    </main>

  </div>
</div>

Can't use NYS Design System web components in your project? Try using the CSS Variables instead.

Accessibility

The <nys-onthispage> component includes the following accessibility features:

  • Links are wrapped in a <nav> landmark that is labeled by the visible heading

  • The link for the active section has aria-current="location"

  • Clicking a link moves focus to the target heading, so keyboard and screen reader users land in the right section

  • The target heading gets a temporary tabindex="-1" so it can take focus. It's removed on blur

  • Smooth scrolling is turned off automatically for users with prefers-reduced-motion enabled

  • Every link is a real anchor with an href that points to the heading id

  • Place the component before <main> in the source so reading and tab order match its position above the content on mobile and tablet. Use the grid and order classes to move it to the side on desktop

    Additional accessibility test and build content is coming soon, in a new format.

Options

Placement matters, especially on mobile and tablet. See Page layout before you build.

How the list is generated

The component creates the list from your page's headings and updates it as they change. It skips headings that are hidden, empty, inside a <nys-accordion>, or marked with data-nys-onthispage-ignore.

Scope

Headings inside <main> are tracked by default. Set scope to a CSS selector, like #guide, to track a different container. If it matches more than one element, the first is used.

Apply for a permit

Eligibility

You must be a New York State resident aged 18 or older.

How to apply

Complete the online form and upload your supporting documents.

Copy Code
<nys-onthispage scope="#guide"></nys-onthispage>

<article id="guide">
  <h2>Eligibility</h2>
  <h2>How to apply</h2>
</article>

Heading level

h2 headings are tracked by default. Set headingTracker to h3 through h6 to track a different level. h1 is never tracked.

This is a <h3> tag

This is another <h3> tag

This is also a <h3> tag

This is a <h4> tag

Copy Code
<nys-onthispage headingTracker="h3"></nys-onthispage>

Excluding a heading

Add data-nys-onthispage-ignore to a heading, to leave it out of the navigation.

Eligibility

External resources

How to apply

Copy Code
<nys-onthispage></nys-onthispage>

<main>
  <h1>Eligibility</h1>

  <div>
    <h2 data-nys-onthispage-ignore>Related links</h2>
    <div>
      <h2>External resources</h2>
    </div>
  </div>

  <h2>How to apply</h2>
</main>

Page layout

On desktop, place the component in a side column (for left-to-right languages):

  • Left by default.
  • Right if the site has vertical navigation. Keep it on the right on every page, even when the vertical navigation isn't shown.

Don't hide it on small screens. Put each side navigation in its own <aside> and place the asides before <main> so they sit above the content. Add nys-desktop:nys-order-last to the "on this page" <aside> to move it to the right on desktop.


On mobile and tablet, the navigation stacks above the content. On desktop, it moves to a side column. Use the order utility classes to set its visual position.

Copy Code
<h1>Welcome to the Empire State of Mind</h1>

<div class="nys-grid-container">
  <div class="nys-grid-row nys-grid-gap">
    <aside class="nys-desktop:nys-grid-col-2 nys-order-first">
      <nys-verticalnav heading="NYS Info" headingLevel="h2" >
        <ul>
          <li><a href="/">Home</a></li>
          <li><a href="/explore">About</a></li>
          <li>
            <h3>Season Explore</h3>
            <ul>
              <li><a href="">Spring</a></li>
              <li><a href="">Summer</a></li>
              <li><a href="">Fall</a></li>
              <li><a href="">Winter</a></li>
            </ul>
          </li>
        </ul>
      </nys-verticalnav>
    </aside>

    <aside class="nys-desktop:nys-grid-col-2 nys-order-first nys-desktop:nys-order-last">
      <nys-onthispage offset="24" scope="#onthispage-layout"></nys-onthispage>
    </aside>

    <main id="onthispage-layout" class="nys-desktop:nys-grid-col-fill">
      <h4>Apple picking season</h4>
      <p>Orchards from the Hudson Valley to the Finger Lakes are open.</p>
      <h4>Pizza by the slice</h4>
      <p>Fold it. Do not eat it with a fork!</p>
      <h4>Road trip to Niagara Falls</h4>
      <p>Pack snacks and enjoy one of the most beautiful natural wonders in the world.</p>
    </main>

  </div>
</div>

Sticky behavior

The component is already sticky. Place it directly inside the <aside>, without a wrapper <div>, which has no extra height for it to stick in. You don't need to add position: sticky.

Disable Sticky

The navigation is sticky by default and stays at the top of the viewport as the user scrolls. This only applies on viewports 480px or wider. Set disableSticky to let it scroll with the page.

Eligibility

You must be a New York State resident aged 18 or older.

How to apply

Complete the online form and upload your supporting documents.

Copy Code
<nys-onthispage disableSticky></nys-onthispage>

Offset

offset is the distance in pixels from the top of the viewport to the trigger line. A heading becomes active when its top edge reaches this line. It also sets scroll-margin-top on tracked headings and top on the sticky navigation. The default is 80.

Increase offset if the page has a fixed header, so headings don't end up behind it after a click.

Custom heading

Set heading to change the title above the list. The default is "On this page".

Eligibility

You must be a New York State resident aged 18 or older.

How to apply

Complete the online form and upload your supporting documents.

What happens next

We review applications within 10 business days.

Copy Code
<nys-onthispage heading="In this guide"></nys-onthispage>

Heading ids

Links point to the heading id. If a heading has none, one is generated from its text, like #how-to-apply. Duplicates get a number, like #how-to-apply-2. Ids you set are never changed.

Copy Code
<h2 id="eligibility">Eligibility</h2>  <!-- #eligibility -->
<h2>How to apply</h2>                  <!-- #how-to-apply -->
<h2>How to apply</h2>                  <!-- #how-to-apply-2 -->

Updating the URL

Set hashBehavior to control what happens to the URL when a link is clicked.

  • none: leaves the URL alone (default)
  • hash: adds a history entry for each click
  • replace: updates the URL without adding a history entry
Copy Code
<nys-onthispage hashBehavior="replace"></nys-onthispage>

Smooth scrolling

Links scroll smoothly by default. Set disableSmoothScroll to jump straight to the section. Users with prefers-reduced-motion enabled always get instant scrolling.

Copy Code
<nys-onthispage disableSmoothScroll></nys-onthispage>

Usage

Do

  • Use as a table of contents on lengthy pages to give a structural overview and let users jump to a section.

  • Use on pages with at least three distinct sections, or content longer than three screen heights.

  • Keep the link order the same as the order of the section headings on the page.

  • Use "On this page" as the heading so the nav is clearly labeled and stands apart from other links.

  • Keep the link text identical to the heading it links to.

  • Let link text wrap to a second line instead of truncating it.

  • Clearly show the active link so users can track their position as they scroll or after they click a link.

  • On desktop, place it in the left sidebar. If vertical navigation is on the left, place it in the right sidebar.

  • On tablet and mobile, place it above the main content and below the page heading.

Don't

  • Don't use on pages with minimal content or a small number of sections, where scrolling isn't an issue.

  • Don't use to link to other pages. Use <nys-verticalnav> instead.

  • Don't use for heading levels lower than H3, such as H4, H5, or H6.

  • Don't shorten, rephrase, or summarize a heading for its link.

  • Properties

    Property Type Default
    id string "" (auto-generated)
    headingTracker "h2" | "h3" | "h4" | "h5" | "h6" "h2"
    heading string "On this page"
    scope string (CSS selector) "main"
    disableSticky boolean false
    offset number (px) 80
    disableSmoothScroll boolean false
    hashBehavior "none" | "hash" | "replace" "none"
    Attribute Used on Description
    data-nys-onthispage-ignore Heading (h2 to h6) or any container Leaves the heading, or every heading inside the container, out of the list.

    Style Overrides

    There are no existing CSS variables for this component. Explore existing options, or propose a new one with a Component Proposal.

    Events

    Some components emit nys- events with a detail object instead of relying on native events alone. Pick one approach per interaction. Don't mix native and nys- listeners for the same thing.

    The <nys-onthispage> component emits one custom event:

    Event details

    Name Description Return details
    nys-click Fired when a link is activated.
    • id (string): The id of the target heading.
    • label (string): The link text, taken from the heading text.
    • index (number): The zero-based position of the link in the list.

    You can listen to this event using JavaScript:

    Copy Code
    // Select the onthispage component
    const toc = document.querySelector("nys-onthispage");
    
    // Listen for link activation
    toc.addEventListener("nys-click", (event) => {
      const { id, label, index } = event.detail;
    
      /**
      * In the first example on this page, clicking "How to apply" logs:
      * Clicked "How to apply" (#how-to-apply, position 1)
      */
      console.log(`Clicked "${label}" (#${id}, position ${index})`);
    });

    This component has no dependencies on other NYSDS components.

    Suggest a New Component

    Do you have an idea for a new NYS Design System web component? Look through the existing proposals in our GitHub discussions board to see if someone already proposed something similar. If not, feel free to submit one.