1
1
import React , { forwardRef , MouseEventHandler } from 'react'
2
- import styled , { css } from 'styled-components'
3
- import { get } from '../constants'
4
- import sx , { SxProp } from '../sx'
2
+ import { Box } from '..'
3
+ import { merge , SxProp } from '../sx'
5
4
import TokenBase , { defaultTokenSize , isTokenInteractive , TokenBaseProps } from './TokenBase'
6
5
import RemoveTokenButton from './_RemoveTokenButton'
7
6
import TokenTextContainer from './_TokenTextContainer'
@@ -16,53 +15,33 @@ export interface TokenProps extends TokenBaseProps {
16
15
17
16
const tokenBorderWidthPx = 1
18
17
19
- const DefaultTokenStyled = styled ( TokenBase ) < TokenProps & { isTokenInteractive : boolean } & SxProp > `
20
- background-color: ${ get ( 'colors.neutral.subtle' ) } ;
21
- border-color: ${ props => ( props . isSelected ? get ( 'colors.fg.default' ) : get ( 'colors.border.subtle' ) ) } ;
22
- border-style: solid;
23
- border-width: ${ tokenBorderWidthPx } px;
24
- color: ${ props => ( props . isSelected ? get ( 'colors.fg.default' ) : get ( 'colors.fg.muted' ) ) } ;
25
- max-width: 100%;
26
- padding-right: ${ props => ( ! props . hideRemoveButton ? 0 : undefined ) } ;
27
- position: relative;
28
- ${ sx }
29
-
30
- ${ props => {
31
- if ( props . isTokenInteractive ) {
32
- return css `
33
- & : hover {
34
- background-color : ${ get ( 'colors.neutral.muted' ) } ;
35
- box-shadow : ${ get ( 'colors.shadow.medium' ) } ;
36
- color : ${ get ( 'colors.fg.default' ) } ;
37
- }
38
- `
39
- }
40
- } }
41
- `
42
-
43
- const LeadingVisualContainer = styled ( 'span' ) < Pick < TokenBaseProps , 'size' > > `
44
- flex-shrink: 0;
45
- line-height: 0;
46
-
47
- ${ props => {
48
- switch ( props . size ) {
49
- case 'large' :
50
- case 'extralarge' :
51
- case 'xlarge' :
52
- return css `
53
- margin-right : ${ get ( 'space.2' ) } ;
54
- `
55
- default :
56
- return css `
57
- margin-right : ${ get ( 'space.1' ) } ;
58
- `
59
- }
60
- } }
61
- `
18
+ const LeadingVisualContainer : React . FC < Pick < TokenBaseProps , 'size' > > = ( { children, size} ) => (
19
+ < Box
20
+ sx = { {
21
+ flexShrink : 0 ,
22
+ lineHeight : 0 ,
23
+ marginRight : size && [ 'large' , 'extralarge' , 'xlarge' ] . includes ( size ) ? 2 : 1
24
+ } }
25
+ >
26
+ { children }
27
+ </ Box >
28
+ )
62
29
63
30
const Token = forwardRef < HTMLAnchorElement | HTMLButtonElement | HTMLSpanElement , TokenProps & SxProp > (
64
31
( props , forwardedRef ) => {
65
- const { as, onRemove, id, leadingVisual : LeadingVisual , text, size, hideRemoveButton, href, onClick, ...rest } = props
32
+ const {
33
+ as,
34
+ onRemove,
35
+ id,
36
+ leadingVisual : LeadingVisual ,
37
+ text,
38
+ size,
39
+ hideRemoveButton,
40
+ href,
41
+ onClick,
42
+ sx : sxProp = { } ,
43
+ ...rest
44
+ } = props
66
45
const hasMultipleActionTargets = isTokenInteractive ( props ) && Boolean ( onRemove ) && ! hideRemoveButton
67
46
const onRemoveClick : MouseEventHandler = e => {
68
47
e . stopPropagation ( )
@@ -73,15 +52,35 @@ const Token = forwardRef<HTMLAnchorElement | HTMLButtonElement | HTMLSpanElement
73
52
href,
74
53
onClick
75
54
}
55
+ const sx = merge (
56
+ {
57
+ backgroundColor : 'neutral.subtle' ,
58
+ borderColor : props . isSelected ? 'fg.default' : 'border.subtle' ,
59
+ borderStyle : 'solid' ,
60
+ borderWidth : `${ tokenBorderWidthPx } px` ,
61
+ color : props . isSelected ? 'fg.default' : 'fg.muted' ,
62
+ maxWidth : '100%' ,
63
+ paddingRight : ! ( hideRemoveButton || ! onRemove ) ? 0 : undefined ,
64
+ ...( isTokenInteractive ( props )
65
+ ? {
66
+ '&:hover' : {
67
+ backgroundColor : 'neutral.muted' ,
68
+ boxShadow : 'shadow.medium' ,
69
+ color : 'fg.default'
70
+ }
71
+ }
72
+ : { } )
73
+ } ,
74
+ sxProp as SxProp
75
+ )
76
76
77
77
return (
78
- < DefaultTokenStyled
78
+ < TokenBase
79
79
onRemove = { onRemove }
80
- hideRemoveButton = { hideRemoveButton || ! onRemove }
81
80
id = { id ?. toString ( ) }
82
81
text = { text }
83
82
size = { size }
84
- isTokenInteractive = { isTokenInteractive ( props ) }
83
+ sx = { sx }
85
84
{ ...( ! hasMultipleActionTargets ? interactiveTokenProps : { } ) }
86
85
{ ...rest }
87
86
ref = { forwardedRef }
@@ -109,7 +108,7 @@ const Token = forwardRef<HTMLAnchorElement | HTMLButtonElement | HTMLSpanElement
109
108
}
110
109
/>
111
110
) : null }
112
- </ DefaultTokenStyled >
111
+ </ TokenBase >
113
112
)
114
113
}
115
114
)
0 commit comments