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

CompactBrandTitle.tsx

108 lines3.6 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 /**
23 * Fills the parent row and drops the stage badge when the full title does not fit.
24 * Narrow Android sidebar headers otherwise draw their actions over the badge.
25 */
26 readonly fit?: boolean;
27 } = {},
28) {
29 const stageLabel = resolveMobileStageLabel(Constants.expoConfig?.extra?.appVariant);
30 const titleOffset = brandTitleOffset();
31 const { scale } = useAndroidControlSizing();
32 const [availableWidth, setAvailableWidth] = useState<number | null>(null);
33 // Measured only while the badge shows, so hiding it does not make it look like it fits.
34 const [fullWidth, setFullWidth] = useState<number | null>(null);
35 const showStage =
36 !props.fit || availableWidth === null || fullWidth === null || fullWidth <= availableWidth;
37​
38 const title = (
39 <View
40 onLayout={
41 props.fit && showStage ? (event) => setFullWidth(event.nativeEvent.layout.width) : undefined
42 }
43 aria-level={1}
44 accessibilityLabel={props.accessibilityLabel ?? "T3 Code, Threads"}
45 accessible
46 role="heading"
47 className="flex-row items-center gap-1.5"
48 style={[
49 { marginLeft: titleOffset },
50 Platform.OS === "android" && { gap: 5.25 * scale },
51 props.fit && { flexShrink: 0 },
52 ]}
53 >
54 <T3Wordmark colorClassName="accent-icon" height={Math.round(15 * scale)} />
55 <Text
56 allowFontScaling={props.allowFontScaling}
57 className="font-t3-medium text-foreground-muted"
58 style={{ fontSize: 21 * scale, letterSpacing: -0.5 * scale }}
59 >
60 Code
61 </Text>
62 {showStage ? (
63 <View
64 className="rounded-full bg-subtle px-1.5 py-0.5"
65 style={
66 Platform.OS === "android"
67 ? { paddingHorizontal: 5.25 * scale, paddingVertical: 1.75 * scale }
68 : undefined
69 }
70 >
71 <Text
72 allowFontScaling={props.allowFontScaling}
73 className="font-t3-bold text-foreground-muted uppercase"
74 style={{ fontSize: 9 * scale, letterSpacing: 0.9 * scale }}
75 >
76 {stageLabel}
77 </Text>
78 </View>
79 ) : null}
80 </View>
81 );
82​
83 return props.fit ? (
84 <View
85 className="min-w-0 flex-1 flex-row items-center"
86 onLayout={(event) => setAvailableWidth(event.nativeEvent.layout.width)}
87 >
88 {title}
89 </View>
90 ) : (
91 title
92 );
94​
95export function renderCompactBrandTitle() {
96 return <CompactBrandTitle allowFontScaling={Platform.OS === "ios"} />;
98​
99export function getCompactBrandHeaderOptions(
100 fallbackTitleStyle?: NativeStackNavigationOptions["headerTitleStyle"],
101): NativeStackNavigationOptions {
102 return {
103 headerTitle: renderCompactBrandTitle,
104 headerTitleStyle: fallbackTitleStyle,
105 title: "Threads",
106 unstable_headerLeftItems: undefined,
107 };