-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
202 lines (179 loc) · 4.09 KB
/
index.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// @ts-nocheck
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';
import { forwardRef } from '@wordpress/element';
import { useInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Tooltip from '../tooltip';
import Icon from '../icon';
import { VisuallyHidden } from '../visually-hidden';
const disabledEventsOnDisabledButton = [ 'onMouseDown', 'onClick' ];
function useDeprecatedProps( {
isDefault,
isPrimary,
isSecondary,
isTertiary,
isLink,
variant,
...otherProps
} ) {
let computedVariant = variant;
if ( isPrimary ) {
computedVariant ??= 'primary';
}
if ( isTertiary ) {
computedVariant ??= 'tertiary';
}
if ( isSecondary ) {
computedVariant ??= 'secondary';
}
if ( isDefault ) {
deprecated( 'Button isDefault prop', {
since: '5.4',
alternative: 'variant="secondary"',
version: '6.2',
} );
computedVariant ??= 'secondary';
}
if ( isLink ) {
computedVariant ??= 'link';
}
return {
...otherProps,
variant: computedVariant,
};
}
export function Button( props, ref ) {
const {
href,
target,
isSmall,
isPressed,
isBusy,
isDestructive,
className,
disabled,
icon,
iconPosition = 'left',
iconSize,
showTooltip,
tooltipPosition,
shortcut,
label,
children,
text,
variant,
__experimentalIsFocusable: isFocusable,
describedBy,
...additionalProps
} = useDeprecatedProps( props );
const instanceId = useInstanceId(
Button,
'components-button__description'
);
const classes = classnames( 'components-button', className, {
'is-secondary': variant === 'secondary',
'is-primary': variant === 'primary',
'is-small': isSmall,
'is-tertiary': variant === 'tertiary',
'is-pressed': isPressed,
'is-busy': isBusy,
'is-link': variant === 'link',
'is-destructive': isDestructive,
'has-text': !! icon && !! children,
'has-icon': !! icon,
} );
const trulyDisabled = disabled && ! isFocusable;
const Tag = href !== undefined && ! trulyDisabled ? 'a' : 'button';
const tagProps =
Tag === 'a'
? { href, target }
: {
type: 'button',
disabled: trulyDisabled,
'aria-pressed': isPressed,
};
if ( disabled && isFocusable ) {
// In this case, the button will be disabled, but still focusable and
// perceivable by screen reader users.
tagProps[ 'aria-disabled' ] = true;
for ( const disabledEvent of disabledEventsOnDisabledButton ) {
additionalProps[ disabledEvent ] = ( event ) => {
event.stopPropagation();
event.preventDefault();
};
}
}
// Should show the tooltip if...
const shouldShowTooltip =
! trulyDisabled &&
// An explicit tooltip is passed or...
( ( showTooltip && label ) ||
// There's a shortcut or...
shortcut ||
// There's a label and...
( !! label &&
// The children are empty and...
! children?.length &&
// The tooltip is not explicitly disabled.
false !== showTooltip ) );
const descriptionId = describedBy ? instanceId : null;
const describedById =
additionalProps[ 'aria-describedby' ] || descriptionId;
const element = (
<Tag
{ ...tagProps }
{ ...additionalProps }
className={ classes }
aria-label={ additionalProps[ 'aria-label' ] || label }
aria-describedby={ describedById }
ref={ ref }
>
{ icon && iconPosition === 'left' && (
<Icon icon={ icon } size={ iconSize } />
) }
{ text && <>{ text }</> }
{ icon && iconPosition === 'right' && (
<Icon icon={ icon } size={ iconSize } />
) }
{ children }
</Tag>
);
if ( ! shouldShowTooltip ) {
return (
<>
{ element }
{ describedBy && (
<VisuallyHidden>
<span id={ descriptionId }>{ describedBy }</span>
</VisuallyHidden>
) }
</>
);
}
return (
<>
<Tooltip
text={ children?.length && describedBy ? describedBy : label }
shortcut={ shortcut }
position={ tooltipPosition }
>
{ element }
</Tooltip>
{ describedBy && (
<VisuallyHidden>
<span id={ descriptionId }>{ describedBy }</span>
</VisuallyHidden>
) }
</>
);
}
export default forwardRef( Button );