Skip to content

Commit e9777c1

Browse files
committed
Fix highlight being shown when !enableAnimation
Also fix an inline style always being rendered even if the props were left at their default values.
1 parent c6593d8 commit e9777c1

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

.eslintrc.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module.exports = {
1919
'testing-library/custom-renders': 'off',
2020
},
2121
rules: {
22-
'no-param-reassign': 'off',
2322
'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
2423
'no-restricted-syntax': [
2524
'error',

src/Skeleton.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SkeletonStyleProps } from './SkeletonStyleProps'
77
const defaultBaseColor = '#ebebeb'
88
const defaultHighlightColor = '#f5f5f5'
99

10+
// For performance & cleanliness, don't add any inline styles unless we have to
1011
function styleOptionsToCssProperties({
1112
baseColor,
1213
highlightColor,
@@ -20,9 +21,7 @@ function styleOptionsToCssProperties({
2021
duration,
2122
enableAnimation = true,
2223
}: SkeletonStyleProps & { circle: boolean }): CSSProperties {
23-
const style: CSSProperties = {
24-
animationDirection: direction === 'rtl' ? 'reverse' : 'normal',
25-
}
24+
const style: CSSProperties = {}
2625

2726
if (direction === 'rtl') style.animationDirection = 'reverse'
2827

@@ -43,19 +42,16 @@ function styleOptionsToCssProperties({
4342
}
4443

4544
if (typeof baseColor !== 'undefined' || typeof highlightColor !== 'undefined') {
46-
baseColor ??= defaultBaseColor
47-
highlightColor ??= defaultHighlightColor
48-
49-
style.backgroundColor = baseColor
45+
style.backgroundColor = baseColor ?? defaultBaseColor
5046
style.backgroundImage = `linear-gradient(
5147
90deg,
52-
${baseColor},
53-
${highlightColor},
54-
${baseColor}
48+
${baseColor ?? defaultBaseColor},
49+
${highlightColor ?? defaultHighlightColor},
50+
${baseColor ?? defaultBaseColor}
5551
)`
5652
}
5753

58-
if (!enableAnimation) style.animation = 'none'
54+
if (!enableAnimation) style.backgroundImage = 'none'
5955

6056
return style
6157
}

src/__tests__/Skeleton.test.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ it('renders a skeleton', () => {
1010

1111
expect(skeletonElements).toHaveLength(1)
1212
expect(skeletonElements[0]).toBeVisible()
13+
14+
// No inline styles should be rendered by default
15+
expect(skeletonElements[0]).not.toHaveAttribute('style')
1316
})
1417

1518
it('renders the required number of skeletons', () => {
@@ -50,13 +53,13 @@ it('ignores borderRadius if circle=true', () => {
5053

5154
it('disables the animation if and only if enableAnimation is false', () => {
5255
const { rerender } = render(<Skeleton />)
53-
expect(getSkeleton()).toHaveStyle({ animation: '' })
56+
expect(getSkeleton()).toHaveStyle({ backgroundImage: /linear-gradient/ })
5457

5558
rerender(<Skeleton enableAnimation />)
56-
expect(getSkeleton()).toHaveStyle({ animation: '' })
59+
expect(getSkeleton()).toHaveStyle({ backgroundImage: /linear-gradient/ })
5760

5861
rerender(<Skeleton enableAnimation={false} />)
59-
expect(getSkeleton()).toHaveStyle({ animation: 'none' })
62+
expect(getSkeleton()).toHaveStyle({ backgroundImage: 'none' })
6063
})
6164

6265
it('uses a custom className', () => {

0 commit comments

Comments
 (0)