Provider (프로바이더)
HeroUINativeProvider는 HeroUI Native를 React Native 앱에서 구성하고 초기화하는 루트 프로바이더 컴포넌트예요. 안전 영역(insets), 텍스트, 텍스트 입력, 애니메이션, 토스트, 레이아웃 방향 설정 등 애플리케이션 전역 구성과 포털 관리를 제공해요.
출처: 문서
본문
HeroUINativeProvider는 React Native 애플리케이션에서 HeroUI Native를 구성하고 초기화하는 루트 프로바이더 컴포넌트예요. 애플리케이션을 위한 전역 구성과 포털 관리를 제공해요.
Overview
프로바이더는 HeroUI Native의 메인 진입점 역할을 하며, 애플리케이션을 필수 컨텍스트와 구성으로 감싸요:
- Safe Area Insets:
SafeAreaListener를 통해 안전 영역 insets 업데이트를 자동 처리하고, Tailwind 클래스(예:pb-safe-offset-3)에서 사용할 수 있도록 Uniwind과 동기화해요 - Text Configuration: 모든 HeroUI 컴포넌트에서 일관성을 위한 전역 텍스트 컴포넌트 설정
- Text Input Configuration: 모든 HeroUI 입력 컴포넌트에서 일관성을 위한 전역 텍스트 입력 설정
- Animation Configuration: 애플리케이션 전체에서 모든 애니메이션을 비활성화하는 전역 애니메이션 제어
- Toast Configuration: insets, 기본 prop, 래퍼 컴포넌트를 포함한 전역 토스트 시스템 구성
- Layout Direction: 스타일 대신 JavaScript로 실행되는 컴포넌트 로직이 사용하는 전역 우측-좌측(RTL) 플래그
- Portal Management: 앱 계층 위에 렌더링되는 오버레이, 모달 및 기타 컴포넌트를 처리해요
Basic Setup
애플리케이션 루트를 프로바이더로 감싸세요:
import { HeroUINativeProvider } from 'heroui-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<HeroUINativeProvider>{/* Your app content */}</HeroUINativeProvider>
</GestureHandlerRootView>
);
}
Configuration Options
프로바이더는 다음 옵션을 가진 config prop을 받아요:
Text Component Configuration
HeroUI Native 내 모든 Text 컴포넌트를 위한 전역 설정이에요. 이런 prop들은 애플리케이션의 모든 Text 컴포넌트에서 전역으로 구성하는 것이 합리적인 것만 신중하게 골랐어요:
import { HeroUINativeProvider } from 'heroui-native';
import type { HeroUINativeConfig } from 'heroui-native';
const config: HeroUINativeConfig = {
textProps: {
// Disable font scaling for accessibility
allowFontScaling: false,
// Auto-adjust font size to fit container
adjustsFontSizeToFit: false,
// Maximum font size multiplier when scaling
maxFontSizeMultiplier: 1.5,
// Minimum font scale (iOS only, 0.01-1.0)
minimumFontScale: 0.5,
},
};
export default function App() {
return (
<HeroUINativeProvider config={config}>
{/* Your app content */}
</HeroUINativeProvider>
);
}
Text Input Component Configuration
HeroUI Native 내 모든 TextInput 기반 컴포넌트(예: Input, TextArea, SearchField, InputGroup, InputOTP)를 위한 전역 설정이에요. 이런 prop들은 애플리케이션의 모든 입력에서 전역으로 구성하는 것이 합리적인 것만 신중하게 골랐어요:
import { HeroUINativeProvider } from 'heroui-native';
import type { HeroUINativeConfig } from 'heroui-native';
const config: HeroUINativeConfig = {
textInputProps: {
// Respect Text Size accessibility settings
allowFontScaling: false,
// Maximum font size multiplier when allowFontScaling is enabled
maxFontSizeMultiplier: 1.5,
},
};
export default function App() {
return (
<HeroUINativeProvider config={config}>
{/* Your app content */}
</HeroUINativeProvider>
);
}
Note: 이런 prop들은 기본값으로 적용돼요. 개별 입력 컴포넌트에 직접 전달된 prop은 해당 전역 값을 재정의해요.
Animation Configuration
애플리케이션 전체를 위한 전역 애니메이션 구성이에요:
const config: HeroUINativeConfig = {
// Disable all animations across the application (cascades to all children)
animation: 'disable-all',
};
Note:
'disable-all'로 설정하면 애플리케이션 전체의 모든 애니메이션이 비활성화돼요. 접근성 또는 성능 최적화에 유용해요.
Developer Information Configuration
콘솔에 표시되는 개발자용 정보 메시지를 제어해요:
const config: HeroUINativeConfig = {
devInfo: {
// Disable styling principles information message
stylingPrinciples: false,
},
};
Note: 기본적으로 정보 메시지는 활성화돼 있어요. 개발 중 콘솔에 표시되는 스타일링 원칙 메시지를 비활성화하려면
stylingPrinciples: false로 설정하세요.
Toast Configuration
insets, 기본 prop, 래퍼 컴포넌트를 포함한 전역 토스트 시스템을 구성해요. 토스트 프로바이더를 완전히 비활성화할 수도 있어요:
Option 1: Disable Toast Provider
const config: HeroUINativeConfig = {
// Disable toast provider entirely
toast: false,
// or
toast: 'disabled',
};
Note: toast가 비활성화(
false또는'disabled')되면ToastProvider가 렌더링되지 않고, 애플리케이션에서 토스트 기능을 사용할 수 없게 돼요.
Option 2: Configure Toast Provider
import { KeyboardAvoidingView } from 'react-native';
const config: HeroUINativeConfig = {
toast: {
// Global toast configuration (used as defaults for all toasts)
defaultProps: {
variant: 'default',
placement: 'top',
isSwipeable: true,
animation: true,
},
// Insets for spacing from screen edges (added to safe area insets)
insets: {
top: 0, // Default: iOS = 0, Android = 12
bottom: 6, // Default: iOS = 6, Android = 12
left: 12, // Default: 12
right: 12, // Default: 12
},
// Maximum number of visible toasts before opacity starts fading
maxVisibleToasts: 3,
// Custom wrapper function to wrap the toast content
contentWrapper: (children) => (
<KeyboardAvoidingView
behavior="padding"
keyboardVerticalOffset={24}
className="flex-1"
>
{children}
</KeyboardAvoidingView>
),
},
};
Layout Direction Configuration
HeroUI Native 컴포넌트에 어떤 레이아웃 방향으로 렌더링하는지 알려줘요:
import { I18nManager } from 'react-native';
const config: HeroUINativeConfig = {
isRTL: I18nManager.isRTL,
};
컴포넌트는 Yoga 논리 속성(start/end)으로 스스로 미러링되므로 대부분의 UI는 자동으로 뒤집혀요. 이 플래그는 Slider 제스처 델타와 popover의 start/end 정렬처럼 JavaScript로 실행되는 로직만 다뤄요.
Note:
isRTL은 기본적으로I18nManager.isRTL이에요. 앱이 전역 RTL 상태와 다른 방향으로 렌더링할 때만 설정하세요.
서브트리의 방향을 재정의하려면 Uniwind의 rtl: variant용 LayoutDirection 및 Yoga 레이아웃용 direction 스타일과 함께 LayoutDirectionScope로 감싸세요:
<LayoutDirection rtl={false}>
<View className="flex-1" style={{ direction: 'ltr' }}>
<LayoutDirectionScope isRTL={false}>
{children}
<PortalHost name="preview" />
</LayoutDirectionScope>
</View>
</LayoutDirection>
Note: 포털 콘텐츠는 앱 루트에 렌더링되어 스코프를 벗어나므로, 그 안에 커스텀
name을 가진PortalHost를 렌더링하고 일치하는hostName을 오버레이에 전달하세요. 자신의 컴포넌트에서는useIsRTL훅으로 유효 방향을 읽으세요.
Complete Example
모든 구성 옵션을 보여주는 종합 예제예요:
import { HeroUINativeProvider } from 'heroui-native';
import type { HeroUINativeConfig } from 'heroui-native';
import { I18nManager } from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
const config: HeroUINativeConfig = {
// Global text configuration
textProps: {
minimumFontScale: 0.5,
maxFontSizeMultiplier: 1.5,
allowFontScaling: true,
adjustsFontSizeToFit: false,
},
// Global text input configuration
textInputProps: {
allowFontScaling: true,
maxFontSizeMultiplier: 1.5,
},
// Global animation configuration
animation: 'disable-all', // Optional: disable all animations
// Developer information messages configuration
devInfo: {
stylingPrinciples: true, // Optional: disable styling principles message
},
// Global layout direction (defaults to I18nManager.isRTL)
isRTL: I18nManager.isRTL,
// Global toast configuration
// Option 1: Configure toast with custom settings
toast: {
defaultProps: {
variant: 'default',
placement: 'top',
},
insets: {
top: 0,
bottom: 6,
left: 12,
right: 12,
},
maxVisibleToasts: 3,
},
// Option 2: Disable toast entirely
// toast: false,
// or
// toast: 'disabled',
};
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<HeroUINativeProvider config={config}>
<YourApp />
</HeroUINativeProvider>
</GestureHandlerRootView>
);
}
Integration with Expo Router
Expo Router를 사용할 때는 루트 레이아웃을 감싸세요:
// app/_layout.tsx
import { HeroUINativeProvider } from 'heroui-native';
import type { HeroUINativeConfig } from 'heroui-native';
import { Stack } from 'expo-router';
const config: HeroUINativeConfig = {
textProps: {
minimumFontScale: 0.5,
maxFontSizeMultiplier: 1.5,
},
};
export default function RootLayout() {
return (
<HeroUINativeProvider config={config}>
<Stack />
</HeroUINativeProvider>
);
}
Architecture
Provider Hierarchy
HeroUINativeProvider는 내부적으로 여러 프로바이더를 구성해요:
HeroUINativeProvider
├── SafeAreaListener (handles safe area insets updates)
│ └── LayoutDirectionProvider (layout direction configuration)
│ └── GlobalAnimationSettingsProvider (animation configuration)
│ └── TextComponentProvider (text configuration)
│ └── TextInputComponentProvider (text input configuration)
│ └── ToastProvider (toast configuration, conditionally rendered)
│ └── Your App
│ └── PortalHost (for overlays)
Note:
ToastProvider는toast구성에 따라 조건부로 렌더링돼요.toast가false또는'disabled'로 설정되면ToastProvider가 렌더링되지 않고, 앱 콘텐츠와PortalHost는TextInputComponentProvider바로 아래에 렌더링돼요.
Safe Area Insets Handling
프로바이더는 react-native-safe-area-context의 SafeAreaListener로 애플리케이션을 자동으로 감싸요. 이 컴포넌트는 리렌더링을 유발하지 않고 안전 영역 insets와 프레임 변경을 수신하며, onChange 콜백을 통해 Uniwind를 최신 insets로 자동 업데이트해요.
Raw Provider
HeroUINativeProviderRaw는 번들 최적화를 위해 설계된 HeroUINativeProvider의 경량 variant예요. ToastProvider와 PortalHost를 제외해서, 실제로 필요한 것만 설치하고 추가할 수 있는 최소 시작점을 제공해요. 그 HeroUINativeConfigRaw 구성은 toast를 제외하고 HeroUINativeConfig와 동일한 옵션을 받으므로, textProps, textInputProps, animation, devInfo, isRTL은 모두 동일하게 동작해요.
When to Use
번들에 포함할 의존성을 완전히 제어하고 싶을 때 HeroUINativeProviderRaw를 사용하세요. heroui-native/provider-raw에서 raw 프로바이더를 가져오면 다음 의존성은 선택 사항이 되며, 해당 컴포넌트를 사용할 때만 필요해요:
- react-native-screens -- 오버레이 컴포넌트(Popover, Dialog)에 필요
- @gorhom/bottom-sheet -- BottomSheet 컴포넌트에 필요
- react-native-svg -- 아이콘을 사용하는 컴포넌트(Accordion, Alert, Checkbox 등)에 필요
Setup
import {
HeroUINativeProviderRaw,
type HeroUINativeConfigRaw,
} from 'heroui-native/provider-raw';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
const config: HeroUINativeConfigRaw = {
textProps: {
maxFontSizeMultiplier: 1.5,
},
};
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<HeroUINativeProviderRaw config={config}>
{/* Your app content */}
</HeroUINativeProviderRaw>
</GestureHandlerRootView>
);
}
Adding Toast and Portal Manually
raw 프로바이더로 토스트나 포털 기능이 필요하면 직접 추가하세요:
import { HeroUINativeProviderRaw } from 'heroui-native/provider-raw';
import { PortalHost } from 'heroui-native/portal';
import { ToastProvider } from 'heroui-native/toast';
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<HeroUINativeProviderRaw>
<ToastProvider>
{/* Your app content */}
<PortalHost />
</ToastProvider>
</HeroUINativeProviderRaw>
</GestureHandlerRootView>
);
}
Provider Hierarchy
HeroUINativeProviderRaw
├── SafeAreaListener (handles safe area insets updates)
│ └── LayoutDirectionProvider (layout direction configuration)
│ └── GlobalAnimationSettingsProvider (animation configuration)
│ └── TextComponentProvider (text configuration)
│ └── TextInputComponentProvider (text input configuration)
│ └── Your App
Best Practices
1. Single Provider Instance
앱 루트에는 항상 단일 HeroUINativeProvider를 사용하세요. 여러 프로바이더를 중첩하지 마세요:
// ❌ Bad
<HeroUINativeProvider>
<SomeComponent>
<HeroUINativeProvider> {/* Don't do this */}
<AnotherComponent />
</HeroUINativeProvider>
</SomeComponent>
</HeroUINativeProvider>
// ✅ Good
<HeroUINativeProvider>
<SomeComponent>
<AnotherComponent />
</SomeComponent>
</HeroUINativeProvider>
2. Configuration Object
매 렌더링마다 재생성되지 않도록 구성은 컴포넌트 밖에서 정의하세요:
// ❌ Bad
function App() {
return (
<HeroUINativeProvider
config={{
textProps: {
/* inline config */
},
}}
>
{/* ... */}
</HeroUINativeProvider>
);
}
// ✅ Good
const config: HeroUINativeConfig = {
textProps: {
maxFontSizeMultiplier: 1.5,
},
};
function App() {
return (
<HeroUINativeProvider config={config}>{/* ... */}</HeroUINativeProvider>
);
}
3. Text Configuration
텍스트 prop을 구성할 때는 접근성을 고려하세요:
const config: HeroUINativeConfig = {
textProps: {
// Allow font scaling for accessibility
allowFontScaling: true,
// But limit maximum scale
maxFontSizeMultiplier: 1.5,
},
};
TypeScript Support
프로바이더는 완전히 타입이 지정돼 있어요. 더 나은 IDE 지원을 위해 타입을 가져오세요:
import { HeroUINativeProvider, type HeroUINativeConfig } from 'heroui-native';
const config: HeroUINativeConfig = {
// Full type safety and autocomplete
textProps: {
allowFontScaling: true,
maxFontSizeMultiplier: 1.5,
},
textInputProps: {
allowFontScaling: true,
maxFontSizeMultiplier: 1.5,
},
animation: 'disable-all', // Optional: disable all animations
devInfo: {
stylingPrinciples: true, // Optional: disable styling principles message
},
isRTL: false, // Optional: layout direction, defaults to I18nManager.isRTL
// Toast configuration options:
// - false or 'disabled': Disable toast provider
// - ToastProviderProps object: Configure toast settings
toast: {
defaultProps: {
variant: 'default',
placement: 'top',
},
insets: {
top: 0,
bottom: 6,
left: 12,
right: 12,
},
},
};
Related
- Quick Start - 기본 설정 안내
- Theming - 색상과 테마 커스터마이징
- Styling - Tailwind로 스타일링하기