import { JSX } from "react";
import { Image, ImageStyle, StyleSheet, ViewStyle, Platform, TouchableOpacity } from "react-native";
import Block from "./Block";
import { useTheme, useColors } from "./theme";
import Icon from "./Icon";
import Text from "./Text";
interface ImageProps {
image?: string;
imageBlockStyle?: ViewStyle;
imageStyle?: ImageStyle;
}
function renderImage({
image,
imageBlockStyle,
imageStyle,
}: ImageProps): JSX.Element | null {
const theme = useTheme();
const colors = useColors();
if (!image) return null;
return (
);
}
interface AvatarProps {
avatar?: string;
}
function renderAvatar({
avatar,
}: AvatarProps): JSX.Element | null {
const theme = useTheme();
const colors = useColors();
if (!avatar) return null;
return ;
}
interface LocationProps {
location?: string;
locationColor?: string;
}
function renderLocation({
location,
locationColor,
}: LocationProps): JSX.Element | null {
const theme = useTheme();
const colors = useColors();
if (!location) return null;
if (typeof location !== 'string') {
return location as JSX.Element;
}
return (
{location}
);
}
interface AuthorProps {
avatar?: string;
title?: string;
titleColor?: string;
caption?: string;
captionColor?: string;
location?: string;
locationColor?: string;
footerStyle?: ViewStyle;
rightSideComponent?: React.ReactNode;
}
function renderAuthor({
avatar,
title,
titleColor,
caption,
captionColor,
location,
locationColor,
footerStyle,
rightSideComponent,
}: AuthorProps): JSX.Element | null {
const theme = useTheme();
const colors = useColors();
if (!title && !caption) return null;
return (
{renderAvatar({avatar})}
{title && (
{title}
)}
{rightSideComponent && (
{rightSideComponent}
)}
{caption && (
{caption}
)}
{location && (
{renderLocation({location, locationColor})}
)}
);
}
interface CardProps {
avatar?: string;
title?: string;
titleColor?: string;
caption?: string;
captionColor?: string;
location?: string;
locationColor?: string;
footerStyle?: ViewStyle;
image?: string;
imageBlockStyle?: ViewStyle;
imageStyle?: ImageStyle;
borderless?: boolean;
card?: boolean;
shadow?: boolean;
children?: React.ReactNode;
style?: ViewStyle;
// Additional props for compatibility
neutral?: boolean;
fullBackgroundImage?: boolean;
authorImageSrc?: string;
authorTitle?: string;
authorSubTitle?: string;
onPress?: () => void;
rightSideComponent?: React.ReactNode;
flex?: boolean;
shadowColor?: string;
}
function Card({
avatar,
title,
titleColor,
caption,
captionColor,
location,
locationColor,
borderless = false,
footerStyle,
image,
imageBlockStyle,
imageStyle,
children,
card = true,
shadow = true,
style,
neutral = false,
fullBackgroundImage = false,
authorImageSrc,
authorTitle,
authorSubTitle,
onPress,
rightSideComponent,
flex = false,
shadowColor,
}: CardProps): JSX.Element {
const theme = useTheme();
const colors = useColors();
// Use authorImageSrc as avatar if provided
const finalAvatar = authorImageSrc || avatar;
// Use authorTitle as title if provided
const finalTitle = authorTitle || title;
// Use authorSubTitle as caption if provided
const finalCaption = authorSubTitle || caption;
const styleCard = [
borderless && { borderWidth: 0 },
neutral && { backgroundColor: 'transparent', borderWidth: 0 },
fullBackgroundImage && {
backgroundColor: 'transparent',
borderWidth: 0,
overflow: 'hidden',
borderRadius: 0,
},
flex && { flex: 1 },
{
borderWidth: 0.5,
borderColor: colors.border,
borderRadius: 12,
backgroundColor: colors.surface,
marginVertical: 8,
...Platform.select({
ios: {
shadowColor: shadowColor || colors.border,
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
},
android: {
elevation: 2,
},
web: {
boxShadow: '0px 2px 4px rgba(0, 0, 0, 0.1)',
},
}),
},
style
].filter(Boolean) as any;
const cardContent = (
{fullBackgroundImage && image && (
)}
{!fullBackgroundImage && renderImage({image, imageBlockStyle, imageStyle})}
{renderAuthor({
avatar: finalAvatar,
title: finalTitle,
titleColor,
caption: finalCaption,
captionColor,
location,
locationColor,
footerStyle,
rightSideComponent,
})}
{children}
);
if (onPress) {
return (
{cardContent}
);
}
return cardContent;
}
const styles = (theme: ReturnType, colors: ReturnType) => {
return StyleSheet.create({
card: {
borderWidth: 0,
backgroundColor: colors.surface,
width: theme.sizes.CARD_WIDTH,
marginVertical: theme.sizes.CARD_MARGIN_VERTICAL,
...Platform.select({
ios: {
shadowColor: colors.border,
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 4,
},
android: {
elevation: 2,
},
web: {
boxShadow: '0px 2px 4px rgba(0, 0, 0, 0.1)',
},
}),
},
footer: {
justifyContent: 'flex-start',
alignItems: 'flex-start',
paddingHorizontal: theme.sizes.CARD_FOOTER_HORIZONTAL,
paddingVertical: theme.sizes.CARD_FOOTER_VERTICAL,
backgroundColor: colors.transparent,
zIndex: 1,
},
avatar: {
width: theme.sizes.CARD_AVATAR_WIDTH,
height: theme.sizes.CARD_AVATAR_HEIGHT,
borderRadius: theme.sizes.CARD_AVATAR_RADIUS,
},
title: {
justifyContent: 'flex-start',
alignItems: 'flex-start',
},
imageBlock: {
borderWidth: 0,
overflow: 'hidden',
},
image: {
width: 'auto',
height: theme.sizes.CARD_IMAGE_HEIGHT,
},
round: {
borderRadius: theme.sizes.CARD_ROUND,
},
rounded: {
borderRadius: theme.sizes.CARD_ROUNDED,
},
});
};
export default Card;