Skip to content

Support providing a space-separated list of classes when using [class] bindings with an array literal. #54605

Closed as not planned

Description

@Profana92

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

core

Is this a regression?

Yes

Description

Host property on Angular Component decorator doesnt apply more than one class. @HostBinding works fine.

Code example:

This works just fine. Every class is an element of an array. even the calculated one is ok.

host: {
'[class]': ['flex', 'max-w-max', orientation === 'row' && !reversed ? 'flex-row': '',],
},

However this doesn't work. Two classes in a string are not added to element class:

host: {
'[class]': ['bg-primary p-10'],
},

When used with @HostBinding works just fine:

@HostBinding('attr.class')
get Classes() {
return ['bg-primary p-10']
}

Same story happens with strings returned by functions:

host: { '[class]': [getOrientationDirection(orientation), getOrientationAlign(orientation), getGap(elementGap)], },

This works, as long as each returns ONLY ONE class, for example:

getOrientationDirection(orientation: AppFlexDirection) {
if (orientation === 'row') {
return 'flex-row';
}
return '';
}

When returning 2 classes it doesnt work. So after extensive testing i came into conclusion, that the implementation of host is not working very well with tailwind, however @HostBinding works just fine, however its now a legacy feature, cant be fully replaced by host at this point.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

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

Angular CLI: 17.0.9
Node: 20.10.0
Package Manager: npm 10.2.3
OS: darwin arm64

Angular: 17.0.8
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic
... platform-server, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1700.9
@angular-devkit/build-angular   17.0.9
@angular-devkit/core            17.0.9
@angular-devkit/schematics      17.0.9
@angular/cdk                    17.0.5
@angular/cli                    17.0.9
@angular/material               17.0.5
@angular/ssr                    17.0.9
@schematics/angular             17.0.9
rxjs                            7.8.1
typescript                      5.2.2
webpack                         5.89.0
zone.js                         0.14.2

Anything else?

No response

