-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplatformTextDefaults.ts
More file actions
56 lines (50 loc) · 1.92 KB
/
Copy pathplatformTextDefaults.ts
File metadata and controls
56 lines (50 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Platform, Pressable, Text, TextInput } from "react-native";
import { FONT_UI_SANS_REGULAR } from "./fonts";
import { uiTextVerticalCompensationTransform } from "./theme";
type WithDefaultStyle = {
defaultProps?: { style?: unknown };
};
/**
* Android adds extra font padding to `Text` / `TextInput` by default, which pushes glyphs down in
* fixed-height rows. The prop is Android-only (`false` elsewhere is ignored).
*/
function appendDefaultStyle(ctor: WithDefaultStyle, patch: object): void {
const prev = ctor.defaultProps?.style;
const base = Array.isArray(prev) ? prev : prev != null ? [prev] : [];
ctor.defaultProps = {
...(ctor.defaultProps ?? {}),
style: [...base, patch],
};
}
/** Call once at startup (e.g. from `app/_layout.tsx` before React renders). */
export function applyPlatformTextDefaults(): void {
appendDefaultStyle(Text as WithDefaultStyle, { includeFontPadding: false });
appendDefaultStyle(TextInput as WithDefaultStyle, { includeFontPadding: false });
applyPlatformWebInteractionDefaults();
}
/** Web: pointer cursor on pressables; avoids I-beam over label text inside buttons. */
export function applyPlatformWebInteractionDefaults(): void {
if (Platform.OS !== "web") {
return;
}
appendDefaultStyle(Pressable as WithDefaultStyle, {
cursor: "pointer",
userSelect: "none",
});
}
let uiSansFontFamilyApplied = false;
/** After `useFonts` succeeds — sets default UI sans for `Text` / `TextInput` on all platforms. */
export function ensureUiSansFontFamilyDefaults(): void {
if (uiSansFontFamilyApplied) return;
uiSansFontFamilyApplied = true;
appendDefaultStyle(Text as WithDefaultStyle, {
fontFamily: FONT_UI_SANS_REGULAR,
paddingVertical: 0,
...uiTextVerticalCompensationTransform,
});
appendDefaultStyle(TextInput as WithDefaultStyle, {
fontFamily: FONT_UI_SANS_REGULAR,
paddingVertical: 0,
...uiTextVerticalCompensationTransform,
});
}