|
| 1 | +import React from "react"; |
| 2 | +import { twMerge } from "tailwind-merge"; |
| 3 | + |
| 4 | +const defaultVariantMapping = { |
| 5 | + h1: { component: "h1", className: "text-4xl font-bold" }, |
| 6 | + h2: { component: "h2", className: "text-3xl font-bold" }, |
| 7 | + h3: { component: "h3", className: "text-2xl font-bold" }, |
| 8 | + h4: { component: "h4", className: "text-xl font-bold" }, |
| 9 | + h5: { component: "h5", className: "text-lg font-bold" }, |
| 10 | + h6: { component: "h6", className: "text-base font-bold" }, |
| 11 | + subtitle1: { component: "h6", className: "text-base font-medium" }, |
| 12 | + subtitle2: { component: "h6", className: "text-sm font-medium" }, |
| 13 | + body1: { component: "p", className: "text-base" }, |
| 14 | + body2: { component: "p", className: "text-sm" }, |
| 15 | + button: { component: "span", className: "text-sm uppercase font-medium" }, |
| 16 | + caption: { component: "span", className: "text-xs" }, |
| 17 | + overline: { component: "span", className: "text-xs uppercase" }, |
| 18 | + inherit: { component: "p", className: "" }, |
| 19 | +}; |
| 20 | + |
| 21 | +const alignClasses = { |
| 22 | + center: "text-center", |
| 23 | + justify: "text-justify", |
| 24 | + left: "text-left", |
| 25 | + right: "text-right", |
| 26 | + inherit: "", |
| 27 | +}; |
| 28 | + |
| 29 | +const Typography = React.forwardRef( |
| 30 | + ( |
| 31 | + { |
| 32 | + component: _Component, |
| 33 | + variant = "body1", |
| 34 | + align = "inherit", |
| 35 | + gutterBottom = false, |
| 36 | + noWrap = false, |
| 37 | + paragraph = false, |
| 38 | + className, |
| 39 | + variantMapping, |
| 40 | + ...props |
| 41 | + }: any, |
| 42 | + ref |
| 43 | + ) => { |
| 44 | + const _variantMapping = variantMapping || defaultVariantMapping |
| 45 | + |
| 46 | + const variantProps = (!paragraph && _variantMapping[variant]) || { component: _Component || "p", className: "" }; |
| 47 | + |
| 48 | + const appliedClasses = twMerge( |
| 49 | + variantProps.className, |
| 50 | + alignClasses[align], |
| 51 | + gutterBottom && "mb-2", |
| 52 | + noWrap && "truncate line-clamp-1", |
| 53 | + // paragraph && "mb-6", |
| 54 | + className |
| 55 | + ); |
| 56 | + |
| 57 | + const Component = _Component || (variantProps.component || variantProps); |
| 58 | + |
| 59 | + return <Component ref={ref} className={appliedClasses} {...props}>{props.children}</Component>; |
| 60 | + } |
| 61 | +); |
| 62 | + |
| 63 | +export default Typography; |
0 commit comments