Project hover
Thumbnails float with the cursor.
Customize
<ProjectHoverSection projects={projects} />Changes show live and stay in the address, so a link keeps them.
Code
"use client";
import { useId, useRef, useState, useSyncExternalStore, type MouseEvent } from "react";
import { gsap } from "gsap";
import { useGSAP } from "@gsap/react";
// A list of projects. Hover a row and a photo grows at the cursor, follows it around and slides to
// that row's picture. Tab to a row and the photo shows up beside it, so keyboard users get it too.
// On touch screens there is no cursor, so each row opens like an accordion and shows its photo inline.
// The look comes from `currentColor`: the list takes the text colour of whatever it sits in.
export type ProjectHoverItem = {
/** The big name on the left of the row. */
title: string;
/** The small line on the right (a year, a discipline). */
subtitle: string;
/** The photo shown for this row. */
image: string;
/** Describes the photo for screen readers. Defaults to the title. */
alt?: string;
/** Makes the row a link. Without it the row is a button. */
href?: string;
};
export type ProjectHoverSectionProps = {
/** The rows, top to bottom. Three to eight look best. */
projects: ProjectHoverItem[];
/** Width of the floating photo, in pixels. */
thumbnailWidth?: number;
/** Height of the floating photo, in pixels. */
thumbnailHeight?: number;
/** Corner radius of the floating photo, in pixels. */
thumbnailRadius?: number;
/** How long the photo takes to catch up with the cursor, in seconds. */
followDuration?: number;
/** How the photo changes between rows: "slide" scrolls the strip, "fade" cross-fades. */
transition?: "slide" | "fade";
/** How far the title and subtitle of the hovered row move apart, in pixels. */
shift?: number;
/** The opacity of the hovered row, from 0 to 1. 1 leaves it alone. */
dimOpacity?: number;
/** The most the photo leans into the direction of the cursor, in degrees. 0 keeps it straight. */
tilt?: number;
/** Called when a row is clicked or pressed. */
onSelect?: (project: ProjectHoverItem, index: number) => void;
/** Extra classes for the outer box. It is as wide as its parent and as tall as the list. */
className?: string;
};
// A mouse (or a pen) that can hover. Everything else gets the tap-to-open accordion.
const FINE_POINTER = "(hover: hover) and (pointer: fine)";
function subscribeToPointer(onChange: () => void) {
const query = window.matchMedia(FINE_POINTER);
query.addEventListener("change", onChange);
return () => query.removeEventListener("change", onChange);
}
// The server and the first client render say "mouse", then this settles on the real answer.
// The layout itself is decided in CSS, so nothing jumps; this only drives the aria attributes.
function useTouchLayout() {
return useSyncExternalStore(
subscribeToPointer,
() => !window.matchMedia(FINE_POINTER).matches,
() => false,
);
}
export function ProjectHoverSection({
projects,
thumbnailWidth = 380,
thumbnailHeight = 250,
thumbnailRadius = 10,
followDuration = 0.4,
transition = "slide",
shift = 15,
dimOpacity = 0.5,
tilt = 5,
onSelect,
className = "",
}: ProjectHoverSectionProps) {
const boxRef = useRef<HTMLDivElement>(null);
const listRef = useRef<HTMLUListElement>(null);
const thumbRef = useRef<HTMLDivElement>(null);
const stripRef = useRef<HTMLDivElement>(null);
const [openIndex, setOpenIndex] = useState<number | null>(null);
const touchLayout = useTouchLayout();
const panelId = useId();
useGSAP(
() => {
const box = boxRef.current;
const list = listRef.current;
const thumb = thumbRef.current;
const strip = stripRef.current;
if (!box || !list || !thumb || !strip) return;
const rows = gsap.utils.toArray<HTMLElement>("[data-row]", list);
const slides = gsap.utils.toArray<HTMLElement>("[data-slide]", strip);
const last = rows.length - 1;
if (last < 0) return;
// matchMedia re-runs this block if the visitor plugs in a mouse or flips the motion setting
const mm = gsap.matchMedia();
mm.add({ fine: FINE_POINTER, calm: "(prefers-reduced-motion: reduce)" }, (context, contextSafe) => {
const { fine, calm } = context.conditions as { fine: boolean; calm: boolean };
if (!fine || !contextSafe) return;
// contextSafe returns a plain Function, so give the handler its own type back
const safe = <Handler extends (...args: never[]) => void>(handler: Handler) => contextSafe(handler) as Handler;
// The photo starts hidden, centred on its own middle so x and y are the cursor position.
// With reduced motion it never scales or travels: it just appears, and fades.
gsap.set(thumb, { xPercent: -50, yPercent: -50, scale: calm ? 1 : 0, autoAlpha: 0, force3D: true });
// Slide: pictures sit one below the other and the strip moves. Fade: they sit on top of each other.
gsap.set(slides, { yPercent: (i: number) => (transition === "slide" ? i * 100 : 0), autoAlpha: (i: number) => (transition === "slide" || i === 0 ? 1 : 0) });
gsap.set(strip, { yPercent: 0 });
const moveX = calm ? null : gsap.quickTo(thumb, "x", { duration: followDuration, ease: "power3.out" });
const moveY = calm ? null : gsap.quickTo(thumb, "y", { duration: followDuration, ease: "power3.out" });
const lean = calm || tilt === 0 ? null : gsap.quickTo(thumb, "rotation", { duration: 0.6, ease: "power3.out" });
// When the cursor stops, the photo straightens up
const straighten = lean ? gsap.delayedCall(0.08, () => lean(0)).pause() : null;
let shown = false;
let pointerInside = false;
let activeIndex = -1;
let lastClientX = 0;
const markActive = (index: number) => {
rows.forEach((row, i) => {
if (i === index) {
row.dataset.active = "true";
if (!calm) row.dataset.shift = "true";
} else {
delete row.dataset.active;
delete row.dataset.shift;
}
});
};
const showPicture = (index: number, instant: boolean) => {
if (transition === "slide") {
gsap.to(strip, { yPercent: -100 * index, duration: instant || calm ? 0 : 0.4, ease: "power2.out", overwrite: "auto" });
} else {
gsap.to(slides, { autoAlpha: (i: number) => (i === index ? 1 : 0), duration: instant || calm ? 0 : 0.4, ease: "power2.out", overwrite: "auto" });
}
};
// x and y are measured from the top left corner of the box the photo lives in
const reveal = (index: number, x: number, y: number) => {
const wasShown = shown;
if (!wasShown) {
gsap.set(thumb, { x, y });
// One call in the quickTo functions' own tween, so they start from here too
moveX?.(x);
moveY?.(y);
}
if (index !== activeIndex) showPicture(index, !wasShown);
activeIndex = index;
markActive(index);
shown = true;
gsap.to(thumb, { scale: 1, autoAlpha: 1, duration: calm ? 0.15 : 0.4, ease: "power2.out", overwrite: "auto" });
};
const hide = () => {
if (!shown) return;
shown = false;
activeIndex = -1;
markActive(-1);
lean?.(0);
gsap.to(thumb, { scale: calm ? 1 : 0, autoAlpha: 0, duration: calm ? 0.1 : 0.3, ease: "power2.out", overwrite: "auto" });
};
const toBox = (clientX: number, clientY: number) => {
const rect = box.getBoundingClientRect();
return { x: clientX - rect.left, y: clientY - rect.top };
};
const isMouse = (event: PointerEvent) => event.pointerType === "mouse" || event.pointerType === "pen";
const onListEnter = safe((event: PointerEvent) => {
if (isMouse(event)) pointerInside = true;
});
const onListMove = safe((event: PointerEvent) => {
if (!isMouse(event) || !shown) return;
const { x, y } = toBox(event.clientX, event.clientY);
if (moveX && moveY) {
moveX(x);
moveY(y);
} else {
gsap.set(thumb, { x, y });
}
if (lean && straighten) {
lean(gsap.utils.clamp(-tilt, tilt, (event.clientX - lastClientX) * 0.5));
straighten.restart(true);
}
lastClientX = event.clientX;
});
const onListLeave = safe((event: PointerEvent) => {
if (!isMouse(event)) return;
pointerInside = false;
hide();
});
const cleanups: (() => void)[] = [];
list.addEventListener("pointerenter", onListEnter);
list.addEventListener("pointermove", onListMove);
list.addEventListener("pointerleave", onListLeave);
cleanups.push(() => {
list.removeEventListener("pointerenter", onListEnter);
list.removeEventListener("pointermove", onListMove);
list.removeEventListener("pointerleave", onListLeave);
});
rows.forEach((row, index) => {
const onRowEnter = safe((event: PointerEvent) => {
if (!isMouse(event)) return;
lastClientX = event.clientX;
const { x, y } = toBox(event.clientX, event.clientY);
reveal(index, x, y);
});
// Keyboard focus: the photo appears beside the row. A mouse click also focuses a row,
// but then :focus-visible is false and the cursor is already driving.
const onRowFocus = safe(() => {
if (pointerInside || !row.matches(":focus-visible")) return;
const rect = row.getBoundingClientRect();
const { x, y } = toBox(rect.left + rect.width * 0.62, rect.top + rect.height / 2);
reveal(index, x, y);
moveX?.(x);
moveY?.(y);
});
const onRowBlur = safe((event: FocusEvent) => {
if (pointerInside || rows.includes(event.relatedTarget as HTMLElement)) return;
hide();
});
row.addEventListener("pointerenter", onRowEnter);
row.addEventListener("focus", onRowFocus);
row.addEventListener("blur", onRowBlur);
cleanups.push(() => {
row.removeEventListener("pointerenter", onRowEnter);
row.removeEventListener("focus", onRowFocus);
row.removeEventListener("blur", onRowBlur);
});
});
return () => {
cleanups.forEach((cleanup) => cleanup());
straighten?.kill();
markActive(-1);
};
});
return () => mm.revert();
},
{ scope: boxRef, dependencies: [projects.length, transition, followDuration, tilt], revertOnUpdate: true },
);
const handleClick = (event: MouseEvent<HTMLElement>, project: ProjectHoverItem, index: number) => {
onSelect?.(project, index);
if (!touchLayout) return;
// On touch the row opens its picture. A linked row gets a "View project" link inside the panel.
if (project.href) event.preventDefault();
setOpenIndex((current) => (current === index ? null : index));
};
return (
<div
ref={boxRef}
className={`relative w-full ${className}`}
style={{ "--phs-shift": `${shift}px`, "--phs-dim": dimOpacity } as React.CSSProperties}
>
<ul ref={listRef} className="m-0 mx-auto flex w-full max-w-[1000px] list-none flex-col border-b border-current/20 p-0">
{projects.map((project, index) => {
const open = touchLayout && openIndex === index;
const panelName = `${panelId}-${index}`;
const rowProps = {
"data-row": "",
className:
"group relative flex w-full cursor-pointer flex-col items-start gap-1 border-t border-current/20 bg-transparent px-4 py-5 text-left text-inherit no-underline outline-none transition-opacity duration-300 focus-visible:bg-current/[0.04] data-[active=true]:opacity-[var(--phs-dim)] sm:flex-row sm:items-center sm:justify-between sm:gap-6 md:px-14 md:py-9 [@media(hover:hover)_and_(pointer:fine)]:focus-visible:outline-none",
onClick: (event: MouseEvent<HTMLElement>) => handleClick(event, project, index),
"aria-expanded": touchLayout ? open : undefined,
"aria-controls": touchLayout ? panelName : undefined,
};
const content = (
<>
<span className="block text-[clamp(1.6rem,4.4vw,3.5rem)] leading-[1.05] font-medium tracking-[-0.035em] transition-transform duration-500 ease-out group-data-[shift=true]:-translate-x-[var(--phs-shift)]">
{project.title}
</span>
<span className="flex items-center gap-4 text-sm opacity-70 transition-transform duration-500 ease-out group-data-[shift=true]:translate-x-[var(--phs-shift)] md:text-base [@media(hover:none)]:sm:mr-8">
{project.subtitle}
</span>
{/* Touch only: a plus that turns into a minus while the row is open */}
<span aria-hidden="true" className="absolute top-1/2 right-4 block size-3 -translate-y-1/2 opacity-70 md:right-14 [@media(hover:hover)_and_(pointer:fine)]:hidden">
<span className="absolute top-1/2 left-0 h-px w-full -translate-y-1/2 bg-current" />
<span className={`absolute top-0 left-1/2 h-full w-px -translate-x-1/2 bg-current transition-transform duration-300 ${open ? "scale-y-0" : ""}`} />
</span>
</>
);
return (
<li key={`${index}-${project.title}`} className="m-0 p-0">
{project.href ? (
<a href={project.href} role={touchLayout ? "button" : undefined} {...rowProps}>
{content}
</a>
) : (
<button type="button" {...rowProps}>
{content}
</button>
)}
{/* Touch only: the picture opens under the row */}
<div
id={panelName}
inert={!open}
className={`grid transition-[grid-template-rows] duration-500 ease-out motion-reduce:transition-none [@media(hover:hover)_and_(pointer:fine)]:hidden ${open ? "grid-rows-[1fr]" : "grid-rows-[0fr]"}`}
>
<div className="min-h-0 overflow-hidden">
<div className="px-4 pb-5">
<img
src={project.image}
alt={project.alt ?? project.title}
loading="lazy"
decoding="async"
draggable={false}
className={`block w-full object-cover transition-opacity duration-500 ${open ? "opacity-100" : "opacity-0"}`}
style={{ aspectRatio: `${thumbnailWidth} / ${thumbnailHeight}`, borderRadius: thumbnailRadius }}
/>
{project.href ? (
<a href={project.href} className="mt-3 inline-block text-sm underline underline-offset-4">
View project
</a>
) : null}
</div>
</div>
</div>
</li>
);
})}
</ul>
{/* The floating photo. Decorative: every row already says what the picture is. */}
<div
ref={thumbRef}
aria-hidden="true"
className="pointer-events-none absolute top-0 left-0 z-50 hidden overflow-hidden border border-current/20 shadow-[0_30px_60px_-24px_rgba(0,0,0,0.45)] [@media(hover:hover)_and_(pointer:fine)]:block"
style={{ width: thumbnailWidth, height: thumbnailHeight, borderRadius: thumbnailRadius, opacity: 0, visibility: "hidden", willChange: "transform" }}
>
<div ref={stripRef} className="absolute inset-0">
{projects.map((project, index) => (
<div key={`${index}-${project.image}`} data-slide="" className="absolute inset-0">
<img src={project.image} alt="" decoding="async" draggable={false} className="block h-full w-full object-cover" />
</div>
))}
</div>
</div>
</div>
);
}
The component. Installs to components/dimaac/project-hover-section.tsx.
Install
With the shadcn CLI
npx shadcn@latest add https://ui.dimaac.com/r/project-hover-section.jsonIt adds components/dimaac/project-hover-section.tsx and installs what it imports. With the @dimaac namespace in components.json (Docs) it's just npx shadcn@latest add @dimaac/project-hover-section.
By hand
npm install gsap @gsap/react- Install the packages it uses: gsap, @gsap/react.
- Copy the component file below into components/dimaac/project-hover-section.tsx.
- It's styled with Tailwind v4 classes. If that folder sits outside the places Tailwind scans, add it with @source in your CSS.
- Import it with import { ProjectHoverSection } from "@/components/dimaac/project-hover-section" and give its parent a size: it fills it.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
projects (required) | ProjectHoverItem[] | - | The rows, top to bottom. Three to eight look best. |
thumbnailWidth | number | 380 | Width of the floating photo, in pixels. |
thumbnailHeight | number | 250 | Height of the floating photo, in pixels. |
thumbnailRadius | number | 10 | Corner radius of the floating photo, in pixels. |
followDuration | number | 0.4 | How long the photo takes to catch up with the cursor, in seconds. |
transition | "slide" | "fade" | "slide" | How the photo changes between rows: "slide" scrolls the strip, "fade" cross-fades. |
shift | number | 15 | How far the title and subtitle of the hovered row move apart, in pixels. |
dimOpacity | number | 0.5 | The opacity of the hovered row, from 0 to 1. 1 leaves it alone. |
tilt | number | 5 | The most the photo leans into the direction of the cursor, in degrees. 0 keeps it straight. |
onSelect | (project: ProjectHoverItem, index: number) => void | - | Called when a row is clicked or pressed. |
className | string | "" | Extra classes for the outer box. It is as wide as its parent and as tall as the list. |
Use with AI
Agent prompt
Paste it into Claude Code, Cursor or any coding agent, or open it straight in a chat. It carries your Customize settings.
Add the Project hover component (<ProjectHoverSection />) from DiMaac UI to this project.
Docs: https://ui.dimaac.com/components/project-hover-section
Source and dependencies (shadcn registry JSON): https://ui.dimaac.com/r/project-hover-section.json
Install it with:
npx shadcn@latest add https://ui.dimaac.com/r/project-hover-section.json
Then use it like this:
import { ProjectHoverSection } from "@/components/dimaac/project-hover-section";
<ProjectHoverSection projects={projects} />
If the shadcn CLI isn't set up here, fetch the registry JSON above, write files[0].content to components/dimaac/project-hover-section.tsx and install gsap, @gsap/react.
It uses Tailwind v4 classes, so make sure Tailwind scans that folder. Give its parent a size, keep light and dark mode working and keep its prefers-reduced-motion fallback.
Full index for agents: https://ui.dimaac.com/llms.txtFiles agents read
- llms.txt
- Every component and how to install it
- project-hover-section.md
- This page as Markdown: install, props and the full source
- project-hover-section.json
- The shadcn registry item
About
A list of projects for a portfolio. Hover a row and a photo grows at the cursor, trails it with a soft delay, leans into the direction you move and slides to that row's picture while the row's title and subtitle drift apart. Tab to a row and the photo appears beside it, so it works from the keyboard. It listens only inside its own list, takes its colours from the text colour around it, and on touch screens turns into a tap-to-open accordion that shows each photo inline. With reduced motion the photo just fades in, still, at the cursor. Ported from the first DiMaac UI and rebuilt with scoped GSAP calls, real links or buttons for the rows, and a CSS-only choice between mouse and touch.
- Installs to
components/dimaac/project-hover-section.tsx- Uses
- gsap, @gsap/react
- Tags
- gsap, cursor, hover, list, portfolio, images
Concrete corner by Ricardo Matos on Lummi (Lummi License (free for personal and commercial use)). Red wall interior by Siren Media on Lummi (Lummi License (free for personal and commercial use)). Palm pool by Binks AI on Lummi (Lummi License (free for personal and commercial use)). White corner by Mikiwa on Lummi (Lummi License (free for personal and commercial use)). Concrete balconies by Mariate on Lummi (Lummi License (free for personal and commercial use)).