|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import type { RecipeVariantProps } from '../slot-recipe'; |
| 4 | +import { defineSlotRecipe, useRecipe } from '../slot-recipe'; |
| 5 | +import { TextContext } from './text'; |
| 6 | + |
| 7 | +export const cardRecipe = defineSlotRecipe(theme => ({ |
| 8 | + slots: { |
| 9 | + root: { slot: 'card-root' }, |
| 10 | + header: { slot: 'card-header' }, |
| 11 | + content: { slot: 'card-content' }, |
| 12 | + footer: { slot: 'card-footer' }, |
| 13 | + }, |
| 14 | + base: { |
| 15 | + root: { |
| 16 | + backgroundColor: theme.color.card, |
| 17 | + color: theme.color.cardForeground, |
| 18 | + borderRadius: theme.rounded.lg, |
| 19 | + display: 'flex', |
| 20 | + flexDirection: 'column', |
| 21 | + rowGap: theme.spacing(5), |
| 22 | + width: '100%', |
| 23 | + overflow: 'hidden', |
| 24 | + border: `1px solid ${theme.color.border}`, |
| 25 | + }, |
| 26 | + header: { |
| 27 | + display: 'flex', |
| 28 | + flexDirection: 'column', |
| 29 | + paddingBlockStart: theme.spacing(5), |
| 30 | + paddingInline: theme.spacing(4), |
| 31 | + }, |
| 32 | + content: { |
| 33 | + paddingInline: theme.spacing(4), |
| 34 | + flex: '1 1 auto', |
| 35 | + }, |
| 36 | + footer: { |
| 37 | + paddingBlockEnd: theme.spacing(5), |
| 38 | + paddingInline: theme.spacing(4), |
| 39 | + }, |
| 40 | + }, |
| 41 | + variants: { |
| 42 | + alignment: { |
| 43 | + start: { |
| 44 | + header: { alignItems: 'flex-start', textAlign: 'start' }, |
| 45 | + }, |
| 46 | + center: { |
| 47 | + header: { alignItems: 'center', textAlign: 'center' }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + defaultVariants: { |
| 52 | + alignment: 'start', |
| 53 | + }, |
| 54 | +})); |
| 55 | + |
| 56 | +type CardVariantProps = RecipeVariantProps<typeof cardRecipe>; |
| 57 | + |
| 58 | +const CardVariantContext = React.createContext<CardVariantProps>({}); |
| 59 | + |
| 60 | +declare module '../registry' { |
| 61 | + interface MosaicSlotRegistry { |
| 62 | + 'card-root': true; |
| 63 | + 'card-header': true; |
| 64 | + 'card-content': true; |
| 65 | + 'card-footer': true; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +const Root = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<'div'>>(function CardRoot(props, ref) { |
| 70 | + const variantProps = React.useContext(CardVariantContext); |
| 71 | + const { root } = useRecipe(cardRecipe, { variants: variantProps }); |
| 72 | + return ( |
| 73 | + <div |
| 74 | + ref={ref} |
| 75 | + {...root} |
| 76 | + {...props} |
| 77 | + /> |
| 78 | + ); |
| 79 | +}); |
| 80 | + |
| 81 | +const Header = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<'div'>>(function CardHeader(props, ref) { |
| 82 | + const variantProps = React.useContext(CardVariantContext); |
| 83 | + const { header } = useRecipe(cardRecipe, { variants: variantProps }); |
| 84 | + return ( |
| 85 | + <TextContext.Provider value={{ intent: 'mutedForeground' }}> |
| 86 | + <div |
| 87 | + ref={ref} |
| 88 | + {...header} |
| 89 | + {...props} |
| 90 | + /> |
| 91 | + </TextContext.Provider> |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +const Content = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<'div'>>( |
| 96 | + function CardContent(props, ref) { |
| 97 | + const { content } = useRecipe(cardRecipe); |
| 98 | + return ( |
| 99 | + <div |
| 100 | + ref={ref} |
| 101 | + {...content} |
| 102 | + {...props} |
| 103 | + /> |
| 104 | + ); |
| 105 | + }, |
| 106 | +); |
| 107 | + |
| 108 | +const Footer = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<'div'>>(function CardFooter(props, ref) { |
| 109 | + const { footer } = useRecipe(cardRecipe); |
| 110 | + return ( |
| 111 | + <div |
| 112 | + ref={ref} |
| 113 | + {...footer} |
| 114 | + {...props} |
| 115 | + /> |
| 116 | + ); |
| 117 | +}); |
| 118 | + |
| 119 | +export interface CardProps extends React.ComponentPropsWithoutRef<'div'> { |
| 120 | + alignment?: CardVariantProps['alignment']; |
| 121 | +} |
| 122 | + |
| 123 | +export function Card({ alignment, children, ...props }: CardProps) { |
| 124 | + return ( |
| 125 | + <CardVariantContext.Provider value={{ alignment }}> |
| 126 | + <Root {...props}>{children}</Root> |
| 127 | + </CardVariantContext.Provider> |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +Card.Root = Root; |
| 132 | +Card.Header = Header; |
| 133 | +Card.Content = Content; |
| 134 | +Card.Footer = Footer; |
0 commit comments