Skip to content

Commit

Permalink
prefix animation names
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Oct 20, 2020
1 parent 6b35af3 commit e2055c9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
81 changes: 81 additions & 0 deletions __tests__/plugins/animation.test.js
@@ -0,0 +1,81 @@
import invokePlugin from '../util/invokePlugin'
import plugin from '../../src/plugins/animation'

test('defining animation and keyframes', () => {
const config = {
theme: {
animation: {
none: 'none',
spin: 'spin 1s linear infinite',
ping: 'ping 1s cubic-bezier(0, 0, 0.2, 1) infinite',
},
keyframes: {
spin: { to: { transform: 'rotate(360deg)' } },
ping: { '75%, 100%': { transform: 'scale(2)', opacity: '0' } },
},
},
variants: {
animation: [],
},
}

const { utilities } = invokePlugin(plugin(), config)

expect(utilities).toEqual([
[
{
'@keyframes ping': { '75%, 100%': { opacity: '0', transform: 'scale(2)' } },
'@keyframes spin': { to: { transform: 'rotate(360deg)' } },
},
{ respectImportant: false },
],
[
{
'.animate-none': { animation: 'none' },
'.animate-ping': { animation: 'ping 1s cubic-bezier(0, 0, 0.2, 1) infinite' },
'.animate-spin': { animation: 'spin 1s linear infinite' },
},
[],
],
])
})

test('defining animation and keyframes with prefix', () => {
const config = {
prefix: 'tw-',
theme: {
animation: {
none: 'none',
spin: 'spin 1s linear infinite',
ping: 'ping 1s cubic-bezier(0, 0, 0.2, 1) infinite',
},
keyframes: {
spin: { to: { transform: 'rotate(360deg)' } },
ping: { '75%, 100%': { transform: 'scale(2)', opacity: '0' } },
},
},
variants: {
animation: [],
},
}

const { utilities } = invokePlugin(plugin(), config)

expect(utilities).toEqual([
[
{
'@keyframes tw-ping': { '75%, 100%': { opacity: '0', transform: 'scale(2)' } },
'@keyframes tw-spin': { to: { transform: 'rotate(360deg)' } },
},
{ respectImportant: false },
],
[
{
'.animate-none': { animation: 'none' },
'.animate-ping': { animation: 'tw-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite' },
'.animate-spin': { animation: 'tw-spin 1s linear infinite' },
},
[],
],
])
})
18 changes: 15 additions & 3 deletions src/plugins/animation.js
@@ -1,16 +1,28 @@
import _ from 'lodash'
import nameClass from '../util/nameClass'
import animationParser from '../util/animationParser'

export default function () {
return function ({ addUtilities, theme, variants }) {
return function ({ addUtilities, theme, variants, config }) {
const prefix = config('prefix')
const keyframesConfig = theme('keyframes')
const keyframesStyles = _.mapKeys(keyframesConfig, (_keyframes, name) => `@keyframes ${name}`)
const keyframesStyles = _.mapKeys(
keyframesConfig,
(_keyframes, name) => `@keyframes ${prefix === undefined ? name : [prefix, name].join('')}`
)

addUtilities(keyframesStyles, { respectImportant: false })

const animationConfig = theme('animation')
const utilities = _.mapValues(
_.mapKeys(animationConfig, (_animation, suffix) => nameClass('animate', suffix)),
(animation) => ({ animation })
prefix === undefined
? (animation) => ({ animation })
: function (animation) {
const { name } = animationParser(animation)
if (name === undefined) return { animation }
return { animation: animation.replace(name, [prefix, name].join('')) }
}
)
addUtilities(utilities, variants('animation'))
}
Expand Down

0 comments on commit e2055c9

Please sign in to comment.