1
1
import type { MouseEventHandler } from 'react'
2
2
import React , { forwardRef } from 'react'
3
3
import Box from '../Box'
4
- import type { BetterSystemStyleObject , SxProp } from '../sx'
5
- import { merge } from '../sx'
4
+ import { merge , type BetterSystemStyleObject , type SxProp } from '../sx'
6
5
import { defaultSxProp } from '../utils/defaultSxProp'
7
6
import type { TokenBaseProps } from './TokenBase'
8
7
import TokenBase , { defaultTokenSize , isTokenInteractive } from './TokenBase'
9
8
import RemoveTokenButton from './_RemoveTokenButton'
10
9
import TokenTextContainer from './_TokenTextContainer'
11
10
import type { ForwardRefComponent as PolymorphicForwardRefComponent } from '../utils/polymorphic'
12
11
import VisuallyHidden from '../_VisuallyHidden'
12
+ import { useFeatureFlag } from '../FeatureFlags'
13
+
14
+ import classes from './Token.module.css'
15
+ import { clsx } from 'clsx'
13
16
14
17
// Omitting onResize and onResizeCapture because seems like React 18 types includes these menthod in the expansion but React 17 doesn't.
15
18
// TODO: This is a temporary solution until we figure out why these methods are causing type errors.
@@ -20,6 +23,8 @@ export interface TokenProps extends TokenBaseProps, SxProp {
20
23
leadingVisual ?: React . ElementType
21
24
}
22
25
26
+ const CSS_MODULES_FEATURE_FLAG = 'primer_react_css_modules_team'
27
+
23
28
const tokenBorderWidthPx = 1
24
29
25
30
const LeadingVisualContainer : React . FC < React . PropsWithChildren < Pick < TokenBaseProps , 'size' > > > = ( { children, size} ) => (
@@ -35,6 +40,8 @@ const LeadingVisualContainer: React.FC<React.PropsWithChildren<Pick<TokenBasePro
35
40
)
36
41
37
42
const Token = forwardRef ( ( props , forwardedRef ) => {
43
+ const enabled = useFeatureFlag ( CSS_MODULES_FEATURE_FLAG )
44
+
38
45
const {
39
46
as,
40
47
onRemove,
@@ -46,6 +53,8 @@ const Token = forwardRef((props, forwardedRef) => {
46
53
href,
47
54
onClick,
48
55
sx : sxProp = defaultSxProp ,
56
+ className,
57
+ style,
49
58
...rest
50
59
} = props
51
60
const hasMultipleActionTargets = isTokenInteractive ( props ) && Boolean ( onRemove ) && ! hideRemoveButton
@@ -58,7 +67,8 @@ const Token = forwardRef((props, forwardedRef) => {
58
67
href,
59
68
onClick,
60
69
}
61
- const sx = merge < BetterSystemStyleObject > (
70
+
71
+ const mergedSx = merge < BetterSystemStyleObject > (
62
72
{
63
73
backgroundColor : 'neutral.subtle' ,
64
74
borderColor : props . isSelected ? 'fg.default' : 'border.subtle' ,
@@ -80,16 +90,56 @@ const Token = forwardRef((props, forwardedRef) => {
80
90
sxProp ,
81
91
)
82
92
93
+ if ( enabled ) {
94
+ return (
95
+ < TokenBase
96
+ onRemove = { onRemove }
97
+ id = { id ?. toString ( ) }
98
+ className = { clsx ( className , classes . Token ) }
99
+ text = { text }
100
+ size = { size }
101
+ sx = { sxProp }
102
+ data-is-selected = { props . isSelected }
103
+ data-is-remove-btn = { ! ( hideRemoveButton || ! onRemove ) }
104
+ { ...( ! hasMultipleActionTargets ? interactiveTokenProps : { } ) }
105
+ { ...rest }
106
+ ref = { forwardedRef }
107
+ style = { { borderWidth : `${ tokenBorderWidthPx } px` , ...style } }
108
+ >
109
+ { LeadingVisual ? (
110
+ < LeadingVisualContainer size = { size } >
111
+ < LeadingVisual />
112
+ </ LeadingVisualContainer >
113
+ ) : null }
114
+ < TokenTextContainer { ...( hasMultipleActionTargets ? interactiveTokenProps : { } ) } >
115
+ { text }
116
+ { onRemove && < VisuallyHidden > (press backspace or delete to remove)</ VisuallyHidden > }
117
+ </ TokenTextContainer >
118
+
119
+ { ! hideRemoveButton && onRemove ? (
120
+ < RemoveTokenButton
121
+ borderOffset = { tokenBorderWidthPx }
122
+ onClick = { onRemoveClick }
123
+ size = { size }
124
+ isParentInteractive = { isTokenInteractive ( props ) }
125
+ aria-hidden = { hasMultipleActionTargets ? 'true' : 'false' }
126
+ />
127
+ ) : null }
128
+ </ TokenBase >
129
+ )
130
+ }
131
+
83
132
return (
84
133
< TokenBase
85
134
onRemove = { onRemove }
86
135
id = { id ?. toString ( ) }
87
136
text = { text }
88
137
size = { size }
89
- sx = { sx }
138
+ sx = { mergedSx }
90
139
{ ...( ! hasMultipleActionTargets ? interactiveTokenProps : { } ) }
91
140
{ ...rest }
92
141
ref = { forwardedRef }
142
+ style = { style }
93
143
>
94
144
{ LeadingVisual ? (
95
145
< LeadingVisualContainer size = { size } >
0 commit comments