Skip to content

Commit 5da2398

Browse files
authored
Merge pull request #992 from AlexVipond/prefixNegativeModifiers_for_boxShadow_and_letterSpacing_and_lineHeight
Support negative prefix syntax for boxShadow and letterSpacing
2 parents 25b3aba + e5c08f6 commit 5da2398

File tree

7 files changed

+195
-4
lines changed

7 files changed

+195
-4
lines changed

__tests__/plugins/boxShadow.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import _ from 'lodash'
2+
import escapeClassName from '../../src/util/escapeClassName'
3+
import plugin from '../../src/plugins/boxShadow'
4+
5+
test('box shadow can use default keyword and negative prefix syntax', () => {
6+
const addedUtilities = []
7+
8+
const config = {
9+
theme: {
10+
boxShadow: {
11+
default: '0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06)',
12+
md: '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)',
13+
'-': 'inset 0 2px 4px 0 rgba(0, 0, 0, 0.06)',
14+
'-md': '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)',
15+
},
16+
},
17+
variants: {
18+
boxShadow: ['responsive'],
19+
},
20+
}
21+
22+
const getConfigValue = (path, defaultValue) => _.get(config, path, defaultValue)
23+
const pluginApi = {
24+
config: getConfigValue,
25+
e: escapeClassName,
26+
theme: (path, defaultValue) => getConfigValue(`theme.${path}`, defaultValue),
27+
variants: (path, defaultValue) => {
28+
if (_.isArray(config.variants)) {
29+
return config.variants
30+
}
31+
32+
return getConfigValue(`variants.${path}`, defaultValue)
33+
},
34+
addUtilities(utilities, variants) {
35+
addedUtilities.push({
36+
utilities,
37+
variants,
38+
})
39+
},
40+
}
41+
42+
plugin()(pluginApi)
43+
44+
expect(addedUtilities).toEqual([
45+
{
46+
utilities: {
47+
'.shadow': {
48+
'box-shadow': '0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06)',
49+
},
50+
'.shadow-md': {
51+
'box-shadow': '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)',
52+
},
53+
'.-shadow': {
54+
'box-shadow': 'inset 0 2px 4px 0 rgba(0, 0, 0, 0.06)',
55+
},
56+
'.-shadow-md': {
57+
'box-shadow': '0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06)',
58+
},
59+
},
60+
variants: ['responsive'],
61+
},
62+
])
63+
})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import _ from 'lodash'
2+
import escapeClassName from '../../src/util/escapeClassName'
3+
import plugin from '../../src/plugins/letterSpacing'
4+
5+
test('letter spacing can use negative prefix syntax', () => {
6+
const addedUtilities = []
7+
8+
const config = {
9+
theme: {
10+
letterSpacing: {
11+
'-1': '-0.025em',
12+
'1': '0.025em',
13+
},
14+
},
15+
variants: {
16+
letterSpacing: ['responsive'],
17+
},
18+
}
19+
20+
const getConfigValue = (path, defaultValue) => _.get(config, path, defaultValue)
21+
const pluginApi = {
22+
config: getConfigValue,
23+
e: escapeClassName,
24+
theme: (path, defaultValue) => getConfigValue(`theme.${path}`, defaultValue),
25+
variants: (path, defaultValue) => {
26+
if (_.isArray(config.variants)) {
27+
return config.variants
28+
}
29+
30+
return getConfigValue(`variants.${path}`, defaultValue)
31+
},
32+
addUtilities(utilities, variants) {
33+
addedUtilities.push({
34+
utilities,
35+
variants,
36+
})
37+
},
38+
}
39+
40+
plugin()(pluginApi)
41+
42+
expect(addedUtilities).toEqual([
43+
{
44+
utilities: {
45+
'.-tracking-1': { 'letter-spacing': '-0.025em' },
46+
'.tracking-1': { 'letter-spacing': '0.025em' },
47+
},
48+
variants: ['responsive'],
49+
},
50+
])
51+
})

