forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_TextInputInnerAction.tsx
107 lines (98 loc) · 3.11 KB
/
_TextInputInnerAction.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React, {forwardRef} from 'react'
import {IconProps} from '@primer/octicons-react'
import {Box, Button, IconButton, Tooltip} from '.'
import {ButtonProps} from './Button'
import {BetterSystemStyleObject, merge, SxProp} from './sx'
type TextInputActionProps = Omit<React.HTMLProps<HTMLButtonElement>, 'aria-label' | 'size'> & {
/** @deprecated Text input action buttons should only use icon buttons */
children?: React.ReactNode
/** Text that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies. */
['aria-label']?: string
/** The icon to render inside the button */
icon?: React.FunctionComponent<IconProps>
/**
* @deprecated Text input action buttons should only use the 'invisible' button variant
* Determine's the styles on a button one of 'default' | 'primary' | 'invisible' | 'danger'
*/
variant?: ButtonProps['variant']
} & SxProp
const invisibleButtonStyleOverrides = {
color: 'fg.default',
paddingTop: '2px',
paddingRight: '4px',
paddingBottom: '2px',
paddingLeft: '4px',
position: 'relative',
'@media (pointer: coarse)': {
':after': {
content: '""',
position: 'absolute',
left: 0,
right: 0,
transform: 'translateY(-50%)',
top: '50%',
minHeight: '44px'
}
}
}
const ConditionalTooltip: React.FC<{
['aria-label']?: string
children: React.ReactNode
}> = ({'aria-label': ariaLabel, children}) => (
<>
{ariaLabel ? (
<Tooltip
aria-label={ariaLabel}
sx={{
/* inline-block is used to ensure the tooltip dimensions don't
collapse when being used with `grid` or `inline` children */
display: 'inline-block'
}}
>
{children}
</Tooltip>
) : (
children
)}
</>
)
const TextInputAction = forwardRef<HTMLButtonElement, TextInputActionProps>(
({'aria-label': ariaLabel, children, icon, sx: sxProp, variant, ...rest}, forwardedRef) => {
const sx =
variant === 'invisible'
? merge<BetterSystemStyleObject>(invisibleButtonStyleOverrides, sxProp || {})
: sxProp || {}
if ((icon && !ariaLabel) || (!children && !ariaLabel)) {
// eslint-disable-next-line no-console
console.warn('Use the `aria-label` prop to provide an accessible label for assistive technology')
}
return (
<Box as="span" className="TextInput-action" margin={1}>
{icon && !children ? (
<Tooltip aria-label={ariaLabel}>
<IconButton
variant={variant}
type="button"
icon={icon}
aria-label={ariaLabel}
size="small"
sx={sx}
{...rest}
ref={forwardedRef}
/>
</Tooltip>
) : (
<ConditionalTooltip aria-label={ariaLabel}>
<Button variant={variant} size="small" type="button" sx={sx} {...rest} ref={forwardedRef}>
{children}
</Button>
</ConditionalTooltip>
)}
</Box>
)
}
)
TextInputAction.defaultProps = {
variant: 'invisible'
}
export default TextInputAction