import { PolymorphicComponentProps } from '@kalink-ui/dibbly'; import { ElementType, ReactElement, ReactNode } from 'react'; import { Spacing, TypographySize, TypographyVariant } from '../../styles'; import { ConditionalWrapper } from '../conditional-wrapper'; import { Text, TextProps, TextVariants } from '../text'; import { headingRootResponsive, pretitleResponsive, subtitleResponsive, } from './heading.responsive'; import type { Responsive } from '../../styles/responsive'; export type HeadingTypes = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; export type HeadingProps = PolymorphicComponentProps & Omit & { /** * The typography used to render the text. */ variant?: Responsive< Extract >; /** * The size of the typography used to render the text. */ size?: Responsive; /** * If provided, the text will be rendered before the title. */ pretitle?: ReactElement>; /** * If provided, the text will be rendered after the title. */ subtitle?: ReactElement>; /** * The text to render. */ children: ReactNode; /** * The class to pass to the root element. */ rootClassName?: string; align?: Responsive>; }; const headingMapping: Record< HeadingTypes, { variant: TypographyVariant; size: TypographySize } > = { h1: { variant: 'display', size: 'large' }, h2: { variant: 'display', size: 'medium' }, h3: { variant: 'display', size: 'small' }, h4: { variant: 'headline', size: 'large' }, h5: { variant: 'headline', size: 'medium' }, h6: { variant: 'headline', size: 'small' }, }; export function Heading(props: HeadingProps) { const { children, use = 'h2', size, variant, align, pretitle, subtitle, rootClassName, ref, ...rest } = props; return ( {pretitle} ['use']} align={align} variant={variant ?? headingMapping[use].variant} size={size ?? headingMapping[use].size} {...rest} > {children} {subtitle} ); } export type HeadingPretitleProps = Omit, 'children'> & { children?: string | null; spacing?: Responsive; }; Heading.Pretitle = function HeadingPretitle({ variant = 'title', size = 'medium', spacing, children, className, ...rest }: HeadingPretitleProps) { return ( children && ( {children} ) ); }; export type HeadingSubtitleProps = Omit, 'children'> & { children?: string | null; spacing?: Responsive; }; Heading.Subtitle = function HeadingSubtitle({ variant = 'title', size = 'medium', spacing, children, className, ...rest }: HeadingSubtitleProps) { return ( children && ( {children} ) ); };