Skip to content

Commit 519a2c7

Browse files
authored
Merge pull request #21 from maxholman/refactor-variants
fix: allow components to be specified for Heading and Paragraph
2 parents 87c9eed + e61cc0b commit 519a2c7

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

lib/typography.tsx

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export type HeadingProps<T extends keyof ReactHTMLElementsHacked = 'p'> = Merge<
3333
CommonTextProps & { level?: HeadingLevel }
3434
>;
3535

36-
function headingProps(level: HeadingLevel): HeadingProps {
36+
function headingProps(
37+
level: HeadingLevel,
38+
): Partial<HeadingProps<`h${HeadingLevel}`>> {
3739
switch (level) {
3840
case '1':
3941
return { fontSize: '5', fontWeight: 'bold' };
@@ -44,9 +46,17 @@ function headingProps(level: HeadingLevel): HeadingProps {
4446
case '4':
4547
return { fontSize: '2', fontWeight: 'semibold' };
4648
case '5':
47-
return { fontSize: '1', secondary: true, fontWeight: 'medium' };
49+
return {
50+
fontSize: '1',
51+
secondary: true,
52+
fontWeight: 'medium',
53+
};
4854
case '6':
49-
return { fontSize: '0', secondary: true, fontWeight: 'medium' };
55+
return {
56+
fontSize: '0',
57+
secondary: true,
58+
fontWeight: 'medium',
59+
};
5060
default:
5161
return {};
5262
}
@@ -67,32 +77,44 @@ export const ExactText = forwardRef(
6777
),
6878
);
6979

70-
export const Heading = forwardRef<HTMLHeadingElement, HeadingProps>(
71-
({ className, secondary, level = '3', ...props }, ref) => {
80+
export const Heading = forwardRef(
81+
<T extends keyof ReactHTMLElementsHacked = 'h3'>(
82+
{
83+
level = '3',
84+
component = `h${level}`,
85+
className,
86+
secondary,
87+
...props
88+
}: HeadingProps<T>,
89+
forwardedRef: ForwardedRef<ReactHTMLElementsHacked[T]>,
90+
) => {
7291
const { secondary: headingIsSecondary, ...rest } = headingProps(level);
7392
return (
7493
<Box
75-
ref={ref}
94+
component={component}
95+
ref={forwardedRef}
7696
lineHeight="heading"
7797
className={[
7898
className,
7999
(headingIsSecondary || secondary) && secondaryClassName,
80100
]}
81-
component={`h${level}`}
82101
{...rest}
83102
{...props}
84103
/>
85104
);
86105
},
87106
);
88107

89-
export const Paragraph = forwardRef<HTMLParagraphElement, ParagraphProps>(
90-
({ className, secondary, ...props }, ref) => (
108+
export const Paragraph = forwardRef(
109+
<T extends keyof ReactHTMLElementsHacked = 'p'>(
110+
{ component = 'p', className, secondary, ...props }: ParagraphProps<T>,
111+
forwardedRef: ForwardedRef<ReactHTMLElementsHacked[T]>,
112+
) => (
91113
<Box
92-
ref={ref}
114+
component={component}
115+
ref={forwardedRef}
93116
className={[className, secondary && secondaryClassName]}
94117
lineHeight="paragraph"
95-
component="p"
96118
{...props}
97119
/>
98120
),

0 commit comments

Comments
 (0)