Activity

  1. JoostK commented on Feb 26, 2024

    @JoostK
    Member

    This is expected, as there is a difference between attr.class and class; the former is an attribute binding, whereas the second is a class property binding. The property binding applies the individual items in the array as individual classes, crucially without parsing them as they are provided as an array. Class names with spaces are invalid and are therefore not actually applied as class name, as the browser rejects setting them.

  2. pkozlowski-opensource commented on Feb 26, 2024

    @pkozlowski-opensource
    Member

    I guess part of the confusion might be coming from the fact that NgClass does this splitting: https://github.com/angular/angular/blob/main/packages/common/src/directives/ng_class.ts#L184-L190

    Or at least this is a difference between class bindings and NgClass and we should strive to minimize those.

  3. Profana92 commented on Feb 27, 2024

    @Profana92
    Author

    This is expected, as there is a difference between attr.class and class; the former is an attribute binding, whereas the second is a class property binding. The property binding applies the individual items in the array as individual classes, crucially without parsing them as they are provided as an array. Class names with spaces are invalid and are therefore not actually applied as class name, as the browser rejects setting them.

    Not completly sure it that it a attr.class / class issue, as

    @HostBinding('class')
    get Classes() {
    return 'p-icon-small w-full h-full';
    }
    same story as an array:

    @HostBinding('attr.class')
    get Classes() {
    return ['flex bg-primary box-shadow-2xl border-2 border-accent'];
    }

    also works just fine. The difference is, when @HostBinding to attr.class is used together with host property its appending the classes and styles to class list and if @HostBinding to class then it overwrites them. So @HostBinding is recognizing classes in a string and host property doesn't. Keeping in mind, that host property should be a replacement for @HostBinding we can see, that implementation is different and it cannot be a replacement out of the box at it current state.

    According to docs:
    https://angular.dev/guide/components/host-elements#the-hostbinding-and-hostlistener-decorators

    Always prefer using the host property over @HostBinding and @HostListener. These decorators exist exclusively for backwards compatibility.

    If we are supposed to prefer host over @HostBinding shouldn't the function-wise implementation be the same or at least not less complete in some aspects?

  4. JoostK commented on Feb 27, 2024

    @JoostK
    Member

    I am confused, as you're showing three different things: attr.class with an array, class without an array, and class with an array. The non-array form of a class binding parses the provided string into its separate class names, as separated by whitespace. The array form of class expects the individual classes in the array to be valid class names already, so they aren't individually parsed.

  5. Profana92 commented on Feb 27, 2024

    @Profana92
    Author

    I am confused, as you're showing three different things: attr.class with an array, class without an array, and class with an array. The non-array form of a class binding parses the provided string into its separate class names, as separated by whitespace. The array form of class expects the individual classes in the array to be valid class names already, so they aren't individually parsed.

    It seems you're completely correct, I will try to group it to be more understandable and to present my point correctly. The basic problems are difficulties with proper conditional setting of multiple classes with host property:

    This works, strings are getting interpreted properly even if more than one.

    host: {
        '[class]': `'bg-primary p-10'`,
      },
    

    This works as long as returning one class per array index:

    host: {
    '[class]': ['bg-primary', 'p-10'],
    },
    

    this doesn't work, as we try to return more than one:

    host: {
    '[class]': ['bg-primary p-10'],
    },
    

    With attr.class this applies classes without classes definition, so classes are there but without styling with result of class="bg-primary,p-10", so classes separated by comma:

    host: {
    '[attr.class]': ['bg-primary p-10'],
    },
    

    Meaning, there is a difference in implementation of host for class and attr.class, string and array of strings.

    When we need conditional classes, which is my situation, you can also have template literal, but only one condition can be applied then, not suitable for anything more complex.

    host: {
        '[class]': `size==="medium" ? "p-10 bg-primary" : ""`,
      },
    

    or an array of conditions, but then you can return only one class per array index, which makes an array very complex as we need to check same condition for every class we want to add, if we return more than one per index then it breaks.

    host: {
    '[class]': ['flex', 'max-w-max', 
            orientation === 'row' && !reversed ? 'flex-row': '',
            orientation === 'row' && !reversed ? 'justify-center': '',
            orientation === 'row' && !reversed ? 'items-center': '',],
    },
    

    There is also a @HostBinding decorator, which actually also struggles to work with extensive testing, it also works as long as returs one item, it can return a string of classes, but only one string of classes, if more it just applies the last array item, both attr.class and class act similar in this manner:

      @HostBinding('attr.class')
      get Classes() {
        return ['bg-primary border-2 border-accent'];
      }
    
    @HostBinding('class')
      get Classes() {
           return ['bg-primary border-2 border-accent'];
      }
    

    but when more than one item in array, then only last gets applied:

      @HostBinding('class')
      get Classes() {
        return ['bg-primary border-2 border-accent', 'border-dotted'];
      }
    

    All of the problems doesn't exist on ngClass, as we can do basically every of the above:

    [ngClass]="['bg-primary border-2', 'border-accent', size==='medium' ? 'p-5 m-20':'']"
    

    This applies all the classes, string of multiple classes, multiple array strings, conditional strings of one or more classes and they all work, which is actually best implementation.

    In conclusion, host and @HostBinding doesn't seem to have the same implementation and are confusing in relation with ngClass, as i struggle to find a good way to apply multiple class to host element conditionally with both of those solutions. The same rules that apply for ngClass doesn't apply to host property and to @HostBinding. I think, that ngClass and host implementation of those things should be a bit more similar (ngClass is working perfectly and @HostBinding is legacy), because as for now it is confusing and doesn't really work well with tailwind and i struggle to find a good solution for conditional class implementation in host property. There is always an option to throw a div around everything in html file, but thats always an extra div, that doesn't present enough value.

  6. added
    area: core
    on Feb 27, 2024
  7. added this to the needsTriage milestone on Feb 27, 2024
  8. pkozlowski-opensource commented on Feb 28, 2024

    @pkozlowski-opensource
    Member

    Relevant: #40623

  9. pkozlowski-opensource commented on Feb 28, 2024

    @pkozlowski-opensource
    Member

    @Profana92 the points you are rising are well understood and boil down to 2 points:

    • need for providing a space-separated list of classes when using [class] bindings with an array literal;
    • aligning behaviour of the style-specific [class] binding with what is possible with ngClass

    I've changed the title of this issue to better reflect the crux of the issue.

    The second point is discussed in #40623.

  10. changed the title [-]Bug: Angular Host property not processing tailwind classes properly. Works fine with @HostBinding. [/-] [+]Support providing a space-separated list of classes when using [class] bindings with an array literal.[/+] on Feb 28, 2024
  11. added
    feature
    on Feb 28, 2024
  12. modified the milestones: needsTriage, Backlog on Feb 28, 2024
  13. pkozlowski-opensource commented on Feb 28, 2024

    @pkozlowski-opensource
    Member

    Note: the support for space-separated list of classes was not initially added in the [class] bindings due to the performance considerations.

  14. pkozlowski-opensource commented on Feb 28, 2024

    @pkozlowski-opensource
    Member

    Discussed this during a team meeting and decided against supporting this feature due to its performance cost (increased code size of the framework and runtime processing cost).

  15. angular-automatic-lock-bot commented on Mar 30, 2024

    @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.

  16. locked and limited conversation to collaborators on Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: host and host bindingscore: styling bindingsfeature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions