|
2 | 2 | // markdown file and add links from here |
3 | 3 |
|
4 | 4 | import { Platform } from "react-native" |
5 | | -import type { FontSource } from "expo-font" |
6 | | -import { |
7 | | - SpaceGrotesk_300Light as spaceGroteskLight, |
8 | | - SpaceGrotesk_400Regular as spaceGroteskRegular, |
9 | | - SpaceGrotesk_500Medium as spaceGroteskMedium, |
10 | | - SpaceGrotesk_600SemiBold as spaceGroteskSemiBold, |
11 | | - SpaceGrotesk_700Bold as spaceGroteskBold, |
12 | | -} from "@expo-google-fonts/space-grotesk" |
| 5 | +import { FontSource, useFonts } from "expo-font" |
13 | 6 |
|
14 | | -export const customFontsToLoadWebOnly = |
15 | | - Platform.OS === "web" |
16 | | - ? { |
17 | | - spaceGroteskLight, |
18 | | - spaceGroteskRegular, |
19 | | - spaceGroteskMedium, |
20 | | - spaceGroteskSemiBold, |
21 | | - spaceGroteskBold, |
22 | | - } |
23 | | - : ({} as Record<string, FontSource>) |
| 7 | +export const FONT_FILES: Record<string, string> = { |
| 8 | + SpaceGrotesk_300Light: require("../../assets/fonts/SpaceGrotesk-300Light.ttf"), |
| 9 | + SpaceGrotesk_400Regular: require("../../assets/fonts/SpaceGrotesk-400Regular.ttf"), |
| 10 | + SpaceGrotesk_500Medium: require("../../assets/fonts/SpaceGrotesk-500Medium.ttf"), |
| 11 | + SpaceGrotesk_600SemiBold: require("../../assets/fonts/SpaceGrotesk-600SemiBold.ttf"), |
| 12 | + SpaceGrotesk_700Bold: require("../../assets/fonts/SpaceGrotesk-700Bold.ttf"), |
| 13 | +} |
| 14 | + |
| 15 | +export const customFontsToLoad = FONT_FILES as Record<string, FontSource> |
| 16 | + |
| 17 | +/** |
| 18 | + * On iOS and Android, the fonts are embedded as part of the app binary |
| 19 | + * using the expo config plugin in `app.json`. See the project |
| 20 | + * [`app.json`](../../app.json) for the expo-fonts configuration. The assets |
| 21 | + * are added via the `app/assets/fonts` folder. This config plugin |
| 22 | + * does NOT work for web, so we have to dynamically load the fonts via this hook. |
| 23 | + * |
| 24 | + * For more info: https://docs.expo.dev/versions/latest/sdk/font/ |
| 25 | + */ |
| 26 | +export const useCustomFonts = (): [boolean, Error | null] => { |
| 27 | + if (Platform.OS === "web") { |
| 28 | + // eslint-disable-next-line react-hooks/rules-of-hooks |
| 29 | + return useFonts(customFontsToLoad) |
| 30 | + } |
| 31 | + |
| 32 | + // On native, fonts are precompiled and ready |
| 33 | + return [true, null] |
| 34 | +} |
24 | 35 |
|
25 | 36 | const fonts = { |
26 | 37 | spaceGrotesk: { |
27 | | - // The way expo-fonts config plugin applies |
28 | | - // fonts to the individual platforms, the names come out different |
29 | | - // on ios and android. For web, we have to load fonts asynchronously |
30 | | - // using useFonts. |
31 | 38 | light: Platform.select({ |
32 | 39 | ios: "SpaceGrotesk-Light", |
33 | 40 | android: "SpaceGrotesk-300Light", |
34 | | - web: "spaceGroteskLight", |
| 41 | + web: "SpaceGrotesk_300Light", |
35 | 42 | }), |
36 | 43 | normal: Platform.select({ |
37 | 44 | ios: "SpaceGrotesk-Regular", |
38 | 45 | android: "SpaceGrotesk-400Regular", |
39 | | - web: "spaceGroteskRegular", |
| 46 | + web: "SpaceGrotesk_400Regular", |
40 | 47 | }), |
41 | 48 | medium: Platform.select({ |
42 | 49 | ios: "SpaceGrotesk-Medium", |
43 | 50 | android: "SpaceGrotesk-500Medium", |
44 | | - web: "spaceGroteskMedium", |
| 51 | + web: "SpaceGrotesk_500Medium", |
45 | 52 | }), |
46 | 53 | semiBold: Platform.select({ |
47 | 54 | ios: "SpaceGrotesk-SemiBold", |
48 | 55 | android: "SpaceGrotesk-600SemiBold", |
49 | | - web: "spaceGroteskSemiBold", |
| 56 | + web: "SpaceGrotesk_600SemiBold", |
50 | 57 | }), |
51 | 58 | bold: Platform.select({ |
52 | 59 | ios: "SpaceGrotesk-Bold", |
53 | 60 | android: "SpaceGrotesk-700Bold", |
54 | | - web: "spaceGroteskBold", |
| 61 | + web: "SpaceGrotesk_700Bold", |
55 | 62 | }), |
56 | 63 | }, |
57 | 64 | helveticaNeue: { |
|
0 commit comments