acupedia-mobile-ui/src/features/content/components/layout/ContentPageHeader.tsx

163 lines
3.6 KiB
TypeScript

import React, { type ReactNode } from "react";
import { Platform, Pressable, StyleSheet, Text, View } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { router } from "expo-router";
import { moderateScale } from "react-native-size-matters";
import { Colors } from "@/theme";
export interface ContentPageHeaderProps {
title: string;
subtitle?: string;
right?: ReactNode;
fallbackRoute?: string;
onTitlePress?: () => void;
titleAccessibilityLabel?: string;
onBack?: () => void;
}
export function ContentPageHeader({
title,
subtitle,
right,
fallbackRoute = "/modules",
onTitlePress,
titleAccessibilityLabel,
onBack,
}: ContentPageHeaderProps) {
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}>
{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={10}
onPress={handleBack}
style={({ pressed }) => [
styles.iconButton,
pressed && styles.pressed,
]}
>
<Ionicons
name="chevron-back"
size={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",
},
iconButton: {
width: 40,
height: 40,
borderRadius: 20,
alignItems: "center",
justifyContent: "center",
},
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,
},
});