Skip to content
Tutorials Center
My learning

Topics

Lesson 126 of 132 ยท Projects

Project: Portfolio Site with Animations

Build a React portfolio site with a sticky nav, active-section tracking, scroll reveal, parallax, reduced-motion support and animated routes, all without a library.

A portfolio site is the project almost every developer builds, and it is a good place to practise animation that serves the content. You will not need an animation library: CSS transitions, IntersectionObserver and one scroll listener are enough for the effects people actually use, and they are cheap for the browser to run.

You will build the site in three stages. First comes the layout, with a sticky navigation bar that highlights the section you are reading. Then you will add scroll-triggered reveals and a parallax hero, both respecting the visitor's "reduce motion" setting. Finally you will add routes with an enter animation for project pages. You will use effects and cleanup, refs, custom hooks and the ideas from the animation lesson and client-side routing.

Animation principles for a portfolio

  • Animate opacity and transform only. They can be handled by the GPU. Animating top, height or margin forces layout work on every frame.
  • Let CSS do the tweening. React changes a value (a class or a style); the browser animates between the old and new look. React does not need to render 60 times a second for a fade.
  • Keep scroll work out of state. A scroll listener that calls setState on every event re-renders the component constantly. Writing straight to an element's style through a ref avoids that.
  • Respect prefers-reduced-motion. Some people get dizzy or distracted by movement, so animations should turn off for them. Content must never depend on an animation to appear.

Stage 1: layout and the active section

The page is data-driven: a list of projects rendered with map. For the nav we want to know which section is in the middle of the screen. Polling scroll positions is clumsy; IntersectionObserver tells us when an element crosses a boundary. A rootMargin of -40% 0px -55% 0px shrinks the observed area to a thin band near the middle of the viewport, so "active" means "the section crossing the middle".

The hook subscribes to an outside system, so it is an effect with a cleanup that calls observer.disconnect(). The list of ids is a constant outside the component, so the effect runs once.

Stage 1: layout and active nav link

Scroll the preview and watch the underlined link change. Two details: aria-current marks the active link for assistive technology and doubles as the CSS hook, so there is no extra class to keep in sync. And the sticky header is plain CSS, with no JavaScript involved. Whenever CSS can do the job, let it.

Stage 2: reveal on scroll, parallax and reduced motion

Three small building blocks:

  1. usePrefersReducedMotion reads a media query. The value lives outside React (in the browser), so useSyncExternalStore is the right hook (external stores). Its snapshot is a boolean, which compares by value.
  2. Reveal wraps content, starts it invisible and slightly lower, and flips it to visible the first time it enters the viewport. It then disconnects the observer, because the effect is done. With reduced motion, it is simply always visible.
  3. Parallax moves a background layer at 40% of the scroll speed. The scroll handler writes style.transform through a ref and is throttled with requestAnimationFrame, so React never re-renders.
Stage 2: reveal, parallax, reduced motion

Scroll slowly: the header background moves slower than the page and each skill fades up as it arrives. To test reduced motion, switch on "reduce motion" in your operating system's accessibility settings and reload; everything is visible immediately and nothing moves.

Notice that each effect returns a cleanup: observer.disconnect() and removeEventListener plus cancelAnimationFrame. Without them, observers and listeners would pile up, especially in development where components mount repeatedly (effect cleanup).

Stage 3: routes and enter animations

Now give projects their own pages. The router is the same hash router as in the blog project. Because the hash now carries the route (#/projects/2), in-page anchors like #about are no longer available, so we scroll with scrollIntoView from a button instead.

The enter animation uses a CSS @keyframes rule. The trick is key: wrapping the page in <main key={path}> makes React replace the element whenever the route changes, so the browser plays the animation again for the new page (reconciliation). An effect scrolls to the top and updates document.title for each page.

Stage 3: animated routes

Click between pages: each one fades up because key={path} makes React create a fresh <main>. The CSS media query in the stylesheet turns the animation off for visitors who asked for less motion. This pairs well with the transitions you will meet in transitions when route changes load data.

Ideas to extend it

  • Add a contact form whose submit handler prints the entered message on the page (no real server in the preview).
  • Combine the stages: reveal the project cards from stage 2 on the home page of stage 3.
  • Add a dark-mode toggle that switches CSS custom properties on the root element.
  • Measure it: run the finished site through the profiling lesson tools and check there are no re-renders while you scroll.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which CSS properties are best to animate for smooth effects?

    Opacity and transform can be composited by the GPU without recalculating layout on every frame. The other properties trigger layout work.
  2. 2. Why does the parallax scroll handler write to ref.current.style instead of calling setState?

    High-frequency events should not trigger renders when no JSX depends on the value. A ref lets you update the element directly while React stays idle.
  3. 3. What does key={path} on the main element achieve?

    A changed key makes React unmount the old element and mount a new one. A newly inserted element runs its CSS animation from the start.
  4. 4. How should the site treat visitors with prefers-reduced-motion: reduce?

    Content must always be reachable. For reduced motion, reveal effects become instant and parallax and page animations switch off.

Summary

  • Animate opacity and transform with CSS transitions or keyframes; React only changes the values or classes.
  • Use IntersectionObserver for active sections and reveal effects, and always disconnect it in the cleanup.
  • Keep scroll-linked effects out of state: write to a ref's style inside a requestAnimationFrame-throttled listener.
  • Read prefers-reduced-motion with useSyncExternalStore and make every animation optional.
  • A changing key replays enter animations on route changes; hash routes need scrollIntoView instead of #anchor links.

Next: Practice Problems: Components, Props and State