-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
ListItemButton.tsx
275 lines (256 loc) · 9.34 KB
/
ListItemButton.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import composeClasses from '@mui/base/composeClasses';
import { useButton } from '@mui/base/ButtonUnstyled';
import { styled, useThemeProps } from '../styles';
import {
ListItemButtonOwnerState,
ExtendListItemButton,
ListItemButtonTypeMap,
} from './ListItemButtonProps';
import listItemButtonClasses, { getListItemButtonUtilityClass } from './listItemButtonClasses';
import ListItemButtonOrientationContext from './ListItemButtonOrientationContext';
import RowListContext from '../List/RowListContext';
const useUtilityClasses = (ownerState: ListItemButtonOwnerState) => {
const { color, disabled, focusVisible, focusVisibleClassName, selected, variant } = ownerState;
const slots = {
root: [
'root',
disabled && 'disabled',
focusVisible && 'focusVisible',
color && `color${capitalize(color)}`,
selected && 'selected',
variant && `variant${capitalize(variant)}`,
],
};
const composedClasses = composeClasses(slots, getListItemButtonUtilityClass, {});
if (focusVisible && focusVisibleClassName) {
composedClasses.root += ` ${focusVisibleClassName}`;
}
return composedClasses;
};
export const StyledListItemButton = styled('div')<{ ownerState: ListItemButtonOwnerState }>(
({ theme, ownerState }) => [
{
...(ownerState.selected && {
'--List-decorator-color': 'initial',
}),
...(ownerState.disabled && {
'--List-decorator-color':
theme.vars.palette[ownerState.color!]?.[`${ownerState.variant!}DisabledColor`],
}),
WebkitTapHighlightColor: 'transparent',
boxSizing: 'border-box',
position: 'relative',
display: 'flex',
flexDirection: ownerState.orientation === 'vertical' ? 'column' : 'row',
alignItems: 'center',
textAlign: 'initial',
textDecoration: 'initial', // reset native anchor tag
backgroundColor: 'initial', // reset button background
cursor: 'pointer',
// In some cases, ListItemButton is a child of ListItem so the margin needs to be controlled by the ListItem. The value is negative to account for the ListItem's padding
marginInline: 'var(--List-itemButton-marginInline)',
marginBlock: 'var(--List-itemButton-marginBlock)',
...(ownerState['data-first-child'] === undefined && {
marginInlineStart: ownerState.row ? 'var(--List-gap)' : undefined,
marginBlockStart: ownerState.row ? undefined : 'var(--List-gap)',
}),
// account for the border width, so that all of the ListItemButtons content aligned horizontally
paddingBlock: 'calc(var(--List-item-paddingY) - var(--variant-borderWidth, 0px))',
// account for the border width, so that all of the ListItemButtons content aligned vertically
paddingInlineStart:
'calc(var(--List-item-paddingLeft) + var(--List-item-startActionWidth, var(--internal-startActionWidth, 0px)))', // --internal variable makes it possible to customize the actionWidth from the top List
paddingInlineEnd:
'calc(var(--List-item-paddingRight) + var(--List-item-endActionWidth, var(--internal-endActionWidth, 0px)))', // --internal variable makes it possible to customize the actionWidth from the top List
minBlockSize: 'var(--List-item-minHeight)',
border: 'none',
borderRadius: 'var(--List-item-radius)',
flexGrow: ownerState.row ? 0 : 1,
flexBasis: ownerState.row ? 'auto' : '0%', // for long text (in vertical), displays in multiple lines.
flexShrink: 0,
minInlineSize: 0,
// 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, color 250ms cubic-bezier(0.4, 0, 0.2, 1) 0ms',
fontSize: 'var(--List-item-fontSize)',
fontFamily: theme.vars.fontFamily.body,
...(ownerState.selected && {
fontWeight: theme.vars.fontWeight.md,
}),
[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!] },
{
[`&.${listItemButtonClasses.disabled}`]:
theme.variants[`${ownerState.variant!}Disabled`]?.[ownerState.color!],
},
],
);
const ListItemButtonRoot = styled(StyledListItemButton, {
name: 'JoyListItemButton',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})({});
const ListItemButton = React.forwardRef(function ListItemButton(inProps, ref) {
const props = useThemeProps<typeof inProps & { component?: React.ElementType }>({
props: inProps,
name: 'JoyListItemButton',
});
const row = React.useContext(RowListContext);
const {
children,
className,
action,
component = 'div',
orientation = 'horizontal',
role,
selected = false,
color = selected ? 'primary' : 'neutral',
variant = 'plain',
...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,
focusVisible,
orientation,
row,
selected,
variant,
};
const classes = useUtilityClasses(ownerState);
const rootProps = getRootProps();
return (
<ListItemButtonOrientationContext.Provider value={orientation}>
<ListItemButtonRoot
as={component}
className={clsx(classes.root, className)}
ownerState={ownerState}
{...other}
{...rootProps}
role={role ?? rootProps.role}
>
{children}
</ListItemButtonRoot>
</ListItemButtonOrientationContext.Provider>
);
}) as ExtendListItemButton<ListItemButtonTypeMap>;
ListItemButton.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,
}),
}),
]),
/**
* If `true`, the list item is focused during the first mount.
* Focus will also be triggered if the value changes from false to true.
* @default false
*/
autoFocus: PropTypes.bool,
/**
* The content of the component.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The color of the component. It supports those theme colors that make sense for this component.
* @default 'neutral'
*/
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 content direction flow.
* @default 'horizontal'
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* @ignore
*/
role: PropTypes /* @typescript-to-proptypes-ignore */.string,
/**
* Use to apply selected styling.
* @default false
*/
selected: PropTypes.bool,
/**
* 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 'plain'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['outlined', 'plain', 'soft', 'solid']),
PropTypes.string,
]),
} as any;
export default ListItemButton;