Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixes
- Fix `muted` prop in `Video` component @layershifter ([#1847](https://github.com/stardust-ui/react/pull/1847))
- Fix `felaRenderer` is used in `Provider` explicitly @lucivpav ([#1842](https://github.com/stardust-ui/react/pull/1842))

<!--------------------------------[ v0.37.0 ]------------------------------- -->
## [v0.37.0](https://github.com/stardust-ui/react/tree/v0.37.0) (2019-08-26)
Expand Down
6 changes: 6 additions & 0 deletions packages/internal-tooling/eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ module.exports = {
'@stardust-ui/no-visibility-modifiers': 'error',
},
},
{
files: '**/*.{ts,tsx}',
rules: {
'no-dupe-class-members': 'off',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we get false positive in RendererMock.tsx, see typescript-eslint/typescript-eslint#291

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed at the moment, but I think it will be useful to keep this change, as described in the link above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, let's keep it

},
},
],
settings: {
'import/resolver': {
Expand Down
29 changes: 15 additions & 14 deletions packages/react/src/components/Provider/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as React from 'react'
// @ts-ignore
import { RendererProvider, ThemeProvider, ThemeContext } from '@stardust-ui/react-fela'

import { felaRenderer, ChildrenComponentProps, setUpWhatInput } from '../../lib'
import { ChildrenComponentProps, setUpWhatInput } from '../../lib'

import {
ThemePrepared,
Expand Down Expand Up @@ -86,23 +86,24 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {
static Box = ProviderBox
static contextType = ThemeContext

outgoingContext: ProviderContextPrepared
staticStylesRendered: boolean = false

renderStaticStyles = (mergedTheme: ThemePrepared) => {
renderStaticStyles = (renderer: Renderer, mergedTheme: ThemePrepared) => {
const { siteVariables } = mergedTheme
const { staticStyles } = this.props.theme

if (!staticStyles) return

const renderObject = (object: StaticStyleObject) => {
_.forEach(object, (style, selector) => {
felaRenderer.renderStatic(style as IStyle, selector)
renderer.renderStatic(style as IStyle, selector)
})
}

staticStyles.forEach((staticStyle: StaticStyle) => {
if (typeof staticStyle === 'string') {
felaRenderer.renderStatic(staticStyle)
renderer.renderStatic(staticStyle)
} else if (_.isPlainObject(staticStyle)) {
renderObject(staticStyle as StaticStyleObject)
} else if (_.isFunction(staticStyle)) {
Expand All @@ -116,7 +117,7 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {
})
}

renderFontFaces = () => {
renderFontFaces = (renderer: Renderer) => {
const { fontFaces } = this.props.theme

if (!fontFaces) return
Expand All @@ -126,7 +127,7 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {
throw new Error(`fontFaces must be objects, got: ${typeof font}`)
}

felaRenderer.renderFont(font.name, font.paths, font.props)
renderer.renderFont(font.name, font.paths, font.props)
}

fontFaces.forEach((font: FontFace) => {
Expand All @@ -135,7 +136,7 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {
}

componentDidMount() {
this.renderFontFaces()
this.renderFontFaces(this.outgoingContext.renderer)
if (this.props.target) {
setUpWhatInput(this.props.target)
}
Expand Down Expand Up @@ -165,26 +166,26 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {
const incomingContext: ProviderContextPrepared = overwrite ? {} : this.context
// rehydration disabled to avoid leaking styles between renderers
// https://github.com/rofrischmann/fela/blob/master/docs/api/fela-dom/rehydrate.md
const outgoingContext: ProviderContextPrepared = mergeContexts(incomingContext, inputContext)
this.outgoingContext = mergeContexts(incomingContext, inputContext)

this.renderStaticStylesOnce(outgoingContext.theme)
this.renderStaticStylesOnce(this.outgoingContext.theme)

const rtlProps: { dir?: 'rtl' | 'ltr' } = {}
// only add dir attribute for top level provider or when direction changes from parent to child
if (
!this.context ||
(this.context.rtl !== outgoingContext.rtl && _.isBoolean(outgoingContext.rtl))
(this.context.rtl !== this.outgoingContext.rtl && _.isBoolean(this.outgoingContext.rtl))
) {
rtlProps.dir = outgoingContext.rtl ? 'rtl' : 'ltr'
rtlProps.dir = this.outgoingContext.rtl ? 'rtl' : 'ltr'
}

return (
<RendererProvider
renderer={outgoingContext.renderer}
renderer={this.outgoingContext.renderer}
target={target}
{...{ rehydrate: false }}
>
<ThemeProvider theme={outgoingContext} overwrite>
<ThemeProvider theme={this.outgoingContext} overwrite>
<ProviderBox as={as} variables={variables} {...unhandledProps} {...rtlProps}>
{children}
</ProviderBox>
Expand All @@ -196,7 +197,7 @@ class Provider extends React.Component<WithAsProp<ProviderProps>> {
renderStaticStylesOnce = (mergedTheme: ThemePrepared) => {
const { staticStyles } = this.props.theme
if (!this.staticStylesRendered && staticStyles) {
this.renderStaticStyles(mergedTheme)
this.renderStaticStyles(this.outgoingContext.renderer, mergedTheme)
this.staticStylesRendered = true
}
}
Expand Down
48 changes: 48 additions & 0 deletions packages/react/test/specs/components/Provider/Provider-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as React from 'react'

import Provider from 'src/components/Provider/Provider'
import ProviderConsumer from 'src/components/Provider/ProviderConsumer'
import { ThemeInput } from 'src/themes/types'
import { createRenderer } from '@stardust-ui/fela'

describe('Provider', () => {
test('is exported', () => {
Expand Down Expand Up @@ -184,4 +186,50 @@ describe('Provider', () => {
})
})
})

describe('calls provided renderer', () => {
test('calls renderFont', () => {
const theme: ThemeInput = {
fontFaces: [
{
name: 'Segoe UI',
paths: ['public/fonts/segoe-ui-regular.woff2'],
style: { fontWeight: 400 },
},
],
}
const renderer = createRenderer()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot use felaRenderer, because the test would pass even without this fix.

const renderFont = jest.spyOn(renderer, 'renderFont')

mount(
<Provider theme={theme} renderer={renderer}>
<div />
</Provider>,
)

expect(renderFont).toHaveBeenCalled()
})
})

test('calls renderStatic', () => {
const theme: ThemeInput = {
staticStyles: [
{
a: {
textDecoration: 'none',
},
},
],
}
const renderer = createRenderer()
const renderStatic = jest.spyOn(renderer, 'renderStatic')

mount(
<Provider theme={theme} renderer={renderer}>
<div />
</Provider>,
)

expect(renderStatic).toHaveBeenCalled()
})
})