Skip to content

Commit

Permalink
[JIT] Support animation lists (#5252)
Browse files Browse the repository at this point in the history
* Support animation lists

* Update tests
  • Loading branch information
bradlc authored Aug 21, 2021
1 parent ca1a167 commit b56b4c0
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 79 deletions.
21 changes: 13 additions & 8 deletions src/plugins/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ export default function () {
matchUtilities(
{
animate: (value, { includeRules }) => {
let { name: animationName } = parseAnimationValue(value)
let animations = parseAnimationValue(value)

if (keyframes[animationName] !== undefined) {
includeRules(keyframes[animationName], { respectImportant: false })
}

if (animationName === undefined || keyframes[animationName] === undefined) {
return { animation: value }
for (let { name } of animations) {
if (keyframes[name] !== undefined) {
includeRules(keyframes[name], { respectImportant: false })
}
}

return {
animation: value.replace(animationName, prefixName(animationName)),
animation: animations
.map(({ name, value }) => {
if (name === undefined || keyframes[name] === undefined) {
return value
}
return value.replace(name, prefixName(name))
})
.join(', '),
}
},
},
Expand Down
9 changes: 4 additions & 5 deletions src/util/parseAnimationValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ const DIGIT = /^(\d+)$/

export default function parseAnimationValue(input) {
let animations = input.split(COMMA)
let result = animations.map((animation) => {
let result = {}
let parts = animation.trim().split(SPACE)
return animations.map((animation) => {
let value = animation.trim()
let result = { value }
let parts = value.split(SPACE)
let seen = new Set()

for (let part of parts) {
Expand Down Expand Up @@ -58,6 +59,4 @@ export default function parseAnimationValue(input) {

return result
})

return animations.length > 1 ? result : result[0]
}
32 changes: 0 additions & 32 deletions tests/jit/animations.test.css

This file was deleted.

3 changes: 0 additions & 3 deletions tests/jit/animations.test.html

This file was deleted.

196 changes: 186 additions & 10 deletions tests/jit/animations.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import postcss from 'postcss'
import fs from 'fs'
import path from 'path'
import tailwind from '../../src/jit/index.js'

Expand All @@ -9,22 +8,199 @@ function run(input, config = {}) {
})
}

test('animations', () => {
test('basic', () => {
let config = {
darkMode: 'class',
mode: 'jit',
purge: [path.resolve(__dirname, './animations.test.html')],
corePlugins: {},
theme: {},
plugins: [],
purge: [
{
raw: `
<div class="animate-spin"></div>
<div class="hover:animate-ping"></div>
<div class="group-hover:animate-bounce"></div>
`,
},
],
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
let expectedPath = path.resolve(__dirname, './animations.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')
expect(result.css).toMatchFormattedCss(`
@keyframes spin {
to {
transform: rotate(360deg);
}
}
.animate-spin {
animation: spin 1s linear infinite;
}
@keyframes ping {
75%,
100% {
transform: scale(2);
opacity: 0;
}
}
.hover\\:animate-ping:hover {
animation: ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
}
@keyframes bounce {
0%,
100% {
transform: translateY(-25%);
animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
}
50% {
transform: none;
animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
}
}
.group:hover .group-hover\\:animate-bounce {
animation: bounce 1s infinite;
}
`)
})
})

test('custom', () => {
let config = {
mode: 'jit',
purge: [{ raw: `<div class="animate-one"></div>` }],
theme: {
extend: {
keyframes: {
one: { to: { transform: 'rotate(360deg)' } },
},
animation: {
one: 'one 2s',
},
},
},
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
@keyframes one {
to {
transform: rotate(360deg);
}
}
.animate-one {
animation: one 2s;
}
`)
})
})

test('custom prefixed', () => {
let config = {
mode: 'jit',
prefix: 'tw-',
purge: [{ raw: `<div class="tw-animate-one"></div>` }],
theme: {
extend: {
keyframes: {
one: { to: { transform: 'rotate(360deg)' } },
},
animation: {
one: 'one 2s',
},
},
},
}

let css = `@tailwind utilities`

expect(result.css).toMatchFormattedCss(expected)
return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
@keyframes tw-one {
to {
transform: rotate(360deg);
}
}
.tw-animate-one {
animation: tw-one 2s;
}
`)
})
})

test('multiple', () => {
let config = {
mode: 'jit',
purge: [{ raw: `<div class="animate-multiple"></div>` }],
theme: {
extend: {
animation: {
multiple: 'bounce 2s linear, pulse 3s ease-in',
},
},
},
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
@keyframes bounce {
0%,
100% {
transform: translateY(-25%);
animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
}
50% {
transform: none;
animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
}
}
@keyframes pulse {
50% {
opacity: 0.5;
}
}
.animate-multiple {
animation: bounce 2s linear, pulse 3s ease-in;
}
`)
})
})

test('multiple custom', () => {
let config = {
mode: 'jit',
purge: [{ raw: `<div class="animate-multiple"></div>` }],
theme: {
extend: {
keyframes: {
one: { to: { transform: 'rotate(360deg)' } },
two: { to: { transform: 'scale(1.23)' } },
},
animation: {
multiple: 'one 2s, two 3s',
},
},
},
}

let css = `@tailwind utilities`

return run(css, config).then((result) => {
expect(result.css).toMatchFormattedCss(`
@keyframes one {
to {
transform: rotate(360deg);
}
}
@keyframes two {
to {
transform: scale(1.23);
}
}
.animate-multiple {
animation: one 2s, two 3s;
}
`)
})
})
Loading

0 comments on commit b56b4c0

Please sign in to comment.