Chakra 설치
npx create-next-app@latest ./
npm install @chakra-ui/react@2.8.2 @chakra-ui/next-js@2.1.5 @emotion/react@11.11.1 @emotion/styled@11.11.0 framer-motion@10.16.4
yarn add @chakra-ui/react@2.8.2 @chakra-ui/next-js@2.1.5 @emotion/react@11.11.1 @emotion/styled@11.11.0 framer-motion@10.16.4
Chakra Provider 적용하기
package.json
// package.json
{
"dependencies": {
"@chakra-ui/next-js": "^2.1.5",
"@chakra-ui/react": "^2.8.2",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"framer-motion": "^10.16.4",
"next": "15.0.3",
"react": "19.0.0-rc-66855b96-20241106",
"react-dom": "19.0.0-rc-66855b96-20241106"
}
}
providers 제작
'use client'
import { CacheProvider } from '@chakra-ui/next-js'
import { ChakraProvider, extendTheme } from '@chakra-ui/react'
const theme = extendTheme({
})
export function Providers({ children }) {
return (
<CacheProvider>
<ChakraProvider theme={theme}>
{children}
</ChakraProvider>
</CacheProvider>
)
}
app/layout 감싸기
import localFont from "next/font/local";
import "./globals.css";
import { Providers } from "./providers";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<Providers>
{children}
</Providers>
</body>
</html>
);
}
Chakra Theme 적용
1. 구조
src/
└── theme/
├── components/
│ ├── button.js
│ ├── input.js
│ └── card.js
├── foundations/
│ ├── colors.js
│ ├── typography.js
│ └── spacing.js
└── index.js
2. 각 컴포넌트 테마 설정
theme/components/container.js
export const Container = {
baseStyle: {
maxW: '1340px',
mx: 'auto',
px: { base: '5', md: '6', lg: '8' },
py: { base: '5', md: '6', lg: '8' },
},
sizes: {
sm: {
maxW: '640px',
},
md: {
maxW: '768px',
},
lg: {
maxW: '1024px',
},
xl: {
maxW: '1200px',
},
'2xl': {
maxW: '1400px',
},
full: {
maxW: 'none',
}
},
variants: {
primary: {
bg: 'white',
shadow: 'md',
rounded: 'lg',
},
secondary: {
bg: 'gray.50',
rounded: 'md',
},
responsive: {
px: { base: '4', md: '8', lg: '12' },
py: { base: '4', md: '6', lg: '8' },
}
},
defaultProps: {
size: 'xl',
}
}
export const Button = {
baseStyle: {
fontWeight: 'bold',
borderRadius: 'md',
},
sizes: {
sm: {
fontSize: 'sm',
px: 4,
py: 2,
},
md: {
fontSize: 'md',
px: 6,
py: 3,
},
},
variants: {
outline: {
bg: 'blue.500',
color: 'white',
_hover: {
bg: 'blue.600',
},
},
ghost: {
bg: 'gray.200',
color: 'gray.800',
_hover: {
bg: 'gray.300',
},
},
},
defaultProps: {
size: 'md',
variant: 'primary',
},
}
export const Input = {
baseStyle: {
field: {
borderRadius: 'md',
},
},
variants: {
outline: {
field: {
borderColor: 'gray.200',
_focus: {
borderColor: 'blue.500',
boxShadow: '0 0 0 1px blue.500',
},
},
},
},
defaultProps: {
variant: 'outline',
},
}
3. 기본 스타일 설정:
theme/foundations/colors.js
export const colors = {
brand: {
50: '#f7fafc',
100: '#edf2f7',
900: '#1a202c',
},
}
theme/foundations/typography.js
export const typography = {
fonts: {
body: 'var(--font-geist-sans)',
heading: 'var(--font-geist-sans)',
mono: 'var(--font-geist-mono)',
},
fontSizes: {
xs: '0.75rem',
sm: '0.875rem',
md: '1rem',
lg: '1.125rem',
xl: '1.25rem',
},
}
4. 모든 테마를 하나로 통합하는 theme/index.js
import { extendTheme } from '@chakra-ui/react'
import { Button } from './components/button'
import { Input } from './components/input'
import { colors } from './foundations/colors'
import { typography } from './foundations/typography'
const theme = extendTheme({
colors,
...typography,
components: {
Container,
Button,
Input,
},
})
export default theme
5. providers.js에서 테마 적용:
'use client'
import { CacheProvider } from '@chakra-ui/next-js'
import { ChakraProvider } from '@chakra-ui/react'
import theme from '@/theme'
export function Providers({ children }) {
return (
<CacheProvider>
<ChakraProvider theme={theme}>
{children}
</ChakraProvider>
</CacheProvider>
)
}
6. 사용
'use client'
import { Button, Input } from '@chakra-ui/react'
export default function MyComponent() {
return (
<div>
<Button variant="primary">Primary Button</Button>
<Button variant="secondary">Secondary Button</Button>
<Input placeholder="Custom styled input" />
</div>
)
}