Skip to content

Commit e2055c9

Browse files
committed
prefix animation names
1 parent 6b35af3 commit e2055c9

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

__tests__/plugins/animation.test.js

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

src/plugins/animation.js

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

45
export default function () {
5-
return function ({ addUtilities, theme, variants }) {
6+
return function ({ addUtilities, theme, variants, config }) {
7+
const prefix = config('prefix')
68
const keyframesConfig = theme('keyframes')
7-
const keyframesStyles = _.mapKeys(keyframesConfig, (_keyframes, name) => `@keyframes ${name}`)
9+
const keyframesStyles = _.mapKeys(
10+
keyframesConfig,
11+
(_keyframes, name) => `@keyframes ${prefix === undefined ? name : [prefix, name].join('')}`
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+
prefix === undefined
20+
? (animation) => ({ animation })
21+
: function (animation) {
22+
const { name } = animationParser(animation)
23+
if (name === undefined) return { animation }
24+
return { animation: animation.replace(name, [prefix, name].join('')) }
25+
}
1426
)
1527
addUtilities(utilities, variants('animation'))
1628
}

0 commit comments

Comments
 (0)