Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CompactBrandTitle.tsx

105 lines3.5 KB

1import { NATIVE_WORKSPACE_COLUMNS_SUPPORTED } from "../native/NativeWorkspaceColumns";
2import Constants from "expo-constants";
3import type { NativeStackNavigationOptions } from "@react-navigation/native-stack";
4import { useState } from "react";
5import { Platform, View } from "react-native";
6​
7import { AppText as Text } from "./ui/AppText";
8import { T3Wordmark } from "./T3Wordmark";
9import { IPAD_HOME_TITLE_OFFSET } from "../lib/layoutMetrics";
10import { resolveMobileStageLabel } from "../lib/mobileBranding";
11import { useAndroidControlSizing } from "./ui/useAndroidControlSizing";
12​
13export function brandTitleOffset(): number {
14 if (Platform.OS !== "ios") return 0;
15 return Platform.isPad && !NATIVE_WORKSPACE_COLUMNS_SUPPORTED ? IPAD_HOME_TITLE_OFFSET : 0;
17​
18export function CompactBrandTitle(
19 props: {
20 readonly allowFontScaling?: boolean;
21 readonly accessibilityLabel?: string;
22 /** Fills the row and hides the stage badge when the full title does not fit. */
23 readonly fit?: boolean;
24 } = {},
25) {
26 const stageLabel = resolveMobileStageLabel(Constants.expoConfig?.extra?.appVariant);
27 const titleOffset = brandTitleOffset();
28 const { scale } = useAndroidControlSizing();
29 const [availableWidth, setAvailableWidth] = useState<number | null>(null);
30 // Measured only while the badge shows, so hiding it does not make it look like it fits.
31 const [fullWidth, setFullWidth] = useState<number | null>(null);
32 const showStage =
33 !props.fit || availableWidth === null || fullWidth === null || fullWidth <= availableWidth;
34​
35 const title = (
36 <View
37 onLayout={
38 props.fit && showStage ? (event) => setFullWidth(event.nativeEvent.layout.width) : undefined
39 }
40 aria-level={1}
41 accessibilityLabel={props.accessibilityLabel ?? "T3 Code, Threads"}
42 accessible
43 role="heading"
44 className="flex-row items-center gap-1.5"
45 style={[
46 { marginLeft: titleOffset },
47 Platform.OS === "android" && { gap: 5.25 * scale },
48 props.fit && { flexShrink: 0 },
49 ]}
50 >
51 <T3Wordmark colorClassName="accent-icon" height={Math.round(15 * scale)} />
52 <Text
53 allowFontScaling={props.allowFontScaling}
54 className="font-t3-medium text-foreground-muted"
55 style={{ fontSize: 21 * scale, letterSpacing: -0.5 * scale }}
56 >
57 Code
58 </Text>
59 {showStage ? (
60 <View
61 className="rounded-full bg-subtle px-1.5 py-0.5"
62 style={
63 Platform.OS === "android"
64 ? { paddingHorizontal: 5.25 * scale, paddingVertical: 1.75 * scale }
65 : undefined
66 }
67 >
68 <Text
69 allowFontScaling={props.allowFontScaling}
70 className="font-t3-bold text-foreground-muted uppercase"
71 style={{ fontSize: 9 * scale, letterSpacing: 0.9 * scale }}
72 >
73 {stageLabel}
74 </Text>
75 </View>
76 ) : null}
77 </View>
78 );
79​
80 return props.fit ? (
81 <View
82 className="min-w-0 flex-1 flex-row items-center"
83 onLayout={(event) => setAvailableWidth(event.nativeEvent.layout.width)}
84 >
85 {title}
86 </View>
87 ) : (
88 title
89 );
91​
92export function renderCompactBrandTitle() {
93 return <CompactBrandTitle allowFontScaling={Platform.OS === "ios"} />;
95​
96export function getCompactBrandHeaderOptions(
97 fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"],
98): NativeStackNavigationOptions {
99 return {
100 headerTitle: renderCompactBrandTitle,
101 headerTitleStyle: fallbackTitleStyle,
102 title: "Threads",
103 unstable_headerLeftItems: undefined,
104 };