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

Support @apply for classes outside of a @layer #5378

Merged
merged 3 commits into from
Sep 8, 2021

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Sep 3, 2021

This PR will allow you to apply classes that are not inside a @layer directive. This is an important part of making JIT the default for backwards compatibility reasons. In short:

The following already worked:

@tailwind utilities;

.foo {
  @apply bg-red-500 hover:bg-red-600;
}

This got converted to the following output:

.foo {
  --tw-bg-opacity: 1;
  background-color: rgba(239, 68, 68, var(--tw-bg-opacity));
}
.foo:hover {
   --tw-bg-opacity: 1;
  background-color: rgba(220, 38, 38, var(--tw-bg-opacity));
}

However, it was not possible to do this:

@tailwind utilities;

.foo {
  @apply bg-red-500 hover:bg-red-600;
}

.bar {
  @apply hover:foo;
}

This resulted in the following output:

CssSyntaxError: The `hover:foo` class does not exist. If `hover:foo` is a custom class, make sure it is defined within a `@layer` directive.

But with this PR, it results in the following output:

.foo {
  --tw-bg-opacity: 1;
  background-color: rgba(239, 68, 68, var(--tw-bg-opacity));
}
.foo:hover {
  --tw-bg-opacity: 1;
  background-color: rgba(220, 38, 38, var(--tw-bg-opacity));
}
.bar:hover {
  --tw-bg-opacity: 1;
  background-color: rgba(239, 68, 68, var(--tw-bg-opacity));
}
.bar:hover:hover {
  --tw-bg-opacity: 1;
  background-color: rgba(220, 38, 38, var(--tw-bg-opacity));
}

There is this weird issue that .bar:hover:hover exists, but I will fix that in another PR. (Note: this is an existing issue, and not a newly introduced issue)


Apart from the backwards compatibility aspect, the biggest difference is the behaviour whether you are using the class or not.

@tailwind utilities;

.foo { /* This will _always_ exist in your output, even if you didn't use the "foo" class in your content. */
  
}

@layer components {
  .bar { /* This will _only_ exist in your output if you used the "bar" class in your content. */
  }
}

@RobinMalfait RobinMalfait changed the title support @apply for classes outside of a @layer Support @apply for classes outside of a @layer Sep 3, 2021
RobinMalfait and others added 3 commits September 7, 2021 17:34
The `layer` was not taken into account yet when we resolved the rules
from the applyCache. This is because we set the `classCache` to the
`matches` inside of the `generateRules` function. You can think of them
as "raw" rules I guess. However, it is later in that function that we
apply the `layerOrder` to the `sort`.

This does mean that when you `@apply font-bold text-red-500` that the
rules inside the `.target {}` will be in order of the "normal" css as
well.
@RobinMalfait RobinMalfait force-pushed the apply-classes-outside-layers branch from d5e7c08 to cd0675d Compare September 7, 2021 20:30
}

.bar:hover {
--tw-text-opacity: 1;
color: rgba(16, 185, 129, var(--tw-text-opacity));
color: rgba(34, 197, 94, var(--tw-text-opacity));
Copy link
Member Author

@RobinMalfait RobinMalfait Sep 7, 2021

Choose a reason for hiding this comment

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

The color value looks odd, but that's because of a rebase and the colors in #5384 have been merged since.
The "old" value is text-emerald-500 and the new value is text-green-500

@adamwathan adamwathan merged commit f99302c into master Sep 8, 2021
@adamwathan adamwathan deleted the apply-classes-outside-layers branch September 8, 2021 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants