SunUI
Founding$79 lifetime updates till Jan 11Claim a seat
FreeScroll

Back To Top

Round button that appears after you scroll past a threshold and takes you back to the top, with a ring in the brand spot colour around it that fills as you read.

When to use it

  • Long category pages, search results and articles

Watch out

  • Keep it clear of chat widgets or cookie banners in the same corner
  • Turn off showProgress if scroll-progress is already on the page

Let your agent add it

One copy holds the install command, when to use it, the props and the design rules. Paste it into Claude Code, Cursor or Codex.

Install with the CLI

Uses the shadcn CLI. The source is copied into your project, so you own every line.

$ npx shadcn@latest add https://sunui.synvize.com/r/back-to-top.json

Or install manually

  1. 1. Install the dependencies.
    $ npm install clsx tailwind-merge framer-motion
  2. 2. Add a cn helper at lib/utils.ts if you do not have one. Not using shadcn/ui? Also add sunui-theme.css for the colour tokens (how). Then copy the component.
    back-to-top.tsx
    "use client";
    
    import { AnimatePresence, motion, useMotionValueEvent, useReducedMotion, useScroll } from "framer-motion";
    import { useState, type RefObject } from "react";
    
    import { cn } from "@/lib/utils";
    
    const springs = {
      snappy: { type: "spring", stiffness: 520, damping: 34 },
      gentle: { type: "spring", stiffness: 240, damping: 30 },
    } as const;
    
    export interface BackToTopProps {
      /** Pass this when used inside a scrollable container. Defaults to the window */
      container?: RefObject<HTMLElement | null>;
      /** How many px must be scrolled before it appears */
      threshold?: number;
      /** Shows reading progress on the outer ring */
      showProgress?: boolean;
      /** fixed: pinned to a window corner. contained: pinned to the corner of the nearest relative container (for a scrollable container, wrap it in sticky) */
      position?: "fixed" | "contained";
      label?: string;
      className?: string;
    }
    
    /** Brand accent color: follows the theme's --primary by default, and buyers can set --sunui-spot to use their own brand color. Used only for selected, deal and current-progress states */
    const spot = "text-[color:var(--sunui-spot,var(--primary))]";
    
    const R = 21;
    const C = 2 * Math.PI * R;
    
    export function BackToTop({ container, threshold = 400, showProgress = true, position = "fixed", label = "Back to top", className }: BackToTopProps) {
      const reduce = useReducedMotion();
      const { scrollY, scrollYProgress } = useScroll(container ? { container, ...{ layoutEffect: false } } : undefined);
      const [shown, setShown] = useState(false);
      const [progress, setProgress] = useState(0);
    
      useMotionValueEvent(scrollY, "change", (y) => setShown(y > threshold));
      useMotionValueEvent(scrollYProgress, "change", (p) => setProgress(p));
    
      const toTop = () => {
        const target = container?.current ?? window;
        target.scrollTo({ top: 0, behavior: reduce ? "auto" : "smooth" });
      };
    
      return (
        <AnimatePresence>
          {shown && (
            <motion.button
              type="button"
              aria-label={label}
              onClick={toTop}
              initial={reduce ? { opacity: 0 } : { opacity: 0, y: 16 }}
              animate={{ opacity: 1, y: 0, transition: reduce ? { duration: 0 } : springs.gentle }}
              exit={reduce ? { opacity: 0, transition: { duration: 0 } } : { opacity: 0, y: 16, transition: { duration: 0.15 } }}
              whileTap={reduce ? undefined : { scale: 0.94, transition: springs.snappy }}
              className={cn(
                "z-40 grid size-12 place-items-center rounded-full border border-foreground/10 bg-card text-foreground outline-offset-2 focus-visible:outline-2 shadow-[0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]",
                position === "fixed" ? "fixed bottom-6 right-6" : "absolute bottom-4 right-4",
                className,
              )}
            >
              {showProgress && (
                <svg viewBox="0 0 48 48" className="absolute inset-0 size-full -rotate-90" aria-hidden="true">
                  <circle cx="24" cy="24" r={R} className={spot} fill="none" stroke="currentColor" strokeWidth="1.5" strokeDasharray={C} strokeDashoffset={C * (1 - progress)} strokeLinecap="round" />
                </svg>
              )}
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className="relative size-5" aria-hidden="true">
                <path d="M12 19V5M6 11l6-6 6 6" />
              </svg>
            </motion.button>
          )}
        </AnimatePresence>
      );
    }