apps/docs/content/docs/en/native/getting-started/(handbook)/provider.mdx
The HeroUINativeProvider is the root provider component that configures and initializes HeroUI Native in your React Native application. It provides global configuration and portal management for your application.
The provider serves as the main entry point for HeroUI Native, wrapping your application with essential contexts and configurations:
SafeAreaListener and syncs them with Uniwind for use in Tailwind classes (e.g., pb-safe-offset-3)Wrap your application root with the provider:
import { HeroUINativeProvider } from 'heroui-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<HeroUINativeProvider></HeroUINativeProvider>
</GestureHandlerRootView>
);
}
The provider accepts a config prop with the following options:
Global settings for all Text components within HeroUI Native. These props are carefully selected to include only those that make sense to configure globally across all Text components in the application:
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}>
</HeroUINativeProvider>
);
}
Global settings for all TextInput-based components within HeroUI Native (e.g. Input, TextArea, SearchField, InputGroup, InputOTP). These props are carefully selected to include only those that make sense to configure globally across all inputs in the application:
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}>
</HeroUINativeProvider>
);
}
Global animation configuration for the entire application:
const config: HeroUINativeConfig = {
// Disable all animations across the application (cascades to all children)
animation: 'disable-all',
};
Control developer-facing informational messages displayed in the console:
const config: HeroUINativeConfig = {
devInfo: {
// Disable styling principles information message
stylingPrinciples: false,
},
};
Configure the global toast system including insets, default props, and wrapper components. You can also disable the toast provider entirely:
Option 1: Disable Toast Provider
const config: HeroUINativeConfig = {
// Disable toast provider entirely
toast: false,
// or
toast: 'disabled',
};
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>
),
},
};
Here's a comprehensive example showing all configuration options:
import { HeroUINativeProvider } from 'heroui-native';
import type { HeroUINativeConfig } from 'heroui-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 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>
);
}
When using Expo Router, wrap your root layout:
// 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>
);
}
The HeroUINativeProvider internally composes multiple providers:
HeroUINativeProvider
├── SafeAreaListener (handles safe area insets updates)
│ └── GlobalAnimationSettingsProvider (animation configuration)
│ └── TextComponentProvider (text configuration)
│ └── TextInputComponentProvider (text input configuration)
│ └── ToastProvider (toast configuration, conditionally rendered)
│ └── Your App
│ └── PortalHost (for overlays)
The provider automatically wraps your application with SafeAreaListener from react-native-safe-area-context. This component listens to safe area insets and frame changes without triggering re-renders, and automatically updates Uniwind with the latest insets via the onChange callback.
HeroUINativeProviderRaw is a lightweight variant of HeroUINativeProvider designed for bundle optimization. It excludes ToastProvider and PortalHost, giving you a bare minimum starting point where you only install and add what you actually need.
Use HeroUINativeProviderRaw when you want full control over which dependencies are included in your bundle. With the raw provider imported from heroui-native/provider-raw, the following dependencies are optional and only required if you use the corresponding components:
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}>
</HeroUINativeProviderRaw>
</GestureHandlerRootView>
);
}
If you need toast or portal functionality with the raw provider, add them yourself:
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>
<PortalHost />
</ToastProvider>
</HeroUINativeProviderRaw>
</GestureHandlerRootView>
);
}
HeroUINativeProviderRaw
├── SafeAreaListener (handles safe area insets updates)
│ └── GlobalAnimationSettingsProvider (animation configuration)
│ └── TextComponentProvider (text configuration)
│ └── TextInputComponentProvider (text input configuration)
│ └── Your App
Always use a single HeroUINativeProvider at the root of your app. Don't nest multiple providers:
// ❌ Bad
<HeroUINativeProvider>
<SomeComponent>
<HeroUINativeProvider>
<AnotherComponent />
</HeroUINativeProvider>
</SomeComponent>
</HeroUINativeProvider>
// ✅ Good
<HeroUINativeProvider>
<SomeComponent>
<AnotherComponent />
</SomeComponent>
</HeroUINativeProvider>
Define your configuration outside the component to prevent recreating on each render:
// ❌ 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>
);
}
Consider accessibility when configuring text props:
const config: HeroUINativeConfig = {
textProps: {
// Allow font scaling for accessibility
allowFontScaling: true,
// But limit maximum scale
maxFontSizeMultiplier: 1.5,
},
};
The provider is fully typed. Import types for better IDE support:
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
},
// 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,
},
},
};