Skip to content

Commit e0c47c4

Browse files
committed
feat(theme): add custom font configuration support to RNCProvider
- Add fontConfig, fontsLoaded, and onFontLoadError props to RNCProvider - Include documentation and examples for custom font setup - Update CHANGELOG.md with new features and fixes - Add badge to setup guide for new font configuration section
1 parent 7276e9e commit e0c47c4

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Issue and pull request templates
1313
- Security policy
1414
- Code of conduct
15+
- ✨ Custom font configuration support in `RNCProvider`
16+
- 📚 Font configuration documentation and examples
17+
- 🎨 `fontConfig`, `fontsLoaded`, and `onFontLoadError` props to `RNCProvider`
18+
- 📖 Custom font setup guide with Expo Google Fonts integration
1519

1620
### Changed
1721
- Improved project structure documentation
22+
- 🔄 Enhanced `RNCProvider` with font loading capabilities
23+
- 📚 Updated API documentation with new font-related props
1824

1925
### Deprecated
2026
- Nothing
@@ -23,7 +29,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2329
- Nothing
2430

2531
### Fixed
26-
- Nothing
32+
- 🐛 Fixed theme persistence issue where only current theme mode was saved
33+
- 🔧 Fixed `updateCustomTheme` to save both light and dark variants when preset is applied
34+
- ✨ Added missing `fontFamily: 'System'` to all typography variants in preset themes
2735

2836
### Security
2937
- Added security policy and guidelines

