Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 3x 3x 486x 486x 486x 486x | import React from "react";
import { prefersReducedMotion, useReveal } from "./useReveal";
/**
* Fades and rises its children into view the first time they are scrolled to.
*
* Five pages declared their own `Reveal` and `Home.tsx` a sixth as
* `RevealSection`. They were the same component: the only differences were
* how far the content travelled (20–40px) and how long it took (0.5–0.7s),
* and those differences tracked how large the revealed block was. So they
* became a named `size` rather than six copies:
*
* | `size` | Travel | Duration | For |
* |---|---|---|---|
* | `sm` | 20px | 0.5s | a paragraph or a list item |
* | `md` (default) | 28px | 0.6s | a card or a block of copy |
* | `lg` | 32px | 0.65s | a group of cards |
* | `xl` | 40px | 0.7s | a full page section |
*
* Stagger a list by feeding the index into `delay`:
*
* ```tsx
* {items.map((item, i) => (
* <Reveal key={item.id} delay={i * 0.1}>
* <Card item={item} />
* </Reveal>
* ))}
* ```
*
* Respects `prefers-reduced-motion`: the content renders in place, with no
* transition. None of the six copies did, which meant a viewer who had asked
* the OS for less motion got the full travel-and-fade on every section.
*/
export type RevealSize = "sm" | "md" | "lg" | "xl";
const SIZES: Record<RevealSize, { distance: number; duration: number }> = {
sm: { distance: 20, duration: 0.5 },
md: { distance: 28, duration: 0.6 },
lg: { distance: 32, duration: 0.65 },
xl: { distance: 40, duration: 0.7 },
};
export interface RevealProps {
children: React.ReactNode;
/** Seconds to wait before starting — stagger a list with `i * 0.1`. */
delay?: number;
size?: RevealSize;
/** How much of the element must be in view before it reveals, 0–1. */
threshold?: number;
className?: string;
}
export const Reveal: React.FC<RevealProps> = ({
children,
delay = 0,
size = "md",
threshold = 0.1,
className = "",
}) => {
const { ref, visible } = useReveal(threshold);
const { distance, duration } = SIZES[size];
const still = prefersReducedMotion();
return (
<div
ref={ref}
className={className}
style={{
opacity: visible ? 1 : 0,
transform: visible ? "translateY(0)" : `translateY(${distance}px)`,
transition: still
? undefined
: `opacity ${duration}s ease ${delay}s, transform ${duration}s ease ${delay}s`,
}}
>
{children}
</div>
);
};
export default Reveal;
|