테마 (Theming)
HeroUI Native는 CSS 변수를 기반으로 테마를 구성해요. 표준 CSS만으로 색상부터 컴포넌트 스타일까지 모든 것을 커스터마이징할 수 있지요. 이번 가이드에서는 테마가 동작하는 원리와 커스텀 테마·커스텀 색상·커스텀 폰트를 적용하는 방법을 배워볼게요.
출처: 문서
본문
HeroUI Native는 표준 CSS를 사용해 테마를 구성합니다. 색상부터 컴포넌트 스타일까지 모든 것을 커스터마이징할 수 있어요.
동작 원리 (How It Works)
HeroUI Native의 테마 시스템은 Uniwind를 통해 Tailwind CSS v4 테마 위에 구축되어 있어요. heroui-native/styles를 가져오면 Tailwind에 내장된 색상 팔레트를 사용하고, 이를 시맨틱 변수에 매핑하며, 라이트/다크 테마를 자동으로 전환하고, CSS 레이어와 @theme 지시어를 활용해 구성을 정리해요.
네이밍 패턴 (Naming pattern):
- 접미사가 없는 색상은 배경입니다 (예:
--accent) -foreground가 붙은 색상은 해당 배경 위의 텍스트용이에요 (예:--accent-foreground)
빠른 시작 (Quick Start)
컴포넌트에 색상 적용하기:
import { View, Text } from 'react-native';
<View className="bg-background flex-1">
<Text className="text-foreground">Your app content</Text>
</View>;
테마 전환하기:
HeroUI Native는 Uniwind을 통해 다크 모드를 자동으로 지원해요. 시스템 기본 설정이나 수동 선택에 따라 라이트/다크 변형 사이를 전환합니다:
import { Uniwind, useUniwind } from 'uniwind';
import { Button } from 'heroui-native';
function ThemeToggle() {
const { theme } = useUniwind();
return (
<Button
onPress={() => Uniwind.setTheme(theme === 'light' ? 'dark' : 'light')}
>
<Button.Label>
Toggle {theme === 'light' ? 'Dark' : 'Light'} Mode
</Button.Label>
</Button>
);
}
색상 오버라이드하기:
/* global.css */
@layer theme {
@variant light {
/* Override any color variable */
--accent: oklch(0.65 0.25 270); /* Custom indigo accent */
--success: oklch(0.65 0.15 155);
}
@variant dark {
--accent: oklch(0.65 0.25 270);
--success: oklch(0.75 0.12 155);
}
}
참고: 전체 색상 팔레트와 시각적 참조는 Colors 문서를 확인하세요.
나만의 테마 만들기:
Uniwind의 variant 시스템을 사용해 여러 테마를 만들 수 있어요. 완전한 커스텀 테마 문서는 Uniwind Custom Themes Guide를 참고하세요.
참고:
중요: 모든 테마는 동일한 변수를 정의해야 해요. 필요한 모든 변수의 전체 목록은 Default Theme 섹션을 참고하세요.
/* global.css */
@layer theme {
:root {
@variant ocean-light {
/* Base Colors */
--background: oklch(0.95 0.02 230);
--foreground: oklch(0.25 0.04 230);
/* Surface: Used for non-overlay components (cards, accordions, disclosure groups) */
--surface: oklch(0.98 0.01 230);
--surface-foreground: oklch(0.3 0.045 230);
--surface-secondary: oklch(0.96 0.012 230);
--surface-secondary-foreground: oklch(0.3 0.045 230);
--surface-tertiary: oklch(0.94 0.015 230);
--surface-tertiary-foreground: oklch(0.3 0.045 230);
/* Overlay: Used for floating/overlay components (dialogs, popovers, modals, menus) */
--overlay: oklch(0.998 0.003 230);
--overlay-foreground: oklch(0.3 0.045 230);
--backdrop: oklch(0% 0 0 / 20%);
--muted: oklch(0.55 0.035 230);
--default: oklch(0.94 0.018 230);
--default-foreground: oklch(0.4 0.05 230);
/* Accent */
--accent: oklch(0.6 0.2 230);
--accent-foreground: oklch(0.98 0.005 230);
/* Form Field Defaults - Colors */
--field-background: oklch(0.98 0.01 230);
--field-foreground: oklch(0.25 0.04 230);
--field-placeholder: var(--muted);
--field-border: transparent;
/* Status Colors */
--success: oklch(0.72 0.14 165);
--success-foreground: oklch(0.25 0.08 165);
--warning: oklch(0.78 0.12 85);
--warning-foreground: oklch(0.3 0.08 85);
--danger: oklch(0.68 0.18 15);
--danger-foreground: oklch(0.98 0.005 15);
/* Component Colors */
--segment: oklch(0.98 0.01 230);
--segment-foreground: oklch(0.25 0.04 230);
/* Misc Colors */
--border: oklch(0 0 0 / 0%);
--separator: oklch(0.91 0.015 230);
--focus: var(--accent);
--link: oklch(0.62 0.17 230);
/* Shadows */
--surface-shadow:
0 2px 4px 0 rgba(0, 0, 0, 0.04), 0 1px 2px 0 rgba(0, 0, 0, 0.06),
0 0 1px 0 rgba(0, 0, 0, 0.06);
--overlay-shadow:
0 2px 8px 0 rgba(0, 0, 0, 0.02), 0 -6px 12px 0 rgba(0, 0, 0, 0.01),
0 14px 28px 0 rgba(0, 0, 0, 0.03);
--field-shadow:
0 2px 4px 0 rgba(0, 0, 0, 0.04), 0 1px 2px 0 rgba(0, 0, 0, 0.06),
0 0 1px 0 rgba(0, 0, 0, 0.06);
}
@variant ocean-dark {
/* Base Colors */
--background: oklch(0.15 0.04 230);
--foreground: oklch(0.94 0.01 230);
/* Surface: Used for non-overlay components (cards, accordions, disclosure groups) */
--surface: oklch(0.2 0.048 230);
--surface-foreground: oklch(0.9 0.015 230);
--surface-secondary: oklch(0.24 0.046 230);
--surface-secondary-foreground: oklch(0.9 0.015 230);
--surface-tertiary: oklch(0.27 0.044 230);
--surface-tertiary-foreground: oklch(0.9 0.015 230);
/* Overlay: Used for floating/overlay components (dialogs, popovers, modals, menus) */
--overlay: oklch(0.23 0.045 230);
--overlay-foreground: oklch(0.9 0.015 230);
--backdrop: oklch(0% 0 0 / 20%);
--muted: oklch(0.5 0.04 230);
--default: oklch(0.25 0.05 230);
--default-foreground: oklch(0.88 0.018 230);
/* Accent */
--accent: oklch(0.72 0.21 230);
--accent-foreground: oklch(0.15 0.04 230);
/* Form Field Defaults - Colors */
--field-background: var(--default);
--field-foreground: var(--foreground);
--field-placeholder: var(--muted);
--field-border: transparent;
/* Status Colors */
--success: oklch(0.68 0.16 165);
--success-foreground: oklch(0.95 0.008 165);
--warning: oklch(0.75 0.14 90);
--warning-foreground: oklch(0.2 0.04 90);
--danger: oklch(0.65 0.2 20);
--danger-foreground: oklch(0.95 0.008 20);
/* Component Colors */
--segment: oklch(0.22 0.046 230);
--segment-foreground: oklch(0.9 0.015 230);
/* Misc Colors */
--border: oklch(0 0 0 / 0%);
--separator: oklch(0.28 0.045 230);
--focus: var(--accent);
--link: oklch(0.75 0.18 230);
/* Shadows */
--surface-shadow: 0 0 0 0 transparent inset; /* No shadow on dark mode */
--overlay-shadow: 0 0 1px 0 rgba(255, 255, 255, 0.3) inset;
--field-shadow: 0 0 0 0 transparent inset; /* Transparent shadow to allow ring utilities to work */
}
}
}
중요: 커스텀 테마를 추가할 때는 Metro 설정에 등록해야 해요:
// metro.config.js
const { withUniwindConfig } = require('uniwind/metro');
const {
wrapWithReanimatedMetroConfig,
} = require('react-native-reanimated/metro-config');
const config = {
// ... your existing config
};
module.exports = withUniwindConfig(wrapWithReanimatedMetroConfig(config), {
cssEntryFile: './global.css',
dtsFile: './src/uniwind.d.ts',
extraThemes: ['ocean-light', 'ocean-dark'],
});
앱에서 테마를 적용해요:
import { Uniwind } from 'uniwind';
import { Button } from 'heroui-native';
function App() {
return (
<Button onPress={() => Uniwind.setTheme('ocean-light')}>
<Button.Label>Ocean Theme</Button.Label>
</Button>
);
}
커스텀 색상 추가 (Adding Custom Colors)
테마에 나만의 시맨틱 색상을 추가해요:
@layer theme {
@variant light {
--info: oklch(0.6 0.15 210);
--info-foreground: oklch(0.98 0 0);
}
@variant dark {
--info: oklch(0.7 0.12 210);
--info-foreground: oklch(0.15 0 0);
}
}
/* Make the color available to Tailwind */
@theme inline {
--color-info: var(--info);
--color-info-foreground: var(--info-foreground);
}
이제 컴포넌트에서 사용해요:
import { View, Text } from 'react-native';
<View className="bg-info p-4 rounded-lg">
<Text className="text-info-foreground">Info message</Text>
</View>;
커스텀 폰트 (Custom Fonts)
앱에서 커스텀 폰트 패밀리를 사용하려면 폰트를 로드한 다음 폰트 CSS 변수를 오버라이드해야 해요.
1. 앱에서 폰트 로드 (Load Fonts in Your App)
먼저 커스텀 폰트를 로드해요 (예를 들어 Expo의 useFonts 훅 사용):
import { useFonts } from 'expo-font';
import { HeroUINativeProvider } from 'heroui-native';
import {
YourFont_400Regular,
YourFont_500Medium,
YourFont_600SemiBold,
} from '@expo-google-fonts/your-font';
export default function App() {
const [fontsLoaded] = useFonts({
YourFont_400Regular,
YourFont_500Medium,
YourFont_600SemiBold,
});
if (!fontsLoaded) {
return null; // Or return a loading screen
}
return <HeroUINativeProvider>{/* Your app content */}</HeroUINativeProvider>;
}
2. 폰트 CSS 변수 구성 (Configure Font CSS Variables)
폰트를 로드한 뒤 global.css 파일에서 폰트 CSS 변수를 오버라이드해요:
@theme {
--font-normal: 'YourFont-400Regular';
--font-medium: 'YourFont-500Medium';
--font-semibold: 'YourFont-600SemiBold';
--font-bold: 'YourFont-700Bold';
}
참고: CSS 변수의 폰트 이름은 로드한 폰트의 PostScript 이름과 일치해야 해요. 폰트 패키지 문서를 확인하거나 useFonts 훅에 나타난 그대로의 폰트 이름을 사용하세요.
HeroUI Native의 모든 컴포넌트는 이 폰트 변수를 자동으로 사용해, 앱 전체에서 일관된 타이포그래피를 보장해요.
폰트 두께가 결정되는 방식 (How Font Weights Resolve)
컴포넌트는 font-normal, font-medium, font-semibold, font-bold 유틸리티를 통해 두께를 요청해요. 이 유틸리티는 위의 변수를 정의했는지에 따라 다르게 처리됩니다:
- 커스텀 폰트가 정의된 경우 — 두께가 일치하는 패밀리를 선택하므로,
--font-semibold는 이미 semibold인 페이스를 가리켜야 해요. 숫자형font-weight는 적용되지 않는데, 숫자형 두께를 단일 두께 페이스와 짝지으면 iOS가 패밀리에서 가장 무거운 페이스를 선택하기 때문이에요. - 커스텀 폰트가 없는 경우 — 동일한 두께가 시스템 폰트에 숫자형
font-weight로 적용돼요.
어느 하나라도 정의한다면 네 변수를 모두 정의하세요. 두 모드는 두께별로 처리되므로, 변수가 없는 두께는 플랫폼 폰트의 숫자형 두께로 대체되는 반면 나머지는 커스텀 패밀리를 유지해요. 서체를 섞는 것 외에도, 숫자형 두께와 단일 두께 페이스를 짝지으면 iOS가 패밀리의 가장 무거운 페이스로 텍스트를 렌더링할 수 있는데, 이는 정확히 iOS가 잘못 처리하는 경우이기 때문이에요.
변수 참조 (Variables Reference)
HeroUI는 세 가지 유형의 변수를 정의해요:
- Base Variables —
--white,--black같은 변하지 않는 값 - Theme Variables — 라이트/다크 테마 사이에서 바뀌는 색상
- Calculated Variables — 자동으로 생성되는 hover(누름) 상태와 크기 변형
전체 참조는 다음을 확인하세요: Colors Documentation, Default Theme Variables, Shared Theme Utilities
계산 변수 (Tailwind):
Tailwind의 @theme 지시어를 사용해 hover(누름) 상태와 radius 변형에 대한 계산 변수를 자동으로 생성해요. 이들은 theme.css에 정의되어 있어요:
Field border 유틸리티: 필드 테두리 두께에는
border-field-width를, 필드 테두리 색상에는border-field-border를 사용하세요. 둘 다 필요할 땐 함께 사용하세요 (border-field-width border-field-border).border-field클래스는--color-field의 테두리 색상 유틸리티로만 남아 있으니 두께에는 사용하지 마세요. 이전에 두께에border-field를 사용했다면border-field-width로 마이그레이션하세요. 두께는--field-border-width원시값으로 커스터마이징해요.
이제 폼 컨트롤은 --field-* 변수와 그 계산된 hover/focus 변형에 의존해요. 버튼이나 카드 같은 표면에 영향을 주지 않으면서 input, checkbox, radio, OTP 슬롯을 다시 스타일링하려면 테마에서 이 변수들을 업데이트하세요.
선명한 팔레트 (Vibrant Palette)
기본적으로 HeroUI Native는 시맨틱 색상을 전경색과 혼합해 부드러운 배경에서 더 나은 대비를 제공하는 접근 가능한 soft 전경색을 사용해요. 더 채도가 높고 선명한 soft 전경색을 선호한다면, 기본 스타일과 함께 선택적 heroui-native/styles/vibrant 스타일시트를 가져오세요:
/* global.css */
@import "heroui-native/styles";
@import "heroui-native/styles/vibrant"; /* [!code highlight] */
이렇게 하면 모든 *-soft-foreground 변수 (accent, success, warning, danger)가 시맨틱 색상 92%에 전경색 8%만 혼합된 값으로 바뀌어요. 원본 색상에 더 가깝지만 대비는 약간 높아져요. Alert, Avatar, Button, Chip, Toast 같은 컴포넌트는 soft 변형에서 자동으로 새 soft 전경색을 적용해요 — 컴포넌트 prop 변경은 필요 없어요.
| Mode | Accessible (default) | Vibrant |
|---|---|---|
| Soft foreground | color-mix(color 70-80%, foreground 30-40%) |
color-mix(color 92%, foreground 8%) |
참고:
선명한 팔레트는 대비보다 시각적 채도를 우선시해요. 특히 더 밝은 accent 색상의 일부 조합에서는 WCAG 접근성 가이드라인을 충족하지 못할 수 있어요.
참고:
선택적 선명한 팔레트는 v1.0.4부터 사용할 수 있어요.