-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
IconButton.tsx
229 lines (211 loc) · 7.89 KB
/
IconButton.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import { useButton } from '@mui/base/ButtonUnstyled';
import composeClasses from '@mui/base/composeClasses';
import { styled, useThemeProps } from '../styles';
import useSlot from '../utils/useSlot';
import iconButtonClasses, { getIconButtonUtilityClass } from './iconButtonClasses';
import { IconButtonOwnerState, IconButtonTypeMap, ExtendIconButton } from './IconButtonProps';
const useUtilityClasses = (ownerState: IconButtonOwnerState) => {
const { color, disabled, focusVisible, focusVisibleClassName, size, variant } = ownerState;
const slots = {
root: [
'root',
disabled && 'disabled',
focusVisible && 'focusVisible',
variant && `variant${capitalize(variant)}`,
color && `color${capitalize(color)}`,
size && `size${capitalize(size)}`,
],
};
const composedClasses = composeClasses(slots, getIconButtonUtilityClass, {});
if (focusVisible && focusVisibleClassName) {
composedClasses.root += ` ${focusVisibleClassName}`;
}
return composedClasses;
};
export const StyledIconButton = styled('button')<{ ownerState: IconButtonOwnerState }>(
({ theme, ownerState }) => [
{
'--Icon-margin': 'initial', // reset the icon's margin.
'--CircularProgress-size': 'var(--Icon-fontSize)',
...(ownerState.size === 'sm' && {
'--Icon-fontSize': 'calc(var(--IconButton-size, 2rem) / 1.6)', // 1.25rem by default
minWidth: 'var(--IconButton-size, 2rem)', // use min-width instead of height to make the button resilient to its content
minHeight: 'var(--IconButton-size, 2rem)', // use min-height instead of height to make the button resilient to its content
fontSize: theme.vars.fontSize.sm,
paddingInline: '2px', // add a gap, in case the content is long, e.g. multiple icons
}),
...(ownerState.size === 'md' && {
'--Icon-fontSize': 'calc(var(--IconButton-size, 2.5rem) / 1.667)', // 1.5rem by default
minWidth: 'var(--IconButton-size, 2.5rem)',
minHeight: 'var(--IconButton-size, 2.5rem)',
fontSize: theme.vars.fontSize.md,
paddingInline: '0.25rem',
}),
...(ownerState.size === 'lg' && {
'--Icon-fontSize': 'calc(var(--IconButton-size, 3rem) / 1.714)', // 1.75rem by default
minWidth: 'var(--IconButton-size, 3rem)',
minHeight: 'var(--IconButton-size, 3rem)',
fontSize: theme.vars.fontSize.lg,
paddingInline: '0.375rem',
}),
WebkitTapHighlightColor: 'transparent',
paddingBlock: 0,
fontFamily: theme.vars.fontFamily.body,
fontWeight: theme.vars.fontWeight.md,
margin: `var(--IconButton-margin)`, // to be controlled by other components, eg. Input
borderRadius: `var(--IconButton-radius, ${theme.vars.radius.sm})`, // to be controlled by other components, eg. Input
border: 'none',
boxSizing: 'border-box',
backgroundColor: 'transparent',
cursor: 'pointer',
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
position: 'relative',
// TODO: discuss the transition approach in a separate PR. This value is copied from mui-material Button.
transition:
'background-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms, box-shadow 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms, border-color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms, color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
[theme.focus.selector]: theme.focus.default,
},
theme.variants[ownerState.variant!]?.[ownerState.color!],
{ '&:hover': theme.variants[`${ownerState.variant!}Hover`]?.[ownerState.color!] },
{ '&:active': theme.variants[`${ownerState.variant!}Active`]?.[ownerState.color!] },
{
[`&.${iconButtonClasses.disabled}`]:
theme.variants[`${ownerState.variant!}Disabled`]?.[ownerState.color!],
},
],
);
export const IconButtonRoot = styled(StyledIconButton, {
name: 'JoyIconButton',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})({});
const IconButton = React.forwardRef(function IconButton(inProps, ref) {
const props = useThemeProps<typeof inProps & { component?: React.ElementType }>({
props: inProps,
name: 'JoyIconButton',
});
const {
children,
action,
component = 'button',
color = 'primary',
variant = 'soft',
size = 'md',
...other
} = props;
const buttonRef = React.useRef<HTMLElement | null>(null);
const handleRef = useForkRef(buttonRef, ref);
const { focusVisible, setFocusVisible, getRootProps } = useButton({
...props,
ref: handleRef,
});
React.useImperativeHandle(
action,
() => ({
focusVisible: () => {
setFocusVisible(true);
buttonRef.current?.focus();
},
}),
[setFocusVisible],
);
const ownerState = {
...props,
component,
color,
variant,
size,
focusVisible,
};
const classes = useUtilityClasses(ownerState);
const [SlotRoot, rootProps] = useSlot('root', {
ref,
className: classes.root,
elementType: IconButtonRoot,
getSlotProps: getRootProps,
externalForwardedProps: { ...other, component },
ownerState,
});
return <SlotRoot {...rootProps}>{children}</SlotRoot>;
}) as ExtendIconButton<IconButtonTypeMap>;
IconButton.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
/**
* @ignore
*/
children: PropTypes.node,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'primary'
*/
color: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['danger', 'info', 'neutral', 'primary', 'success', 'warning']),
PropTypes.string,
]),
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* This prop can help identify which element has keyboard focus.
* The class name will be applied when the element gains the focus through keyboard interaction.
* It's a polyfill for the [CSS :focus-visible selector](https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo).
* The rationale for using this feature [is explained here](https://github.com/WICG/focus-visible/blob/HEAD/explainer.md).
* A [polyfill can be used](https://github.com/WICG/focus-visible) to apply a `focus-visible` class to other components
* if needed.
*/
focusVisibleClassName: PropTypes.string,
/**
* The size of the component.
*/
size: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['sm', 'md', 'lg']),
PropTypes.string,
]),
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
/**
* @default 0
*/
tabIndex: PropTypes.number,
/**
* The variant to use.
* @default 'soft'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default IconButton;