import { createStyledContext, styled, YStack, Text } from '@tamagui/core'
// 1. Define variant types
type CardVariants = {
size?: 'small' | 'medium' | 'large'
variant?: 'outlined' | 'filled'
}
// 2. Create context with defaults
const CardContext = createStyledContext<CardVariants>({
size: 'medium',
variant: 'filled',
})
// 3. Create parent component
export const Card = styled(YStack, {
context: CardContext,
variants: {
size: {
small: { padding: '$2', gap: '$2' },
medium: { padding: '$4', gap: '$3' },
large: { padding: '$6', gap: '$4' },
},
variant: {
outlined: {
borderWidth: 1,
borderColor: '$borderColor',
},
filled: {
backgroundColor: '$background',
},
},
},
})
// 4. Create child components that inherit context
export const CardTitle = styled(Text, {
context: CardContext,
variants: {
size: {
small: { fontSize: '$3', fontWeight: '600' },
medium: { fontSize: '$5', fontWeight: '600' },
large: { fontSize: '$7', fontWeight: '700' },
},
},
})
export const CardDescription = styled(Text, {
context: CardContext,
variants: {
size: {
small: { fontSize: '$2', color: '$color11' },
medium: { fontSize: '$3', color: '$color11' },
large: { fontSize: '$4', color: '$color11' },
},
},
})
// 5. Use the compound component
function Example() {
return (
<Card size="large" variant="outlined">
<CardTitle>Title</CardTitle>
<CardDescription>Description</CardDescription>
</Card>
)
}