-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathSkeleton.js
251 lines (231 loc) · 6.79 KB
/
Skeleton.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
'use client';
import * as React from 'react';
import clsx from 'clsx';
import PropTypes from 'prop-types';
import { keyframes, css } from '@mui/system';
import composeClasses from '@mui/utils/composeClasses';
import { alpha, unstable_getUnit as getUnit, unstable_toUnitless as toUnitless } from '../styles';
import styled from '../styles/styled';
import { useDefaultProps } from '../DefaultPropsProvider';
import { getSkeletonUtilityClass } from './skeletonClasses';
const useUtilityClasses = (ownerState) => {
const { classes, variant, animation, hasChildren, width, height } = ownerState;
const slots = {
root: [
'root',
variant,
animation,
hasChildren && 'withChildren',
hasChildren && !width && 'fitContent',
hasChildren && !height && 'heightAuto',
],
};
return composeClasses(slots, getSkeletonUtilityClass, classes);
};
const pulseKeyframe = keyframes`
0% {
opacity: 1;
}
50% {
opacity: 0.4;
}
100% {
opacity: 1;
}
`;
const waveKeyframe = keyframes`
0% {
transform: translateX(-100%);
}
50% {
/* +0.5s of delay between each loop */
transform: translateX(100%);
}
100% {
transform: translateX(100%);
}
`;
const SkeletonRoot = styled('span', {
name: 'MuiSkeleton',
slot: 'Root',
overridesResolver: (props, styles) => {
const { ownerState } = props;
return [
styles.root,
styles[ownerState.variant],
ownerState.animation !== false && styles[ownerState.animation],
ownerState.hasChildren && styles.withChildren,
ownerState.hasChildren && !ownerState.width && styles.fitContent,
ownerState.hasChildren && !ownerState.height && styles.heightAuto,
];
},
})(
({ theme, ownerState }) => {
const radiusUnit = getUnit(theme.shape.borderRadius) || 'px';
const radiusValue = toUnitless(theme.shape.borderRadius);
return {
display: 'block',
// Create a "on paper" color with sufficient contrast retaining the color
backgroundColor: theme.vars
? theme.vars.palette.Skeleton.bg
: alpha(theme.palette.text.primary, theme.palette.mode === 'light' ? 0.11 : 0.13),
height: '1.2em',
...(ownerState.variant === 'text' && {
marginTop: 0,
marginBottom: 0,
height: 'auto',
transformOrigin: '0 55%',
transform: 'scale(1, 0.60)',
borderRadius: `${radiusValue}${radiusUnit}/${
Math.round((radiusValue / 0.6) * 10) / 10
}${radiusUnit}`,
'&:empty:before': {
content: '"\\00a0"',
},
}),
...(ownerState.variant === 'circular' && {
borderRadius: '50%',
}),
...(ownerState.variant === 'rounded' && {
borderRadius: (theme.vars || theme).shape.borderRadius,
}),
...(ownerState.hasChildren && {
'& > *': {
visibility: 'hidden',
},
}),
...(ownerState.hasChildren &&
!ownerState.width && {
maxWidth: 'fit-content',
}),
...(ownerState.hasChildren &&
!ownerState.height && {
height: 'auto',
}),
};
},
({ ownerState }) =>
ownerState.animation === 'pulse' &&
css`
animation: ${pulseKeyframe} 2s ease-in-out 0.5s infinite;
`,
({ ownerState, theme }) =>
ownerState.animation === 'wave' &&
css`
position: relative;
overflow: hidden;
/* Fix bug in Safari https://bugs.webkit.org/show_bug.cgi?id=68196 */
-webkit-mask-image: -webkit-radial-gradient(white, black);
&::after {
animation: ${waveKeyframe} 2s linear 0.5s infinite;
background: linear-gradient(
90deg,
transparent,
${(theme.vars || theme).palette.action.hover},
transparent
);
content: '';
position: absolute;
transform: translateX(-100%); /* Avoid flash during server-side hydration */
bottom: 0;
left: 0;
right: 0;
top: 0;
}
`,
);
const Skeleton = React.forwardRef(function Skeleton(inProps, ref) {
const props = useDefaultProps({ props: inProps, name: 'MuiSkeleton' });
const {
animation = 'pulse',
className,
component = 'span',
height,
style,
variant = 'text',
width,
...other
} = props;
const ownerState = {
...props,
animation,
component,
variant,
hasChildren: Boolean(other.children),
};
const classes = useUtilityClasses(ownerState);
return (
<SkeletonRoot
as={component}
ref={ref}
className={clsx(classes.root, className)}
ownerState={ownerState}
{...other}
style={{
width,
height,
...style,
}}
/>
);
});
Skeleton.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 animation.
* If `false` the animation effect is disabled.
* @default 'pulse'
*/
animation: PropTypes.oneOf(['pulse', 'wave', false]),
/**
* Optional children to infer width and height from.
*/
children: PropTypes.node,
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* @ignore
*/
className: PropTypes.string,
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: PropTypes.elementType,
/**
* Height of the skeleton.
* Useful when you don't want to adapt the skeleton to a text element but for instance a card.
*/
height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* @ignore
*/
style: PropTypes.object,
/**
* 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 type of content that will be rendered.
* @default 'text'
*/
variant: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([
PropTypes.oneOf(['circular', 'rectangular', 'rounded', 'text']),
PropTypes.string,
]),
/**
* Width of the skeleton.
* Useful when the skeleton is inside an inline element with no width of its own.
*/
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
};
export default Skeleton;