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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@murtuzaalisurti/back-to-top

v3.0.4

Published

<back-to-top> web component by Murtuzaali Surti

Downloads

8

Readme

<back-to-top>

A <back-to-top> button web component with throttle support. Fallbacks to anchor link.

<back-to-top throttle="600">
    <!-- 👇 fallback anchor link (v3.0) -->
    <a href="#" class="back-to-top-fallback" style="position: fixed; left: 1rem; bottom: 2rem;">back-to-top</a>
    <!-- 👇 insert button content here -->
    <template>
        button content here
    </template>
</back-to-top>
.back-to-top, a.back-to-top-fallback {
    bottom: 3rem;
    left: 2rem;
    width: 3rem;
    height: 3rem;
    border: none;
    border-radius: 50%;
    z-index: 10;
    cursor: pointer;
    transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
}
a.back-to-top-fallback {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: lightgray;
}
a.back-to-top-fallback svg, .back-to-top svg {
    height: 70%;
}

Installation

You can use it as an npm package or from a CDN.

1. NPM

  • Install the package
npm i @murtuzaalisurti/back-to-top
  • Import the package in a JS module using:
import "@murtuzaalisurti/back-to-top"
// ... some other code
  • And refer to that file using a script tag such as:
<script src="./your-js-module" type="module"></script>

2. CDN

Grab the js file from a CDN using a script tag such as:

<script type="module" src="https://www.unpkg.com/@murtuzaalisurti/back-to-top@latest/public/main.js"></script>

Usage

With the js script in place, just add this component in your HTML.

<back-to-top throttle="600"></back-to-top>

Fallback Anchor Link (v3.0)

With version 3.0 you can specify a fallback anchor link which is useful when javascript can't execute.

<back-to-top throttle="600">
    <!-- 👇 fallback anchor link (v3.0) -->
    <a href="#" class="back-to-top-fallback" style="position: fixed; left: 1rem; bottom: 2rem;">back-to-top</a>
</back-to-top>

From 3.0.1, you need to specify the back-to-top-fallback class for the fallback element if your fallback element is other than an anchor tag.

Customizable Button Content

You can now specify the button content using a template element inside the back-to-top component.

<back-to-top throttle="600">
    <template>
        button content here
    </template>
</back-to-top>

You can style this component however you want (the .back-to-top class is automatically added to the button for you), here are some styles to start with:

.back-to-top, a.back-to-top-fallback {
    bottom: 3rem;
    left: 2rem;
    width: 3rem;
    height: 3rem;
    border: none;
    border-radius: 50%;
    z-index: 10;
    cursor: pointer;
    transition: opacity 0.3s ease-in-out, visibility 0.3s ease-in-out;
}
a.back-to-top-fallback {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: lightgray;
}
a.back-to-top-fallback svg, .back-to-top svg {
    height: 70%;
}

Options

Introduced in v2.0, you can now use the throttle attribute to set a throttle rate for the back-to-top button. The value is in milliseconds.

<back-to-top throttle="600">
    <!-- 👇 fallback anchor link (v3.0) -->
    <a href="#" class="back-to-top-fallback" style="position: fixed; left: 1rem; bottom: 2rem;">back-to-top</a>
    <!-- 👇 insert button content here -->
    <template>
        button content here
    </template>
</back-to-top>

Event Throttling

  • This package uses lodash for throttling scroll events in order to show/hide the back-to-top button component.
  • It uses a custom lodash build generated from lodash include=throttle -p.
  • Throttle rate modification support introduced in v2.0

Demo

Try it on Codepen or, see it in action here https://murtuzaalisurti.github.io/back-to-top/.

Some other sites using this web component