Skip to content

Commit

Permalink
fix class name ordering bug in node < 11
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-rogerson committed Mar 29, 2020
1 parent 2065f65 commit 811d85d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion __fixtures__/-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import tw from './macro'

// This tests the screens end up in the order they are defined in the config

tw.div`xl:bg-red-500 lg:bg-blue-500 fill-current md:bg-pink-500 sm:bg-orange-500 sm:text-yellow-500 hidden`
tw.div`xl:bg-red-500 lg:bg-blue-500 bg-green-500 fill-current md:bg-pink-500 sm:bg-orange-500 sm:text-yellow-500 hidden`
3 changes: 2 additions & 1 deletion __snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ import tw from './macro'
// This tests the screens end up in the order they are defined in the config
tw.div\`xl:bg-red-500 lg:bg-blue-500 fill-current md:bg-pink-500 sm:bg-orange-500 sm:text-yellow-500 hidden\`
tw.div\`xl:bg-red-500 lg:bg-blue-500 bg-green-500 fill-current md:bg-pink-500 sm:bg-orange-500 sm:text-yellow-500 hidden\`
↓ ↓ ↓ ↓ ↓ ↓
import _styled from '@emotion/styled'
// This tests the screens end up in the order they are defined in the config
_styled.div({
backgroundColor: '#48bb78',
fill: 'currentColor',
display: 'none',
'@media (min-width: 640px)': {
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"chalk": "^3.0.0",
"dlv": "^1.1.3",
"dset": "^2.0.1",
"tailwindcss": "^1.2.0"
"tailwindcss": "^1.2.0",
"timsort": "^0.3.0"
},
"devDependencies": {
"@babel/plugin-syntax-jsx": "^7.8.3",
Expand Down
12 changes: 9 additions & 3 deletions src/screens.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dlv from 'dlv'
import timSort from 'timsort'

const stringifyScreen = (config, screenName) => {
const screen = dlv(config, ['theme', 'screens', screenName])
Expand All @@ -24,11 +25,16 @@ const stringifyScreen = (config, screenName) => {
return str ? `@media ${str}` : ''
}

const orderByScreens = (classNames, screens) =>
classNames.sort((a, b) => {
const orderByScreens = (classNames, screens) => {
const screenCompare = (a, b) => {
const A = a.includes(':') ? a.split(':')[0] : a
const B = b.includes(':') ? b.split(':')[0] : b
return screens.indexOf(A) < screens.indexOf(B) ? -1 : 1
})
}
// Tim Sort provides accurate sorting in node < 11
// https://github.com/ben-rogerson/twin.macro/issues/20
timSort.sort(classNames, screenCompare)
return classNames
}

export { stringifyScreen, orderByScreens }

0 comments on commit 811d85d

Please sign in to comment.