Skip to content

Simpler support for responsive components and style layers #2031

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

Merged
merged 11 commits into from
Jul 15, 2020

Conversation

adamwathan
Copy link
Member

This PR is an alternate approach to what was presented in #2025.

This PR adds first-class support for variants for "component" styles. It differs from #2025 in the following ways:

Component and utility responsive styles are intermingled

Rather than inserting all responsive component styles, then all responsive utility styles, this PR groups them together by screen size.

So the general order in the CSS is this:

  • Base component styles
  • Base utility styles
  • sm component styles
  • sm utility styles
  • md component styles
  • md utility styles
  • lg component styles
  • lg utility styles
  • xl component styles
  • xl utility styles

This leads to more intuitive results when combining components and utilities at various breakpoints.

No @screens components/utilities directives

Because of the change explained above, there is no need to introduce @screens components/utilities. Instead, we are keeping @tailwind screens, since all responsive styles are now zipped together per screen size, no matter what layer (components/utilities) they belong to.

No @responsive components { ... } syntax

The argument for the @responsive at-rule has been removed. Now any responsive variants generated using the addComponents API are just automatically categorized as components, so this doesn't need to be done explicitly.

No support for children in @tailwind directives

Support for things like this have been removed:

@tailwind components {
  .btn { ... }
}

While I think this is still a useful idea at a high level, this implementation had the problem of not allowing you to split your styles into multiple files, because this is invalid syntax using postcss-import:

@tailwind components {
  @import './components/button';
}

New @layer directive has been added

A new @layer directive has been introduced as a better solution to nesting children within @tailwind components { ... }.

It looks like this:

@layer components {
  .btn { ... }
}

This lets you "bucket" your custom styles into the right "layer" (term comes from ITCSS) so Tailwind can mark them appropriately and purge them properly based on your purge mode configuration.

You can nest @variants and @responsive directives within @layer directives to make sure all generated variants are also categorized correctly:

@layer components {
  @responsive {
    .prose { ... }
  }
}

I probably won't document this for a while because it's more of an implementation detail than a feature, but this is how you'd properly bucket things in custom CSS if you aren't using the plugin system instead.

Risks

I don't think there's any real issues here — there were some tricky internal tweaks necessary to get everything working but all of the tests are passing so things seem good. The closest thing to an alarm-sounding issue I ran into was that for a minute @apply stopped working because every generated Tailwind style was nested under a @layer, and @apply doesn't support applying classes that are nested under another rule. Flattening the layers to control comments before substituting @apply at-rules was enough to fix it, but does make me slightly nervous there could be other similar types of issues that crop up.

Nevertheless I feel very confident in our test suite, so since everything is passing I would be very surprised if we ran into an issue that wasn't solvable.

This fixes an important issue in the Tailwind Typography plugin so I'd like to get this merged and released as v1.5 (sadly a waste of a good version number) today pending any problems anyone points out 👍

@adamwathan adamwathan merged commit 9310efb into master Jul 15, 2020
@adamwathan adamwathan deleted the simple-component-variants branch July 15, 2020 16:51
thecrypticace added a commit that referenced this pull request Jun 5, 2025
Here is everything you need to know about this upgrade. Please take a
good look at what changed and the test results before merging this pull
request.

### What changed?




