Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 602e6b0

Browse files
authored
Merge pull request #150 from tailwindlabs/fix/animation-apply
Fix @apply of animation utilities
2 parents dfbc228 + ff28f2f commit 602e6b0

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

src/lib/expandApplyAtRules.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,27 @@ function expandApplyAtRules(context) {
117117

118118
for (let [meta, node] of rules) {
119119
let root = postcss.root({ nodes: [node.clone()] })
120+
let canRewriteSelector = node.type !== 'atrule' || (node.type === 'atrule' && node.name !== 'keyframes');
120121

121-
root.walkRules((rule) => {
122-
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)
123-
rule.walkDecls((d) => {
124-
d.important = important
122+
if (canRewriteSelector) {
123+
root.walkRules((rule) => {
124+
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)
125+
126+
rule.walkDecls((d) => {
127+
d.important = important
128+
})
125129
})
126-
})
130+
}
127131

128132
siblings.push([meta, root.nodes[0]])
129133
}
130134
}
131135

132136
// Inject the rules, sorted, correctly
133-
for (let [, sibling] of siblings.sort(([a], [z]) => bigSign(z.sort - a.sort))) {
134-
// `apply.parent` is referring to the node at `.abc` in: .abc { @apply mt-2 }
135-
apply.parent.after(sibling)
136-
}
137+
const nodes = siblings.sort(([a], [z]) => bigSign(a.sort - z.sort)).map(s => s[1])
138+
139+
// `apply.parent` is referring to the node at `.abc` in: .abc { @apply mt-2 }
140+
apply.parent.after(nodes)
137141

138142
// If there are left-over declarations, just remove the @apply
139143
if (apply.parent.nodes.length > 1) {

tests/00-kitchen-sink.test.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ div {
243243
--tw-numeric-fraction: var(--tw-empty, /*!*/ /*!*/);
244244
font-variant-numeric: var(--tw-ordinal) var(--tw-slashed-zero) var(--tw-numeric-figure)
245245
var(--tw-numeric-spacing) var(--tw-numeric-fraction);
246-
--tw-numeric-spacing: tabular-nums;
247246
--tw-ordinal: ordinal;
247+
--tw-numeric-spacing: tabular-nums;
248248
}
249249
.custom-component {
250250
background: #123456;

tests/10-apply.test.css

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
border-radius: 0.375rem;
1212
--tw-bg-opacity: 1;
1313
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
14-
padding-top: 0.5rem;
15-
padding-bottom: 0.5rem;
1614
padding-left: 1rem;
1715
padding-right: 1rem;
16+
padding-top: 0.5rem;
17+
padding-bottom: 0.5rem;
1818
}
1919
.class-order {
2020
padding: 2rem;
21-
padding-top: 1.75rem;
22-
padding-bottom: 1.75rem;
2321
padding-left: 0.75rem;
2422
padding-right: 0.75rem;
25-
padding-right: 0.25rem;
23+
padding-top: 1.75rem;
24+
padding-bottom: 1.75rem;
2625
padding-top: 1rem;
26+
padding-right: 0.25rem;
2727
}
2828
.with-additional-properties {
2929
font-weight: 500;
@@ -104,10 +104,10 @@
104104
border-radius: 0.375rem;
105105
--tw-bg-opacity: 1;
106106
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
107-
padding-top: 0.5rem;
108-
padding-bottom: 0.5rem;
109107
padding-left: 1rem;
110108
padding-right: 1rem;
109+
padding-top: 0.5rem;
110+
padding-bottom: 0.5rem;
111111
}
112112
.multiple-variants:hover,
113113
.selectors-variants:hover {
@@ -149,8 +149,8 @@
149149
--tw-numeric-fraction: var(--tw-empty, /*!*/ /*!*/);
150150
font-variant-numeric: var(--tw-ordinal) var(--tw-slashed-zero) var(--tw-numeric-figure)
151151
var(--tw-numeric-spacing) var(--tw-numeric-fraction);
152-
--tw-numeric-spacing: tabular-nums;
153152
--tw-ordinal: ordinal;
153+
--tw-numeric-spacing: tabular-nums;
154154
--tw-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
155155
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
156156
var(--tw-shadow);
@@ -187,18 +187,18 @@
187187
}
188188
.btn {
189189
border-radius: 0.25rem;
190-
padding-left: 1rem;
191-
padding-right: 1rem;
192190
padding-top: 0.5rem;
193191
padding-bottom: 0.5rem;
192+
padding-left: 1rem;
193+
padding-right: 1rem;
194194
font-weight: 700;
195195
}
196196
.btn-blue {
197197
border-radius: 0.25rem;
198-
padding-left: 1rem;
199-
padding-right: 1rem;
200198
padding-top: 0.5rem;
201199
padding-bottom: 0.5rem;
200+
padding-left: 1rem;
201+
padding-right: 1rem;
202202
font-weight: 700;
203203
--tw-bg-opacity: 1;
204204
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
@@ -265,3 +265,19 @@
265265
color: green;
266266
font-weight: 700;
267267
}
268+
@keyframes spin {
269+
to {
270+
transform: rotate(360deg);
271+
}
272+
}
273+
.foo {
274+
animation: spin 1s linear infinite;
275+
}
276+
@keyframes pulse {
277+
50% {
278+
opacity: 0.5;
279+
}
280+
}
281+
.bar {
282+
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite !important;
283+
}

tests/10-apply.test.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@
3232
<div class="recursive-apply-c"></div>
3333
<div class="use-with-other-properties-base use-with-other-properties-component"></div>
3434
<div class="a b"></div>
35+
<div class="foo"></div>
36+
<div class="bar"></div>
3537
</body>
3638
</html>

tests/10-apply.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ test('@apply', () => {
105105
.custom-util {
106106
custom: stuff;
107107
}
108+
109+
.foo {
110+
@apply animate-spin;
111+
}
112+
113+
.bar {
114+
@apply animate-pulse !important;
115+
}
108116
}
109117
`
110118

0 commit comments

Comments
 (0)