Focus Cards
A grid of cards where hovering focuses one card and softly blurs the rest.
Focus Cards
A grid of cards where hovering focuses one card and softly blurs the rest.
The grid displays image cards in a responsive layout. When you hover over a card, it stays sharp while all other cards blur with blur-sm and scale down to 98%, creating a depth-of-field effect. The hovered card reveals its title at the bottom with a gradient fade-in. Moving to another card smoothly transitions the focus, and leaving the grid resets all cards to sharp focus.
Installation
Install the component with the Craft-UI Blocks CLI — it drops focus-cards.tsx into your project and wires up its runtime dependencies:
npx mvpblocks add focus-cardspnpm dlx mvpblocks add focus-cardsyarn dlx mvpblocks add focus-cardsbunx mvpblocks add focus-cardsFile Structure
The CLI saves the component to your project's components/ui folder:
Import the default export from @/components/ui/focus-cards. The card data type is exported as FocusCard.
Usage
FocusCards is a client component that takes an array of card objects. Each card needs a title and image source. Drop it into any section:
"use client";
import { FocusCards } from "@/components/ui/focus-cards";
const cards = [
{
title: "Forest Adventure",
src: "https://images.unsplash.com/photo-1441974231531-c6227db76b6e?q=80&auto=format&fit=crop&w=600",
},
{
title: "Valley of life",
src: "https://images.unsplash.com/photo-1500534314209-a25ddb2bd429?q=80&auto=format&fit=crop&w=600",
},
{
title: "Mountain Vista",
src: "https://images.unsplash.com/photo-1470252649378-9c29740c9fa8?q=80&auto=format&fit=crop&w=600",
},
];
export function CardGallery() {
return <FocusCards cards={cards} />;
}Add more cards to fill the grid:
const portfolioCards = [
{ title: "Urban Architecture", src: "/images/urban.jpg" },
{ title: "Natural Landscapes", src: "/images/nature.jpg" },
{ title: "Abstract Patterns", src: "/images/abstract.jpg" },
{ title: "Street Photography", src: "/images/street.jpg" },
{ title: "Minimalist Design", src: "/images/minimal.jpg" },
{ title: "Cultural Heritage", src: "/images/heritage.jpg" },
];
<FocusCards cards={portfolioCards} />Use with dynamic data from an API:
const [cards, setCards] = useState([]);
useEffect(() => {
fetch("/api/gallery")
.then((res) => res.json())
.then((data) => setCards(data));
}, []);
<FocusCards cards={cards} />Theming
The cards use semantic theme colors. Each card has bg-secondary as its surface, creating a consistent look across light and dark modes. The card container is rounded-lg with overflow-hidden to clip the image.
Images are rendered with Next.js Image component using fill layout and object-cover fit, ensuring they fill the entire card while maintaining aspect ratio. The sizes attribute is set to (max-width: 768px) 100vw, 33vw for responsive image loading.
The title overlay appears with a bg-black/50 backdrop when hovered. The title text uses a gradient from foreground to muted-foreground with bg-clip-text and text-transparent, creating a subtle fade effect. Font size is text-xl on mobile and text-2xl on medium screens.
The grid is max-w-5xl mx-auto with md:px-8 padding on larger screens. Cards span one column on mobile and three columns on medium screens and up with gap-10 spacing.
Card height is h-60 (240px) on mobile and h-96 (384px) on medium screens, providing more vertical space as the viewport grows.
Animations
- Blur and scale: When any card is hovered, all other cards transition to
blur-smandscale-[0.98]over300mswith ease-out timing. The hovered card remains at full scale and sharp focus. - Title fade: The title overlay transitions from
opacity-0toopacity-100over300mswhen its card is hovered, with the same ease-out curve. - Reset: Moving the pointer off all cards resets them to sharp focus and full scale simultaneously, also over
300ms. - No motion reduction: The component uses CSS transitions only, which respect
prefers-reduced-motion: reduceautomatically by reducing duration to near-zero.
How it works
The component lifts hover state to the parent FocusCards component via a hovered state that tracks the currently hovered card's index (or null when none are hovered). Each Card component receives this shared state and its own index.
When a card's onMouseEnter fires, it calls setHovered(index) to update the parent state. When onMouseLeave fires, it calls setHovered(null). All cards re-render when hovered changes, but only to update their CSS classes based on whether they match the hovered index.
The blur and scale are applied conditionally: hovered !== null && hovered !== index means "some card is hovered but it's not me", which triggers the blur and scale classes. The title overlay uses hovered === index to show only on the focused card.
The Card component is wrapped in React.memo, which prevents unnecessary re-renders when the parent re-renders but the card's props haven't changed. Since the hovered state is passed as a prop, React.memo's shallow comparison sees when it changes and allows the re-render, but cards whose index and data haven't changed skip rendering when other cards are added or removed from the array.
Images use Next.js Image component with fill layout, which positions the image absolutely to fill its container. The sizes attribute tells the browser to load images at full viewport width on mobile and one-third width on medium screens, allowing efficient responsive loading.
The title sits in an absolutely positioned overlay with flexbox centering at items-end, pushing the text to the bottom with py-8 px-4 padding. The gradient text uses bg-gradient-to-b from-foreground to-muted-foreground with bg-clip-text text-transparent to clip the gradient to the text shape.
Props
FocusCards
Prop
Type
FocusCard Object
Prop
Type
Registry Bundle
Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: next (for the Image component), react.
Contact
If you find any bug or issue, feel free to raise an issue.
Last updated on