import { useId } from 'react' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' import Block from '../Primitives/Block/Block' import ButtonV2 from '../ButtonV2/ButtonV2' import type { CardV2TokensType } from './cardV2.tokens' import { useCardV2Context } from './CardV2Context' import { CardV2Orientation, type CardV2ActionsProps, type CardV2BodyProps, type CardV2FooterProps, type CardV2HeaderProps, type CardV2MediaProps, type CardV2MetaProps, type CardV2SectionProps, } from './cardV2.types' import { renderCardV2Text, toCardV2ActionArray } from './CardV2.utils' const useCardV2SectionContext = () => { const context = useCardV2Context() const responsiveTokens = useResponsiveTokens('CARDV2') const fallbackId = useId() return { tokens: context?.tokens ?? responsiveTokens, centered: context?.centered ?? false, scrollable: context?.scrollable ?? false, ids: context?.ids ?? { eyebrow: `${fallbackId}-card-eyebrow`, title: `${fallbackId}-card-title`, subtitle: `${fallbackId}-card-subtitle`, description: `${fallbackId}-card-description`, }, } } export const CardV2Meta = ({ title, truncateTitle, subtitle, eyebrow, centered, }: CardV2MetaProps) => { const { tokens, centered: contextCentered, ids } = useCardV2SectionContext() const isCentered = centered ?? contextCentered if (!eyebrow && !title && !subtitle) return null return ( {renderCardV2Text({ value: eyebrow, id: ids.eyebrow, as: 'p', styles: tokens.header.eyebrow as React.CSSProperties, dataElement: 'card-eyebrow', })} {renderCardV2Text({ value: title, id: ids.title, as: 'h2', styles: tokens.header.title as React.CSSProperties, dataElement: 'card-title', truncate: truncateTitle, })} {renderCardV2Text({ value: subtitle, id: ids.subtitle, as: 'p', styles: tokens.header.subtitle as React.CSSProperties, dataElement: 'card-subtitle', })} ) } export const CardV2Header = ({ title, truncateTitle, subtitle, eyebrow, leadingSlot, trailingSlot, centered, }: CardV2HeaderProps) => { const { tokens, centered: contextCentered, ids } = useCardV2SectionContext() const isCentered = centered ?? contextCentered const hasHeader = Boolean( leadingSlot || eyebrow || title || subtitle || trailingSlot ) if (!hasHeader) return null return ( {leadingSlot && {leadingSlot}} {trailingSlot && ( {trailingSlot} )} ) } export const CardV2Media = ({ children, orientation, width, height, minHeight, }: CardV2MediaProps) => { const { tokens, centered } = useCardV2SectionContext() const isHorizontal = orientation === CardV2Orientation.HORIZONTAL if (!children) return null return ( {children} ) } export const CardV2Actions = ({ actions, centered }: CardV2ActionsProps) => { const { tokens, centered: contextCentered } = useCardV2SectionContext() const isCentered = centered ?? contextCentered const actionItems = toCardV2ActionArray(actions) if (actionItems.length === 0) return null return ( {actionItems.map((action, index) => ( ))} ) } export const CardV2Content = ({ children }: CardV2SectionProps) => { const { centered } = useCardV2SectionContext() if (!children) return null return ( {children} ) } export const CardV2Body = ({ description, actions, centered, scrollable, children, }: CardV2BodyProps) => { const { tokens, centered: contextCentered, scrollable: contextScrollable, ids, } = useCardV2SectionContext() const isCentered = centered ?? contextCentered const isScrollable = scrollable ?? contextScrollable const hasBody = Boolean(description || children || actions) if (!hasBody) return null return ( {renderCardV2Text({ value: description, id: ids.description, as: 'p', styles: tokens.body.description as React.CSSProperties, dataElement: 'card-description', })} {children} ) } export const CardV2Footer = ({ children, actions, centered, divider = false, }: CardV2FooterProps) => { const { tokens, centered: contextCentered } = useCardV2SectionContext() const isCentered = centered ?? contextCentered const hasFooter = Boolean(children || actions) if (!hasFooter) return null return ( {children && {children}} ) }