1

I have a component called app-list-container, the structure of which is as follows:

HTML

<ul [attr.styleName]="styleName" [style.flexGrow]="flexGrow">
    <!-- ITEM <li> -->
    <ng-content></ng-content>
</ul>

SCSS

@import 'placeholders/scrollbar';

ul {
    &[styleName='CHAT_MESSAGE'],
    &[styleName='CHAT_LAST_MESSAGE'] {
        @extend %scrollbar-y;
    }
}

With this I generate the items (<li>) inside this component ... Now I don't understand why it is not applying this css rule.

The rule is only applied when I insert it in his father (app-list-container), for me it does not make sense to also insert the style through the father.

Notice in the image below that the css is being applied to the html element, but if it is applied to the <ul> it doesn't work, but it works if it is applied to the parent (<app-list-containr>):

enter image description here

What to do? (Note: I like to do and know the CORRECT mode).

3
  • [attr.styleName]="styleName" shouldn't it be [attr.styleName]="CHAT_MESSAGE" ? (Unless styleName is a variable that contains CHAT_MESSAGE). Also be sure in your scss file there is no space between & and [stylename=CHAT_MESSAGE] but it seems to be ok in the code you gave us. If everything is OK then please provide a repro because it is working fine on mystackblitz stackblitz.com/edit/… Commented May 30, 2020 at 23:47
  • This way you did it works, but that's where my question is ... the <ul> is encapsulated in a component called <app-list-container>, correct? So when I call <app-list-container> on another component it doesn't apply styling to <ul>, only if I inserted the styling directly into <app-list-container> Commented May 30, 2020 at 23:55
  • I think it has something to do with @HostBinding, but I didn't understand how to do this data-binding that I want with it. Commented May 31, 2020 at 0:16

1 Answer 1

2

For those who want to know, I did it as follows:

TS:

export class ListContainerComponent implements OnInit {
    @HostBinding('attr.styleName') hostStyleName: IListStyleName;
    @Input() styleName: IListStyleName;
    @Input() flexGrow = ICssFlexGrow.INITIAL;

    constructor() {}

    ngOnInit(): void {
        this.checkInput();
        this.setHostStyleName(this.styleName);
    }

    protected checkInput(): void {
        if (!IListStyleName[this.styleName.toUpperCase()]) {
            throw new Error(`The inserted style: "${this.styleName}" does not exist in this component!`);
        }

        if (!ICssFlexGrow[this.flexGrow.toUpperCase()]) {
            throw new Error(`The inserted flex-grown: "${this.flexGrow}" does not exist in this component!`);
        }
    }

    protected setHostStyleName(styleName: IListStyleName): void {
        this.hostStyleName = styleName;
    }
}

SCSS:

:host {
    &[styleName='CHAT_MESSAGE'],
    &[styleName='CHAT_LAST_MESSAGE'] {
        @extend %scrollbar-y;
    }
}
Sign up to request clarification or add additional context in comments.

Comments