Skip to content

Commit

Permalink
fix: issue where variants would sometimes apply only once (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal authored Feb 14, 2021
1 parent f8dc4bb commit e67e071
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .bin/build-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import nodemon from 'nodemon'
import process from 'process'

nodemon(
[
// prettier-ignore
`--watch packages/core/src`,
`--watch packages/core/tests`,
`--watch packages/react/src`,
`--watch packages/react/tests`,
`--exec "clear; ${['npm', 'run', 'build'].concat(process.argv.slice(2)).join(' ')}"`,
].join(' '),
).on('quit', () => process.exit())
13 changes: 13 additions & 0 deletions .bin/test-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import nodemon from 'nodemon'
import process from 'process'

nodemon(
[
// prettier-ignore
`--watch packages/core/src`,
`--watch packages/core/tests`,
`--watch packages/react/src`,
`--watch packages/react/tests`,
`--exec "clear; ${['node', '.bin/test.js'].concat(process.argv.slice(2)).join(' ')}"`,
].join(' '),
).on('quit', () => process.exit())
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"scripts": {
"bootstrap": "lerna bootstrap --use-workspaces",
"build": "node .bin/build",
"build:watch": "nodemon --watch packages/core/src --watch packages/core/tests --watch packages/react/src --watch packages/react/tests --exec \"clear; npm run build\"",
"build:watch": "node .bin/build-watch",
"prerelease": "npm run build && npm run test",
"release": "lerna publish",
"release:canary": "npm run prerelease && lerna publish --dist-tag canary",
"release:pack": "npm run prerelease && lerna exec -- npm pack",
"postinstall": "run-s bootstrap",
"test": "node .bin/test",
"test:watch": "nodemon --watch packages/core/src --watch packages/core/tests --watch packages/react/src --watch packages/react/tests --exec \"clear; npm run test\""
"test:watch": "node .bin/test-watch"
},
"workspaces": [
"packages/*",
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,14 @@ const createCss = (init) => {

if (condition != null) {
if (!conditionVariants[condition]) {
const conditionalVariantClassName = (conditionVariants[condition] = variantClassName + '--' + getHashString('', condition))
const conditionalVariantClassName = variantClassName + '--' + getHashString('', condition)
const conditionalVariantCssText = getComputedCss({ [condition]: { ['.' + conditionalVariantClassName]: variantStyle } })

variantRules.addCss(conditionalVariantCssText)
classNames.push(conditionalVariantClassName)
conditionVariants[condition] = [conditionalVariantCssText, conditionalVariantClassName]
}

variantRules.addCss(conditionVariants[condition][0])
classNames.push(conditionVariants[condition][1])
} else {
variantRules.addCss(variantCssText)
}
Expand Down Expand Up @@ -228,10 +230,11 @@ const createCss = (init) => {

let expressClassNames = new Set(classNames())

for (const propName in defaultVariants)
for (const propName in defaultVariants) {
if (!(propName in props) && propName in variants) {
props[propName] = defaultVariants[propName]
}
}

if (classProp in props) {
String(props[classProp]).split(/\s+/).forEach(expressClassNames.add, expressClassNames)
Expand Down
19 changes: 19 additions & 0 deletions packages/core/tests/variants.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,23 @@ describe('Conditional variants', () => {
expect(component({ size: { bp1: 'small', bp2: 'large' } }).className).toBe([componentClassName, componentSmallBp1ClassName, componentLargeBp2ClassName].join(' '))
expect(toString()).toBe([componentSmallBp1CssText, componentLargeBp2CssText].join(''))
})

test('Renders a component with a conditional variant repeatedly', () => {
const { css, toString } = createCss(config)
const component = css(componentConfig)
const componentClassName = `sx03kze`
const componentSmallBp1ClassName = `${componentClassName}tmy8g--size-small--5m2l7`
const componentLargeBp2ClassName = `${componentClassName}fhyhx--size-large--8c3r4`
const componentSmallBp1CssText = `@media (max-width: 767px){.${componentSmallBp1ClassName}{font-size:16px;}}`
const componentLargeBp2CssText = `@media (min-width: 768px){.sx03kzefhyhx--size-large--8c3r4{font-size:24px;}}`

expect(component({ size: { bp1: 'small', bp2: 'large' } }).className).toBe([componentClassName, componentSmallBp1ClassName, componentLargeBp2ClassName].join(' '))
expect(toString()).toBe([componentSmallBp1CssText, componentLargeBp2CssText].join(''))

expect(component({ size: { bp1: 'small', bp2: 'large' } }).className).toBe([componentClassName, componentSmallBp1ClassName, componentLargeBp2ClassName].join(' '))
expect(toString()).toBe([componentSmallBp1CssText, componentLargeBp2CssText].join(''))

expect(component({ size: { bp1: 'small', bp2: 'large' } }).className).toBe([componentClassName, componentSmallBp1ClassName, componentLargeBp2ClassName].join(' '))
expect(toString()).toBe([componentSmallBp1CssText, componentLargeBp2CssText].join(''))
})
})

0 comments on commit e67e071

Please sign in to comment.