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

Allow theme(…) options when using @import #16514

Merged
merged 4 commits into from
Feb 20, 2025

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Feb 13, 2025

This PR improves the developer experience when trying to use theme(…) options on an import.

Today, if you want to use Tailwind CSS, you can import it as:

@import "tailwindcss";

But if you want to use any of the theme(…) options, like the static theme option, then you had to use this instead:

@layer theme, base, components, utilities;

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

In this scenario you have to be careful, because the layer(…) must be the first option after an import (according to the spec). So if you use @import 'tailwindcss/theme' theme(static) layer(theme); then that's not going to work either.

This PR solves that by allowing you to use the theme(…) options directly on the @import statement:

@import 'tailwindcss' theme(static);

The only edge case is when you want to use theme(reference). A typical use case is for projects with <style> blocks where you want to reference the CSS variables from the theme.

If you use @import 'tailwindcss' theme(reference);, then all @theme blocks will be references and you can reference theme values. This is good. The bad part is that @import 'tailwindcss'; also includes preflight CSS. This means that we will import the preflight CSS for every <style> block. This is probably not what you want.

The solution is to use @reference 'tailwindcss'; instead which strips all of that information and only gives you access to CSS variables.

This PR also makes sure that if you use theme(reference) on an import that we still throw an error and suggest you use @reference instead. This is not a breaking change because right now if you use @import with theme(…) options it will already throw an error.

Test plan:

  1. Added dedicated tests to make sure we don't throw anymore.
  2. Added test to make sure that we do throw when using theme(reference) on an import.

@RobinMalfait RobinMalfait changed the title Improve DX when importing with theme(…) options Allow theme(…) options when using @import Feb 13, 2025
@RobinMalfait RobinMalfait marked this pull request as ready for review February 13, 2025 18:26
@RobinMalfait RobinMalfait requested a review from a team as a code owner February 13, 2025 18:26
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.

Noice

@RobinMalfait RobinMalfait force-pushed the feat/improve-theme-options-dx branch from 68b5231 to 84ae243 Compare February 20, 2025 15:54
@RobinMalfait RobinMalfait enabled auto-merge (squash) February 20, 2025 15:54
@RobinMalfait RobinMalfait merged commit 3f270d2 into main Feb 20, 2025
5 checks passed
@RobinMalfait RobinMalfait deleted the feat/improve-theme-options-dx branch February 20, 2025 15:57
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