11import { clsx } from 'clsx'
2- import styled , { type StyledComponent } from 'styled-components'
2+ import { type StyledComponent } from 'styled-components'
33import React , { forwardRef } from 'react'
44import type { SystemCommonProps , SystemTypographyProps } from '../constants'
5- import { COMMON , TYPOGRAPHY } from '../constants'
65import type { SxProp } from '../sx'
7- import sx from '../sx'
8- import { useFeatureFlag } from '../FeatureFlags'
9- import Box from '../Box'
106import { useRefObjectAsForwardedRef } from '../hooks'
11- import type { ComponentProps } from '../utils/types'
12- import { includesSystemProps } from '../utils/includeSystemProps'
137import classes from './Text.module.css'
8+ import { toggleSxComponent } from '../internal/utils/toggleSxComponent'
149
1510type StyledTextProps = {
1611 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -19,95 +14,30 @@ type StyledTextProps = {
1914 weight ?: 'light' | 'normal' | 'medium' | 'semibold'
2015} & SystemTypographyProps &
2116 SystemCommonProps &
22- SxProp
23-
24- const StyledText = styled . span < StyledTextProps > `
25- ${ TYPOGRAPHY } ;
26- ${ COMMON } ;
27-
28- &:where([data-size='small']) {
29- font-size: var(--text-body-size-small, 0.75rem);
30- line-height: var(--text-body-lineHeight-small, 1.6666);
31- }
32-
33- &:where([data-size='medium']) {
34- font-size: var(--text-body-size-medium, 0.875rem);
35- line-height: var(--text-body-lineHeight-medium, 1.4285);
36- }
37-
38- &:where([data-size='large']) {
39- font-size: var(--text-body-size-large, 1rem);
40- line-height: var(--text-body-lineHeight-large, 1.5);
41- }
42-
43- &:where([data-weight='light']) {
44- font-weight: var(--base-text-weight-light, 300);
45- }
46-
47- &:where([data-weight='normal']) {
48- font-weight: var(--base-text-weight-normal, 400);
49- }
50-
51- &:where([data-weight='medium']) {
52- font-weight: var(--base-text-weight-medium, 500);
53- }
54-
55- &:where([data-weight='semibold']) {
56- font-weight: var(--base-text-weight-semibold, 600);
57- }
58-
59- ${ sx } ;
60- `
17+ SxProp &
18+ React . HTMLAttributes < HTMLSpanElement >
6119
20+ const TextBaseComponent = toggleSxComponent ( 'span' ) as React . ComponentType < StyledTextProps >
6221const Text = forwardRef ( ( { as : Component = 'span' , className, size, weight, ...props } , forwardedRef ) => {
63- const enabled = useFeatureFlag ( 'primer_react_css_modules_ga' )
64-
6522 const innerRef = React . useRef < HTMLElement > ( null )
6623 useRefObjectAsForwardedRef ( forwardedRef , innerRef )
67- if ( enabled ) {
68- // If props includes TYPOGRAPHY or COMMON props, pass them to the Box component
69- if ( includesSystemProps ( props ) ) {
70- return (
71- // @ts -ignore shh
72- < Box
73- as = { Component }
74- className = { clsx ( className , classes . Text ) }
75- data-size = { size }
76- data-weight = { weight }
77- { ...props }
78- // @ts -ignore shh
79- ref = { innerRef }
80- />
81- )
82- }
83-
84- return (
85- < Component
86- className = { clsx ( className , classes . Text ) }
87- data-size = { size }
88- data-weight = { weight }
89- { ...props }
90- // @ts -ignore shh
91- ref = { innerRef }
92- />
93- )
94- }
9524
9625 return (
97- < StyledText
26+ < TextBaseComponent
9827 as = { Component }
99- className = { className }
28+ className = { clsx ( className , classes . Text ) }
10029 data-size = { size }
10130 data-weight = { weight }
10231 { ...props }
10332 // @ts -ignore shh
10433 ref = { innerRef }
10534 />
10635 )
36+
10737 // eslint-disable-next-line @typescript-eslint/no-explicit-any
10838} ) as StyledComponent < 'span' , any , StyledTextProps , never >
10939
11040Text . displayName = 'Text'
11141
112- export type TextProps = ComponentProps < typeof StyledText >
42+ export type TextProps = StyledTextProps
11343export default Text
0 commit comments