npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@parsonic/back-to-top

v1.3.1

Published

Scroll back to top of page web component

Readme

Back To Top

A web component for scrolling back to the top of the page.

Install

npm install --save @parsonic/back-to-top

Usage

Use copy to clipboard with your favourite bundler or directly from a CDN. A minified build is provided as min.js with a source map.

Quick start

Add a script tag with the minified build and use the element in your page.

<script
  defer
  src="https://cdn.jsdelivr.net/npm/@parsonic/back-to-top/min.js"></script>
<back-to-top data-scroll-behavior="smooth"></back-to-top>

Bundler

Import the BackToTop component at the root of your application and register it.

import BackToTop from '@parsonic/back-to-top/BackToTop.js'

BackToTop.register()

// Use <back-to-top></back-to-top> in your page or components

CDN

Import the BackToTop component from the CDN and register it before using.

<script type="module">
  import BackToTop from 'https://cdn.jsdelivr.net/npm/@parsonic/back-to-top/BackToTop.js'

  BackToTop.register()
</script>

<back-to-top></back-to-top>

If you prefer to give the element an alternative tag name you can pass this to the register method.

// To use as <my-back-to-top></my-back-to-top>
BackToTop.register('my-back-to-top')

Scroll behavior

The scroll behavior when the button is clicked can be set by specifying the data-scroll-behavior attribute.

<back-to-top data-scroll-behavior="smooth"></back-to-top>

Alternatively, the setting for scroll-behavior can be set via CSS for the document or container being scrolled.

html {
  scroll-behavior: smooth;
}

Appearance

The button will appear once the container has scrolled past a threshold and the user starts to scroll back up the page or container. The default threshold is 500px and this can be overridden using the data-threshold attribute.

<back-to-top data-threshold="0"></back-to-top>

Scrolling inside a container element

The back to top button may also be used when scrolling within an element by providing the element id in the data-scroll-container attribute. If the button needs to be aligned to the element then update the default positioning with CSS.

<style>
  .page {
    height: 800px;
    position: relative;
  }

  #container {
    height: 100%;
    overflow: auto;
  }

  back-to-top {
    position: absolute;
  }
</style>

<div class="page">
  <div id="container">
    <!--- long form content --->
  </div>
  <back-to-top data-scroll-container="container"></back-to-top>
</div>

Focusing an element

To apply focus to a focusable element when the scroll button is clicked, add an id to the element and pass this id as the data-focus-target attribute.

<div class="page">
  <div id="container">
    <h2><a id="section-title" href="#section-title">Section name</a></h2>
    <!--- long form content --->
  </div>
  <back-to-top
    data-scroll-container="container"
    data-focus-target="section-title"></back-to-top>
</div>

[!NOTE] focus-visible styles will not be applied to the target when focused and applying this via the focus method is not currently supported. See options.focusVisible

Customising the button

The default button comprises of a button element with an SVG icon. A default ARIA label of Back to top is applied to the button and can be overridden using the data-button-label attribute.

<back-to-top data-button-label="Back to top"></back-to-top>

The button can be replaced entirely using the default slot or styled via the button css part.

<style>
  back-to-top::part(button) {
    background-color: rgb(0 0 0 / 40%);
  }
</style>

<back-to-top>
  <button>Back 👆</button>
</back-to-top>

To style the icon inside the button use the icon css part to target the SVG elements.

back-to-top::part(icon) {
  stroke: black;
}

To replace the icon use the icon slot.

<back-to-top>
  <span slot="icon">👆</span>
</back-to-top>

The button may also be styled using the CSS custom properties listed below and via. the data-state attribute which will have one of two states, active or inactive depending on whether the button should be visible or not.

:root {
  --btt-button-background: rgb(0 0 0 / 60%);
  --btt-button-background-hover: rgb(0 0 0 / 80%);
  --btt-button-border: none;
  --btt-button-color: white;
  --btt-button-inset: auto 2rem 2rem auto;
  --btt-button-padding: 0.5rem;
  --btt-button-radius: 999px;
  --btt-button-size: 1.5rem;
}