components icon indicating copy to clipboard operation
components copied to clipboard

Make content projection selectors more lenient

Open SvenBudak opened this issue 7 years ago • 11 comments

For example i tryed to pass a own component as mat-card-avatar into the <mat-card-header>:

<mat-card-header>
    <app-image class="mat-card-avatar"></app-image>
    <mat-card-title>Name</mat-card-title>
    <mat-card-subtitle>Subtitle</mat-card-subtitle>
</mat-card-header>

But this dont work, because <mat-card-header> allow only his own sub components on correct place: https://github.com/angular/components/blob/master/src/material/card/card-header.html

<ng-content select="[mat-card-avatar], [matCardAvatar]"></ng-content>
<div class="mat-card-header-text">
  <ng-content
      select="mat-card-title, mat-card-subtitle,
      [mat-card-title], [mat-card-subtitle],
      [matCardTitle], [matCardSubtitle]"></ng-content>
</div>
<ng-content></ng-content>

Please add [.mat-card-avatar] as class to line 1. Would be good if this is possible in every material component.

The current solution looks like:

<mat-card-header>
    <div mat-card-avatar>
        <app-image class="mat-card-avatar"></app-image>
    </div>
    <mat-card-title>Name</mat-card-title>
    <mat-card-subtitle>Subtitle</mat-card-subtitle>
</mat-card-header>

SvenBudak avatar May 13 '19 20:05 SvenBudak

This can be achieved using ngProjectAs.

Airblader avatar May 14 '19 19:05 Airblader

@Airblader Thanks for this, but it dont really works in this case. Its the first time i hear about this feature. but after testing i see not any change. (Angular 7 ofc)

Sure that this is not depricated? My IDE also dont give me an autocompletation named ngProjectAs

SvenBudak avatar May 14 '19 19:05 SvenBudak

@SvenBudak You can refer to the following StackBlitz example to see how you can use ngProjectAs for your case. The feature is not really well documented on the Angular wiki, but it basically allows a part of your template (i.e. app-image) to project itself as whatever you specify (i.e. ngProjectAs="[mat-card-avatar]", meaning it will get caught by all select="[mat-card-avatar]"), so it can fulfill ng-content's [select] requirements.

ViktorSlavov avatar May 20 '19 11:05 ViktorSlavov

@ViktorSlavov Thanks for this cool demonstration. but the styling is not working in this way. :) mat-card-avatar is 40x40 and have border-radius: 50%

SvenBudak avatar May 21 '19 16:05 SvenBudak

@SvenBudak I had some styles set in my custom component's css. They'll take precedence over the styles applied by the .mat-card-avatar class. If you leave only the display: block styling under :host, the app-image component will fallback to the styles provided by .mat-card-avatar

ViktorSlavov avatar May 22 '19 06:05 ViktorSlavov

I know that I can do this. But it feels like a dirty hack. What is if material style guide change soon to non border radius avatars? Then I need to change all manually...

SvenBudak avatar May 22 '19 06:05 SvenBudak

@SvenBudak , I'm not sure I phrased it correctly - I meant the opposite - you can remove all of the styles for :host in the app-image.css and it will fallback to the styles provided by material for .mat-card-avatar - here is an updated example.

ViktorSlavov avatar May 22 '19 11:05 ViktorSlavov

But why not simply add in material elements a selector for class variant? this is much more easy for usage...

SvenBudak avatar May 24 '19 14:05 SvenBudak

@SvenBudak You can refer to the following StackBlitz example to see how you can use ngProjectAs for your case. The feature is not really well documented on the Angular wiki, but it basically allows a part of your template (i.e. app-image) to project itself as whatever you specify (i.e. ngProjectAs="[mat-card-avatar]", meaning it will get caught by all select="[mat-card-avatar]"), so it can fulfill ng-content's [select] requirements.

Super helpful. Thx

ntziolis avatar Aug 19 '19 20:08 ntziolis

Closing this since I don't see us adding this type of accommodation since our components are generally designed to work with a certain structure, and since you can accomplish this already via ngProjectAs (the framework should be adding docs for this in Q1 2021).

jelbourn avatar Jan 12 '21 23:01 jelbourn

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.