Skip to content

using special tailwind class names with recommended [class] bindings #61671

Closed

Description

@imaksp

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

Hello, new style guide recommends class binding over ngClass but it seems it works only for simple class names.
this is example from doc:

<!-- PREFER -->
<div [class.admin]="isAdmin" [class.dense]="density === 'high'">
<!-- AVOID -->
<div [ngClass]="{admin: isAdmin, dense: density === 'high'}">

but if we try to use class names like text-primary/80, data-[size='large']:p-8 it gives syntax error.

<!-- Does not work -->
<div [class.text-primary/80]="isAdmin" [class.data-[size='large']:p-8]="density === 'high'">
<!--  Works -->
<span [class]="{ 'text-green-500/80': true }"> tag </span>

I think style guide should be updated with more examples,
also we could use object like syntax with class binding , but is it recommended?

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)


Anything else?

No response

Activity

  1. e-oz commented on May 24, 2025

    @e-oz

    You can use
    div class="{{ dense() ? 'any classes you-want' : '' }}"

  2. e-oz commented on May 25, 2025

    @e-oz
  3. imaksp commented on May 26, 2025

    @imaksp
    ContributorAuthor

    Also about the object usage: https://stackblitz.com/edit/stackblitz-starters-12ekjcer?file=src%2Fmain.ts

    Ok, so it seems object syntax is not very useful in such cases.
    and if we use like this div class="{{ dense() ? 'any classes you-want' : '' }}" then we cannot add other classes which are common as multiple class attributes will override each other.

    as a workaround we can add conditional data attribute and set single class normally with tailwind `data-* as show below:

    <span
      [attr.data-active]="isActive() ? '' : null"
      class="p-2 text-sm text-gray-200/80 data-active:text-green-300/80"
    >
      Label
    </span>

    this works perfectly but in this case Angular won't be adding any classes conditionally.

  4. e-oz commented on May 26, 2025

    @e-oz

    then we cannot add other classes

    there is no such limitation

  5. added
    area: compiler
    on May 26, 2025
  6. modified the milestone: needsTriage on May 26, 2025
  7. JeanMeche commented on May 28, 2025

    @JeanMeche
    Member

    We discuss this a bit with the team.

    The binding syntax with complex classnames are a bit hard to read.

    [class.data-[size='large']:p-8]="density === 'high'

    For now we would recommend using the binding of objects.

    In an external template file

    <div [class]="{`data-[size='large']:p-8`: true}"></div>

    It's a bit more tricky in inline template because this requires additional escaping:

    @Component({
      template: `<div [class]="{'data-[size=\\\'large\\\']:p-8': true}"></div>`,
    })

    As we have an acceptable alternative today, we'll continue accepting feedback to this issue and we'll think more about it in the future.

  8. imaksp commented on May 29, 2025

    @imaksp
    ContributorAuthor

    The binding syntax with complex classnames are a bit hard to read.

    Hi, I could agree with this for complex classes like data-[size='large']:p-8 , but as a 1st step consider supporting simpler classes having / character, as classes like text-white/80 , text-xl/relaxed are very common, also it would be great if it could work in same way for SFC too. Thanks.

  9. anthony-b-dev commented on Jun 21, 2025

    @anthony-b-dev

    why is there a limitation on class names? Shouldn't this just be a simple string application?

    I'm currently trying to conditionally add a whole host of tailwind classes and they're all getting stripped out when trying to use this syntax.

            <div
              class="relative px-3 py-2 hover:bg-gray-100 active:inset-shadow-sm active:inset-shadow-gray-200"
              [class]="{
                'after:bg-orange-500 after:absolute after:bottom-1 after:left-5/100 after:h-0.75 after:w-9/10':
                  store.view(),
              }"
            >
              {{ menu }}
            </div>
  10. JeanMeche commented on Jun 21, 2025

    @JeanMeche
    Member

    This is by design, the underlying APIs don't support adding multiple class in a single string (classList.add throws).
    Also Angular doesn't want to have to add the cost of handling that implicitly on each and every class.

  11. anthony-b-dev commented on Jun 21, 2025

    @anthony-b-dev

    how does ngClass do it? I would've thought that the same functionality would have ported over.
    Should I multiple [class]={'className: store.view()'} to the div or stay with ngClass?

  12. drakedeatonuk commented on Jun 21, 2025

    @drakedeatonuk
    <div [class]="{`data-[size='large']:p-8`: true}"></div>
    

    This isn't really an effective solution if an element has many static classes and only a few dynamic classes. In such cases it's still easier to just keep all an element's static classes in class and the few dynamic classes in ngClass.

    Aesthetically Ii like the new recommended class bindings approach but until it's a more comprehensive I think I'll still be falling back to good ol' ngClass.

  13. added a commit that references this issue on Jul 22, 2025
    e70a592
  14. self-assigned this
    on Jul 22, 2025
  15. added a commit that references this issue on Jul 23, 2025
    7767aa6
  16. angular-automatic-lock-bot commented on Aug 23, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  17. locked and limited conversation to collaborators on Aug 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilercompiler: parsercore: styling bindingsstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions