Code Monkey home page Code Monkey logo

react-scroll-to-top's People

Contributors

hermannygaard avatar mciszczon avatar patotoma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-scroll-to-top's Issues

Scroll is not working with material ui

I am using React Material UI Dialog Component.

and the content Inside the dialog component was large. I enabled the react scroll to the top plugin for that dialog component.

but It's not working.

your suggestions and ideas are much appreciated.

Smooth scrolling

Hi there! Thanks for this cool little lib! I'm adding it to one of my projects right now and thought that having the smooth scroll feature would be great.

I see that you use:

document.documentElement.scrollTop = 0

in your code, but if you switched to using:

window.scrollTo({ top: 0, behavior: 'smooth' })

then it would work out of the box for most of the browers (here's a Can I Use page for this feature). It could be implemented as a opt-in feature (just adding a bool prop smooth would do the trick).

What do you think? I could make a PR of course if you want.

Feature request: ability to pass in an HTMLElement to use as reference, instead of window

Currently, the component always uses window to listen & react to scroll events.
In some applications (e.g., Next.js), what scrolls is actually a specific div (e.g., div id="__next" ...) in the case of Next.js.

It would be great to be able to pass in an HTML element for this component to listen/react to, and set scroll position of.

I've done the following for my project:

import React, { useState, useEffect } from "react";
import {FaArrowUp} from "react-icons/fa";

type ScrollToTopProps = React.HTMLAttributes<HTMLButtonElement> & {
  scrollingElement?: HTMLElement | undefined;
  top?: number;
  smooth?: boolean;
  icon?: React.ReactNode;
  ariaLabelText?: string;
};

function scrollToTop(smooth = false, scrollingElement: HTMLElement | undefined) {
  if(!scrollingElement) {
    if (smooth) {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
      return;
    }
    document.documentElement.scrollTop = 0;
  } else {
    if (smooth) {
      scrollingElement.scrollTo({
        top: 0,
        behavior: "smooth",
      });
      return;
    }
    scrollingElement.scrollTop = 0;
  }
}

const ScrollToTop = ({
                       scrollingElement,
                       top = 20,
                       className = "scroll-to-top",
                       icon,
                       ariaLabelText = "Scroll to top",
                       smooth = false,
                     }: ScrollToTopProps) => {
  const [visible, setVisible] = useState(false);
  const onScroll = () => {
    if(!scrollingElement) {
      setVisible(document.documentElement.scrollTop > top);
    } else {
      setVisible(scrollingElement.scrollTop > top);
    }
  };
  useEffect(() => {
    if(!scrollingElement) {
      document.addEventListener("scroll", onScroll);
    } else {
      //const scrollingElement = document.getElementById("__next")!;
      scrollingElement.addEventListener("scroll", onScroll);
    }

    // Remove listener on unmount
    return () => {
      if(!scrollingElement) {
        document.removeEventListener("scroll", onScroll);
      } else {
        scrollingElement.removeEventListener("scroll", onScroll);
      }
    }
  });

  return <>
      {visible && (
        <button
          className={className}
          onClick={() => scrollToTop(smooth, scrollingElement)}
          aria-label={ariaLabelText}
        >
          {icon || (
            <FaArrowUp />
          )}
        </button>
      )}
    </>;
};

export default ScrollToTop;

I then use it as follows:

<ScrollToTop smooth={true} scrollingElement={IS_BROWSER? document.getElementById("__next")!: undefined} className="scroll-to-top" icon={<FaArrowUp className="w-full h-full text-white" />} />

Compatibility Issue with Next.js Server Components

Issue Description:

I am encountering a compatibility issue with the react-scroll-to-top package when using it in conjunction with Next.js server components. The error message suggests a problem related to the use of useState in server components.

Steps to Reproduce:

  1. Include react-scroll-to-top in a Next.js project with server components.
  2. Attempt to use the useState hook provided by the react-scroll-to-top package in a server component.
  3. Observe the error: "⨯ useState only works in Client Components. Add the 'use client' directive at the top of the file to use it."

Expected Behavior:

The react-scroll-to-top package should work seamlessly with Next.js server components, and the useState hook should function as expected.

Additional Information:

  • Next.js version: 14.0.4
  • react-scroll-to-top version:3.0.0

Please let me know if there are any suggested workarounds or if there is an estimated timeline for resolving this compatibility issue.

Thank you for your attention to this matter.

Does not work on react material UI Dialog Component

I am using React Material UI Dialog Component.

and the content Inside the dialog component was large. I am enabled the react scroll to top plugin for that dialog component.

but Its not working.

your suggestions and ideas are much appreciated

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.