|
2 | 2 |
|
3 | 3 | import { ApplicationEvent, ReactNativeToWebEvent } from '@standardnotes/snjs' |
4 | 4 | import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react' |
5 | | -import { Button, Dimensions, Keyboard, KeyboardEvent, Platform, Text, View } from 'react-native' |
| 5 | +import { AppState, Button, Dimensions, Keyboard, KeyboardEvent, Platform, Text, View } from 'react-native' |
6 | 6 | import VersionInfo from 'react-native-version-info' |
7 | 7 | import { WebView, WebViewMessageEvent } from 'react-native-webview' |
8 | 8 | import { OnShouldStartLoadWithRequest, WebViewNativeConfig } from 'react-native-webview/lib/WebViewTypes' |
@@ -52,6 +52,18 @@ const MobileWebAppContents = ({ destroyAndReload }: { destroyAndReload: () => vo |
52 | 52 | const useFlexLayout = Platform.OS === 'ios' || androidVersion < 35 |
53 | 53 | const [webViewContainerHeight, setWebViewContainerHeight] = useState(screenHeight) |
54 | 54 |
|
| 55 | + const applyDynamicTypeFontScale = useCallback((fontScale?: number) => { |
| 56 | + if (Platform.OS !== 'ios') { |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + const scale = fontScale ?? Dimensions.get('window').fontScale |
| 61 | + webViewRef.current?.injectJavaScript(` |
| 62 | + document.documentElement.style.fontSize = 'calc(1rem * ${scale})'; |
| 63 | + true; |
| 64 | + `) |
| 65 | + }, []) |
| 66 | + |
55 | 67 | useEffect(() => { |
56 | 68 | const removeStateServiceListener = stateService.addEventObserver((event: ReactNativeToWebEvent) => { |
57 | 69 | webViewRef.current?.postMessage(JSON.stringify({ reactNativeEvent: event, messageType: 'event' })) |
@@ -150,6 +162,27 @@ const MobileWebAppContents = ({ destroyAndReload }: { destroyAndReload: () => vo |
150 | 162 | } |
151 | 163 | }, [webViewRef, stateService, device, androidBackHandlerService, colorSchemeService, insets.bottom, screenHeight]) |
152 | 164 |
|
| 165 | + useEffect(() => { |
| 166 | + if (Platform.OS !== 'ios') { |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + const dimensionsListener = Dimensions.addEventListener('change', ({ window }) => { |
| 171 | + applyDynamicTypeFontScale(window.fontScale) |
| 172 | + }) |
| 173 | + |
| 174 | + const appStateListener = AppState.addEventListener('change', (state) => { |
| 175 | + if (state === 'active') { |
| 176 | + applyDynamicTypeFontScale() |
| 177 | + } |
| 178 | + }) |
| 179 | + |
| 180 | + return () => { |
| 181 | + dimensionsListener.remove() |
| 182 | + appStateListener.remove() |
| 183 | + } |
| 184 | + }, [applyDynamicTypeFontScale]) |
| 185 | + |
153 | 186 | useEffect(() => { |
154 | 187 | return notifee.onForegroundEvent(({ type, detail }) => { |
155 | 188 | if (type !== EventType.ACTION_PRESS) { |
@@ -305,6 +338,7 @@ const MobileWebAppContents = ({ destroyAndReload }: { destroyAndReload: () => vo |
305 | 338 | } |
306 | 339 | if (message === 'appLoaded') { |
307 | 340 | setDidLoadEnd(true) |
| 341 | + applyDynamicTypeFontScale() |
308 | 342 | return |
309 | 343 | } |
310 | 344 | try { |
|
0 commit comments