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

next-scroll-to-id

v1.0.2

Published

this package will allow user to scroll certain id with option of bunch of customization props

Downloads

16

Readme

next-scroll-to-id

A simple React hook for smoothly scrolling to specific elements by their ID in a Next.js application. It also supports scrolling to the top of the page and provides customizable behavior, such as scroll offsets, delays, and completion callbacks.

Features

  • Smooth scrolling to any element by its ID.
  • Configurable scroll behavior: offsets, delays, and behavior options.
  • Optional scroll-to-top functionality.
  • Easy integration in any Next.js project.
  • Customizable scrolling with callbacks to trigger actions once scrolling is complete.

Installation

Install the package via npm or yarn:

npm install next-scroll-to-id

Or with yarn:

yarn add next-scroll-to-id

Demo

Check out a live demo of the package in action:

Live Demo

Usage

Below is a complete example of how to use the useSmoothScroll hook to scroll to specific sections of a page or scroll back to the top.

Example Usage

// Import required dependencies
import React, { useState } from "react";
import useSmoothScroll from "next-scroll-to-id";
import { GoMoveToTop } from "react-icons/go";

const LandingPage = () => {
  // Manage the scrollToId state, which determines which section to scroll to
  const [scrollToId, setScrollToId] = useState(null);

  // Get the scroll-to-top method from the hook
  const { triggerScrollToTop } = useSmoothScroll({
    scrollToId, // The target element to scroll to (specified by its ID)
    offset: -50, // Optional: Adjust the final scroll position by -50px
  });

  return (
    <div className="relative w-[100%]">
      {/* Navigation bar to trigger scrolling */}
      <nav className="flex sticky top-0 z-[10] gap-[1rem] justify-center items-center w-full h-[4rem] bg-black text-white">
        <li
          onClick={() => setScrollToId("div-1")}
          className="capitalize list-none cursor-pointer"
        >
          div-1
        </li>
        <li
          onClick={() => setScrollToId("div-2")}
          className="capitalize list-none cursor-pointer"
        >
          div-2
        </li>
        <li
          onClick={() => setScrollToId("div-3")}
          className="capitalize list-none cursor-pointer"
        >
          div-3
        </li>
        <li
          onClick={() => setScrollToId("div-4")}
          className="capitalize list-none cursor-pointer"
        >
          div-4
        </li>
        <li
          onClick={() => setScrollToId("div-5")}
          className="capitalize list-none cursor-pointer"
        >
          div-5
        </li>
      </nav>

      {/* Content sections to scroll to */}
      <div
        id="div-1"
        className="flex justify-center items-center text-white text-[3rem] bg-gray-500 w-full h-[50vh]"
      >
        <h2>Div -1</h2>
      </div>
      <div
        id="div-2"
        className="flex justify-center items-center text-white text-[3rem] bg-green-500 w-full h-[50vh]"
      >
        <h2>Div -2</h2>
      </div>
      <div
        id="div-3"
        className="flex justify-center items-center text-white text-[3rem] bg-pink-500 w-full h-[50vh]"
      >
        <h2>Div -3</h2>
      </div>
      <div
        id="div-4"
        className="flex justify-center items-center text-white text-[3rem] bg-yellow-500 w-full h-[50vh]"
      >
        <h2>Div -4</h2>
      </div>
      <div
        id="div-5"
        className="flex justify-center items-center text-white text-[3rem] bg-orange-500 w-full h-[50vh]"
      >
        <h2>Div -5</h2>
      </div>

      {/* Scroll to top button */}
      <div
        onClick={triggerScrollToTop}
        title="Go to top"
        className="cursor-pointer fixed flex justify-center items-center text-[1.5rem] bottom-[2rem] right-[2rem] text-black bg-white size-[3rem] rounded-full"
      >
        <GoMoveToTop />
      </div>
    </div>
  );
};

export default LandingPage;

Hook Options

The useSmoothScroll hook accepts the following options:

| Option | Type | Default | Description | | ------------------ | ---------------- | ----------- | --------------------------------------------------- | | scrollToId | string | '' | The ID of the element to scroll to. | | offset | number | -100 | Additional offset for the scroll position. | | behavior | ScrollBehavior | 'smooth' | Scroll behavior: 'smooth' or 'auto'. | | enabled | boolean | true | Enables or disables the scroll action. | | delay | number | 0 | Delay before starting the scroll (in milliseconds). | | onScrollComplete | () => void | undefined | Callback invoked once the scroll action completes. |

Methods

The hook provides a method that can be triggered directly:

  • triggerScrollToTop: Scrolls to the top of the page with smooth behavior by default.

Customization

You can adjust the scrolling behavior by modifying options like offset, behavior, or delay to suit your needs. The onScrollComplete callback allows you to trigger additional logic (e.g., analytics tracking or UI updates) after the scroll completes.