apps/docs/astro.config.mjs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export default defineConfig({
3030
},
3131
},
3232
{ label: 'Quick Start', slug: 'getting-started/quick-start' },
33-
{ label: 'Setup Guide', slug: 'getting-started/setup-guide' },
33+
{
34+
label: 'Setup Guide',
35+
slug: 'getting-started/setup-guide',
36+
badge: { text: 'New', variant: 'success' },
37+
},
3438
],
3539
},
3640
{

apps/docs/src/content/docs/api/provider.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Provider component is used to wrap your app content and provide the theme contex
2222
| `loadingComponent?` | `ReactNode` | `undefined` | Custom loading component to display during theme initialization |
2323
| `splashDuration?` | `number` | `150` | Minimum splash duration in milliseconds to prevent flash |
2424
| `fallbackTheme?` | `'light' \| 'dark'` | `'light'` | Fallback theme to use when system theme is unavailable |
25+
| `fontConfig?` | <a href='/api/provider#fontconfig'>`FontConfig`</a> | `undefined` | Custom font configuration for typography |
26+
| `fontsLoaded?` | `boolean` | `true` | External font loading state indicator |
27+
| `onFontLoadError?` | `(error: string) => void` | `undefined` | Callback function when font loading fails |
2528

2629
### BottomSheetProps
2730

@@ -62,6 +65,15 @@ Provider component is used to wrap your app content and provide the theme contex
6265
| `defaultLocale` | `string` | No | `undefined` | The default locale code to use when no specific locale is selected or when the requested locale is not supported. Should be one of the locales listed in `supportedLocales`. |
6366
| `autoDetectLocale` | `boolean` | No | `undefined` | Whether to automatically detect the user's preferred locale from browser settings or other sources. When `true`, the application will attempt to match the user's preference with supported locales. |
6467

68+
### FontConfig
69+
70+
| Property | Type | Required | Default | Description |
71+
|----------|------|----------|---------|-------------|
72+
| `regular` | `string` | No | `'System'` | Font family name for regular weight text |
73+
| `medium` | `string` | No | `'System'` | Font family name for medium weight text |
74+
| `semiBold` | `string` | No | `'System'` | Font family name for semi-bold weight text |
75+
| `bold` | `string` | No | `'System'` | Font family name for bold weight text |
76+
6577
### `useTheme`
6678

6779
| Property/Method | Type | Description |

apps/docs/src/content/docs/getting-started/setup-guide.mdx

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,105 @@ themeRegistry.registerPreset('custom', customThemeConfig);
279279
</TabItem>
280280
</Tabs>
281281

282+
## Custom Font Configuration <Badge variant='success' text='New'/>
283+
284+
### Setting Up Custom Fonts with Expo
285+
286+
RNC Theme supports custom font configuration for enhanced typography. Here's how to set up custom fonts using Expo Google Fonts:
287+
288+
<Steps>
289+
1. **Install required dependencies**
290+
```bash
291+
npx expo install expo-font @expo-google-fonts/poppins
292+
```
293+
294+
2. **Import fonts and utilities**
295+
```tsx title="_layout.tsx"
296+
import { useFonts } from 'expo-font';
297+
import { utils, RNCProvider } from 'rnc-theme';
298+
import {
299+
Poppins_400Regular,
300+
Poppins_500Medium,
301+
Poppins_600SemiBold,
302+
Poppins_700Bold,
303+
} from '@expo-google-fonts/poppins';
304+
```
305+
306+
3. **Create font configuration**
307+
```tsx title="_layout.tsx"
308+
// Font config
309+
const fontConfig = utils.createExpoFontConfig({
310+
'Poppins-Regular': Poppins_400Regular,
311+
'Poppins-Medium': Poppins_500Medium,
312+
'Poppins-SemiBold': Poppins_600SemiBold,
313+
'Poppins-Bold': Poppins_700Bold,
314+
});
315+
```
316+
317+
4. **Load fonts and handle loading states**
318+
```tsx title="_layout.tsx"
319+
export default function RootLayout() {
320+
const [fontsLoaded, error] = useFonts({
321+
'Poppins-Regular': Poppins_400Regular,
322+
'Poppins-Medium': Poppins_500Medium,
323+
'Poppins-SemiBold': Poppins_600SemiBold,
324+
'Poppins-Bold': Poppins_700Bold,
325+
});
326+
327+
const handleFontLoadError = (error: string) => {
328+
console.warn('Font loading error:', error);
329+
// You could show a toast notification or handle the error as needed
330+
};
331+
332+
useEffect(() => {
333+
if (error) throw error;
334+
}, [error]);
335+
336+
useEffect(() => {
337+
if (fontsLoaded) {
338+
SplashScreen.hideAsync();
339+
}
340+
}, [fontsLoaded]);
341+
342+
if (!fontsLoaded) {
343+
return null;
344+
}
345+
346+
return (
347+
<GestureHandlerRootView>
348+
<RNCProvider
349+
defaultTheme="system"
350+
fontConfig={fontConfig}
351+
fontsLoaded={fontsLoaded}
352+
onFontLoadError={handleFontLoadError}
353+
showLoadingSplash={true}
354+
splashDuration={200}
355+
>
356+
{/* Your app content */}
357+
</RNCProvider>
358+
</GestureHandlerRootView>
359+
);
360+
}
361+
```
362+
</Steps>
363+
364+
### Font Configuration Options
365+
366+
| Option | Type | Description |
367+
|--------|------|-------------|
368+
| `fontConfig` | `FontConfig` | Maps font weights to font family names |
369+
| `fontsLoaded` | `boolean` | Indicates whether fonts have finished loading |
370+
| `onFontLoadError` | `(error: string) => void` | Callback for font loading errors |
371+
| `showLoadingSplash` | `boolean` | Shows loading screen while fonts load |
372+
| `splashDuration` | `number` | Minimum splash screen duration |
373+
374+
<Aside type="tip" title="Font Loading Best Practices">
375+
- Always handle font loading errors gracefully
376+
- Use splash screens to prevent layout shifts
377+
- Provide fallback fonts for better user experience
378+
- Test font loading on different devices and network conditions
379+
</Aside>
380+
282381
## Internationalization (i18n)
283382

284383
### Complete i18n Setup

apps/example/app/_layout.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ const fontConfig = utils.createExpoFontConfig({
3535
});
3636

3737
export default function RootLayout() {
38-
// const [loaded, error] = useFonts({
39-
// SpaceMono: require('../assets/fonts/SpaceMono-Regular.ttf') as FontSource,
40-
// ...FontAwesome.font,
41-
// });
42-
4338
const [fontsLoaded, error] = useFonts({
4439
'Poppins-Regular': Poppins_400Regular,
4540
'Poppins-Medium': Poppins_500Medium,

libs/rnc-theme/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rnc-theme",
3-
"version": "0.1.12",
3+
"version": "0.1.2",
44
"funding": {
55
"type": "github",
66
"url": "https://github.com/masumrpg/react-native-components"

0 commit comments

Comments
 (0)