UNPKG

react-native-unit-components

Version:

Unit React Native components

64 lines (61 loc) 1.98 kB
import { FontWeight } from '../types/shared'; import { UnitSDK } from '../unitSdkManager/UnitSdkManager'; const getFontWeightName = (familyFonts, fontWeight) => { /** * Given family variants and the required weight * Return the closet font that defined by the client. */ if (!fontWeight || fontWeight === 'normal') return 'Regular'; if (fontWeight === 'bold') return 'Bold'; const fontWeightNumber = Number(fontWeight); // Select the closet variant let selectedFontWeight = FontWeight.Regular.valueOf(); let minDistance = 1000; familyFonts.forEach(fontData => { const currDistance = Math.abs(fontData.fontWeight - fontWeightNumber); if (currDistance < minDistance) { minDistance = currDistance; selectedFontWeight = fontData.fontWeight.valueOf(); } }); // FontWeight to String switch (selectedFontWeight) { case FontWeight.Thin: return 'Thin'; case FontWeight.ExtraLight: return 'ExtraLight'; case FontWeight.Light: return 'Light'; case FontWeight.Regular: return 'Regular'; case FontWeight.Medium: return 'Medium'; case FontWeight.SemiBold: return 'SemiBold'; case FontWeight.Bold: return 'Bold'; case FontWeight.ExtraBold: return 'ExtraBold'; case FontWeight.Black: return 'Black'; default: return 'Regular'; } }; export const getFontName = (fontFamily, fontWeight) => { /* * Get the font family name and the font weight name from the THEME object. * Then, return a possible font. */ const fontsInApp = UnitSDK.getFonts(); if (!fontsInApp) { throw new Error('Fonts are not found in the app'); } const familyFonts = fontsInApp[fontFamily]; if (!familyFonts) { throw new Error(`Font family ${fontFamily} not found in the app's fonts`); } const fontWeightName = getFontWeightName(familyFonts, fontWeight); return `${fontFamily}-${fontWeightName}`; }; //# sourceMappingURL=fonts.js.map