|
| 1 | +'use client'; |
| 2 | + |
1 | 3 | import React, { useEffect, useState } from 'react'; |
| 4 | +import { cva, type VariantProps } from 'class-variance-authority'; |
| 5 | +import { cn } from '../lib/utils'; |
| 6 | + |
| 7 | +const containerVariants = cva( |
| 8 | + "container mx-auto transition-colors duration-300", |
| 9 | + { |
| 10 | + variants: { |
| 11 | + maxWidth: { |
| 12 | + sm: "max-w-screen-sm", |
| 13 | + md: "max-w-screen-md", |
| 14 | + lg: "max-w-screen-lg", |
| 15 | + xl: "max-w-screen-xl", |
| 16 | + "2xl": "max-w-screen-2xl", |
| 17 | + full: "max-w-full", |
| 18 | + prose: "max-w-prose", |
| 19 | + }, |
| 20 | + padding: { |
| 21 | + none: "px-0", |
| 22 | + sm: "px-3 sm:px-4", |
| 23 | + md: "px-4 sm:px-6 lg:px-8", |
| 24 | + lg: "px-6 sm:px-10 lg:px-12", |
| 25 | + }, |
| 26 | + withBackground: { |
| 27 | + true: "bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-sm", |
| 28 | + false: "", |
| 29 | + }, |
| 30 | + }, |
| 31 | + defaultVariants: { |
| 32 | + maxWidth: "lg", |
| 33 | + padding: "md", |
| 34 | + withBackground: false, |
| 35 | + }, |
| 36 | + } |
| 37 | +); |
2 | 38 |
|
3 | | -interface ContainerProps { |
| 39 | +interface ContainerProps extends React.HTMLAttributes<HTMLDivElement>, VariantProps<typeof containerVariants> { |
4 | 40 | children: React.ReactNode; |
5 | | - className?: string; |
6 | | - maxWidth?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'full' | 'prose'; |
7 | | - padding?: 'none' | 'sm' | 'md' | 'lg'; |
8 | | - withBackground?: boolean; |
9 | 41 | } |
10 | 42 |
|
11 | | -const Container: React.FC<ContainerProps> = ({ |
12 | | - children, |
13 | | - className = '', |
14 | | - maxWidth = 'lg', |
15 | | - padding = 'md', |
16 | | - withBackground = false |
17 | | -}) => { |
| 43 | +const Container = ({ |
| 44 | + children, |
| 45 | + className, |
| 46 | + maxWidth, |
| 47 | + padding, |
| 48 | + withBackground, |
| 49 | + ...props |
| 50 | +}: ContainerProps) => { |
18 | 51 | const [mounted, setMounted] = useState(false); |
19 | 52 |
|
20 | 53 | useEffect(() => { |
21 | 54 | setMounted(true); |
| 55 | + return () => setMounted(false); |
22 | 56 | }, []); |
23 | 57 |
|
24 | | - // Map maxWidth to tailwind classes |
25 | | - const maxWidthClasses = { |
26 | | - sm: 'max-w-screen-sm', |
27 | | - md: 'max-w-screen-md', |
28 | | - lg: 'max-w-screen-lg', |
29 | | - xl: 'max-w-screen-xl', |
30 | | - '2xl': 'max-w-screen-2xl', |
31 | | - full: 'max-w-full', |
32 | | - prose: 'max-w-prose' |
33 | | - }; |
34 | | - |
35 | | - // Map padding to tailwind classes |
36 | | - const paddingClasses = { |
37 | | - none: 'px-0', |
38 | | - sm: 'px-3 sm:px-4', |
39 | | - md: 'px-4 sm:px-6 lg:px-8', |
40 | | - lg: 'px-6 sm:px-10 lg:px-12' |
41 | | - }; |
42 | | - |
43 | | - const backgroundClasses = withBackground |
44 | | - ? 'bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-md shadow-sm' |
45 | | - : ''; |
46 | | - |
47 | 58 | return ( |
48 | | - <div className={` |
49 | | - container mx-auto ${maxWidthClasses[maxWidth]} ${paddingClasses[padding]} |
50 | | - ${backgroundClasses} transition-all duration-200 ease-in-out |
51 | | - ${mounted ? 'opacity-100' : 'opacity-0 translate-y-2'} |
52 | | - ${className} |
53 | | - `}> |
54 | | - <div className={`${withBackground ? 'p-4 sm:p-6' : ''}`}> |
| 59 | + <div |
| 60 | + className={cn( |
| 61 | + containerVariants({ maxWidth, padding, withBackground }), |
| 62 | + mounted ? "opacity-100 translate-y-0" : "opacity-0 translate-y-2", |
| 63 | + "transition duration-300 ease-in-out", |
| 64 | + className |
| 65 | + )} |
| 66 | + {...props} |
| 67 | + > |
| 68 | + <div className={withBackground ? "p-4 sm:p-6" : ""}> |
55 | 69 | {children} |
56 | 70 | </div> |
57 | 71 | </div> |
|
0 commit comments