Enable React Native strict TypeScript API (#11459)
This commit is contained in:
+6
-1
@@ -3,6 +3,8 @@ import {type FontVariant, type TextStyle} from 'react-native'
|
||||
import {IS_ANDROID, IS_WEB} from '#/env'
|
||||
import {type Device, device} from '#/storage'
|
||||
|
||||
export type MutableTextStyle = {-readonly [K in keyof TextStyle]: TextStyle[K]}
|
||||
|
||||
const WEB_FONT_FAMILIES = `system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"`
|
||||
|
||||
const factor = 0.0625 // 1 - (15/16)
|
||||
@@ -37,7 +39,10 @@ export function setFontFamily(fontFamily: Device['fontFamily']) {
|
||||
/*
|
||||
* Unused fonts are commented out, but the files are there if we need them.
|
||||
*/
|
||||
export function applyFonts(style: TextStyle, fontFamily: 'system' | 'theme') {
|
||||
export function applyFonts(
|
||||
style: MutableTextStyle,
|
||||
fontFamily: 'system' | 'theme',
|
||||
) {
|
||||
if (fontFamily === 'theme') {
|
||||
if (IS_ANDROID) {
|
||||
style.fontFamily =
|
||||
|
||||
@@ -7,7 +7,13 @@ import {
|
||||
import {UITextView} from '@bsky.app/react-native-uitextview'
|
||||
import createEmojiRegex from 'emoji-regex'
|
||||
|
||||
import {type Alf, applyFonts, atoms, flatten} from '#/alf'
|
||||
import {
|
||||
type Alf,
|
||||
applyFonts,
|
||||
atoms,
|
||||
flatten,
|
||||
type MutableTextStyle,
|
||||
} from '#/alf'
|
||||
import {IS_IOS, IS_NATIVE} from '#/env'
|
||||
|
||||
/**
|
||||
@@ -27,7 +33,7 @@ export function normalizeTextStyles(
|
||||
fontFamily: Alf['fonts']['family']
|
||||
} & Pick<Alf, 'flags'>,
|
||||
) {
|
||||
const s = flatten(styles) ?? {}
|
||||
const s: MutableTextStyle = {...flatten(styles)}
|
||||
|
||||
// should always be defined on these components
|
||||
s.fontSize = (s.fontSize || atoms.text_md.fontSize) * fontScale
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import {useEffect, useState} from 'react'
|
||||
import {Dimensions} from 'react-native'
|
||||
import {Dimensions, type DimensionsPayload} from 'react-native'
|
||||
|
||||
/**
|
||||
* Same as `useWindowDimensions().fontScale`, but avoids rerendering
|
||||
@@ -9,9 +9,12 @@ export function useNativeFontScale() {
|
||||
const [fontScale, setFontScale] = useState(Dimensions.get('window').fontScale)
|
||||
|
||||
useEffect(() => {
|
||||
const sub = Dimensions.addEventListener('change', evt => {
|
||||
setFontScale(evt.window.fontScale)
|
||||
})
|
||||
const sub = Dimensions.addEventListener(
|
||||
'change',
|
||||
(evt: DimensionsPayload) => {
|
||||
if (evt.window) setFontScale(evt.window.fontScale)
|
||||
},
|
||||
)
|
||||
return () => sub.remove()
|
||||
}, [])
|
||||
|
||||
|
||||
+16
-3
@@ -1,6 +1,19 @@
|
||||
import {type DimensionValue, StyleSheet} from 'react-native'
|
||||
import {type DimensionValue, type StyleProp, StyleSheet} from 'react-native'
|
||||
|
||||
export const flatten = StyleSheet.flatten
|
||||
export function flatten<T>(
|
||||
style?: StyleProp<T>,
|
||||
): T extends (infer U)[] ? U : T {
|
||||
return (StyleSheet.flatten(
|
||||
style as unknown as Parameters<typeof StyleSheet.flatten>[0],
|
||||
) ?? {}) as T extends (infer U)[] ? U : T
|
||||
}
|
||||
|
||||
/** Flatten React Native styles passed directly to a web-only DOM component. */
|
||||
export function flattenToCSS(style: unknown): React.CSSProperties {
|
||||
return (StyleSheet.flatten(
|
||||
style as Parameters<typeof StyleSheet.flatten>[0],
|
||||
) ?? {}) as React.CSSProperties
|
||||
}
|
||||
|
||||
/**
|
||||
* Coerce a style value to a number. Padding values are typed as
|
||||
@@ -28,7 +41,7 @@ interface PaddingStyle {
|
||||
* non-numeric `DimensionValue` (e.g. percentages) is treated as 0.
|
||||
*/
|
||||
export function extractPadding(style: PaddingStyle | PaddingStyle[]) {
|
||||
const s = flatten(style as any) ?? {}
|
||||
const s = flatten(style)
|
||||
const base = num(s.padding)
|
||||
return {
|
||||
paddingTop: num(s.paddingTop) || num(s.paddingVertical) || base,
|
||||
|
||||
@@ -24,7 +24,7 @@ export function useThemeName(): ThemeName {
|
||||
}
|
||||
|
||||
function getThemeName(
|
||||
colorScheme: ColorSchemeName,
|
||||
colorScheme: ColorSchemeName | null | undefined,
|
||||
colorMode: 'system' | 'light' | 'dark',
|
||||
darkTheme?: ThemeName,
|
||||
) {
|
||||
@@ -39,11 +39,8 @@ function getThemeName(
|
||||
}
|
||||
|
||||
function updateDocument(theme: ThemeName) {
|
||||
// @ts-ignore web only
|
||||
if (IS_WEB && typeof window !== 'undefined') {
|
||||
// @ts-ignore web only
|
||||
const html = window.document.documentElement
|
||||
// @ts-ignore web only
|
||||
const meta = window.document.querySelector('meta[name="theme-color"]')
|
||||
|
||||
// remove any other color mode classes
|
||||
|
||||
Reference in New Issue
Block a user