Skip to content

Commit faa6876

Browse files
committed
Merge branch 'perf/composite-animation' of https://github.com/aboodz/react-loading-skeleton into perf/composite-animation
2 parents d72be92 + a1033cb commit faa6876

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/Skeleton.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ function styleOptionsToCssProperties({
2323
duration,
2424
enableAnimation = defaultEnableAnimation,
2525
}: SkeletonStyleProps & { circle: boolean }): CSSProperties {
26-
const style: CSSProperties = {}
26+
const style: CSSProperties & Record<`--${string}`, string> = {}
2727

28-
if (direction === 'rtl') style.animationDirection = 'reverse'
28+
if (direction === 'rtl') style['--animation-direction'] = 'reverse'
2929

30-
if (typeof duration === 'number') style.animationDuration = `${duration}s`
30+
if (typeof duration === 'number') style['--animation-duration'] = `${duration}s`
3131

3232
if (typeof width === 'string' || typeof width === 'number') style.width = width
3333
if (typeof height === 'string' || typeof height === 'number') style.height = height
@@ -38,13 +38,8 @@ function styleOptionsToCssProperties({
3838
if (circle) style.borderRadius = '50%'
3939

4040
if (typeof baseColor !== 'undefined' || typeof highlightColor !== 'undefined') {
41-
style.backgroundColor = baseColor ?? defaultBaseColor
42-
style.backgroundImage = `linear-gradient(
43-
90deg,
44-
${baseColor ?? defaultBaseColor},
45-
${highlightColor ?? defaultHighlightColor},
46-
${baseColor ?? defaultBaseColor}
47-
)`
41+
style['--base-color'] = baseColor ?? defaultBaseColor;
42+
style['--highlight-color'] = highlightColor ?? defaultHighlightColor;
4843
}
4944

5045
if (!enableAnimation) style.backgroundImage = 'none'

src/skeleton.css

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,46 @@
11
@keyframes react-loading-skeleton {
2-
0% {
3-
background-position: -200px 0;
4-
}
52
100% {
6-
background-position: calc(200px + 100%) 0;
3+
transform: translateX(100%);
74
}
85
}
96

107
.react-loading-skeleton {
118
/* If either color is changed, Skeleton.tsx must be updated as well */
129
--base-color: #ebebeb;
1310
--highlight-color: #f5f5f5;
11+
--animation-duration: 1.5s;
12+
--animation-direction: normal;
1413

1514
background-color: var(--base-color);
15+
16+
width: 100%;
17+
border-radius: 0.25rem;
18+
display: inline-flex;
19+
line-height: 1;
20+
21+
position: relative;
22+
overflow: hidden;
23+
}
24+
25+
.react-loading-skeleton::after {
26+
content: ' ';
27+
display: block;
28+
position: absolute;
29+
left: 0;
30+
right: 0;
31+
height: 100%;
32+
background-repeat: no-repeat;
1633
background-image: linear-gradient(
1734
90deg,
1835
var(--base-color),
1936
var(--highlight-color),
2037
var(--base-color)
2138
);
22-
23-
width: 100%;
24-
background-size: 200px 100%;
25-
background-repeat: no-repeat;
26-
border-radius: 0.25rem;
27-
display: inline-block;
28-
line-height: 1;
39+
transform: translateX(-100%);
2940

3041
animation-name: react-loading-skeleton;
31-
animation-duration: 1.5s;
42+
animation-direction: var(--animation-direction);
43+
animation-duration: var(--animation-duration);
3244
animation-timing-function: ease-in-out;
3345
animation-iteration-count: infinite;
3446
}

0 commit comments

Comments
 (0)