Skip to content

Commit b1c84ae

Browse files
author
Kyler Jensen
committed
feat: added support for url() loaded font faces in web apps
BREAKING CHANGE: the signature of `<FontFacesProvider />` changed and `useFontFaces()` was removed Closes #12
1 parent 25f8ed2 commit b1c84ae

28 files changed

+674
-224
lines changed

.gitignore

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
# npm and yarn
22
node_modules/
3+
coverage/
4+
npm-debug.*
35
*.log
46

57
# macOS
68
.DS_Store
79

810
# @react-native-community/bob
9-
dist/
11+
dist/
12+
13+
# expo
14+
.expo/
15+
*.jks
16+
*.p8
17+
*.p12
18+
*.key
19+
*.mobileprovision
20+
*.orig.*
21+
web-build/
22+
__generated__/

README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,27 @@ This library aims to make life easier by allowing React Native developers to use
4141
yarn add -D @types/react-helmet
4242
```
4343

44-
2. Wrap your application's root component in a `<FontFacesProvider />`:
44+
2. Wrap your application's root component in a `<FontFacesProvider />` and provide a `FontLoader`:
4545

4646
```jsx
4747
// App.tsx
4848

4949
import React from 'react';
5050
import * as Font from 'expo-font';
5151
import { AppContent } from './AppContent';
52-
import { FontFacesProvider } from 'react-native-font-faces';
52+
import { FontFacesProvider, FontLoader, Roboto_All } from 'react-native-font-faces';
5353

5454
export default function App() {
55+
const [error, setError] = React.useState<Error>();
56+
const [loading, setLoading] = React.useState<boolean>(true);
57+
5558
return (
56-
<FontFacesProvider nativeFontLoader={Font.loadAsync}>
57-
<AppContent />
59+
<FontFacesProvider
60+
onError={setError}
61+
onLoading={setLoading}
62+
fontFaces={Roboto_All}
63+
nativeFontLoader={Font.loadAsync}>
64+
<AppContent loading={loaading} error={error} />
5865
</FontFacesProvider>
5966
);
6067
}
@@ -68,11 +75,13 @@ This library aims to make life easier by allowing React Native developers to use
6875
import React from 'react';
6976
import { Helmet } from 'react-helmet';
7077
import { AppLoading } from 'expo';
71-
import { useFontFaces, Roboto_All } from 'react-native-font-faces';
7278

73-
export function AppContent() {}
74-
const [fontsLoaded] = useFontFaces(Roboto_All);
75-
if (fontsLoaded) {
79+
export function AppContent(props: any) {}
80+
if (props.loading) {
81+
return <AppLoading />;
82+
} else if (props.error) {
83+
return <Text>{props.error.message}</Text>
84+
} else {
7685
return (
7786
<View style={styles.container}>
7887
<Helmet>
@@ -87,8 +96,6 @@ This library aims to make life easier by allowing React Native developers to use
8796
<StatusBar style="auto" />
8897
</View>
8998
);
90-
} else {
91-
return <AppLoading />;
9299
}
93100
}
94101

