-
Notifications
You must be signed in to change notification settings - Fork 155
/
Skeleton.tsx
160 lines (126 loc) · 4.35 KB
/
Skeleton.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
/* eslint-disable react/no-array-index-key */
import React, { CSSProperties, PropsWithChildren, ReactElement } from 'react';
import { SkeletonThemeContext } from './SkeletonThemeContext.js';
import { SkeletonStyleProps } from './SkeletonStyleProps.js';
const defaultEnableAnimation = true;
// For performance & cleanliness, don't add any inline styles unless we have to
function styleOptionsToCssProperties({
baseColor,
highlightColor,
width,
height,
borderRadius,
circle,
direction,
duration,
enableAnimation = defaultEnableAnimation,
}: SkeletonStyleProps & { circle: boolean }): CSSProperties {
const style: CSSProperties & Record<`--${string}`, string> = {};
if (direction === 'rtl') style['--animation-direction'] = 'reverse';
if (typeof duration === 'number')
style['--animation-duration'] = `${duration}s`;
if (!enableAnimation) style['--pseudo-element-display'] = 'none';
if (typeof width === 'string' || typeof width === 'number')
style.width = width;
if (typeof height === 'string' || typeof height === 'number')
style.height = height;
if (typeof borderRadius === 'string' || typeof borderRadius === 'number')
style.borderRadius = borderRadius;
if (circle) style.borderRadius = '50%';
if (typeof baseColor !== 'undefined') style['--base-color'] = baseColor;
if (typeof highlightColor !== 'undefined')
style['--highlight-color'] = highlightColor;
return style;
}
export interface SkeletonProps extends SkeletonStyleProps {
count?: number;
wrapper?: React.FunctionComponent<PropsWithChildren<unknown>>;
className?: string;
containerClassName?: string;
containerTestId?: string;
circle?: boolean;
style?: CSSProperties;
}
export function Skeleton({
count = 1,
wrapper: Wrapper,
className: customClassName,
containerClassName,
containerTestId,
circle = false,
style: styleProp,
...originalPropsStyleOptions
}: SkeletonProps): ReactElement {
const contextStyleOptions = React.useContext(SkeletonThemeContext);
const propsStyleOptions = { ...originalPropsStyleOptions };
// DO NOT overwrite style options from the context if `propsStyleOptions`
// has properties explicity set to undefined
for (const [key, value] of Object.entries(originalPropsStyleOptions)) {
if (typeof value === 'undefined') {
delete propsStyleOptions[key as keyof typeof propsStyleOptions];
}
}
// Props take priority over context
const styleOptions = {
...contextStyleOptions,
...propsStyleOptions,
circle,
};
// `styleProp` has the least priority out of everything
const style = {
...styleProp,
...styleOptionsToCssProperties(styleOptions),
};
let className = 'react-loading-skeleton';
if (customClassName) className += ` ${customClassName}`;
const inline = styleOptions.inline ?? false;
const elements: ReactElement[] = [];
const countCeil = Math.ceil(count);
for (let i = 0; i < countCeil; i++) {
let thisStyle = style;
if (countCeil > count && i === countCeil - 1) {
// count is not an integer and we've reached the last iteration of
// the loop, so add a "fractional" skeleton.
//
// For example, if count is 3.5, we've already added 3 full
// skeletons, so now we add one more skeleton that is 0.5 times the
// original width.
const width = thisStyle.width ?? '100%'; // 100% is the default since that's what's in the CSS
const fractionalPart = count % 1;
const fractionalWidth =
typeof width === 'number'
? width * fractionalPart
: `calc(${width} * ${fractionalPart})`;
thisStyle = { ...thisStyle, width: fractionalWidth };
}
const skeletonSpan = (
<span className={className} style={thisStyle} key={i}>
‌
</span>
);
if (inline) {
elements.push(skeletonSpan);
} else {
// Without the <br />, the skeleton lines will all run together if
// `width` is specified
elements.push(
<React.Fragment key={i}>
{skeletonSpan}
<br />
</React.Fragment>
);
}
}
return (
<span
className={containerClassName}
data-testid={containerTestId}
aria-live="polite"
aria-busy={styleOptions.enableAnimation ?? defaultEnableAnimation}
>
{Wrapper
? elements.map((el, i) => <Wrapper key={i}>{el}</Wrapper>)
: elements}
</span>
);
}