Tailwind CSS — PrimeReact과 Tailwind 통합
PrimeReact과 Tailwind CSS를 styled·unstyled 두 모드 모두에서 함께 쓰는 방법을 안내해요. 공식 플러그인 tailwindcss-primeui를 설정하고, CSS 레이어 순서·다크 모드·애니메이션 유틸리티까지 살펴볼게요.
출처: 문서
본문
PrimeReact과 Tailwind CSS를 styled와 unstyled 두 모드 모두에서 연동하는 방법이에요.
개요 (Overview)
Tailwind CSS는 유틸리티 우선(utility-first) 설계에 기반한 인기 있는 CSS 프레임워크예요. 코어는 미리 정의된 CSS 규칙을 가진 유연한 CSS 클래스를 제공해 나만의 UI 요소를 만들 수 있게 해 줘요. 예를 들어 Bootstrap처럼 고정된 btn className을 쓰는 대신, Tailwind는 bg-blue-500, rounded, p-4 같은 원시(primitive) 클래스를 제공해 버튼을 만들 수 있어요.
Tailwind는 훌륭한 CSS 라이브러리지만, Vue.js와 결합했을 때 포괄적인 UI 스위트가 부족해요. 이때 PrimeReact이 등장해서 폭넓은 범위의 접근성 높고 기능이 풍부한 UI 컴포넌트 라이브러리를 제공해요. PrimeReact의 코어는 Tailwind CSS에 의존하지 않으며, 대신 primeui tailwind 플러그인과 unstyled 모드용 Tailwind 버전 같은 필요한 통합 지점을 제공해요.
Tailwind CSS and PrimeReact can be used together via two main approaches. Simple approach is using Tailwind CSS _around_ PrimeReact components for layout purposes as demonstrated in the samples section below, the advanced approach takes the integration a step further by allowing Tailwind CSS _within_ the component internals to style the entire UI suite in unstyled mode.Tailwind 테마 (Tailwind Theme)
PrimeTek은 IntelliSense 지원을 포함한 @apply 지시어 기반으로, 전체 PrimeReact UI 스위트를 unstyled 모드의 Tailwind CSS 버전으로 제공해요. 문서, 데모, 추가 리소스는 PrimeReact의 Tailwind 버전을 방문해요.
플러그인 (Plugin)
tailwindcss-primeui는 PrimeReact 같은 Prime UI 라이브러리와 Tailwind CSS 사이의 클래스 통합을 위해 PrimeTek이 만든 공식 플러그인이에요. styled와 unstyled 두 모드 모두에서 동작하도록 설계됐어요. 예를 들어 styled 모드에서는 primary나 surface 같은 시맨틱 색상을 bg-primary, text-surface-500, text-muted-color 같은 Tailwind 유틸리티로 제공해요.
아직 Tailwind를 통합하지 않았다면 먼저 프로젝트에 Tailwind를 연동해요. 자세한 절차는 Tailwind 문서에서 찾을 수 있어요. Tailwind를 성공적으로 설치했다면 PrimeUI 플러그인 설치로 진행해요. 이 단일 npm 패키지에는 두 가지 라이브러리가 들어 있어요. CSS 버전은 Tailwind v4와 호환되고, JS 버전은 Tailwind v3용으로 설계됐어요.
npm install tailwindcss-primeui
Tailwind v4
tailwindcss 임포트가 있는 CSS 파일에 tailwindcss-primeui 임포트도 추가해요.
@import 'tailwindcss';
@import 'tailwindcss-primeui';
캐스케이드 레이어 (Cascade layer)
PrimeReact은 자신이 렌더링하는 네이티브 요소의 브라우저 기본값을 초기화하는데(preflight, configuration 가이드 참고), 그 규칙들은 primeui 아래에 중첩된 캐스케이드 레이어 primeui.base에 있어요. CSS 레이어는 규칙이 작성된 위치가 아니라 이름이 처음 선언된 위치에 따라 순서가 정해져요. 그래서 tailwindcss가 자신의 레이어를 먼저 선언하면 primeui가 그 뒤에 오게 되어, 리셋이 유틸리티 클래스보다 우선하게 돼요.
이 문제는 서버 렌더링 앱에서만, 그리고 hydration 전에만 나타나요. PrimeReact은 런타임에 레이어 선언을 주입하는데, 이는 첫 번째 서버 렌더링 응답에서 애플리케이션 자신의 CSS보다 뒤에 위치해요. 리셋을 재정의하려던 유틸리티 클래스는 클라이언트가接管하기 전까지 지고, 그래서 첫 페인트가 눈에 띄게 잘못되고 React가 hydration되면 스스로 고쳐져요.
tailwindcss 임포트 위에서 레이어를 선언해 순서를 고쳐요:
@layer primeui;
@import 'tailwindcss';
@import 'tailwindcss-primeui';
primeui만 선언해도 충분해요. 리셋이 있는 primeui.base는 그 안에 중첩돼 있고, 중첩 레이어의 순서는 부모에 의해 정해지기 때문이죠. tailwindcss 임포트 뒤에 선언하는 것은 순서가 이미 확정된 뒤라 아무 효과가 없어요. 이것은 Tailwind뿐 아니라 자체 레이어를 선언하는 모든 프레임워크에 적용돼요.
theme.options.cssLayer가 활성화된 애플리케이션은 이미 이 레이어를 선언했어요. 그 옵션은 PrimeReact의 컴포넌트 스타일, 변수, 전역 스타일을 기본적으로 primeui라는 이름의 레이어(또는 전달된 이름, { name: 'myui' }는 myui를 만듦)로 감싸요. @layer primeui; 한 줄로 리셋과 그 레이어를 모두 다룰 수 있어요.
Tailwind 자체의 preflight를 이미 쓰고 있거나, normalize.css나 Bootstrap의 reboot 같은 다른 리셋을 쓴다면, 리셋을 두 개 유지하지 말고 PrimeReact의 것을 꺼요:
<PrimeReactProvider preflight={false}>
Tailwind v3
Tailwind 설정 파일의 plugins 옵션으로 플러그인을 설정해요.
// tailwind.config.js
import PrimeUI from 'tailwindcss-primeui';
export default {
// ...
plugins: [PrimeUI]
};
확장 (Extensions)
플러그인은 사용 중인 PrimeReact 테마에서 값을 파생한 새 유틸리티 세트로 기본 설정을 확장해요. 모든 variant와 breakpoint를 지원하며, 예를 들어 dark:sm:hover:bg-primary처럼 쓸 수 있어요.
| Class | Property |
|---|---|
primary-[50-950] |
Primary color palette. |
surface-[0-950] |
Surface color palette. |
primary |
Default primary color. |
primary-contrast |
Default primary contrast color. |
primary-emphasis |
Default primary emphasis color. |
border-surface |
Default primary emphasis color. |
bg-emphasis |
Emphasis background e.g. hovered element. |
bg-highlight |
Highlight background. |
bg-highlight-emphasis |
Highlight background with emphasis. |
rounded-border |
Border radius. |
text-color |
Text color with emphasis. |
text-color-emphasis |
Default primary emphasis color. |
text-muted-color |
Secondary text color. |
text-muted-color-emphasis |
Secondary text color with emphasis. |
다크 모드 (Dark Mode)
styled 모드에서 PrimeReact은 테마 구성의 기본 darkModeSelector로 system을 사용해요. 애플리케이션에 다크 모드 스위치가 있다면 darkModeSelector가 Tailwind dark variant와 일치하도록 맞춰야 자연스럽게 연동돼요. 기본 시스템 색상 구성을 사용한다면 이 설정은 필요하지 않아요.
darkModeSelector가 PrimeReact에서 my-app-dark로 설정됐다고 가정해 볼게요.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import { PrimeReactProvider } from '@primereact/core';
import Aura from '@primeuix/themes/aura';
const primereact = {
theme: {
preset: Aura,
options: {
darkModeSelector: '.my-app-dark'
}
}
};
createRoot(document.getElementById('root')!).render(
<StrictMode>
<PrimeReactProvider {...primereact}>
<App />
</PrimeReactProvider>
</StrictMode>
);
Tailwind v4
커스텀 셀렉터로 dark용 커스텀 variant를 추가해요.
@import "tailwindcss";
@import "tailwindcss-primeui";
@custom-variant dark (&:where(.my-app-dark, .my-app-dark *)); //dark mode configuration
Tailwind v3
Tailwind 설정 파일의 plugins 옵션으로 플러그인을 설정해요.
// tailwind.config.js
import PrimeUI from 'tailwindcss-primeui';
export default {
darkMode: ['selector', '[className~="my-app-dark"]'], //dark mode configuration
plugins: [PrimeUI]
};
재정의 (Override)
Tailwind 유틸리티는 CSS 특이성(specificity) 때문에 컴포넌트의 기본 스타일링을 재정의하지 못할 수 있어요. 두 가지 해결책이 있어요: Important와 CSS Layer.
Important
!를 접두사로 사용해 스타일을 강제로 적용해요. 이 방법은 권장되지 않으며, 번들에 불필요한 스타일 클래스를 추가하지 않기 위한 최후의 수단으로 사용해야 해요.
Tailwind v4
<InputText placeholder="Overriden" className="p-8!" />
Tailwind v3
<InputText placeholder="Overriden" className="!p-8" />
CSS Layer
CSS Layer는 CSS 특이성을 제어해서 Tailwind 유틸리티가 컴포넌트를 안전하게 재정의할 수 있게 해 줘요.
Tailwind v4
primereact 레이어가 theme와 base 뒤, utilities 같은 다른 Tailwind 레이어보다 앞에 오도록 해요.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import { PrimeReactProvider } from '@primereact/core';
import Aura from '@primeuix/themes/aura';
const primereact = {
theme: {
preset: Aura,
options: {
cssLayer: {
name: 'primereact',
order: 'theme, base, primereact'
}
}
}
};
createRoot(document.getElementById('root')!).render(
<StrictMode>
<PrimeReactProvider {...primereact}>
<App />
</PrimeReactProvider>
</StrictMode>
);
CSS 구성은 변경할 필요가 없어요.
@import 'tailwindcss';
@import 'tailwindcss-primeui';
Tailwind v3
primereact 레이어는 base와 utilities 사이에 있어야 해요.
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import { PrimeReactProvider } from '@primereact/core';
import Aura from '@primeuix/themes/aura';
const primereact = {
theme: {
preset: Aura,
options: {
cssLayer: {
name: 'primereact',
order: 'tailwind-base, primereact, tailwind-utilities'
}
}
}
};
createRoot(document.getElementById('root')!).render(
<StrictMode>
<PrimeReactProvider {...primereact}>
<App />
</PrimeReactProvider>
</StrictMode>
);
Tailwind v3는 네이티브 layer를 사용하지 않으므로 CSS로 정의해야 해요.
@layer tailwind-base, primereact, tailwind-utilities;
@layer tailwind-base {
@tailwind base;
}
@layer tailwind-utilities {
@tailwind components;
@tailwind utilities;
}
샘플 (Samples)
PrimeReact과 Tailwind CSS를 쓰는 예시 사용 사례예요.
색상 팔레트 (Color Palette)
PrimeReact 색상 팔레트를 유틸리티 클래스로 표현한 예시예요.
<div className="flex flex-col gap-12">
<div className="flex gap-6 flex-wrap">
<div className="rounded-border p-4 border border-transparent flex items-center justify-center bg-primary hover:bg-primary-emphasis text-primary-contrast font-medium flex-auto transition-colors">primary</div>
<div className="rounded-border p-4 border border-transparent flex items-center justify-center bg-highlight hover:bg-highlight-emphasis font-medium flex-auto transition-colors">highlight</div>
<div className="rounded-border p-4 border border-surface flex items-center justify-center text-muted-color hover:text-color hover:bg-emphasis font-medium flex-auto transition-colors">box</div>
</div>
</div>
스타터 (Starter)
Tailwind v4와 PrimeReact 스타터 예시는 예시 대시보드로 통합 설정을 보여 줘요.
애니메이션 (Animations)
플러그인은 styleclass와 animateonscroll 컴포넌트에서 사용할 수 있는 확장된 애니메이션 유틸리티도 추가해요.
애니메이션 이름 (Animation Name)
| Class | Property |
|---|---|
animate-enter |
animation-name: enter; --p-enter-opacity: initial; --p-enter-scale: initial; --p-enter-rotate: initial; --p-enter-translate-x: initial; --p-enter-translate-y: initial; |
animate-leave |
animation-name: leave; --p-leave-opacity: initial; --p-leave-scale: initial; --p-leave-rotate: initial; --p-leave-translate-x: initial; --p-leave-translate-y: initial; |
animate-leave |
fadein 0.15s linear |
animate-fadein |
fadein 0.15s linear |
animate-fadeout |
fadeout 0.15s linear |
animate-slidedown |
slidedown 0.45s ease-in-out |
animate-slideup |
slideup 0.45s cubic-bezier(0, 1, 0, 1) |
animate-scalein |
scalein 0.15s linear |
animate-fadeinleft |
fadeinleft 0.15s linear |
animate-fadeoutleft |
fadeoutleft 0.15s linear |
animate-fadeinright |
fadeinright 0.15s linear |
animate-fadeoutright |
fadeoutright 0.15s linear |
animate-fadeinup |
fadeinup 0.15s linear |
animate-fadeoutup |
fadeoutup 0.15s linear |
animate-fadeindown |
fadeindown 0.15s linear |
animate-fadeoutup |
fadeoutup 0.15s linear |
animate-width |
width 0.15s linear |
animate-flip |
flip 0.15s linear |
animate-flipup |
flipup 0.15s linear |
animate-flipleft |
fadein 0.15s linear |
animate-flipright |
flipright 0.15s linear |
animate-zoomin |
zoomin 0.15s linear |
animate-zoomindown |
zoomindown 0.15s linear |
animate-zoominleft |
zoominleft 0.15s linear |
animate-zoominright |
zoominright 0.15s linear |
animate-zoominup |
zoominup 0.15s linear |
애니메이션 지속 시간 (Animation Duration)
| Class | Property |
|---|---|
animate-duration-0 |
animation-duration: 0s |
animate-duration-75 |
animation-duration: 75ms |
animate-duration-100 |
animation-duration: 100ms |
animate-duration-200 |
animation-duration: 200ms |
animate-duration-300 |
animation-duration: 300ms |
animate-duration-400 |
animation-duration: 400ms |
animate-duration-500 |
animation-duration: 500ms |
animate-duration-700 |
animation-duration: 700ms |
animate-duration-1000 |
animation-duration: 1000ms |
animate-duration-2000 |
animation-duration: 2000ms |
animate-duration-3000 |
animation-duration: 300ms |
animate-duration-[value] |
animation-duration: value |
애니메이션 지연 (Animation Delay)
| Class | Property |
|---|---|
animate-delay-none |
animation-duration: 0s |
animate-delay-75 |
animation-delay: 75ms |
animate-delay-100 |
animation-delay: 100ms |
animate-delay-150 |
animation-delay: 150ms |
animate-delay-200 |
animation-delay: 200ms |
animate-delay-300 |
animation-delay: 300ms |
animate-delay-400 |
animation-delay: 400ms |
animate-delay-500 |
animation-delay: 500ms |
animate-delay-700 |
animation-delay: 700ms |
animate-delay-1000 |
animation-delay: 1000ms |
반복 횟수 (Iteration Count)
| Class | Property |
|---|---|
animate-infinite |
animation-iteration-count: infinite |
animate-once |
animation-iteration-count: 1 |
animate-twice |
animation-iteration-count: 2 |
방향 (Direction)
| Class | Property |
|---|---|
animate-normal |
animation-direction: normal |
animate-reverse |
animation-direction: reverse |
animate-alternate |
animation-direction: alternate |
animate-alternate-reverse |
animation-direction: alternate-reverse |
타이밍 함수 (Timing Function)
| Class | Property |
|---|---|
animate-ease-linear |
animation-timing-function: linear |
animate-ease-in |
animation-timing-function: cubic-bezier(0.4, 0, 1, 1) |
animate-ease-out |
animation-timing-function: cubic-bezier(0, 0, 0.2, 1) |
animate-ease-in-out |
animation-timing-function: cubic-bezier(0.4, 0, 0.2, 1) |
채움 모드 (Fill Mode)
| Class | Property |
|---|---|
animate-fill-none |
animation-fill-mode: normal |
animate-fill-forwards |
animation-fill-mode: forwards |
animate-fill-backwards |
animation-fill-mode: backwards |
animate-fill-both |
animation-fill-mode: both |
재생 상태 (Play State)
| Class | Property |
|---|---|
animate-running |
animation-play-state: running |
animate-paused |
animation-play-state: paused |
뒷면 가시성 상태 (Backface Visibility State)
| Class | Property |
|---|---|
backface-visible |
backface-visibility: visible |
backface-hidden |
backface-visibility: hidden |
페이드 인·아웃 (Fade In and Out)
값은 Tailwind CSS opacity에서 파생돼요. 예를 들어 fade-in-50 과 fade-out-20 처럼요. fade-in-[15] 같은 임의(arbitrary) 값도 지원돼요.
| Class | Property |
|---|---|
fade-in-{value} |
--p-enter-opacity: \{value\} |
fade-out-{value} |
--p-leave-opacity: \{value\} |
줌 인·아웃 (Zoom In and Out)
값은 Tailwind CSS scale에서 파생돼요. 예를 들어 zoom-in-50 과 zoom-out-75 처럼요. zoom-in-[0.8] 같은 임의 값도 지원돼요.
| Class | Property |
|---|---|
zoom-in-{value} |
--p-enter-scale: \{value\} |
zoom-out-{value} |
--p-leave-scale: \{value\} |
스핀 인·아웃 (Spin In and Out)
값은 Tailwind CSS rotate에서 파생돼요. 예를 들어 spin-in-45 과 spin-out-90 처럼요. spin-in-[60deg] 같은 임의 값도 지원돼요.
| Class | Property |
|---|---|
spin-in-{value} |
--p-enter-rotate: \{value\} |
spin-out-{value} |
--p-leave-rotate: \{value\} |
슬라이드 인·아웃 (Slide In and Out)
값은 Tailwind CSS translate에서 파생돼요. 예를 들어 slide-in-from-t-50 과 slide-out-to-l-8 처럼요. slide-in-from-b-[8px] 같은 임의 값도 지원돼요.
| Class | Property |
|---|---|
slide-in-from-t-{value} |
--p-enter-translate-y: -\{value\} |
slide-in-from-b-{value} |
--p-enter-translate-y: \{value\} |
slide-in-from-l-{value} |
--p-enter-translate-x: -\{value\} |
slide-in-from-r-{value} |
--p-enter-translate-x: \{value\} |
slide-out-to-t-{value} |
--p-leave-translate-y: -\{value\} |
slide-out-to-b-{value} |
--p-leave-translate-y: \{value\} |
slide-out-to-l-{value} |
--p-leave-translate-x: -\{value\} |
slide-out-to-r-{value} |
--p-leave-translate-x: \{value\} |