-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathAccordion.js
334 lines (311 loc) · 9.66 KB
/
Accordion.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
'use client';
import * as React from 'react';
import { isFragment } from 'react-is';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import chainPropTypes from '@mui/utils/chainPropTypes';
import composeClasses from '@mui/utils/composeClasses';
import { styled } from '../zero-styled';
import memoTheme from '../utils/memoTheme';
import { useDefaultProps } from '../DefaultPropsProvider';
import Collapse from '../Collapse';
import Paper from '../Paper';
import AccordionContext from './AccordionContext';
import useControlled from '../utils/useControlled';
import useSlot from '../utils/useSlot';
import accordionClasses, { getAccordionUtilityClass } from './accordionClasses';
const useUtilityClasses = (ownerState) => {
const { classes, square, expanded, disabled, disableGutters } = ownerState;
const slots = {
root: [
'root',
!square && 'rounded',
expanded && 'expanded',
disabled && 'disabled',
!disableGutters && 'gutters',
],
heading: ['heading'],
region: ['region'],
};
return composeClasses(slots, getAccordionUtilityClass, classes);
};
const AccordionRoot = styled(Paper, {
name: 'MuiAccordion',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
{ [`& .${accordionClasses.region}`]: styles.region },
styles.root,
!ownerState.square && styles.rounded,
!ownerState.disableGutters && styles.gutters,
];
},
})(
memoTheme(({ theme }) => {
const transition = {
duration: theme.transitions.duration.shortest,
};
return {
position: 'relative',
transition: theme.transitions.create(['margin'], transition),
overflowAnchor: 'none', // Keep the same scrolling position
'&::before': {
position: 'absolute',
left: 0,
top: -1,
right: 0,
height: 1,
content: '""',
opacity: 1,
backgroundColor: (theme.vars || theme).palette.divider,
transition: theme.transitions.create(['opacity', 'background-color'], transition),
},
'&:first-of-type': {
'&::before': {
display: 'none',
},
},
[`&.${accordionClasses.expanded}`]: {
'&::before': {
opacity: 0,
},
'&:first-of-type': {
marginTop: 0,
},
'&:last-of-type': {
marginBottom: 0,
},
'& + &': {
'&::before': {
display: 'none',
},
},
},
[`&.${accordionClasses.disabled}`]: {
backgroundColor: (theme.vars || theme).palette.action.disabledBackground,
},
};
}),
memoTheme(({ theme }) => ({
variants: [
{
props: (props) => !props.square,
style: {
borderRadius: 0,
'&:first-of-type': {
borderTopLeftRadius: (theme.vars || theme).shape.borderRadius,
borderTopRightRadius: (theme.vars || theme).shape.borderRadius,
},
'&:last-of-type': {
borderBottomLeftRadius: (theme.vars || theme).shape.borderRadius,
borderBottomRightRadius: (theme.vars || theme).shape.borderRadius,
// Fix a rendering issue on Edge
'@supports (-ms-ime-align: auto)': {
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
},
},
},
},
{
props: (props) => !props.disableGutters,
style: {
[`&.${accordionClasses.expanded}`]: {
margin: '16px 0',
},
},
},
],
})),
);
const AccordionHeading = styled('h3', {
name: 'MuiAccordion',
slot: 'Heading',
overridesResolver: (props, styles) => styles.heading,
})({
all: 'unset',
});
const Accordion = React.forwardRef(function Accordion(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiAccordion' });
const {
children: childrenProp,
className,
defaultExpanded = false,
disabled = false,
disableGutters = false,
expanded: expandedProp,
onChange,
square = false,
slots = {},
slotProps = {},
TransitionComponent: TransitionComponentProp,
TransitionProps: TransitionPropsProp,
...other
} = props;
const [expanded, setExpandedState] = useControlled({
controlled: expandedProp,
default: defaultExpanded,
name: 'Accordion',
state: 'expanded',
});
const handleChange = React.useCallback(
(event) => {
setExpandedState(!expanded);
if (onChange) {
onChange(event, !expanded);
}
},
[expanded, onChange, setExpandedState],
);
const [summary, ...children] = React.Children.toArray(childrenProp);
const contextValue = React.useMemo(
() => ({ expanded, disabled, disableGutters, toggle: handleChange }),
[expanded, disabled, disableGutters, handleChange],
);
const ownerState = {
...props,
square,
disabled,
disableGutters,
expanded,
};
const classes = useUtilityClasses(ownerState);
const backwardCompatibleSlots = { transition: TransitionComponentProp, ...slots };
const backwardCompatibleSlotProps = { transition: TransitionPropsProp, ...slotProps };
const externalForwardedProps = {
slots: backwardCompatibleSlots,
slotProps: backwardCompatibleSlotProps,
};
const [AccordionHeadingSlot, accordionProps] = useSlot('heading', {
elementType: AccordionHeading,
externalForwardedProps,
className: classes.heading,
ownerState,
});
const [TransitionSlot, transitionProps] = useSlot('transition', {
elementType: Collapse,
externalForwardedProps,
ownerState,
});
return (
<AccordionRoot
className={clsx(classes.root, className)}
ref={ref}
ownerState={ownerState}
square={square}
{...other}
>
<AccordionHeadingSlot {...accordionProps}>
<AccordionContext.Provider value={contextValue}>{summary}</AccordionContext.Provider>
</AccordionHeadingSlot>
<TransitionSlot in={expanded} timeout="auto" {...transitionProps}>
<div
aria-labelledby={summary.props.id}
id={summary.props['aria-controls']}
role="region"
className={classes.region}
>
{children}
</div>
</TransitionSlot>
</AccordionRoot>
);
});
Accordion.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the d.ts file and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* The content of the component.
*/
children: chainPropTypes(PropTypes.node.isRequired, (props) => {
const summary = React.Children.toArray(props.children)[0];
if (isFragment(summary)) {
return new Error(
"MUI: The Accordion doesn't accept a Fragment as a child. " +
'Consider providing an array instead.',
);
}
if (!React.isValidElement(summary)) {
return new Error('MUI: Expected the first child of Accordion to be a valid element.');
}
return null;
}),
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* If `true`, expands the accordion by default.
* @default false
*/
defaultExpanded: PropTypes.bool,
/**
* If `true`, the component is disabled.
* @default false
*/
disabled: PropTypes.bool,
/**
* If `true`, it removes the margin between two expanded accordion items and the increase of height.
* @default false
*/
disableGutters: PropTypes.bool,
/**
* If `true`, expands the accordion, otherwise collapse it.
* Setting this prop enables control over the accordion.
*/
expanded: PropTypes.bool,
/**
* Callback fired when the expand/collapse state is changed.
*
* @param {React.SyntheticEvent} event The event source of the callback. **Warning**: This is a generic event not a change event.
* @param {boolean} expanded The `expanded` state of the accordion.
*/
onChange: PropTypes.func,
/**
* The props used for each slot inside.
* @default {}
*/
slotProps: PropTypes.shape({
heading: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
transition: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
}),
/**
* The components used for each slot inside.
* @default {}
*/
slots: PropTypes.shape({
heading: PropTypes.elementType,
transition: PropTypes.elementType,
}),
/**
* If `true`, rounded corners are disabled.
* @default false
*/
square: 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,
]),
/**
* The component used for the transition.
* [Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.
*/
TransitionComponent: PropTypes.elementType,
/**
* Props applied to the transition element.
* By default, the element is based on this [`Transition`](https://reactcommunity.org/react-transition-group/transition/) component.
*/
TransitionProps: PropTypes.object,
};
export default Accordion;