Skip to content

Commit a5221b8

Browse files
committed
perf: use composite animation
Skeleton is animated using css tranform instead of background repositioning. Previously, changing background position caused the renderer to repaint the gradient on each animation keyframe. This implementation uses translateX to animate the skeleton effect. Using css transform doesn't trigger repaint.
1 parent af829d1 commit a5221b8

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/skeleton.css

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
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

@@ -13,19 +10,31 @@
1310
--highlight-color: #f5f5f5;
1411

1512
background-color: var(--base-color);
13+
14+
width: 100%;
15+
border-radius: 0.25rem;
16+
display: inline-block;
17+
line-height: 1;
18+
19+
position: relative;
20+
overflow: hidden;
21+
}
22+
23+
.react-loading-skeleton::before {
24+
content: ' ';
25+
display: block;
26+
position: absolute;
27+
left: 0;
28+
right: 0;
29+
height: 100%;
30+
background-repeat: no-repeat;
1631
background-image: linear-gradient(
1732
90deg,
1833
var(--base-color),
1934
var(--highlight-color),
2035
var(--base-color)
2136
);
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;
37+
transform: translateX(-100%);
2938

3039
animation-name: react-loading-skeleton;
3140
animation-duration: 1.5s;

0 commit comments

Comments
 (0)