Overview
TheScrollToTop component is a floating action button that appears after scrolling, allowing users to quickly return to the top of the page with a smooth scrolling animation.
Features
- Scroll-Based Visibility: Appears after scrolling 400px down the page
- Smooth Scrolling: Uses smooth scroll behavior when clicked
- Smooth Animations: Fade and slide animations for appearance/disappearance
- Hover Effects: Scale and color change effects on hover
- Fixed Positioning: Stays centered at the bottom of the viewport
- Accessible: Includes proper
aria-labelfor screen readers
Component Structure
The ScrollToTop component is a client-side component that doesn’t accept any props.Implementation
Basic Usage
State Management
The component manages a single piece of state:visible: Controls whether the button is shown based on scroll position
Scroll Detection
The component listens for scroll events and shows the button after 400px:Scroll to Top Function
When clicked, the button smoothly scrolls to the top of the page:Button Implementation
Styling
Button Appearance
- Size: 40px x 40px
- Color: Brand blue (
#163594) - Shape: Circular with
rounded-full - Icon: Upward-pointing arrow SVG
Animation Classes
Visibility States
Visible
opacity-100: Fully visibletranslate-y-0: Original position- Pointer events enabled
Hidden
opacity-0: Invisibletranslate-y-10: Moved 40px downpointer-events-none: No interaction possible
Hover Effects
hover:scale-110: Grows to 110% sizehover:bg-blue-700: Darker blue background
Positioning
The button is centered horizontally at the bottom of the viewport:SVG Arrow Icon
The component uses an inline SVG for the upward arrow:Animation Duration
All transitions use a 300ms duration for quick, responsive animations:Accessibility
The button includes proper accessibility attributes:The button uses
pointer-events-none when hidden to prevent accidental clicks on the invisible element.Dependencies
This component has no external dependencies beyond React, making it lightweight and easy to integrate.
Design Tokens
Colors
- Background:
#163594(Brand blue) - Hover Background:
bg-blue-700 - Text: White
Timing
- Scroll Threshold: 400px
- Transition Duration: 300ms
- Scroll Behavior: Smooth
Sizes
- Button: 40px x 40px
- Icon: 20px x 20px
- Hover Scale: 110%
Customization Examples
Change Scroll Threshold
Change Button Color
Change Position
Instant Scroll (No Animation)
Change Button Size
Browser Support
The smooth scroll behavior is supported in all modern browsers. For older browsers, it falls back to instant scrolling.Scroll Behavior Support
- Chrome: 61+
- Firefox: 36+
- Safari: 15.4+
- Edge: 79+
Performance Considerations
The component uses efficient scroll event handling:- Event listener is properly cleaned up on unmount
- Simple boolean state toggle (no expensive calculations)
- CSS transitions handled by the browser
Source Code Location
/app/components/ScrollToTop.tsx
Related Components
- WhatsappButton - Companion floating button for WhatsApp contact