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

Fix @apply of animation utilities #150

Merged
merged 3 commits into from
Mar 24, 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
22 changes: 13 additions & 9 deletions src/lib/expandApplyAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,27 @@ function expandApplyAtRules(context) {

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

root.walkRules((rule) => {
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)
rule.walkDecls((d) => {
d.important = important
if (canRewriteSelector) {
root.walkRules((rule) => {
rule.selector = replaceSelector(apply.parent.selector, rule.selector, applyCandidate)

rule.walkDecls((d) => {
d.important = important
})
})
})
}

siblings.push([meta, root.nodes[0]])
}
}

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

// `apply.parent` is referring to the node at `.abc` in: .abc { @apply mt-2 }
apply.parent.after(nodes)

// If there are left-over declarations, just remove the @apply
if (apply.parent.nodes.length > 1) {
Expand Down
2 changes: 1 addition & 1 deletion tests/00-kitchen-sink.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ div {
--tw-numeric-fraction: var(--tw-empty, /*!*/ /*!*/);
font-variant-numeric: var(--tw-ordinal) var(--tw-slashed-zero) var(--tw-numeric-figure)
var(--tw-numeric-spacing) var(--tw-numeric-fraction);
--tw-numeric-spacing: tabular-nums;
--tw-ordinal: ordinal;
--tw-numeric-spacing: tabular-nums;
}
.custom-component {
background: #123456;
Expand Down
40 changes: 28 additions & 12 deletions tests/10-apply.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
border-radius: 0.375rem;
--tw-bg-opacity: 1;
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}
.class-order {
padding: 2rem;
padding-top: 1.75rem;
padding-bottom: 1.75rem;
padding-left: 0.75rem;
padding-right: 0.75rem;
padding-right: 0.25rem;
padding-top: 1.75rem;
padding-bottom: 1.75rem;
padding-top: 1rem;
padding-right: 0.25rem;
}
.with-additional-properties {
font-weight: 500;
Expand Down Expand Up @@ -104,10 +104,10 @@
border-radius: 0.375rem;
--tw-bg-opacity: 1;
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
}
.multiple-variants:hover,
.selectors-variants:hover {
Expand Down Expand Up @@ -149,8 +149,8 @@
--tw-numeric-fraction: var(--tw-empty, /*!*/ /*!*/);
font-variant-numeric: var(--tw-ordinal) var(--tw-slashed-zero) var(--tw-numeric-figure)
var(--tw-numeric-spacing) var(--tw-numeric-fraction);
--tw-numeric-spacing: tabular-nums;
--tw-ordinal: ordinal;
--tw-numeric-spacing: tabular-nums;
--tw-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000),
var(--tw-shadow);
Expand Down Expand Up @@ -187,18 +187,18 @@
}
.btn {
border-radius: 0.25rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1rem;
padding-right: 1rem;
font-weight: 700;
}
.btn-blue {
border-radius: 0.25rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
padding-left: 1rem;
padding-right: 1rem;
font-weight: 700;
--tw-bg-opacity: 1;
background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
Expand Down Expand Up @@ -265,3 +265,19 @@
color: green;
font-weight: 700;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
.foo {
animation: spin 1s linear infinite;
}
@keyframes pulse {
50% {
opacity: 0.5;
}
}
.bar {
animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite !important;
}
2 changes: 2 additions & 0 deletions tests/10-apply.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
<div class="recursive-apply-c"></div>
<div class="use-with-other-properties-base use-with-other-properties-component"></div>
<div class="a b"></div>
<div class="foo"></div>
<div class="bar"></div>
</body>
</html>
8 changes: 8 additions & 0 deletions tests/10-apply.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ test('@apply', () => {
.custom-util {
custom: stuff;
}

.foo {
@apply animate-spin;
}

.bar {
@apply animate-pulse !important;
}
}
`

Expand Down