hermannygaard / react-scroll-to-top Goto Github PK
View Code? Open in Web Editor NEWLightweight button component for scrolling to the top of a page
Home Page: https://www.npmjs.com/package/react-scroll-to-top
License: MIT License
Lightweight button component for scrolling to the top of a page
Home Page: https://www.npmjs.com/package/react-scroll-to-top
License: MIT License
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.
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.
The button is not appearing.
Need way to animate button disappearing
ADDED way to animate appear, but can't animate hiding
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" />} />
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:
react-scroll-to-top
in a Next.js project with server components.useState
hook provided by the react-scroll-to-top
package in a server component.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:
react-scroll-to-top
version:3.0.0Please 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.
Say for instance a user has scrolled to the bottom, clicks 'Save', and after save, one would like to scroll the person to the top automatically (without click). How can I do this?
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
Hi,
I'm very new to react, pardon the novice question. What parameters do I need to change to increase the bottom padding of the component?
Regards,
Willem
Component disappears when you add a className property
My designer sent me a branded SVG and this one won't do. Is there any way to change it?
svgPath is working
When using the ScrollToTop component, the arrow in the box isn't centred.
I didn't add any other code.
Repo, (index.tsx): https://github.com/YinChuRijnaard/Portfolio-v3 (exact commit: https://github.com/YinChuRijnaard/Portfolio-v3/commit/5cf0100670f6c2a84fcd8a5928ea8bc854956e96).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.