@livelike/react-native
Version:
LiveLike React Native package
48 lines • 1.3 kB
JavaScript
import { StyleSheet } from 'react-native';
import { useFonts } from './useFonts';
import { useMemo } from 'react';
/**
* @description useFontFamily hook returns an evaluated fontFamily based on fontWeight and fontStyle
* This is useful in case of custom fonts.
* @returns fontFamily and fontFamilyType
*/
export function useFontFamily(_ref) {
let {
style
} = _ref;
const {
fonts
} = useFonts();
return useMemo(() => {
const fontFamilyType = getFontFamilyType(style);
return {
fontFamilyType,
fontFamily: fonts[fontFamilyType]
};
}, [style, fonts]);
}
function getFontFamilyType(style) {
const {
fontWeight,
fontStyle
} = StyleSheet.flatten(style);
const isItalicFontStyle = fontStyle === 'italic';
const weightMap = {
'100': 'thin',
'200': 'extraLight',
'300': 'light',
'500': 'bold',
'600': 'semiBold',
bold: 'bold',
'700': 'bold',
'800': 'extraBold',
'900': 'black'
};
const weightKey = fontWeight === null || fontWeight === void 0 ? void 0 : fontWeight.toString();
const weightType = weightMap[weightKey];
if (weightType) {
return isItalicFontStyle ? `${weightType}Italic` : weightType;
}
return isItalicFontStyle ? 'italic' : 'regular';
}
//# sourceMappingURL=useFontFamily.js.map