@@ -114,10 +121,17 @@ This library aims to make life easier by allowing React Native developers to use
114121
});
115122
```
116123

124+
## Migrating from 2.x
125+
126+
In version 3.x, we simplified `FontFacesProvider` and removed `useFontFaces`. Follow these steps to migrate:
127+
128+
1. Remove all instances of `useFontFaces()`.
129+
2. Update your application's `<FontFacesProvider/>` to provide the `onLoading` and `onError` props (optional).
130+
117131
## Migrating from 1.x
118132

119133
In version 2.x, we introduced `FontFacesProvider` and `useFontFaces`, and removed `enableFontFaces`. Follow these steps to migrate:
120134

121135
1. Remove all instances of `enableFontFaces()`.
122136
2. Add a `<FontFacesProvider/>` around your application's root component.
123-
3. Add `const [fontsLoaded] = useFontFaces(...)` inside an inner function component's body and handle the `fontsLoaded` value appropriately.
137+
3. Add `const [fontsLoaded] = useFontFaces(...)` inside an inner function component's body and handle the `fontsLoaded` value appropriately.

example/App.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
import React from 'react';
22
import * as Font from 'expo-font';
3-
import { Helmet } from 'react-helmet';
43
import { AppLoading } from 'expo';
54
import { StatusBar } from 'expo-status-bar';
65
import { StyleSheet, Text, View } from 'react-native';
7-
import { FontFacesProvider, useFontFaces, Roboto_All } from 'react-native-font-faces';
6+
import { FontFacesProvider, Roboto_All } from 'react-native-font-faces';
87

9-
const AppContent = () => {
10-
const [fontsLoaded] = useFontFaces(Roboto_All);
11-
12-
if (fontsLoaded) {
8+
const AppContent = (props: any) => {
9+
if (props.loading) {
10+
return <AppLoading />;
11+
} else if (props.error) {
12+
return <Text>{props.error.message}</Text>;
13+
} else {
1314
return (
1415
<View style={styles.container}>
15-
<Helmet>
16-
<link
17-
href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900"
18-
rel="stylesheet"
19-
/>
20-
</Helmet>
2116
<Text style={styles.text}>This should be Regular</Text>
2217
<Text style={[styles.text, styles.italic]}>This should be Italic</Text>
2318
<Text style={[styles.text, styles.bold]}>This should be Bold</Text>
@@ -27,15 +22,20 @@ const AppContent = () => {
2722
<StatusBar style="auto" />
2823
</View>
2924
);
30-
} else {
31-
return <AppLoading />;
3225
}
3326
};
3427

3528
export default function App() {
29+
const [error, setError] = React.useState<Error>();
30+
const [loading, setLoading] = React.useState<boolean>(true);
31+
3632
return (
37-
<FontFacesProvider nativeFontLoader={Font.loadAsync}>
38-
<AppContent />
33+
<FontFacesProvider
34+
onError={setError}
35+
onLoading={setLoading}
36+
fontFaces={Roboto_All}
37+
nativeFontLoader={Font.loadAsync}>
38+
<AppContent {...{ loading, error }} />
3939
</FontFacesProvider>
4040
);
4141
}

example/babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const package = require('../package.json');
33

44
const moduleResolverConfig = {
55
alias: {
6-
[package.name]: path.join(__dirname, '..', package.source),
6+
[package.name]: path.join(__dirname, '..', 'src'),
77
},
88
};
99

example/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
"expo-status-bar": "^1.0.2",
1414
"react": "~16.11.0",
1515
"react-dom": "~16.11.0",
16-
"react-helmet": "^6.1.0",
1716
"react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
1817
"react-native-web": "~0.11.7"
1918
},
2019
"devDependencies": {
2120
"@babel/core": "^7.8.6",
2221
"@expo/webpack-config": "^0.12.20",
2322
"@types/react": "~16.9.41",
24-
"@types/react-helmet": "^6.0.0",
2523
"@types/react-native": "~0.62.13",
2624
"expo-font": "^8.2.1",
2725
"react-refresh": "^0.8.3",

example/yarn.lock

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,13 +1793,6 @@
17931793
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
17941794
integrity sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==
17951795

1796-
"@types/react-helmet@^6.0.0":
1797-
version "6.0.0"
1798-
resolved "https://registry.yarnpkg.com/@types/react-helmet/-/react-helmet-6.0.0.tgz#5b74e44a12662ffb12d1c97ee702cf4e220958cf"
1799-
integrity sha512-NBMPAxgjpaMooXa51cU1BTgrX6T+hQbMiLm77JhBbfOzPQea3RB5rNpPOD5xGWHIVpGXHd59cltEzIq0qglGcQ==
1800-
dependencies:
1801-
"@types/react" "*"
1802-
18031796
"@types/react-native@~0.62.13":
18041797
version "0.62.18"
18051798
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.62.18.tgz#ad63691e7c44edef2beeb6af52b2eb942c3ed8a1"
@@ -8094,21 +8087,6 @@ react-error-overlay@^6.0.7:
80948087
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.7.tgz#1dcfb459ab671d53f660a991513cb2f0a0553108"
80958088
integrity sha512-TAv1KJFh3RhqxNvhzxj6LeT5NWklP6rDr2a0jaTfsZ5wSZWHOGeqQyejUp3xxLfPt2UpyJEcVQB/zyPcmonNFA==
80968089

8097-
react-fast-compare@^3.1.1:
8098-
version "3.2.0"
8099-
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb"
8100-
integrity sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA==
8101-
8102-
react-helmet@^6.1.0:
8103-
version "6.1.0"
8104-
resolved "https://registry.yarnpkg.com/react-helmet/-/react-helmet-6.1.0.tgz#a750d5165cb13cf213e44747502652e794468726"
8105-
integrity sha512-4uMzEY9nlDlgxr61NL3XbKRy1hEkXmKNXhjbAIOVw5vcFrsdYbH2FEwcNyWvWinl103nXgzYNlns9ca+8kFiWw==
8106-
dependencies:
8107-
object-assign "^4.1.1"
8108-
prop-types "^15.7.2"
8109-
react-fast-compare "^3.1.1"
8110-
react-side-effect "^2.1.0"
8111-
81128090
react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4:
81138091
version "16.13.1"
81148092
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
@@ -8189,11 +8167,6 @@ react-refresh@^0.8.3:
81898167
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.8.3.tgz#721d4657672d400c5e3c75d063c4a85fb2d5d68f"
81908168
integrity sha512-X8jZHc7nCMjaCqoU+V2I0cOhNW+QMBwSUkeXnTi8IPe6zaRWfn60ZzvFDZqWPfmSJfjub7dDW1SP0jaHWLu/hg==
81918169

8192-
react-side-effect@^2.1.0:
8193-
version "2.1.0"
8194-
resolved "https://registry.yarnpkg.com/react-side-effect/-/react-side-effect-2.1.0.tgz#1ce4a8b4445168c487ed24dab886421f74d380d3"
8195-
integrity sha512-IgmcegOSi5SNX+2Snh1vqmF0Vg/CbkycU9XZbOHJlZ6kMzTmi3yc254oB1WCkgA7OQtIAoLmcSFuHTc/tlcqXg==
8196-
81978170
react-timer-mixin@^0.13.4:
81988171
version "0.13.4"
81998172
resolved "https://registry.yarnpkg.com/react-timer-mixin/-/react-timer-mixin-0.13.4.tgz#75a00c3c94c13abe29b43d63b4c65a88fc8264d3"

package.json

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,51 @@
11
{
22
"name": "react-native-font-faces",
3-
"version": "0.0.0",
4-
"description": "Easily emulate @font-face behavior in react-native",
5-
"repository": "https://github.com/Kjens93/react-native-font-faces.git",
6-
"author": "Kyler Jensen <kjens93@users.noreply.github.com>",
7-
"license": "MIT",
83
"main": "dist/commonjs/index.js",
94
"module": "dist/module/index.js",
10-
"source": "src/index.ts",
5+
"author": "Kyler Jensen <kjens93@users.noreply.github.com>",
116
"typings": "dist/typescript/index.d.ts",
7+
"license": "MIT",
8+
"version": "0.0.0",
129
"react-native": "src/index.ts",
10+
"repository": "https://github.com/Kjens93/react-native-font-faces.git",
11+
"description": "Easily emulate @font-face behavior in react-native",
1312
"files": [
1413
"src",
1514
"dist"
1615
],
1716
"scripts": {
1817
"test": "jest",
1918
"build": "bob build",
20-
"start": "yarn example start",
21-
"release": "semantic-release",
22-
"example": "cd example && yarn"
19+
"release": "semantic-release"
20+
},
21+
"dependencies": {
22+
"react-helmet": "^6.1.0",
23+
"redent": "^3.0.0",
24+
"ts-dedent": "^1.1.1"
2325
},
2426
"peerDependencies": {
25-
"react": ">=16.9.0",
26-
"react-native": ">=0.60.0"
27+
"react": "^16.11.0",
28+
"react-native": "~0.60.0"
2729
},
2830
"devDependencies": {
29-
"@babel/core": "^7.10.5",
31+
"@babel/core": "^7.11.0",
32+
"@babel/preset-env": "^7.11.0",
33+
"@babel/preset-typescript": "^7.10.4",
3034
"@commitlint/cli": "^9.1.1",
3135
"@commitlint/config-conventional": "^9.1.1",
3236
"@react-native-community/bob": "^0.16.2",
33-
"@types/jest": "^26.0.4",
37+
"@types/jest": "^26.0.8",
3438
"@types/react": "^16.9.43",
35-
"@types/react-native": "^0.62.0",
39+
"@types/react-helmet": "^6.0.0",
40+
"@types/react-native": "~0.62.0",
3641
"babel-jest": "^26.1.0",
42+
"babel-plugin-module-resolver": "^4.0.0",
3743
"husky": "^4.2.5",
3844
"jest": "^26.1.0",
39-
"metro-react-native-babel-preset": "^0.60.0",
45+
"metro-react-native-babel-preset": "^0.61.0",
4046
"prettier": "^2.0.5",
4147
"pretty-quick": "^2.0.1",
4248
"semantic-release": "^17.1.1",
43-
"typescript": "^3.9.6"
44-
},
45-
"husky": {
46-
"hooks": {
47-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
48-
"pre-commit": "pretty-quick --staged"
49-
}
50-
},
51-
"dependencies": {}
49+
"typescript": "^3.9.7"
50+
}
5251
}

src/contexts/FontFacesContext.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)