Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(context): Update to latest React Context API #391

Merged
merged 17 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 14 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
3 changes: 0 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
]
],
"env": {
"development": {
"plugins": ["flow-react-proptypes"]
},
"test": {
"plugins": [
[
Expand Down
9 changes: 2 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
"jest": true,
"jasmine": true
},
"plugins": [
"react",
"flowtype"
],
"plugins": ["react", "flowtype"],
"settings": {
"react": {
"version": "15.5"
Expand All @@ -40,9 +37,7 @@
"import/no-unresolved": [
2,
{
"ignore": [
"gymnast"
]
"ignore": ["gymnast"]
}
],
"no-use-before-define": ["error", "nofunc"],
Expand Down
35 changes: 0 additions & 35 deletions flow-typed/npm/prop-types_v15.x.x.js

This file was deleted.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"babel-cli": "6.26.0",
"babel-eslint": "8.2.6",
"babel-jest": "23.4.2",
"babel-plugin-flow-react-proptypes": "24.1.1",
"babel-plugin-lodash": "3.3.4",
"babel-plugin-macros": "2.4.0",
"babel-plugin-module-resolver": "3.1.1",
Expand Down Expand Up @@ -141,7 +140,6 @@
"verifyConditions": "condition-circle"
},
"peerDependencies": {
"prop-types": ">=15",
"react": ">=15",
"react-dom": ">=15"
},
Expand Down
2 changes: 0 additions & 2 deletions scripts/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ module.exports = {
devtool: 'source-map',
externals: {
react: 'react',
'react-dom': 'react-dom',
Copy link
Collaborator

Choose a reason for hiding this comment

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

is react-dom still needed here then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I'll add this back.

'prop-types': 'prop-types',
},
module: {
rules: [
Expand Down
32 changes: 14 additions & 18 deletions src/asGrid/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// @flow
import * as React from 'react'
import { compact } from 'lodash'
import type {
OneResolutionGrid,
ConfigProviderContext,
GridProps,
OneResolution,
} from '../types'
import type { OneResolutionGrid, GridProps, OneResolution } from '../types'
import { styles, getCol } from './grid.styles'
import { getValue } from '../utils'
import getCoreStyles from '../core'
import withResolution from '../withResolution'
import withContext from '../withContext'

const resolutionProperties = ['align', 'justify', 'size']

Expand All @@ -20,17 +16,15 @@ export default function asGrid(
props: $Shape<OneResolution>
) => $Shape<OneResolution> = props => props
): React.ComponentType<GridProps> {
function Grid(
{
align,
className,
justify,
size,
innerRef,
...restProps
}: OneResolutionGrid,
context: ConfigProviderContext
) {
function Grid({
align,
className,
justify,
size,
innerRef,
context,
...restProps
}: OneResolutionGrid) {
const props = getCoreStyles(mapDefaultProps(restProps), context)
const classes = compact([
styles.grid,
Expand All @@ -43,5 +37,7 @@ export default function asGrid(
return <Component ref={innerRef} {...props} className={classes.join(' ')} />
}

return withResolution(Grid, resolutionProperties)
const Resolution = withResolution(Grid, resolutionProperties)

return withContext(Resolution)
}
32 changes: 14 additions & 18 deletions src/asLayout/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// @flow
import * as React from 'react'
import { compact } from 'lodash'
import type {
OneResolutionLayout,
ConfigProviderContext,
LayoutProps,
OneResolution,
} from '../types'
import type { OneResolutionLayout, LayoutProps, OneResolution } from '../types'
import getStyles from './layout.styles'
import getCoreStyles from '../core'
import { getValues } from '../utils/index'
import withResolution from '../withResolution'
import withContext from '../withContext'

const resolutionProperties = ['fixed', 'height', 'overflow']

Expand All @@ -20,17 +16,15 @@ export default function asLayout(
props: $Shape<OneResolution>
) => $Shape<OneResolution> = props => props
): React.ComponentType<LayoutProps> {
function Layout(
{
className,
fixed,
height,
overflow,
innerRef,
...restProps
}: OneResolutionLayout,
context: ConfigProviderContext
) {
function Layout({
className,
fixed,
height,
overflow,
innerRef,
context,
...restProps
}: OneResolutionLayout) {
const props = getCoreStyles(mapDefaultProps(restProps), context)
const styles = getStyles(getValues(context, restProps))
const classes = compact([
Expand All @@ -44,5 +38,7 @@ export default function asLayout(
return <Component ref={innerRef} {...props} className={classes.join(' ')} />
}

return withResolution(Layout, resolutionProperties)
const Resolution = withResolution(Layout, resolutionProperties)

return withContext(Resolution)
}
10 changes: 10 additions & 0 deletions src/configContext/configConsumer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow
import * as React from 'react'
import type { ConfigContextType } from '../types'
import Context from './context'

type Props = { children: (context: ConfigContextType) => ?React.Node }

export default function ConfigConsumer({ children }: Props) {
return <Context.Consumer>{context => children(context)}</Context.Consumer>
}
42 changes: 42 additions & 0 deletions src/configContext/configProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @flow
import * as React from 'react'
import type { ConfigContextType } from '../types'
import Context from './context'

type ProviderProps = {
...ConfigContextType,
children?: React.Node,
}

type ProviderState = ConfigContextType | {}

// This HOC takes a Provider component and wraps it around a Consumer
// component that provides non-defaulted values that have been set from preceding
// Provider components.
class ConfigProvider extends React.Component<ProviderProps, ProviderState> {
static getDerivedStateFromProps(props: ProviderProps, state: ProviderState) {
const { children, ...restProps } = props

return {
...state,
...restProps,
}
}

state = {}

render() {
return (
<Context.Consumer>
{(context: ConfigContextType) => {
const { children } = this.props
const value = { ...context, ...this.state }

return <Context.Provider value={value}>{children}</Context.Provider>
}}
</Context.Consumer>
)
}
}

export default ConfigProvider
10 changes: 10 additions & 0 deletions src/configContext/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @flow
import * as React from 'react'
import type { ConfigContextType } from '../types'
import defaults from '../defaults'

const contextDefaults: ConfigContextType = defaults

const Context = React.createContext(contextDefaults)

export default Context
9 changes: 9 additions & 0 deletions src/configContext/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @flow

import ConfigProvider from './configProvider'
import ConfigConsumer from './configConsumer'

export default {
Provider: ConfigProvider,
Consumer: ConfigConsumer,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

why export both? We only need ConfigConsumer externally (the provider is only used internally, I would not export it)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense. I'll make Provider only exportable inside it's own file but not in gymnast.js

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I was working on removing this, I noticed (via Storybook) that when using <ConfigProvider> imported from a relative path alongside a component (i.e. <Grid>) imported from a gymnast import, that <Grid> will not use the same React.createContext() as provided from <ConfigProvider> when imported from a relative path.

This is only an issue in Storybook in the Config Provider section. However, this shouldn't be a problem in production, since we're only exporting Consumer, which should be using the same React.createcontext() as the Provider imported from gymnast.

Example:

// @flow
// storybook/stories/configProvider/columns.js

import * as React from 'react'
import { times } from 'lodash'
import { number } from '@storybook/addon-knobs'
import { Grid } from 'gymnast'
import { colors } from '../../shared'

// This is using a different `React.createContext()` than the imported
// Grid above. Therefore, Grid isn't going to consume the 'columns' that
// was passed through from this ConfigProvider, and instead, will used
// the default columns value.
import ConfigProvider from '../../../src/configContext/configProvider'

export default () => {
  const columns = number('Columns', 10, { range: true, min: 1, max: 24 })
  const items = number('Items', 20, { range: true, min: 1, max: 48 })

  return (
    <ConfigProvider columns={columns}>
      <Grid padding="0 L/2">
        {times(items, key => (
          <Grid key={key} padding="0 L/2 L" size={1}>
            <Grid
              style={colors.colors1}
              padding="L/2"
              align="center"
              justify="center"
            >
              {key + 1}
            </Grid>
          </Grid>
        ))}
      </Grid>
    </ConfigProvider>
  )
}

This appears to be a known issue via this issue

To alleviate the Storybook problem, I can remove all imports from gymnast and just imported via relative path, like so:

// @flow
// storybook/stories/configProvider/columns.js

import * as React from 'react'
import { times } from 'lodash'
import { number } from '@storybook/addon-knobs'
import { colors } from '../../shared'
import ConsumerProvider from '../../../src/configContext/configProvider'
import Grid from '../../../src/grid'

export default () => {
  const columns = number('Columns', 10, { range: true, min: 1, max: 24 })
  const items = number('Items', 20, { range: true, min: 1, max: 48 })

  return (
    <ConsumerProvider columns={columns}>
      <Grid padding="0 L/2">
        {times(items, key => (
          <Grid key={key} padding="0 L/2 L" size={1}>
            <Grid
              style={colors.colors1}
              padding="L/2"
              align="center"
              justify="center"
            >
              {key + 1}
            </Grid>
          </Grid>
        ))}
      </Grid>
    </ConsumerProvider>
  )
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could add a babel alias to it to make it a bit more convenient to use. If we define one (for stories only) that maps gymnastSrc to the src path we could then do something like gymnastSrc/configContext/configProvider

Copy link
Collaborator

Choose a reason for hiding this comment

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

to ensure the project is still tree-shakeable with babel-plugin-lodash this index.js file needs to contain as default export the same value we export through src/index.js

Loading