Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JIT] Support animation lists #5252

Merged
merged 2 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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