Skip to content

Commit 0876672

Browse files
authored
Fix theme values being blocked by props set to undefined (#129)
* Fix theme values being blocked by props set to undefined Closes #128. * Remove an unnecessary type
1 parent f05ea20 commit 0876672

File tree

5 files changed

+56
-11
lines changed

5 files changed

+56
-11
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 3.0.2
2+
3+
### Bug Fixes
4+
5+
- Fix explicitly setting a `Skeleton` prop to undefined, like `<Skeleton highlightColor={undefined}>`, blocking style options from the `SkeletonTheme`
6+
(#128)
7+
- If you were relying on this behavior to block values from the
8+
`SkeletonTheme`, you can render a nested `SkeletonTheme` to override a
9+
theme defined higher up in the component tree, OR explicitly set one or
10+
more `Skeleton` props back to their default values e.g. `<Skeleton baseColor="#ebebeb" />`
11+
112
## 3.0.1
213

314
### Bug Fixes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-loading-skeleton",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"keywords": [
55
"react",
66
"loading",

src/Skeleton.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,20 @@ export function Skeleton({
6262
circle = false,
6363

6464
style: styleProp,
65-
...propsStyleOptions
65+
...originalPropsStyleOptions
6666
}: SkeletonProps): ReactElement {
6767
const contextStyleOptions = React.useContext(SkeletonThemeContext)
6868

69+
const propsStyleOptions = { ...originalPropsStyleOptions }
70+
71+
// DO NOT overwrite style options from the context if `propsStyleOptions`
72+
// has properties explicity set to undefined
73+
for (const [key, value] of Object.entries(originalPropsStyleOptions)) {
74+
if (typeof value === 'undefined') {
75+
delete propsStyleOptions[key as keyof typeof propsStyleOptions]
76+
}
77+
}
78+
6979
// Props take priority over context
7080
const styleOptions = {
7181
...contextStyleOptions,

src/__stories__/SkeletonTheme.stories.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ export const WithColors: React.VFC = () => (
2828
)
2929

3030
export const NoBorderRadius: React.VFC = () => (
31-
<div>
32-
<SkeletonTheme
33-
baseColor={blueBaseColor}
34-
highlightColor={blueHighlightColor}
35-
borderRadius="0"
36-
>
37-
<Post loading />
38-
</SkeletonTheme>
39-
</div>
31+
<SkeletonTheme
32+
baseColor={blueBaseColor}
33+
highlightColor={blueHighlightColor}
34+
borderRadius="0"
35+
>
36+
<Post loading />
37+
</SkeletonTheme>
4038
)
4139

4240
export const LightAndDarkThemes: React.VFC = () => {
@@ -75,3 +73,16 @@ export const LightAndDarkThemes: React.VFC = () => {
7573
</div>
7674
)
7775
}
76+
77+
export const PropsExplicitlySetToUndefined: React.VFC = () => (
78+
<div>
79+
<p>
80+
This is a test for{' '}
81+
<a href="https://github.com/dvtng/react-loading-skeleton/issues/128">#128</a>.
82+
The skeleton should have Christmas colors.
83+
</p>
84+
<SkeletonTheme baseColor="green" highlightColor="red">
85+
<Skeleton baseColor={undefined} highlightColor={undefined} />
86+
</SkeletonTheme>
87+
</div>
88+
)

src/__tests__/SkeletonTheme.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,16 @@ it('styles the skeleton through a portal', () => {
5454
expect(skeleton).toHaveStyle({ borderRadius: '1rem' })
5555
expect(skeleton.style.getPropertyValue('--base-color')).toBe('black')
5656
})
57+
58+
// Regression test
59+
it('is not blocked by setting Skeleton props to undefined', () => {
60+
render(
61+
<SkeletonTheme baseColor="green" highlightColor="red">
62+
<Skeleton baseColor={undefined} highlightColor={undefined} />
63+
</SkeletonTheme>
64+
)
65+
66+
const skeleton = getSkeleton()
67+
expect(skeleton.style.getPropertyValue('--base-color')).toBe('green')
68+
expect(skeleton.style.getPropertyValue('--highlight-color')).toBe('red')
69+
})

0 commit comments

Comments
 (0)