Please use Desktop to view and interact with components
Components300msspring
Animated Dialog
Modal dialog with backdrop fade and spring animation
modaldialogoverlaypopupshadcn
Useto navigate between components
Preview
This component requires shadcn/ui
This component uses shadcn/ui components. Make sure you have shadcn/ui set up in your project.
- Install shadcn/ui:
npx shadcn-ui@latest init - Install required components based on the imports in the code (e.g.,
npx shadcn-ui@latest add button) - Ensure your
tailwind.config.tsandglobals.cssare configured as per shadcn/ui documentation
Code
TypeScript + React
'use client'
import { useState } from 'react'
import { motion, AnimatePresence } from 'framer-motion'
import { X } from 'lucide-react'
import { Button } from '@/components/ui/button'
export function AnimatedDialog() {
const [isOpen, setIsOpen] = useState(false)
return (
<div>
<Button onClick={() => setIsOpen(true)}>
Open Dialog
</Button>
<AnimatePresence>
{isOpen && (
<>
{/* Backdrop */}
<motion.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="fixed inset-0 z-50 bg-black/50"
onClick={() => setIsOpen(false)}
/>
{/* Dialog */}
<div className="fixed inset-0 z-50 flex items-center justify-center p-4">
<motion.div
initial={{ opacity: 0, scale: 0.95, y: 20 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.95, y: 20 }}
transition={{ type: 'spring', damping: 25, stiffness: 300 }}
className="relative bg-card w-full max-w-md rounded-2xl border bg-[var(--card-bg)] p-6 shadow-2xl"
onClick={(e) => e.stopPropagation()}
>
<motion.button
onClick={() => setIsOpen(false)}
className="absolute right-4 top-4 text-[var(--foreground)]/60 hover:text-[var(--foreground)]"
whileHover={{ rotate: 90, scale: 1.1 }}
whileTap={{ scale: 0.9 }}
>
<X className="h-5 w-5" />
</motion.button>
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.1 }}
>
<h2 className="text-2xl font-semibold">Confirmation</h2>
<p className="mt-2 text-sm text-[var(--foreground)]/70">
Are you sure you want to perform this action? This cannot be undone.
</p>
</motion.div>
<motion.div
className="mt-6 flex gap-2"
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
transition={{ delay: 0.2 }}
>
<Button
variant="outline"
onClick={() => setIsOpen(false)}
className="flex-1"
>
Cancel
</Button>
<Button
onClick={() => setIsOpen(false)}
className="flex-1"
>
Confirm
</Button>
</motion.div>
</motion.div>
</div>
</>
)}
</AnimatePresence>
</div>
)
}
How to Use
- 1Install Framer Motion:
npm install framer-motion - 2Set up shadcn/ui: Install shadcn/ui components used in this code. Check the imports in the code above and install the required components (e.g.,
npx shadcn-ui@latest add button card) - 3Copy the code from above
- 4Paste it into your project and customize as needed
- 5Colors are customizable via Tailwind CSS classes. The default theme uses dark mode colors defined in your globals.css file