__tests__/plugins/zIndex.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import _ from 'lodash'
2+
import escapeClassName from '../../src/util/escapeClassName'
3+
import plugin from '../../src/plugins/zIndex'
4+
5+
test('z index can use negative prefix syntax', () => {
6+
const addedUtilities = []
7+
8+
const config = {
9+
theme: {
10+
zIndex: {
11+
'-20': '-20',
12+
'-10': '-10',
13+
'10': '10',
14+
'20': '20',
15+
},
16+
},
17+
variants: {
18+
zIndex: ['responsive'],
19+
},
20+
}
21+
22+
const getConfigValue = (path, defaultValue) => _.get(config, path, defaultValue)
23+
const pluginApi = {
24+
config: getConfigValue,
25+
e: escapeClassName,
26+
theme: (path, defaultValue) => getConfigValue(`theme.${path}`, defaultValue),
27+
variants: (path, defaultValue) => {
28+
if (_.isArray(config.variants)) {
29+
return config.variants
30+
}
31+
32+
return getConfigValue(`variants.${path}`, defaultValue)
33+
},
34+
addUtilities(utilities, variants) {
35+
addedUtilities.push({
36+
utilities,
37+
variants,
38+
})
39+
},
40+
}
41+
42+
plugin()(pluginApi)
43+
44+
expect(addedUtilities).toEqual([
45+
{
46+
utilities: {
47+
'.-z-20': { 'z-index': '-20' },
48+
'.-z-10': { 'z-index': '-10' },
49+
'.z-10': { 'z-index': '10' },
50+
'.z-20': { 'z-index': '20' },
51+
},
52+
variants: ['responsive'],
53+
},
54+
])
55+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import prefixNegativeModifiers from '../src/util/prefixNegativeModifiers'
2+
3+
test('it does not prefix classes using standard syntax', () => {
4+
expect(prefixNegativeModifiers('base', 'modifier')).toEqual('base-modifier')
5+
})
6+
7+
test('it prefixes classes using negative syntax', () => {
8+
expect(prefixNegativeModifiers('base', '-modifier')).toEqual('-base-modifier')
9+
})
10+
11+
test('it prefixes classes and omits suffix using default negative syntax', () => {
12+
expect(prefixNegativeModifiers('base', '-')).toEqual('-base')
13+
})

src/plugins/boxShadow.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import _ from 'lodash'
2+
import prefixNegativeModifiers from '../util/prefixNegativeModifiers'
23

34
export default function() {
45
return function({ addUtilities, e, theme, variants }) {
56
const utilities = _.fromPairs(
67
_.map(theme('boxShadow'), (value, modifier) => {
7-
const className = modifier === 'default' ? 'shadow' : `shadow-${modifier}`
8+
const className =
9+
modifier === 'default' ? 'shadow' : `${e(prefixNegativeModifiers('shadow', modifier))}`
810
return [
9-
`.${e(className)}`,
11+
`.${className}`,
1012
{
1113
'box-shadow': value,
1214
},

src/plugins/letterSpacing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import _ from 'lodash'
2+
import prefixNegativeModifiers from '../util/prefixNegativeModifiers'
23

34
export default function() {
45
return function({ addUtilities, theme, variants, e }) {
56
const utilities = _.fromPairs(
67
_.map(theme('letterSpacing'), (value, modifier) => {
78
return [
8-
`.${e(`tracking-${modifier}`)}`,
9+
`.${e(prefixNegativeModifiers('tracking', modifier))}`,
910
{
1011
'letter-spacing': value,
1112
},

src/util/prefixNegativeModifiers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import _ from 'lodash'
22

33
export default function prefixNegativeModifiers(base, modifier) {
4-
return _.startsWith(modifier, '-') ? `-${base}-${modifier.slice(1)}` : `${base}-${modifier}`
4+
if (modifier === '-') {
5+
return `-${base}`
6+
} else if (_.startsWith(modifier, '-')) {
7+
return `-${base}-${modifier.slice(1)}`
8+
} else {
9+
return `${base}-${modifier}`
10+
}
511
}

0 commit comments

Comments
 (0)