import React from 'react';
import { TouchableOpacity, TouchableOpacityProps, View } from 'react-native';
import { conditionChaining, useSafeAreaPadding } from '@sendbird/uikit-utils';
import Text, { TextProps } from '../../components/Text';
import type { BaseHeaderProps, HeaderElement } from '../../index';
import createStyleSheet from '../../styles/createStyleSheet';
import useHeaderStyle from '../../styles/useHeaderStyle';
import useUIKitTheme from '../../theme/useUIKitTheme';
export type HeaderProps = BaseHeaderProps<
{
title?: HeaderElement;
left?: HeaderElement;
right?: HeaderElement;
onPressLeft?: () => void;
onPressRight?: () => void;
},
{
clearTitleMargin?: boolean;
clearStatusBarTopInset?: boolean;
statusBarTopInsetAs?: 'padding' | 'margin';
}
>;
const AlignMapper = { left: 'flex-start', center: 'center', right: 'flex-end' } as const;
const Header = ({
children,
titleAlign,
title = null,
left = null,
right = null,
onPressLeft,
onPressRight,
clearTitleMargin = false,
clearStatusBarTopInset = false,
statusBarTopInsetAs = 'padding',
}: HeaderProps) => {
const { topInset, defaultTitleAlign, defaultHeight } = useHeaderStyle();
const { colors } = useUIKitTheme();
const safeArea = useSafeAreaPadding(['left', 'right']);
const actualTitleAlign = titleAlign ?? defaultTitleAlign;
const actualTopInset = clearStatusBarTopInset ? 0 : topInset;
if (!title && !left && !right) {
return (
{children}
);
}
return (
{typeof title === 'string' ? {title} : title}
{children}
);
};
const LeftSide = ({ titleAlign, onPressLeft, left }: HeaderProps) => {
if (titleAlign === 'center') {
return {left && {left}};
}
if (!left) return null;
return (
{left}
);
};
const RightSide = ({ titleAlign, onPressRight, right }: HeaderProps) => {
if (titleAlign === 'center') {
return {right && {right}};
}
if (!right) return null;
return (
{right}
);
};
const HeaderTitle = ({ children, style, ...props }: TextProps) => {
return (
{children}
);
};
const HeaderSubtitle = ({ children, style, ...props }: TextProps) => {
const { colors } = useUIKitTheme();
return (
{children}
);
};
const HeaderButton = ({ children, disabled, onPress, color, ...props }: TouchableOpacityProps & { color?: string }) => {
return (
onPress?.(e)}
activeOpacity={0.7}
>
{conditionChaining(
[typeof children === 'string' || typeof children === 'number'],
[
{children}
,
children,
],
)}
);
};
const styles = createStyleSheet({
container: {
paddingHorizontal: 12,
borderBottomWidth: 1,
},
header: {
flexDirection: 'row',
},
title: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
marginHorizontal: 12,
},
left: {
height: '100%',
minWidth: 32,
alignItems: 'center',
justifyContent: 'center',
marginRight: 12,
},
right: {
height: '100%',
minWidth: 32,
alignItems: 'center',
justifyContent: 'center',
marginLeft: 12,
},
button: {
padding: 4,
},
});
export default Object.assign(Header, {
Button: HeaderButton,
Title: HeaderTitle,
Subtitle: HeaderSubtitle,
});