1 import { VariantProps, cva } from "cva"
2 import { Slot } from "radix-ui"
3 import * as React from "react"
4
5 import { clx } from "@/utils/clx"
6
7 const textVariants = cva({
8 variants: {
9 size: {
10 xsmall: "",
11 small: "",
12 base: "",
13 large: "",
14 xlarge: "",
15 },
16 weight: {
17 regular: "font-normal",
18 plus: "font-medium",
19 },
20 family: {
21 sans: "font-sans",
22 mono: "font-mono",
23 },
24 leading: {
25 normal: "",
26 compact: "",
27 },
28 },
29 defaultVariants: {
30 family: "sans",
31 size: "base",
32 weight: "regular",
33 leading: "normal",
34 },
35 compoundVariants: [
36 {
37 size: "xsmall",
38 leading: "normal",
39 className: "txt-xsmall",
40 },
41 {
42 size: "xsmall",
43 leading: "compact",
44 className: "txt-compact-xsmall",
45 },
46 {
47 size: "small",
48 leading: "normal",
49 className: "txt-small",
50 },
51 {
52 size: "small",
53 leading: "compact",
54 className: "txt-compact-small",
55 },
56 {
57 size: "base",
58 leading: "normal",
59 className: "txt-medium",
60 },
61 {
62 size: "base",
63 leading: "compact",
64 className: "txt-compact-medium",
65 },
66 {
67 size: "large",
68 leading: "normal",
69 className: "txt-large",
70 },
71 {
72 size: "large",
73 leading: "compact",
74 className: "txt-compact-large",
75 },
76 {
77 size: "xlarge",
78 leading: "normal",
79 className: "txt-xlarge",
80 },
81 {
82 size: "xlarge",
83 leading: "compact",
84 className: "txt-compact-xlarge",
85 },
86 ],
87 })
88
89 export interface TextProps
90 extends React.ComponentPropsWithoutRef<"p">,
91 VariantProps<typeof textVariants> {
92 asChild?: boolean
93 as?: "p" | "span" | "div"
94 }
95
96 /**
97 * This component is based on the `p` element and supports all of its props
98 */
99 const Text = React.forwardRef<HTMLParagraphElement, TextProps>(
100 (
101 {
102 className,
103 /**
104 * Whether to remove the wrapper `button` element and use the
105 * passed child element instead.
106 */
107 asChild = false,
108 /**
109 * The wrapper element to use when `asChild` is disabled.
110 */
111 as = "p",
112 /**
113 * The text's size.
114 */
115 size = "base",
116 /**
117 * The text's font weight.
118 */
119 weight = "regular",
120 /**
121 * The text's font family.
122 */
123 family = "sans",
124 /**
125 * The text's line height.
126 */
127 leading = "normal",
128 children,
129 ...props
130 }: TextProps,
131 ref
132 ) => {
133 const Component = asChild ? Slot.Root : as
134
135 return (
136 <Component
137 ref={ref}
138 className={clx(
139 textVariants({ size, weight, family, leading }),
140 className
141 )}
142 {...props}
143 >
144 {children}
145 </Component>
146 )
147 }
148 )
149 Text.displayName = "Text"
150
151 export { Text }