|
1 |
| -import { createElement, forwardRef } from 'react'; |
2 |
| -import styled, { WebTarget } from 'styled-components'; |
| 1 | +import React from 'react'; |
| 2 | +import styled, { PolymorphicComponentProps } from 'styled-components'; |
| 3 | +import { KnownTarget } from 'styled-components/dist/types'; |
3 | 4 |
|
4 |
| -import { Colors, Typography, typography, TypographyProperties } from '../../foundations'; |
| 5 | +import { Colors, Typography, typography } from '../../foundations'; |
5 | 6 | import { TransientProps } from '../../types';
|
6 | 7 |
|
7 |
| -export type TextProps = React.PropsWithChildren<{ |
| 8 | +type TextBaseProps = { |
8 | 9 | variant?: Typography;
|
9 | 10 | color?: Colors;
|
10 |
| - tag?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'span'; |
11 |
| - as?: WebTarget; |
12 |
| - style?: React.CSSProperties; |
13 |
| -}>; |
| 11 | + children?: React.ReactNode; |
| 12 | +}; |
14 | 13 |
|
15 |
| -const StyledText = styled( |
16 |
| - ({ tag = 'span', ...props }: { tag: TextProps['tag'] } & TransientProps<TypographyProperties>) => |
17 |
| - createElement(tag, props), |
18 |
| -)((props) => ({ |
19 |
| - color: 'var(--color)', |
20 |
| - fontFamily: props.$fontFamily, |
21 |
| - fontWeight: props.$fontWeight, |
22 |
| - fontSize: props.$fontSize, |
23 |
| - lineHeight: props.$lineHeight, |
24 |
| -})); |
| 14 | +export type TextProps<T extends KnownTarget = 'span'> = PolymorphicComponentProps< |
| 15 | + 'web', |
| 16 | + TextBaseProps, |
| 17 | + T, |
| 18 | + T |
| 19 | +>; |
25 | 20 |
|
26 |
| -export const Text = forwardRef( |
27 |
| - ( |
28 |
| - { |
29 |
| - tag = 'span', |
30 |
| - variant = 'bodySmall', |
31 |
| - color = Colors.white, |
32 |
| - children, |
33 |
| - style, |
34 |
| - ...props |
35 |
| - }: TextProps, |
36 |
| - ref, |
37 |
| - ) => { |
38 |
| - const { fontFamily, fontSize, fontWeight, lineHeight } = typography[variant]; |
39 |
| - return ( |
40 |
| - <StyledText |
41 |
| - ref={ref} |
42 |
| - tag={tag} |
43 |
| - style={ |
44 |
| - { |
45 |
| - '--color': color, |
46 |
| - ...style, |
47 |
| - } as React.CSSProperties |
48 |
| - } |
49 |
| - $fontFamily={fontFamily} |
50 |
| - $fontWeight={fontWeight} |
51 |
| - $fontSize={fontSize} |
52 |
| - $lineHeight={lineHeight} |
53 |
| - {...props}> |
54 |
| - {children} |
55 |
| - </StyledText> |
56 |
| - ); |
| 21 | +const StyledText = styled.span<TransientProps<TextBaseProps>>( |
| 22 | + ({ $variant = 'bodySmall', $color = Colors.white }) => { |
| 23 | + const { fontFamily, fontSize, fontWeight, lineHeight } = typography[$variant]; |
| 24 | + return ` |
| 25 | + --color: ${$color}; |
| 26 | + |
| 27 | + color: var(--color); |
| 28 | + font-family: ${fontFamily}; |
| 29 | + font-size: ${fontSize}; |
| 30 | + font-weight: ${fontWeight}; |
| 31 | + line-height: ${lineHeight}; |
| 32 | + `; |
57 | 33 | },
|
58 | 34 | );
|
59 | 35 |
|
| 36 | +export const Text = <T extends KnownTarget = 'span'>({ |
| 37 | + variant, |
| 38 | + color, |
| 39 | + children, |
| 40 | + style, |
| 41 | + ...props |
| 42 | +}: TextProps<T>) => { |
| 43 | + return ( |
| 44 | + <StyledText $variant={variant} $color={color} {...props}> |
| 45 | + {children} |
| 46 | + </StyledText> |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
60 | 50 | Text.displayName = 'Text';
|
0 commit comments