Customizable vector icons for React Native with support for multiple icon families and dynamic loading
—
Runtime font loading capabilities for Expo environments with automatic fallback handling. This feature allows fonts to be loaded on-demand without requiring them to be bundled into the app.
Control and monitor dynamic font loading behavior.
/**
* Check if dynamic font loading is currently enabled
* @returns true if dynamic loading is active
*/
function isDynamicLoadingEnabled(): boolean;
/**
* Check if the current environment supports dynamic font loading
* @returns true if Expo modules are available and compatible
*/
function isDynamicLoadingSupported(): boolean;
/**
* Enable or disable dynamic font loading
* @param value - true to enable, false to disable
* @returns true if setting was successful, false if not supported
*/
function setDynamicLoadingEnabled(value: boolean): boolean;
/**
* Set a callback function to handle dynamic loading errors
* @param callback - Function called when font loading fails
*/
function setDynamicLoadingErrorCallback(callback: ErrorCallback): void;
type ErrorCallback = (args: {
error: Error;
fontFamily: string;
fontSource: FontSource;
}) => void;These functions are used internally by icon components but are not typically called directly.
interface DynamicLoader {
/** Check if a font family is already loaded */
isLoaded(fontFamily: string): boolean;
/** Load a font asynchronously from a source */
loadFontAsync(fontFamily: string, fontSource: FontSource): Promise<void>;
}
interface ExpoAssetModule {
downloadAsync(uri: string, hash: string | undefined, type: string): Promise<string>;
}
interface ExpoFontLoaderModule {
getLoadedFonts(): string[];
loadAsync(fontFamilyAlias: string, asset: LoadAsyncAsset): Promise<void>;
}
type LoadAsyncAsset = string | { uri: string; display: string };
type FontSource = any; // Platform-specific asset sourceUsage Examples:
import {
isDynamicLoadingSupported,
isDynamicLoadingEnabled,
setDynamicLoadingEnabled,
setDynamicLoadingErrorCallback
} from '@react-native-vector-icons/common';
// Check if dynamic loading is available
if (isDynamicLoadingSupported()) {
console.log('Dynamic loading is supported');
// Enable dynamic loading
const success = setDynamicLoadingEnabled(true);
console.log('Dynamic loading enabled:', success);
} else {
console.log('Dynamic loading not supported - using static fonts');
}
// Set up error handling
setDynamicLoadingErrorCallback(({ error, fontFamily, fontSource }) => {
console.error(`Failed to load font ${fontFamily}:`, error);
// Could trigger fallback UI or retry logic
});
// Check current state
const isEnabled = isDynamicLoadingEnabled();
console.log('Dynamic loading is currently:', isEnabled ? 'enabled' : 'disabled');Dynamic loading works automatically with icon components created using createIconSet when fontSource is provided:
import { createIconSet } from '@react-native-vector-icons/common';
// This icon component will use dynamic loading if enabled
const DynamicIcon = createIconSet(glyphMap, {
postScriptName: 'CustomFont',
fontFileName: 'CustomFont.ttf',
fontSource: require('./assets/CustomFont.ttf') // Enables dynamic loading
});
// Component behavior:
// 1. Initially renders empty while font loads
// 2. Re-renders with icon once font is available
// 3. Falls back to error callback if loading failsDynamic loading requires specific Expo modules to be available:
// Dynamic loading adds initial delay for first render
// Consider pre-loading critical fonts:
import { setDynamicLoadingEnabled } from '@react-native-vector-icons/common';
// Enable early in app lifecycle
setDynamicLoadingEnabled(true);
// Icons will show loading state until fonts are ready
// Subsequent renders are instant once fonts are cachedCommon failure cases and their handling:
setDynamicLoadingErrorCallback(({ error, fontFamily }) => {
if (error.message.includes('Network')) {
// Handle network failures
console.log('Font download failed - using fallback');
} else if (error.message.includes('Expo')) {
// Handle Expo module issues
console.log('Expo modules not properly configured');
}
});
// Automatic fallbacks:
// 1. Missing Expo modules -> static loading
// 2. Network failures -> error callback + empty icon
// 3. Invalid fonts -> error callback + '?' characterInstall with Tessl CLI
npx tessl i tessl/npm-react-native-vector-icons