@react-native-vector-icons/common
Version:
Customizable Icons for React Native with support for image source and full styling.
59 lines (52 loc) • 2.81 kB
JavaScript
;
import { Platform } from 'react-native';
// this is a file:// uri on native, or an object with uri and display on web
// biome-ignore lint/suspicious/noExplicitAny: this is used internally with globalThis
function getIsDynamicLoadingSupported(globalObj) {
return globalObj?.expo && (Platform.OS === 'web' || typeof globalObj.expo.modules?.ExpoAsset?.downloadAsync === 'function') && typeof globalObj.expo.modules?.ExpoFontLoader?.getLoadedFonts === 'function' && typeof globalObj.expo.modules?.ExpoFontLoader?.loadAsync === 'function';
}
// biome-ignore lint/suspicious/noExplicitAny: this is used internally with globalThis
export function getIsRenderToImageSupported(globalObj) {
return globalObj?.expo && typeof globalObj.expo.modules?.ExpoFontUtils?.renderToImageAsync === 'function';
}
export function assertExpoModulesPresent(globalObj) {
if (!getIsDynamicLoadingSupported(globalObj)) {
throw new Error('Dynamic font loading not supported. Upgrade to latest expo and expo-font.');
}
}
const hasNecessaryExpoModules = (Platform.OS === 'web' || !!globalThis.expo?.modules?.ExpoAsset) && !!globalThis.expo?.modules?.ExpoFontLoader;
const hasNecessaryExpoFeatures = getIsDynamicLoadingSupported(globalThis);
let dynamicFontLoadingEnabled = hasNecessaryExpoFeatures;
export const isDynamicLoadingSupported = () => hasNecessaryExpoFeatures;
/**
* Set whether dynamic loading of fonts is enabled.
* Currently, the presence of Expo Asset and Font Loader modules is a prerequisite for enabling.
* In the future, React Native core apis will be used for dynamic font loading.
*
* @param value - whether dynamic loading of fonts is enabled
* @returns `true` if dynamic loading of fonts was successfully set. `false` otherwise.
* */
export const setDynamicLoadingEnabled = value => {
if (!hasNecessaryExpoFeatures) {
if (process.env.NODE_ENV !== 'production' && !!value) {
const message = hasNecessaryExpoModules ? 'Expo is installed, but does not support dynamic font loading. Make sure to use Expo SDK 54 or newer.' : 'Necessary Expo modules not found. Dynamic font loading is not available when necessary Expo modules are not present.';
console.error(message); // eslint-disable-line no-console
}
return false;
}
dynamicFontLoadingEnabled = !!value;
return true;
};
/**
* Whether dynamic loading of fonts is enabled.
* */
export const isDynamicLoadingEnabled = () => dynamicFontLoadingEnabled;
let dynamicLoadingErrorCallback;
/**
* Set a callback to be called when an error occurs during dynamic font loading.
* */
export const setDynamicLoadingErrorCallback = callback => {
dynamicLoadingErrorCallback = callback;
};
export const getErrorCallback = () => dynamicLoadingErrorCallback;
//# sourceMappingURL=dynamic-loading-setting.js.map