Skip to content

Commit a1033cb

Browse files
committed
fix: customizable css vars
Embrace the usage of css vars to customize skeleton styles.
1 parent 0db9d0d commit a1033cb

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/Skeleton.tsx

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

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

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

3030
if (typeof width === 'string' || typeof width === 'number') style.width = width
3131
if (typeof height === 'string' || typeof height === 'number') style.height = height
@@ -42,13 +42,8 @@ function styleOptionsToCssProperties({
4242
}
4343

4444
if (typeof baseColor !== 'undefined' || typeof highlightColor !== 'undefined') {
45-
style.backgroundColor = baseColor ?? defaultBaseColor
46-
style.backgroundImage = `linear-gradient(
47-
90deg,
48-
${baseColor ?? defaultBaseColor},
49-
${highlightColor ?? defaultHighlightColor},
50-
${baseColor ?? defaultBaseColor}
51-
)`
45+
style['--base-color'] = baseColor ?? defaultBaseColor;
46+
style['--highlight-color'] = highlightColor ?? defaultHighlightColor;
5247
}
5348

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

src/skeleton.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
/* If either color is changed, Skeleton.tsx must be updated as well */
99
--base-color: #ebebeb;
1010
--highlight-color: #f5f5f5;
11+
--animation-duration: 1.5s;
12+
--animation-direction: normal;
1113

1214
background-color: var(--base-color);
1315

@@ -37,7 +39,8 @@
3739
transform: translateX(-100%);
3840

3941
animation-name: react-loading-skeleton;
40-
animation-duration: 1.5s;
42+
animation-direction: var(--animation-direction);
43+
animation-duration: var(--animation-duration);
4144
animation-timing-function: ease-in-out;
4245
animation-iteration-count: infinite;
4346
}

0 commit comments

Comments
 (0)