#### ✳️ postcss (8.5.3 → 8.5.4) ·
[Repo](https://github.com/postcss/postcss) ·
[Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)



<details>
<summary>Release Notes</summary>
<h4><a
href="https://github.com/postcss/postcss/releases/tag/8.5.4">8.5.4</a></h4>

<blockquote><ul dir="auto">
<li>Fixed Parcel compatibility issue (by <a
href="https://bounce.depfu.com/github.com/git-sumitchaudhary">@git-sumitchaudhary</a>).</li>
</ul></blockquote>
<p><em>Does any of this look wrong? <a
href="https://depfu.com/packages/npm/postcss/feedback">Please let us
know.</a></em></p>
</details>

<details>
<summary>Commits</summary>
<p><a
href="https://github.com/postcss/postcss/compare/22c309d32924e1eeb33c80a6a50b7ba8a43a1832...6cb4a6673fb6d8b23eb1ebe66a22b6267ab141de">See
the full diff on Github</a>. The new version differs by 23 commits:</p>
<ul>
<li><a
href="https://github.com/postcss/postcss/commit/6cb4a6673fb6d8b23eb1ebe66a22b6267ab141de"><code>Release
8.5.4 version</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/ec5c1e031083664bed1cf91eaac72f8c61068110"><code>Update
dependencies</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/e85e9385c87499bc7e274c6ce332cf59e3988994"><code>Fix
code format</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/50542335f22e513edaa52cb8bd214370ceef1bc9"><code>fixed
error at line 401 (#2046)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/0538b6309e5ca80044bc8ca34833db422bbaa9fe"><code>docs:
Update README.md (#2044)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/b5f407f3976419310bcc530043f5835b7192f41c"><code>Add
`postcss-fontsource-url` to plugins (#2043)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/94b5260be6fd43f230cc4dd91a7d3c51b09a0e94"><code>Clarify
documentation for `node.source.end.offset` (#2032)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/a20724af14ad05304f99ced858f6f2f47f4a1633"><code>Fix
Markdown syntax</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/cf6b9697519aa0a9feda3b3468a738ba8870b61b"><code>fix:
add a mention for postcssense extension (#2040)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/ff48c29ebb16339188761827701e832f516bfb7d"><code>Increase
size</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/f82c2a60906ee889be26fe8e7e37d90fca23224e"><code>Add
`Input#fromLineAndColumn`, `CssSyntaxError.input.offset` and
`CssSyntaxError.input.endOffset` (#2034)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/7892a29ff8492f11cf2ce07f9aa17def26f96338"><code>Fix
missing offset in `Node#positionInside`, `Node#positionBy` and
`Node#rangeBy` (#2033)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/875f1fb7f863a859d16261b0a24a5823e7a6c40d"><code>Fix
`opts` argument of `Node#rangeBy()` and `Node#positionBy()` cannot be
omitted (#2031)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/25443a59303678426e521fdb434bf95f4925ca14"><code>Update
dependencies</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/0a76df5f60511cb577374f869942e5a52d976666"><code>add
postcss-auto-var-fallback fallback plugin to plugins.md
(#2026)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/3fc6388e91d496105f5a8fe63ab22a9a5a3c5254"><code>Update
Nano ID</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/368bd0092000f01f6e873d9f55ef3c93a9149217"><code>Change
in type definition of property `css` of `result` (#2025)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/9229218b515eaffcd6f5c6bacdee766f27262ebc"><code>Update
dependencies</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/6c2a808c4ac483317be2a906b5ee6d168a16568c"><code>Clean
up code</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/31a5de71d12f16aa31092c86106cd6f181bd1250"><code>Return
type of method `raw` of `Stringifier` can be boolean, as the property
`semicolon` can be boolean (#2024)</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/c5058912f2659f2c81ec7ccf04408fd4f25e4fc0"><code>There
are no other `props` with non-string type.</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/a9cf39e7fe864e87d91d56511fd594a0dc0fbdf5"><code>The
return type of the method rawValue in Stringifier should be `any`
instead of `string`</code></a></li>
<li><a
href="https://github.com/postcss/postcss/commit/7ac43db0c30c08dc799a55e5f2f1f269a61b6856"><code>Update
ESLint config</code></a></li>
</ul>
</details>












---
![Depfu
Status](https://depfu.com/badges/edd6acd35d74c8d41cbb540c30442adf/stats.svg)

[Depfu](https://depfu.com) will automatically keep this PR
conflict-free, as long as you don't add any commits to this branch
yourself. You can also trigger a rebase manually by commenting with
`@depfu rebase`.

<details><summary>All Depfu comment commands</summary>
<blockquote><dl>
<dt>@​depfu rebase</dt><dd>Rebases against your default branch and
redoes this update</dd>
<dt>@​depfu recreate</dt><dd>Recreates this PR, overwriting any edits
that you've made to it</dd>
<dt>@​depfu merge</dt><dd>Merges this PR once your tests are passing and
conflicts are resolved</dd>
<dt>@​depfu cancel merge</dt><dd>Cancels automatic merging of this
PR</dd>
<dt>@​depfu close</dt><dd>Closes this PR and deletes the branch</dd>
<dt>@​depfu reopen</dt><dd>Restores the branch and reopens this PR (if
it's closed)</dd>
<dt>@​depfu pause</dt><dd>Ignores all future updates for this dependency
and closes this PR</dd>
<dt>@​depfu pause [minor|major]</dt><dd>Ignores all future minor/major
updates for this dependency and closes this PR</dd>
<dt>@​depfu resume</dt><dd>Future versions of this dependency will
create PRs again (leaves this PR as is)</dd>
</dl></blockquote>
</details>

Co-authored-by: depfu[bot] <23717796+depfu[bot]@users.noreply.github.com>
Co-authored-by: Jordan Pittman <[email protected]>
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.

1 participant