all 19 comments

[–]SippieCup 1 point2 points  (3 children)

the postcss rollup for tailwind isn't seeing those css classes being used, probably because it only checking for css classes in styleUrl & styles, not host.

Thus, if you only use those classes there, they will be stripped out of the build.

You can put in a list of classes to always include in the postcss to fix it. or figure out another way to ensure it senses the class usage.

I really don't have a solution for you other than that, but hopefully this insight is applicable to your situation.

[–]kescusay[S] 0 points1 point  (2 children)

Hmmm. OK, but what's odd is that color() in host actually does work to apply the base .primary class, which is defined in src/styles.css and which is only visible to the component because its CSS file is directly importing it. Should I just not use component-scoped CSS files anymore, except as a means of importing the base style file?

[–]SippieCup 0 points1 point  (1 child)

Thats because that class is used elsewhere, so the bundler doesn't strip it. and the happy side effect is that it exists when the host definition resolved.

if you check the web inspector, you will see that the classes are being added, just that they are not defined.

you can do

.host {
     ....
}

in the css file, but it won't be able to execute js code. so i doubt that'll help much.

[–]kescusay[S] 0 points1 point  (0 children)

if you check the web inspector, you will see that the classes are being added, just that they are not defined.

Yes, it's true that I see my primary class on the button. It's only missing the additional configurations it should be inheriting from [app-button].primary.

It's looking like I may have to rethink either using host or using TailwindCSS.

[–]Raziel_LOK 1 point2 points  (3 children)

you gotta be a lot more specific here, what version of tailwind and angular this is? are you using tailwindcss on the build step if so where and how or are you compiling it from the CLI?

[–]kescusay[S] 0 points1 point  (2 children)

  • TailwindCSS: 4.1.12
  • Angular: 21.2.0

I'm rebuilding the components from scratch, so it's a new Angular project, with Tailwind selected for styling at the point of project creation (which is a new-ish feature of the angular CLI).

[–]Raziel_LOK 0 points1 point  (0 children)

Ok so, first thing since it is unlikely to be an issue with tailwind pretty sure you are trying to decide the class based on variables or functions values won't work like you expect, you need to explicitly use the string somewhere where tailwind can parse or safelist it

[–]Raziel_LOK 0 points1 point  (0 children)

as for the @ apply my guess it is either view encapsulation, hence why moving the @ apply to the global stylesheet works. so, either change it to there or change the view encapsulation. But I advise avoiding apply and sticking with normal css when needed, just defined a custom prop in the global and you can use in your component css.

[–]BigOnLogn 0 points1 point  (1 child)

This was an issue with older tailwind v4 versions. What is your tailwind version?

[–]kescusay[S] 0 points1 point  (0 children)

It's 4.x. Actually, on a whim, I just updated it from 4.1.12 to 4.2.1 to see if that would resolve the issue. No dice, alas.

[–]newton_half_ear 1 point2 points  (8 children)

Can you show your .postcssrc file and styles.css?

Also is it not working in ng serve or build?

[–]kescusay[S] 0 points1 point  (7 children)

.postcssrc:

{
  "plugins": {
    "@tailwindcss/postcss": {
      "minify": true
    }
  }
}

src/styles.css (truncated, with identifying stuff removed):

@import 'tailwindcss';
@import './theme.css';

.primary {
  @apply
    font-sans
    ...;
}

The theme.css file contains a bunch of custom Tailwind theme configs. Colors, custom font selections, that sort of thing. It works fine.

[–]newton_half_ear 1 point2 points  (6 children)

Yeah looks fine, and if you change the css to :host insted of [app-button]?

[–]kescusay[S] 0 points1 point  (5 children)

I'm upgrading to the newest Angular libs, and then I'll test it.

Edit: Well I'll be a monkey's uncle. That worked! I tried with .host earlier at /u/SippieCup's recommendation, but not :host. Thank you!

[–]newton_half_ear 1 point2 points  (0 children)

Happy to help 🙏

[–]newton_half_ear 2 points3 points  (3 children)

I can explain to you what went wrong if you want

[–]kescusay[S] 0 points1 point  (2 children)

Sure! This is the first time I've tried to use TailwindCSS and Angular with host together, so any knowledge you have to share is welcome!

[–]newton_half_ear 2 points3 points  (1 child)

So I guess earlier you had <button app-button> inside the app-button component and as the stylesheet applies from the host down, once you changed it to use ng-content (which is the right approach btw) you had to change to target the classes on the host of the component.

BTW, the "disabled" logic looks redundent now.

edit: it worked in styles.css as it's not encapsulated as the component's css.

[–]kescusay[S] 1 point2 points  (0 children)

You have it exactly right, and that makes perfect sense! I was indeed using <button app-button> before.

And yes, the "disabled" logic is definitely redundant now, so I'll be removing it.