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

Apply part 2 #7

Merged
merged 5 commits into from
Mar 5, 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: 20 additions & 2 deletions src/index.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
}
.apply-components {
width: 100%;
margin-left: auto;
margin-right: auto;
}
@media (min-width: 640px) {
.apply-components {
Expand All @@ -60,6 +58,10 @@
max-width: 1536px;
}
}
.apply-components {
margin-left: auto;
margin-right: auto;
}
.drop-empty-rules:hover {
font-weight: 700;
}
Expand Down Expand Up @@ -99,6 +101,22 @@
font-weight: 400;
}
}
.apply-order-a {
margin: 1.25rem;
margin-top: 1.5rem;
}
.apply-order-b {
margin: 1.25rem;
margin-top: 1.5rem;
}
.dark .dark\:group:hover .apply-dark-group-example-a {
--tw-bg-opacity: 1;
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
}
.group:hover .group-hover\:dark .apply-dark-group-example-b {
--tw-bg-opacity: 1;
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
}
Comment on lines +112 to +119
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct. I think it should be something like this:

Suggested change
.dark .dark\:group:hover .apply-dark-group-example-a {
--tw-bg-opacity: 1;
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
}
.group:hover .group-hover\:dark .apply-dark-group-example-b {
--tw-bg-opacity: 1;
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
}
.dark .group:hover .apply-dark-group-example-a {
--tw-bg-opacity: 1;
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
}
.dark .group:hover .apply-dark-group-example-b {
--tw-bg-opacity: 1;
background-color: rgba(16, 185, 129, var(--tw-bg-opacity));
}

@media (min-width: 640px) {
@media (prefers-reduced-motion: no-preference) {
.group:active .crazy-example:focus {
Expand Down
12 changes: 12 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ test('it works', () => {
@apply font-bold hover:font-normal;
}
}
.apply-order-a {
@apply m-5 mt-6;
}
.apply-order-b {
@apply mt-6 m-5;
}
.apply-dark-group-example-a {
@apply dark:group-hover:bg-green-500;
}
.apply-dark-group-example-b {
@apply group-hover:dark:bg-green-500;
}
.crazy-example {
@apply sm:motion-safe:group-active:focus:opacity-10;
}
Expand Down
11 changes: 9 additions & 2 deletions src/lib/collapseAdjacentRules.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
let comparisonMap = {
atrule: 'params',
rule: 'selector',
}
let types = new Set(Object.keys(comparisonMap))

function collapseAdjacentRules(context) {
return (root) => {
let currentRule = null
root.each((node) => {
if (node.type !== 'atrule') {
if (!types.has(node.type)) {
currentRule = null
return
}
Expand All @@ -12,7 +18,8 @@ function collapseAdjacentRules(context) {
return
}

if (node.params === currentRule.params) {
let property = comparisonMap[node.type]
if (node[property] === currentRule[property]) {
currentRule.append(node.nodes)
node.remove()
} else {
Expand Down
43 changes: 12 additions & 31 deletions src/lib/expandApplyAtRules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const postcss = require('postcss')
const generateRules = require('./generateRules')
const { bigSign, toPostCssNode, isPlainObject } = require('./utils')
const escape = require('tailwindcss/lib/util/escapeClassName').default
Expand Down Expand Up @@ -68,40 +67,22 @@ function expandApplyAtRules(context) {
}

let rules = context.classCache.get(applyCandidate)
for (let [{ sort, layer }, [selector, rule]] of rules) {
// Nested rules...
if (!isPlainObject(rule)) {
siblings.push([
{ sort, layer },
toPostCssNode(
[selector, updateSelectors(rule, apply, applyCandidate)],
context.postCssNodeCache
),
])
} else {
let appliedSelector = replaceSelector(apply.parent.selector, selector, applyCandidate)

if (appliedSelector !== apply.parent.selector) {
siblings.push([
{ sort, layer },
toPostCssNode([appliedSelector, rule], context.postCssNodeCache),
])
continue
}

// Add declarations directly
for (let property in rule) {
apply.before(postcss.decl({ prop: property, value: rule[property] }))
}
}
for (let [meta, [selector, rule]] of rules) {
siblings.push([
meta,
toPostCssNode(
!isPlainObject(rule)
? [selector, updateSelectors(rule, apply, applyCandidate)]
: [replaceSelector(apply.parent.selector, selector, applyCandidate), rule],
context.postCssNodeCache
),
])
}
}

// Inject the rules, sorted, correctly
for (let [{ sort }, sibling] of siblings.sort(([{ sort: a }], [{ sort: z }]) =>
bigSign(z - a)
)) {
// `apply.parent` is refering to the node at `.abc` in: .abc { @apply mt-2 }
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)
}

Expand Down