Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit 5d20026

Browse files
mnajdovakuzhelov
authored andcommitted
fix(Provider): staticStyles should be executed with the merged siteVariables (#559)
* wip * -added logic for rendering the staticStyles if they are provided, just once in one Provider instance. * -updated changelog * -added for ensuring that staticStyles are executed just once in the Provider -added test for ensuring that staticStyles are invoked with the merged siteVariables * adjust changelog entry
1 parent 5dc926b commit 5d20026

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## [Unreleased]
1919

20+
### Fixes
21+
- Fix `Provider` is not executing staticStyles with the merged siteVariables @mnajdova ([#559](https://github.com/stardust-ui/react/pull/559))
22+
2023
### Features
2124
- `Ref` components uses `forwardRef` API by default @layershifter ([#491](https://github.com/stardust-ui/react/pull/491))
2225

src/components/Provider/Provider.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export interface ProviderProps {
2323
/**
2424
* The Provider passes the CSS in JS renderer and theme to your components.
2525
*/
26-
class Provider extends React.Component<ProviderProps, any> {
26+
class Provider extends React.Component<ProviderProps> {
27+
staticStylesRendered: boolean = false
28+
2729
static propTypes = {
2830
theme: PropTypes.shape({
2931
siteVariables: PropTypes.object,
@@ -54,13 +56,14 @@ class Provider extends React.Component<ProviderProps, any> {
5456

5557
static Consumer = ProviderConsumer
5658

57-
renderStaticStyles = () => {
59+
renderStaticStyles = (mergedTheme: ThemePrepared) => {
5860
// RTL WARNING
5961
// This function sets static styles which are global and renderer agnostic
6062
// With current implementation, static styles cannot differ between LTR and RTL
6163
// @see http://fela.js.org/docs/advanced/StaticStyle.html for details
6264

63-
const { siteVariables, staticStyles } = this.props.theme
65+
const { siteVariables } = mergedTheme
66+
const { staticStyles } = this.props.theme
6467

6568
if (!staticStyles) return
6669

@@ -109,7 +112,6 @@ class Provider extends React.Component<ProviderProps, any> {
109112
}
110113

111114
componentDidMount() {
112-
this.renderStaticStyles()
113115
this.renderFontFaces()
114116
}
115117

@@ -122,7 +124,7 @@ class Provider extends React.Component<ProviderProps, any> {
122124
<ProviderConsumer
123125
render={(incomingTheme: ThemePrepared) => {
124126
const outgoingTheme: ThemePrepared = mergeThemes(incomingTheme, theme)
125-
127+
this.renderStaticStylesOnce(outgoingTheme)
126128
return (
127129
<RendererProvider renderer={outgoingTheme.renderer} {...{ rehydrate: false }}>
128130
<ThemeProvider theme={outgoingTheme}>{children}</ThemeProvider>
@@ -132,6 +134,14 @@ class Provider extends React.Component<ProviderProps, any> {
132134
/>
133135
)
134136
}
137+
138+
renderStaticStylesOnce = (mergedTheme: ThemePrepared) => {
139+
const { staticStyles } = this.props.theme
140+
if (!this.staticStylesRendered && staticStyles) {
141+
this.renderStaticStyles(mergedTheme)
142+
this.staticStylesRendered = true
143+
}
144+
}
135145
}
136146

137147
export default Provider

test/specs/components/Provider/Provider-test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as React from 'react'
12
import Provider from 'src/components/Provider/Provider'
23
import ProviderConsumer from 'src/components/Provider/ProviderConsumer'
4+
import { mount } from 'enzyme'
35

46
describe('Provider', () => {
57
test('is exported', () => {
@@ -9,4 +11,46 @@ describe('Provider', () => {
911
test('has a ProviderConsumer subcomponent', () => {
1012
expect(require('src/index.ts').Provider.Consumer).toEqual(ProviderConsumer)
1113
})
14+
15+
describe('staticStyles', () => {
16+
test('are executed with the merged siteVariables', () => {
17+
const staticStyle = jest.fn()
18+
19+
mount(
20+
<Provider theme={{ siteVariables: { brand: 'blue', background: 'red' } }}>
21+
<Provider
22+
theme={{
23+
siteVariables: { brand: 'yellow', gray: '#868686' },
24+
staticStyles: [staticStyle],
25+
}}
26+
>
27+
<span />
28+
</Provider>
29+
</Provider>,
30+
)
31+
32+
expect(staticStyle).toHaveBeenCalledWith(
33+
expect.objectContaining({
34+
background: 'red',
35+
brand: 'yellow',
36+
gray: '#868686',
37+
}),
38+
)
39+
})
40+
41+
test('are executed only once', () => {
42+
const firstStaticStyle = jest.fn()
43+
const secondStaticStyle = jest.fn()
44+
45+
const providerInstance = mount(
46+
<Provider theme={{ staticStyles: [firstStaticStyle] }}>
47+
<span />
48+
</Provider>,
49+
)
50+
providerInstance.setProps({ theme: { staticStyles: [secondStaticStyle] } })
51+
52+
expect(firstStaticStyle).toHaveBeenCalledTimes(1)
53+
expect(secondStaticStyle).not.toHaveBeenCalled()
54+
})
55+
})
1256
})

0 commit comments

Comments
 (0)