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

feat(asCore): Convert asCore into getCoreStyles helper #376

Merged
merged 6 commits into from
Mar 13, 2018
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: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"editor.detectIndentation": false,
"editor.formatOnSave": false,
"editor.insertSpaces": true,
"editor.tabSize": 2,
"files.associations": {
Expand Down
27 changes: 7 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
"main": "index.js",
"unpkg": "dist/gymnast.min.js",
"homepage": "https://gymnastjs.github.io/gymnast",
"files": [
"package.json",
"README.md",
"LICENSE",
"src",
"dist",
"index.js"
],
"files": ["package.json", "README.md", "LICENSE", "src", "dist", "index.js"],
"repository": {
"type": "git",
"url": "https://github.com/gymnastjs/gymnast"
Expand Down Expand Up @@ -104,10 +97,7 @@
"webpack-bundle-analyzer": "2.10.0"
},
"lint-staged": {
"*.{js,css}": [
"format",
"git add"
]
"*.{js,css}": ["format", "git add"]
},
"bundlesize": [
{
Expand All @@ -116,9 +106,7 @@
}
],
"jest": {
"collectCoverageFrom": [
"src/{,**/}*.js"
],
"collectCoverageFrom": ["src/{,**/}*.js"],
"setupTestFrameworkScriptFile": "<rootDir>/test/env.js",
"moduleNameMapper": {
"^.+[.]css$": "identity-obj-proxy",
Expand All @@ -127,9 +115,7 @@
"transform": {
"^.+[.]js$": "babel-jest"
},
"transformIgnorePatterns": [
"<rootDir>/node_modules\\/(?!picturebook)"
]
"transformIgnorePatterns": ["<rootDir>/node_modules\\/(?!picturebook)"]
},
"scripts": {
"build:dev": "webpack --config scripts/webpack.config.js",
Expand All @@ -148,10 +134,11 @@
"lint": "npm-run-all --parallel lint:*",
"precommit": "lint-staged",
"start:storybook": "picturebook -p 9001 -s ./static -c node_modules/picturebook/config",
"start:webpack": "yarn build:dev -- --watch",
"start:webpack": "yarn build:dev --watch",
"start": "npm-run-all --parallel start:*",
"test:bithound": "bithound check git@github.com:gymnastjs/gymnast.git",
"test:image": "picturebook-image --env iphone7,ie11,safari,firefox,edge,chrome --suiteRetries 3",
"test:image":
"picturebook-image --env iphone7,ie11,safari,firefox,edge,chrome --suiteRetries 3",
"test:size": "bundlesize",
"test": "jest",
"test:cover": "jest --coverage",
Expand Down
1 change: 1 addition & 0 deletions scripts/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader',
},
],
Expand Down
17 changes: 13 additions & 4 deletions src/asGrid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,26 @@ import type {
} from '../types'
import { styles, getCol } from './grid.styles'
import { getValue } from '../utils'
import asCore from '../core/asCore'
import getCoreStyles from '../core'
import withResolution from '../withResolution'

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

export default function asGrid(
Component: React.ComponentType<*> | string
): React.ComponentType<GridProps> {
function Grid(
{ align, className, justify, size, innerRef, ...props }: OneResolutionGrid,
{
align,
className,
justify,
size,
innerRef,
...restProps
}: OneResolutionGrid,
context: ConfigProviderContext
) {
const props = getCoreStyles(restProps, context)
const classes = compact([
styles.grid,
getCol(size, getValue(context, 'columns')),
Expand All @@ -27,8 +36,8 @@ export default function asGrid(
justify && styles[`${justify}Justify`],
])

return <Component {...props} ref={innerRef} className={classes.join(' ')} />
return <Component ref={innerRef} {...props} className={classes.join(' ')} />
Copy link
Collaborator

Choose a reason for hiding this comment

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

just curious on whether this change matters for flow or something like that

}

return asCore(Grid, resolutionProperties)
return withResolution(Grid, resolutionProperties)
}
11 changes: 7 additions & 4 deletions src/asLayout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import type {
LayoutProps,
} from '../types'
import getStyles from './layout.styles'
import asCore from '../core/asCore'
import getCoreStyles from '../core'
import { getValues } from '../utils/index'
import withResolution from '../withResolution'

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

Expand All @@ -22,11 +23,12 @@ export default function asLayout(
height,
overflow,
innerRef,
...props
...restProps
}: OneResolutionLayout,
context: ConfigProviderContext
) {
const styles = getStyles(getValues(context, props))
const props = getCoreStyles(restProps, context)
const styles = getStyles(getValues(context, restProps))
const classes = compact([
className,
fixed && styles[`${fixed}Fixed`],
Expand All @@ -37,5 +39,6 @@ export default function asLayout(

return <Component ref={innerRef} {...props} className={classes.join(' ')} />
}
return asCore(Layout, resolutionProperties)

return withResolution(Layout, resolutionProperties)
}
28 changes: 28 additions & 0 deletions src/core/__snapshots__/core.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getCoreStyles should allow overriding base 1`] = `
Object {
"borderBottomWidth": 20,
"borderLeftWidth": 20,
"borderRightWidth": 20,
"borderTopWidth": 20,
}
`;

exports[`getCoreStyles should return remaining props 1`] = `
Object {
"borderBottomWidth": 16,
"borderLeftWidth": 16,
"borderRightWidth": 16,
"borderTopWidth": 16,
}
`;

exports[`getCoreStyles should return styles 1`] = `
Object {
"borderBottomWidth": 16,
"borderLeftWidth": 16,
"borderRightWidth": 16,
"borderTopWidth": 16,
}
`;
71 changes: 0 additions & 71 deletions src/core/asCore.js

This file was deleted.

45 changes: 15 additions & 30 deletions src/core/core.spec.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
import React from 'react'
import { mount } from 'enzyme'
import asCore from './asCore'
import getCoreStyles from './index'
import defaults from '../defaults'

describe('asCore', () => {
let wrapper
describe('getCoreStyles', () => {
it('should return styles', () => {
const { style } = getCoreStyles({ margin: 2 }, defaults)

it('should allow wrapping any element into a Core', () => {
const Span = asCore(() => <span />)
wrapper = mount(<Span />)

expect(wrapper.html().includes('span')).toBe(true)
})

it('should allow overriding base', () => {
const Grid = asCore('div')

wrapper = mount(<Grid base={2} paddingTop={1} />)

expect(wrapper.html()).toContain('padding-top: 2px')
expect(style).toMatchSnapshot()
})

it('should use default base when no config or no base parameters are provided', () => {
const Grid = asCore('div')
const padding = 1

wrapper = mount(<Grid paddingTop={padding} />)

expect(wrapper.html()).toContain(
`padding-top: ${defaults.base * padding}px`
it('should return remaining props', () => {
const { style, ...rest } = getCoreStyles(
{ margin: 2, foo: 'bar' },
defaults
)
expect(style).toMatchSnapshot()
expect(rest).toEqual({ foo: 'bar' })
})

afterEach(() => {
if (wrapper) {
wrapper.unmount()
}
it('should allow overriding base', () => {
const { style } = getCoreStyles({ margin: 2, base: 10 }, defaults)

expect(style).toMatchSnapshot()
})
})
45 changes: 45 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @flow
import { combineSpacing, getValue } from '../utils'
import type { ConfigProviderContext, OneResolution } from '../types'

export default function getCoreStyles(
props: $Shape<OneResolution>,
context: ConfigProviderContext
) {
const {
base,
margin,
marginBottom,
marginLeft,
marginRight,
marginTop,
padding,
paddingBottom,
paddingLeft,
paddingRight,
paddingTop,
style,
...restProps
} = props

const spacing = combineSpacing({
spacingProps: {
margin,
padding,
marginTop,
marginRight,
marginBottom,
marginLeft,
paddingTop,
paddingRight,
paddingBottom,
paddingLeft,
},
base: getValue(context, 'base', base),
spacingAliases: getValue(context, 'spacingAliases'),
})

const spacingStyles = { ...style, ...spacing }

return { ...restProps, style: spacingStyles }
}
9 changes: 7 additions & 2 deletions src/withResolution/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
hasTrueValues,
isObject,
type ShouldShow,
sharedResolutionProperties,
} from './withResolution.logic'

type Props = { show?: DisplayValues }
Expand All @@ -25,6 +26,10 @@ export default function withResolution(
resolutionKeys: Array<string>,
coercedSupport?: boolean = supportsMatchMedia
) {
const combinedResolutionKeys = sharedResolutionProperties.concat(
resolutionKeys
)

if (!coercedSupport) {
log.warn(errors.NOMATCHMEDIA)
return Component
Expand Down Expand Up @@ -84,7 +89,7 @@ export default function withResolution(
}

anyPropsUseResolutionFormat = () =>
resolutionKeys.some(key => isObject(this.props[key]))
combinedResolutionKeys.some(key => isObject(this.props[key]))

removeMediaQueryListener = (show?: DisplayValues) => {
const queries = this.getQueries(show)
Expand Down Expand Up @@ -114,7 +119,7 @@ export default function withResolution(
const props = getSingleResolutionProps({
props: this.props,
shouldShow: this.state.shouldShow,
resolutionKeys,
resolutionKeys: combinedResolutionKeys,
fallbackDisplayKey: getValue(this.context, 'fallbackDisplayKey'),
})

Expand Down
Loading