200 lines
4.5 KiB
TypeScript
200 lines
4.5 KiB
TypeScript
import React, { type ReactNode, useCallback } from "react";
|
|
import { BackHandler, Platform, Pressable, StyleSheet, Text, View } from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import { router, useFocusEffect } from "expo-router";
|
|
import { moderateScale, scale } from "react-native-size-matters";
|
|
|
|
import { Colors } from "@/theme";
|
|
import {
|
|
type PageTone,
|
|
toneColors,
|
|
usePageTone,
|
|
} from "../../context/PageToneContext";
|
|
|
|
export interface ContentPageHeaderProps {
|
|
title: string;
|
|
subtitle?: string;
|
|
right?: ReactNode;
|
|
fallbackRoute?: string;
|
|
onTitlePress?: () => void;
|
|
titleAccessibilityLabel?: string;
|
|
onBack?: () => void;
|
|
tone?: PageTone;
|
|
titleColor?: string;
|
|
}
|
|
|
|
export function ContentPageHeader({
|
|
title,
|
|
subtitle,
|
|
right,
|
|
fallbackRoute = "/modules",
|
|
onTitlePress,
|
|
titleAccessibilityLabel,
|
|
onBack,
|
|
tone,
|
|
titleColor,
|
|
}: ContentPageHeaderProps) {
|
|
const { tone: contextTone } = usePageTone();
|
|
const effectiveTone = tone || contextTone;
|
|
const resolvedTitleColor =
|
|
titleColor ||
|
|
(effectiveTone && toneColors[effectiveTone]?.title) ||
|
|
Colors.textPrimary;
|
|
useFocusEffect(
|
|
useCallback(() => {
|
|
if (!onBack) return;
|
|
|
|
const subscription = BackHandler.addEventListener(
|
|
"hardwareBackPress",
|
|
() => {
|
|
onBack();
|
|
return true;
|
|
}
|
|
);
|
|
|
|
return () => {
|
|
subscription.remove();
|
|
};
|
|
}, [onBack])
|
|
);
|
|
|
|
const handleBack = () => {
|
|
if (onBack) {
|
|
onBack();
|
|
return;
|
|
}
|
|
|
|
if (router.canGoBack()) {
|
|
router.back();
|
|
return;
|
|
}
|
|
|
|
router.replace(fallbackRoute as never);
|
|
};
|
|
|
|
const titleContent = (
|
|
<View style={styles.titleContent}>
|
|
<Text numberOfLines={1} style={[styles.title, { color: resolvedTitleColor }]}>
|
|
{title}
|
|
</Text>
|
|
{subtitle ? (
|
|
<Text numberOfLines={1} style={styles.subtitle}>
|
|
{subtitle}
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
);
|
|
|
|
return (
|
|
<View style={styles.header}>
|
|
<View style={styles.sideSlot}>
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel="Go back"
|
|
hitSlop={{ top: 12, bottom: 12, left: 12, right: 12 }}
|
|
onPress={handleBack}
|
|
style={({ pressed }) => [
|
|
styles.backButton,
|
|
pressed && styles.backButtonPressed,
|
|
]}
|
|
>
|
|
<Ionicons
|
|
name="chevron-back"
|
|
size={moderateScale(24)}
|
|
color={Colors.textPrimary}
|
|
/>
|
|
</Pressable>
|
|
</View>
|
|
|
|
{onTitlePress ? (
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityLabel={
|
|
titleAccessibilityLabel || `Open ${title} home`
|
|
}
|
|
hitSlop={8}
|
|
onPress={onTitlePress}
|
|
style={({ pressed }) => [
|
|
styles.titleButton,
|
|
pressed && styles.pressed,
|
|
]}
|
|
>
|
|
{titleContent}
|
|
</Pressable>
|
|
) : (
|
|
<View style={styles.titleButton}>{titleContent}</View>
|
|
)}
|
|
|
|
<View style={[styles.sideSlot, styles.rightSlot]}>{right}</View>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
header: {
|
|
minHeight: 64,
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "space-between",
|
|
paddingHorizontal: moderateScale(16),
|
|
paddingTop: Platform.OS === "web" ? 14 : moderateScale(8),
|
|
paddingBottom: moderateScale(8),
|
|
borderBottomWidth: 1,
|
|
borderBottomColor: "#ECECEC",
|
|
backgroundColor: "#FFFFFF",
|
|
},
|
|
sideSlot: {
|
|
width: 82,
|
|
height: 40,
|
|
alignItems: "flex-start",
|
|
justifyContent: "center",
|
|
},
|
|
rightSlot: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
justifyContent: "flex-end",
|
|
},
|
|
backButton: {
|
|
width: scale(38),
|
|
height: scale(38),
|
|
borderRadius: scale(19),
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
backgroundColor: "#F1F5F9",
|
|
},
|
|
backButtonPressed: {
|
|
backgroundColor: "#E2E8F0",
|
|
},
|
|
pressed: {
|
|
opacity: 0.65,
|
|
},
|
|
titleButton: {
|
|
flex: 1,
|
|
minHeight: 40,
|
|
marginHorizontal: moderateScale(12),
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
},
|
|
titleContent: {
|
|
width: "100%",
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
gap: 1,
|
|
},
|
|
title: {
|
|
width: "100%",
|
|
textAlign: "center",
|
|
fontSize: moderateScale(18),
|
|
fontWeight: "700",
|
|
color: Colors.textPrimary,
|
|
},
|
|
subtitle: {
|
|
width: "100%",
|
|
textAlign: "center",
|
|
fontSize: moderateScale(12),
|
|
lineHeight: moderateScale(16),
|
|
fontWeight: "500",
|
|
color: Colors.textSecondary,
|
|
},
|
|
});
|