A step-by-step guide to integrate a modern hero section into your shadcn/Next.js project
This guide will walk you through integrating a modern, animated hero section component into your Next.js project with shadcn UI, Tailwind CSS, and TypeScript. The component features:
First, ensure your project has the correct structure and dependencies. If you don't already have a shadcn project with Tailwind CSS and TypeScript, follow these steps:
# Create a new Next.js project with TypeScript
npx create-next-app@latest my-app --typescript
# Navigate to your project
cd my-app
# Install shadcn CLI
npm install -g shadcn-ui
# Initialize shadcn in your project
npx shadcn-ui@latest init
During the shadcn initialization, select the following options:
Note: If you already have a Next.js project with shadcn UI, you can skip this step.
The hero section component requires several dependencies. Install them with npm:
npm install lucide-react @radix-ui/react-slot class-variance-authority framer-motion clsx tailwind-merge tailwindcss-animate
Note: If you've already installed some of these dependencies as part of your shadcn setup, you don't need to install them again.
Create the necessary directory structure for your components:
# Create components directories
mkdir -p components/ui
mkdir -p components/blocks
# Create lib directory if it doesn't exist
mkdir -p lib
Your project structure should look like this:
If you don't already have it, create the utils.ts file in the lib directory:
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
Note: This utility function is used throughout the shadcn components for combining Tailwind CSS classes.
Now, let's create all the required component files:
'use client';
import { ReactNode } from 'react';
import { motion, Variants } from 'framer-motion';
import { cn } from '@/lib/utils';
import React from 'react';
type PresetType =
| 'fade'
| 'slide'
| 'scale'
| 'blur'
| 'blur-slide'
| 'zoom'
| 'flip'
| 'bounce'
| 'rotate'
| 'swing';
type AnimatedGroupProps = {
children: ReactNode;
className?: string;
variants?: {
container?: Variants;
item?: Variants;
};
preset?: PresetType;
};
const defaultContainerVariants: Variants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
transition: {
staggerChildren: 0.1,
},
},
};
const defaultItemVariants: Variants = {
hidden: { opacity: 0 },
visible: { opacity: 1 },
};
const presetVariants: Record<
PresetType,
{ container: Variants; item: Variants }
> = {
fade: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0 },
visible: { opacity: 1 },
},
},
slide: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, y: 20 },
visible: { opacity: 1, y: 0 },
},
},
scale: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, scale: 0.8 },
visible: { opacity: 1, scale: 1 },
},
},
blur: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, filter: 'blur(4px)' },
visible: { opacity: 1, filter: 'blur(0px)' },
},
},
'blur-slide': {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, filter: 'blur(4px)', y: 20 },
visible: { opacity: 1, filter: 'blur(0px)', y: 0 },
},
},
zoom: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, scale: 0.5 },
visible: {
opacity: 1,
scale: 1,
transition: { type: 'spring', stiffness: 300, damping: 20 },
},
},
},
flip: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, rotateX: -90 },
visible: {
opacity: 1,
rotateX: 0,
transition: { type: 'spring', stiffness: 300, damping: 20 },
},
},
},
bounce: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, y: -50 },
visible: {
opacity: 1,
y: 0,
transition: { type: 'spring', stiffness: 400, damping: 10 },
},
},
},
rotate: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, rotate: -180 },
visible: {
opacity: 1,
rotate: 0,
transition: { type: 'spring', stiffness: 200, damping: 15 },
},
},
},
swing: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, rotate: -10 },
visible: {
opacity: 1,
rotate: 0,
transition: { type: 'spring', stiffness: 300, damping: 8 },
},
},
},
};
function AnimatedGroup({
children,
className,
variants,
preset,
}: AnimatedGroupProps) {
const selectedVariants = preset
? presetVariants[preset]
: { container: defaultContainerVariants, item: defaultItemVariants };
const containerVariants = variants?.container || selectedVariants.container;
const itemVariants = variants?.item || selectedVariants.item;
return (
{React.Children.map(children, (child, index) => (
{child}
))}
);
}
export { AnimatedGroup };
'use client';
import { cn } from '@/lib/utils';
import {
AnimatePresence,
motion,
TargetAndTransition,
Variants,
} from 'framer-motion';
import React from 'react';
type PresetType = 'blur' | 'shake' | 'scale' | 'fade' | 'slide';
type TextEffectProps = {
children: string;
per?: 'word' | 'char' | 'line';
as?: keyof React.JSX.IntrinsicElements;
variants?: {
container?: Variants;
item?: Variants;
};
className?: string;
preset?: PresetType;
delay?: number;
trigger?: boolean;
onAnimationComplete?: () => void;
segmentWrapperClassName?: string;
};
const defaultStaggerTimes: Record<'char' | 'word' | 'line', number> = {
char: 0.03,
word: 0.05,
line: 0.1,
};
const defaultContainerVariants: Variants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
transition: {
staggerChildren: 0.05,
},
},
exit: {
transition: { staggerChildren: 0.05, staggerDirection: -1 },
},
};
const defaultItemVariants: Variants = {
hidden: { opacity: 0 },
visible: {
opacity: 1,
},
exit: { opacity: 0 },
};
const presetVariants: Record<
PresetType,
{ container: Variants; item: Variants }
> = {
blur: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, filter: 'blur(12px)' },
visible: { opacity: 1, filter: 'blur(0px)' },
exit: { opacity: 0, filter: 'blur(12px)' },
},
},
shake: {
container: defaultContainerVariants,
item: {
hidden: { x: 0 },
visible: { x: [-5, 5, -5, 5, 0], transition: { duration: 0.5 } },
exit: { x: 0 },
},
},
scale: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, scale: 0 },
visible: { opacity: 1, scale: 1 },
exit: { opacity: 0, scale: 0 },
},
},
fade: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0 },
visible: { opacity: 1 },
exit: { opacity: 0 },
},
},
slide: {
container: defaultContainerVariants,
item: {
hidden: { opacity: 0, y: 20 },
visible: { opacity: 1, y: 0 },
exit: { opacity: 0, y: 20 },
},
},
};
const AnimationComponent: React.FC<{
segment: string;
variants: Variants;
per: 'line' | 'word' | 'char';
segmentWrapperClassName?: string;
}> = React.memo(({ segment, variants, per, segmentWrapperClassName }) => {
const content =
per === 'line' ? (
{segment}
) : per === 'word' ? (
) : (
{segment.split('').map((char, charIndex) => (
))}
);
if (!segmentWrapperClassName) {
return content;
}
const defaultWrapperClassName = per === 'line' ? 'block' : 'inline-block';
return (
{content}
);
});
AnimationComponent.displayName = 'AnimationComponent';
export function TextEffect({
children,
per = 'word',
as = 'p',
variants,
className,
preset,
delay = 0,
trigger = true,
onAnimationComplete,
segmentWrapperClassName,
}: TextEffectProps) {
let segments: string[];
if (per === 'line') {
segments = children.split('\n');
} else if (per === 'word') {
segments = children.split(/(\s+)/);
} else {
segments = children.split('');
}
const MotionTag = motion[as as keyof typeof motion] as typeof motion.div;
const selectedVariants = preset
? presetVariants[preset]
: { container: defaultContainerVariants, item: defaultItemVariants };
const containerVariants = variants?.container || selectedVariants.container;
const itemVariants = variants?.item || selectedVariants.item;
const ariaLabel = per === 'line' ? undefined : children;
const stagger = defaultStaggerTimes[per];
const delayedContainerVariants: Variants = {
hidden: containerVariants.hidden,
visible: {
...containerVariants.visible,
transition: {
...(containerVariants.visible as TargetAndTransition)?.transition,
staggerChildren:
(containerVariants.visible as TargetAndTransition)?.transition
?.staggerChildren || stagger,
delayChildren: delay,
},
},
exit: containerVariants.exit,
};
return (
{trigger && (
{segments.map((segment, index) => (
))}
)}
);
}
Now, create the main hero section component:
Note: This file is quite large, so we'll just show the beginning. Copy the entire hero-section-1.tsx code from the provided code snippet.
import React from 'react'
import Link from 'next/link'
import { ArrowRight, ChevronRight, Menu, X } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { AnimatedGroup } from '@/components/ui/animated-group'
import { cn } from '@/lib/utils'
const transitionVariants = {
item: {
hidden: {
opacity: 0,
filter: 'blur(12px)',
y: 12,
},
visible: {
opacity: 1,
filter: 'blur(0px)',
y: 0,
transition: {
type: 'spring',
bounce: 0.3,
duration: 1.5,
},
},
},
}
export function HeroSection() {
// ... (rest of the component code)
}
Create a demo page to showcase the hero section:
import { HeroSection } from "@/components/blocks/hero-section-1";
export default function DemoPage() {
return ;
}
Note: Make sure to create the demo directory inside the app folder if it doesn't exist.
Ensure your tailwind.config.js includes all necessary configurations:
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: ["class"],
content: [
'./pages/**/*.{ts,tsx}',
'./components/**/*.{ts,tsx}',
'./app/**/*.{ts,tsx}',
'./src/**/*.{ts,tsx}',
],
theme: {
container: {
center: true,
padding: "2rem",
screens: {
"2xl": "1400px",
},
},
extend: {
colors: {
border: "hsl(var(--border))",
input: "hsl(var(--input))",
ring: "hsl(var(--ring))",
background: "hsl(var(--background))",
foreground: "hsl(var(--foreground))",
primary: {
DEFAULT: "hsl(var(--primary))",
foreground: "hsl(var(--primary-foreground))",
},
secondary: {
DEFAULT: "hsl(var(--secondary))",
foreground: "hsl(var(--secondary-foreground))",
},
destructive: {
DEFAULT: "hsl(var(--destructive))",
foreground: "hsl(var(--destructive-foreground))",
},
muted: {
DEFAULT: "hsl(var(--muted))",
foreground: "hsl(var(--muted-foreground))",
},
accent: {
DEFAULT: "hsl(var(--accent))",
foreground: "hsl(var(--accent-foreground))",
},
card: {
DEFAULT: "hsl(var(--card))",
foreground: "hsl(var(--card-foreground))",
},
},
borderRadius: {
lg: "var(--radius)",
md: "calc(var(--radius) - 2px)",
sm: "calc(var(--radius) - 4px)",
},
keyframes: {
"accordion-down": {
from: { height: 0 },
to: { height: "var(--radix-accordion-content-height)" },
},
"accordion-up": {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: 0 },
},
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
},
},
},
plugins: [require("tailwindcss-animate")],
}
Important: Make sure to install the tailwindcss-animate plugin:
npm install tailwindcss-animate
Start your development server to test the hero section:
npm run dev
Visit http://localhost:3000/demo to see your hero section in action.
Note: If you encounter any issues, check the browser console for errors and make sure all dependencies are installed correctly.
If you encounter errors about missing dependencies, install them:
npm install clsx tailwind-merge tailwindcss-animate
If images don't load, check the paths and consider using Next.js Image component with proper configuration:
import Image from 'next/image';
// Replace
<img
src="https://ik.imagekit.io/lrigu76hy/tailark/night-background.jpg"
alt="background"
className="absolute inset-x-0 top-56 -z-20 hidden lg:top-32 dark:block"
width="3276"
height="4095"
/>
// With
<Image
src="/images/night-background.jpg"
alt="background"
className="absolute inset-x-0 top-56 -z-20 hidden lg:top-32 dark:block"
width={3276}
height={4095}
/>
Make sure all types are properly defined and imported. Check your tsconfig.json for proper path aliases:
{
"compilerOptions": {
"paths": {
"@/*": ["./*"]
}
}
}
Ensure Tailwind is properly configured and all classes are recognized. Check your global CSS file for the required base styles.
Now that you have the hero section integrated, you can customize it to match your brand:
Tip: For the best performance, optimize your images and consider using Next.js Image component with proper sizing.