Skip to content

Commit d3af2e5

Browse files
committed
prefix animation names
1 parent 9c6848d commit d3af2e5

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed

__tests__/plugins/animation.test.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import postcss from 'postcss'
2+
import processPlugins from '../../src/util/processPlugins'
3+
import plugin from '../../src/plugins/animation'
4+
5+
function css(nodes) {
6+
return postcss.root({ nodes }).toString()
7+
}
8+
9+
test('defining animation and keyframes', () => {
10+
const config = {
11+
theme: {
12+
animation: {
13+
none: 'none',
14+
spin: 'spin 1s linear infinite',
15+
ping: 'ping 1s cubic-bezier(0, 0, 0.2, 1) infinite',
16+
},
17+
keyframes: {
18+
spin: { to: { transform: 'rotate(360deg)' } },
19+
ping: { '75%, 100%': { transform: 'scale(2)', opacity: '0' } },
20+
},
21+
},
22+
variants: {
23+
animation: [],
24+
},
25+
}
26+
27+
const { utilities } = processPlugins([plugin()], config)
28+
29+
expect(css(utilities)).toMatchCss(`
30+
@layer utilities {
31+
@variants {
32+
@keyframes spin {
33+
to { transform: rotate(360deg); }
34+
}
35+
@keyframes ping {
36+
75%, 100% { transform: scale(2); opacity: 0; }
37+
}
38+
}
39+
}
40+
41+
@layer utilities {
42+
@variants {
43+
.animate-none { animation: none; }
44+
.animate-spin { animation: spin 1s linear infinite; }
45+
.animate-ping { animation: ping 1s cubic-bezier(0, 0, 0.2, 1) infinite; }
46+
}
47+
}
48+
`)
49+
})
50+
51+
test('defining animation and keyframes with prefix', () => {
52+
const config = {
53+
prefix: 'tw-',
54+
theme: {
55+
animation: {
56+
none: 'none',
57+
spin: 'spin 1s linear infinite',
58+
ping: 'ping 1s cubic-bezier(0, 0, 0.2, 1) infinite',
59+
},
60+
keyframes: {
61+
spin: { to: { transform: 'rotate(360deg)' } },
62+
ping: { '75%, 100%': { transform: 'scale(2)', opacity: '0' } },
63+
},
64+
},
65+
variants: {
66+
animation: [],
67+
},
68+
}
69+
70+
const { utilities } = processPlugins([plugin()], config)
71+
72+
expect(css(utilities)).toMatchCss(`
73+
@layer utilities {
74+
@variants {
75+
@keyframes tw-spin {
76+
to { transform: rotate(360deg); }
77+
}
78+
@keyframes tw-ping {
79+
75%, 100% { transform: scale(2); opacity: 0; }
80+
}
81+
}
82+
}
83+
84+
@layer utilities {
85+
@variants {
86+
.tw-animate-none { animation: none; }
87+
.tw-animate-spin { animation: tw-spin 1s linear infinite; }
88+
.tw-animate-ping { animation: tw-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite; }
89+
}
90+
}
91+
`)
92+
})

src/plugins/animation.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import _ from 'lodash'
22
import nameClass from '../util/nameClass'
3+
import parseAnimationValue from '../util/parseAnimationValue'
34

45
export default function () {
5-
return function ({ addUtilities, theme, variants }) {
6+
return function ({ addUtilities, theme, variants, prefix }) {
7+
const prefixName = (name) => prefix(`.${name}`).slice(1)
68
const keyframesConfig = theme('keyframes')
7-
const keyframesStyles = _.mapKeys(keyframesConfig, (_keyframes, name) => `@keyframes ${name}`)
9+
const keyframesStyles = _.mapKeys(
10+
keyframesConfig,
11+
(_keyframes, name) => `@keyframes ${prefixName(name)}`
12+
)
13+
814
addUtilities(keyframesStyles, { respectImportant: false })
915

1016
const animationConfig = theme('animation')
1117
const utilities = _.mapValues(
1218
_.mapKeys(animationConfig, (_animation, suffix) => nameClass('animate', suffix)),
13-
(animation) => ({ animation })
19+
(animation) => {
20+
const { name } = parseAnimationValue(animation)
21+
if (name === undefined) return { animation }
22+
return { animation: animation.replace(name, prefixName(name)) }
23+
}
1424
)
1525
addUtilities(utilities, variants('animation'))
1626
}

0 commit comments

Comments
 (0)