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

Add support for important in v4 #14448

Merged
merged 16 commits into from
Oct 3, 2024
Merged

Add support for important in v4 #14448

merged 16 commits into from
Oct 3, 2024

Conversation

thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Sep 17, 2024

This PR allows modifying utility output by wrapping all utility declarations in a custom selector or marking all utility declarations as !important. This is the v4 equivalent to the important option in the tailwind.config.js file.

Mark all utility declarations as !important

To add !important to all utility declarations, you add an important flag after the @import statement for tailwindcss or tailwindcss/utilities:

/** Importing `tailwindcss` */
@import "tailwindcss" important;

/** Importing the utilities directly */
@import "tailwindcss/utilities" important;

Example utility output:

.mx-auto {
  margin-left: auto !important;
  margin-right: auto !important;
}

.text-center {
  text-align: center !important;
}

This is equivalent to adding important: true to the tailwind.config.js file — which is still supported for backwards compatibility.

Wrap all utility declarations in a custom selector

For backwards compatibility, if you need to nest all utilities in an #app selector you add important: "#app" to your config file:

export default {
  important: "#app",
  // …
}

This does not bring back support for the respectImportant flag in addUtilities / matchUtilities.

@thecrypticace thecrypticace changed the base branch from next to feat/v4-prefix-compat September 23, 2024 22:29
@thecrypticace thecrypticace force-pushed the feat/v4-prefix-compat branch 3 times, most recently from 6ffec7e to ed8a056 Compare September 24, 2024 14:11
@thecrypticace thecrypticace force-pushed the feat/v4-prefix-compat branch 3 times, most recently from a33fd23 to f06bb16 Compare September 24, 2024 18:52
@thecrypticace thecrypticace force-pushed the feat/v4-important-compat branch 2 times, most recently from e59d8a9 to 1971422 Compare September 24, 2024 20:50
@thecrypticace thecrypticace marked this pull request as ready for review September 24, 2024 21:14
Base automatically changed from feat/v4-prefix-compat to next September 25, 2024 14:06
Copy link
Member

@philipp-spiess philipp-spiess left a comment

Choose a reason for hiding this comment

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

This all seems easy enough, just one question inline. Happy to stamp it but I feel like this could benefit from someone else's 👀 too!

packages/tailwindcss/src/design-system.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/design-system.ts Outdated Show resolved Hide resolved
packages/tailwindcss/src/important.test.ts Outdated Show resolved Hide resolved
@thecrypticace
Copy link
Contributor Author

Recording decisions from our call:

  • Eliminate the selector(#app) feature from the utilities import because we can't quite decide on the best naming or implementation for that API. A native CSS version will eventually be available called scope(…) anyway which would be a far better solution for end users.
  • We'll require users to wrap manually @tailwind utilities with a selector
  • We don't care of intellisense does not show the wrapping selector
  • Backwards compat will look for the @tailwind utilities node in the AST and
    wrap it with #app selector

@RobinMalfait RobinMalfait merged commit af15e1b into next Oct 3, 2024
1 check passed
@RobinMalfait RobinMalfait deleted the feat/v4-important-compat branch October 3, 2024 14:25
@benface
Copy link
Contributor

benface commented Oct 7, 2024

@thecrypticace & @RobinMalfait – I'm surprised you decided to keep supporting this "selector prefix" API in v4, given that no amount of specificity can make a rule in a cascade layer "escape" its layer, i.e. no utility in @layer utilities could ever beat a conflicting rule that is unlayered or in a latter layer (except with !important). So is the use case for this simply to "scope" Tailwind to a particular node?

@thecrypticace
Copy link
Contributor Author

@benface Yeah so we ended up doing it only for backwards compatibility purposes to hopefully be less breaking for the projects that are still using this feature. Ideally though users would switch entirely to relying on cascade layers and remove this from their projects.

@benface
Copy link
Contributor

benface commented Oct 7, 2024

Ideally though users would switch entirely to relying on cascade layers and remove this from their projects.

Well, they have to, as there's no way in Tailwind 4 to render utilities outside the utilities layer, right? Nor do I think there should be... but I would love more control over sub-layers, to make some utilities more (or less) "important" than others.

@thecrypticace
Copy link
Contributor Author

thecrypticace commented Oct 7, 2024

You definitely can. You just have to inline the imports. We rely on CSS's native layer features including layer(…) on @import so to drop the layers entirely you can write this:

@import "tailwindcss/theme";
@import "tailwindcss/preflight";
@import "tailwindcss/utiltiies";

To keep the layers you do this (this is almost exactly what is in index.css):

@layer theme, base, components, utilities;

@import 'tailwindcss/theme' layer(theme);
@import 'tailwindcss/preflight' layer(base);
@import 'tailwindcss/utilities' layer(utilities);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants