1
+ import cx from 'clsx'
1
2
import styled from 'styled-components'
3
+ import React , { forwardRef } from 'react'
2
4
import type { SystemCommonProps , SystemTypographyProps } from '../constants'
3
5
import { COMMON , TYPOGRAPHY } from '../constants'
4
6
import type { SxProp } from '../sx'
5
7
import sx from '../sx'
8
+ import { useFeatureFlag } from '../FeatureFlags'
9
+ import Box from '../Box'
10
+ import { useRefObjectAsForwardedRef } from '../hooks'
11
+ import classes from './Text.module.css'
6
12
import type { ComponentProps } from '../utils/types'
13
+ import type { ForwardRefComponent as PolymorphicForwardRefComponent } from '../utils/polymorphic'
7
14
8
15
type StyledTextProps = {
9
16
size ?: 'large' | 'medium' | 'small'
@@ -12,10 +19,7 @@ type StyledTextProps = {
12
19
SystemCommonProps &
13
20
SxProp
14
21
15
- const Text = styled . span . attrs < StyledTextProps > ( ( { size, weight} ) => ( {
16
- 'data-size' : size ,
17
- 'data-weight' : weight ,
18
- } ) ) < StyledTextProps > `
22
+ const StyledText = styled . span < StyledTextProps > `
19
23
${ TYPOGRAPHY } ;
20
24
${ COMMON } ;
21
25
@@ -52,5 +56,57 @@ const Text = styled.span.attrs<StyledTextProps>(({size, weight}) => ({
52
56
53
57
${ sx } ;
54
58
`
55
- export type TextProps = ComponentProps < typeof Text >
59
+
60
+ const Text = forwardRef ( ( { as : Component = 'span' , className, size, weight, ...props } , forwardedRef ) => {
61
+ const enabled = useFeatureFlag ( 'primer_react_css_modules_team' )
62
+
63
+ const innerRef = React . useRef < HTMLElement > ( null )
64
+ useRefObjectAsForwardedRef ( forwardedRef , innerRef )
65
+
66
+ if ( enabled ) {
67
+ if ( props . sx ) {
68
+ return (
69
+ // @ts -ignore shh
70
+ < Box
71
+ as = { Component }
72
+ className = { cx ( className , classes . Text ) }
73
+ data-size = { size }
74
+ data-weight = { weight }
75
+ { ...props }
76
+ // @ts -ignore shh
77
+ ref = { innerRef }
78
+ />
79
+ )
80
+ }
81
+
82
+ return (
83
+ // @ts -ignore shh
84
+ < Component
85
+ className = { cx ( className , classes . Text ) }
86
+ data-size = { size }
87
+ data-weight = { weight }
88
+ { ...props }
89
+ // @ts -ignore shh
90
+ ref = { innerRef }
91
+ />
92
+ )
93
+ }
94
+
95
+ return (
96
+ // @ts -ignore shh
97
+ < StyledText
98
+ as = { Component }
99
+ className = { className }
100
+ data-size = { size }
101
+ data-weight = { weight }
102
+ { ...props }
103
+ // @ts -ignore shh
104
+ ref = { innerRef }
105
+ />
106
+ )
107
+ } ) as PolymorphicForwardRefComponent < 'span' , StyledTextProps >
108
+
109
+ Text . displayName = 'Text'
110
+
111
+ export type TextProps = ComponentProps < typeof StyledText >
56
112
export default Text
0 commit comments