Skip to main content

ScrollToTopButton Component

· 3 min read
Docux
Curious explorer, a bit of a mad experimenter, and a bit of a contributor.

Developer Development License: MIT

This component adds a fixed floating Back to Top button. It becomes visible after the user scrolls 300px vertically; on click it triggers a smooth scroll to the top while playing a short visual animation on the icon. It does not expose public props: customization (threshold, image) is done by editing the source (index.js). The goal is to keep it lightweight and easy to reuse.

Overview

Implemented features:

  • Appears after 300px scroll (hard‑coded condition)
  • Smooth scroll to top on click
  • CSS animation on click (fly + @keyframes flyUp)
  • Show/Hide using opacity + visibility
  • Scroll listener with cleanup in useEffect
  • Icon image can be swapped by changing the import

Component Code

src/components/ScrollToTopButton/index.js
import React, { useState, useEffect } from "react";
import clsx from "clsx";
import styles from "./styles.module.css";
import buttontop from '@site/static/img/buttontop.PNG'

export default function ScrollToTopButton() {
const [isVisible, setIsVisible] = useState(false);
const [fly, setFly] = useState(false);

useEffect(() => {
const toggleVisibility = () => {
if (window.scrollY > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
window.addEventListener("scroll", toggleVisibility);
return () => window.removeEventListener("scroll", toggleVisibility);
}, []);

const scrollToTop = () => {
setFly(true);
window.scrollTo({ top: 0, behavior: "smooth" });

setTimeout(() => setFly(false), 800);
};

return (
<div
className={clsx(
styles.scrollBtn,
isVisible && styles.show,
fly && styles.fly
)}
onClick={scrollToTop}
>
<img
src={buttontop}
alt="Retour en haut"
width="30"
height="30"
/>
</div>
);
}

Installation

  1. Copy the ScrollToTopButton folder into src/components/
  2. Import the component in your global layout (or a specific page)
  3. Ensure the image import works (/static/img/buttontop.PNG)
  4. (Optional) Replace the image with your own

File structure

src/components/ScrollToTopButton/
├── index.js # Main React component
└── styles.module.css # Scoped CSS Module

Configuration

  • Threshold: edit if (window.scrollY > 300) in index.js
  • Image: replace the imported buttontop image

Customization

Change the image

  1. Place your image inside static/img/
  2. Update the import in index.js:
import buttontop from '@site/static/img/your-image.png'
  1. The component uses it like:
<img
src={buttontop}
alt="Back to top"
width="30"
height="30"
/>

Edit styles

Open styles.module.css and adjust:

  • Position: tweak bottom / right
  • Size: adjust width / height
  • Visual: modify box-shadow
  • Animation: change flyUp keyframes

State & Behavior

  • isVisible: true when window.scrollY > 300, else false
  • fly: toggled to true on click, reset after 800ms
  • Scroll listener: handles visibility logic and is cleaned up on unmount

Animations

Show/Hide

transition: opacity 0.4s, visibility 0.4s;

Click animation (flyUp)

@keyframes flyUp {
0% {
transform: translateY(0) scale(1);
opacity: 1;
}
100% {
transform: translateY(-300px) scale(0.6);
opacity: 0;
}
}

Full example

import React from 'react';
import Layout from '@theme-original/Layout';
import ScrollToTopButton from '@site/src/components/ScrollToTopButton';

export default function LayoutWrapper(props) {
return (
<>
<Layout {...props} />
<ScrollToTopButton />
</>
);
}

Related posts

Retour en haut