CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native-vector-icons

Customizable vector icons for React Native with support for multiple icon families and dynamic loading

Pending
Overview
Eval results
Files

dynamic-loading.mddocs/

Dynamic Font 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.

Capabilities

Loading State Management

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;

Internal Loading Interface

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 source

Usage 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');

Integration with Icon Components

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 fails

Environment Requirements

Dynamic loading requires specific Expo modules to be available:

  • ExpoAsset: For downloading font files (native platforms)
  • ExpoFontLoader: For registering fonts with the system
  • Expo SDK: Version 54 or newer for full compatibility

Platform Support

  • Expo Go: Not supported (fonts must be pre-bundled)
  • Expo Development Build: Fully supported
  • Bare React Native: Not supported without Expo modules
  • Web: Supported through different loading mechanism

Performance Considerations

// 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 cached

Error Scenarios

Common 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 + '?' character

Install with Tessl CLI

npx tessl i tessl/npm-react-native-vector-icons

docs

core-creation.md

dynamic-loading.md

icon-families.md

image-generation.md

index.md

tile.json