From 5160fb42ebf5e23f63e9154bbafa97ce14b48e5e Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 18 Sep 2024 17:42:41 -0500 Subject: [PATCH] Fixes --- app.config.js | 12 +++--- src/alf/fonts.ts | 70 +++++++++++++++++++++------------ src/components/Typography.tsx | 4 +- src/view/com/util/text/Text.tsx | 8 +--- 4 files changed, 54 insertions(+), 40 deletions(-) diff --git a/app.config.js b/app.config.js index b2ef833083..33677eaa5e 100644 --- a/app.config.js +++ b/app.config.js @@ -225,12 +225,12 @@ module.exports = function (config) { 'expo-font', { fonts: [ - './assets/fonts/inter/Inter-Thin.otf', - './assets/fonts/inter/Inter-ThinItalic.otf', - './assets/fonts/inter/Inter-ExtraLight.otf', - './assets/fonts/inter/Inter-ExtraLightItalic.otf', - './assets/fonts/inter/Inter-Light.otf', - './assets/fonts/inter/Inter-LightItalic.otf', + // './assets/fonts/inter/Inter-Thin.otf', + // './assets/fonts/inter/Inter-ThinItalic.otf', + // './assets/fonts/inter/Inter-ExtraLight.otf', + // './assets/fonts/inter/Inter-ExtraLightItalic.otf', + // './assets/fonts/inter/Inter-Light.otf', + // './assets/fonts/inter/Inter-LightItalic.otf', './assets/fonts/inter/Inter-Regular.otf', './assets/fonts/inter/Inter-Italic.otf', './assets/fonts/inter/Inter-Medium.otf', diff --git a/src/alf/fonts.ts b/src/alf/fonts.ts index cfa9f62d97..3ff2bdb6ef 100644 --- a/src/alf/fonts.ts +++ b/src/alf/fonts.ts @@ -1,8 +1,11 @@ import {useFonts as defaultUseFonts} from 'expo-font' +import {isWeb} from '#/platform/detection' import {IS_PROD} from '#/env' import {Device, device} from '#/storage' +const FAMILIES = `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Liberation Sans", Helvetica, Arial, sans-serif` + const factor = 0.0625 // 1 - (15/16) const fontScaleMultipliers: Record = { '-2': 1 - factor * 3, @@ -36,7 +39,12 @@ export function setFontFamily(fontFamily: Device['fontFamily']) { * Unused fonts are commented out, but the files are there if we need them. */ export function useFonts() { - if (IS_PROD) return [true, undefined] + /** + * Local native builds work fine with `expo-font` config plugin, but web does + * not. So in dev, we ensure things are loaded using the async hook. In + * production, fonts load via the config plugin on all platforms. + */ + if (IS_PROD) return [true, null] return defaultUseFonts({ // 'Inter-Thin': require('../../assets/fonts/inter/Inter-Thin.otf'), // 'Inter-ThinItalic': require('../../assets/fonts/inter/Inter-ThinItalic.otf'), @@ -62,31 +70,43 @@ export function useFonts() { /* * Unused fonts are commented out, but the files are there if we need them. */ -export function applyFonts(style: Record) { - style.fontFamily = - { - // '100': 'Inter-Thin', - // '200': 'Inter-ExtraLight', - // '300': 'Inter-Light', - '100': 'Inter-Regular', - '200': 'Inter-Regular', - '300': 'Inter-Regular', - '400': 'Inter-Regular', - '500': 'Inter-Medium', - '600': 'Inter-SemiBold', - '700': 'Inter-Bold', - '800': 'Inter-ExtraBold', - '900': 'Inter-Black', - }[style.fontWeight as string] || 'Inter-Regular' +export function applyFonts( + style: Record, + fontFamily: 'system' | 'theme', +) { + if (fontFamily === 'theme') { + style.fontFamily = + { + // '100': 'Inter-Thin', + // '200': 'Inter-ExtraLight', + // '300': 'Inter-Light', + '100': 'Inter-Regular', + '200': 'Inter-Regular', + '300': 'Inter-Regular', + '400': 'Inter-Regular', + '500': 'Inter-Medium', + '600': 'Inter-SemiBold', + '700': 'Inter-Bold', + '800': 'Inter-ExtraBold', + '900': 'Inter-Black', + }[style.fontWeight as string] || 'Inter-Regular' - if (style.fontStyle === 'italic') { - if (style.fontFamily === 'Inter-Regular') { - style.fontFamily = 'Inter-Italic' - } else { - style.fontFamily += 'Italic' + if (style.fontStyle === 'italic') { + if (style.fontFamily === 'Inter-Regular') { + style.fontFamily = 'Inter-Italic' + } else { + style.fontFamily += 'Italic' + } + } + + // fallback families only supported on web + if (isWeb) { + style.fontFamily += `, ${FAMILIES}` + } + } else { + // fallback families only supported on web + if (isWeb) { + style.fontFamily = style.fontFamily || FAMILIES } } - - style.fontFamily += - ', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Liberation Sans", Helvetica, Arial, sans-serif' } diff --git a/src/components/Typography.tsx b/src/components/Typography.tsx index a5aaee486a..15f88468a7 100644 --- a/src/components/Typography.tsx +++ b/src/components/Typography.tsx @@ -56,9 +56,7 @@ export function normalizeTextStyles( s.lineHeight = s.fontSize } - if (fontFamily === 'theme') { - applyFonts(s) - } + applyFonts(s, fontFamily) return s } diff --git a/src/view/com/util/text/Text.tsx b/src/view/com/util/text/Text.tsx index fe72fe40e6..52a45b0e2e 100644 --- a/src/view/com/util/text/Text.tsx +++ b/src/view/com/util/text/Text.tsx @@ -43,9 +43,7 @@ export function Text({ style, ]) - if (fonts.family === 'theme') { - applyFonts(flattened) - } + applyFonts(flattened, fonts.family) // should always be defined on `typography` // @ts-ignore @@ -73,9 +71,7 @@ export function Text({ style, ]) - if (fonts.family === 'theme') { - applyFonts(flattened) - } + applyFonts(flattened, fonts.family) // should always be defined on `typography` // @ts-ignore