components icon indicating copy to clipboard operation
components copied to clipboard

md-input-container: shadow dom attribute is not set for placeholder <label>

Open MA-Maddin opened this issue 9 years ago • 8 comments

Bug, feature request, or proposal:

Bug

What is the expected behavior?

>> Note: I want to change the style of <input md-input>'s auto generated <label> only within the current component's template. It is working for every auto generated dom part of md-input, but not for its <label>/placeholder

I was trying to change the color of <input md-input>'s <label> with the following CSS:

// example code. see plnkr.
label.md-input-placeholder.md-focused {
    color: red;
}
.md-input-placeholder {
    color: white;
}

the problem is, that attribute selectors for shadow dom attributes (if I'm right with their name) like [_ngcontent-qny-41] will be appended to the styles but not to the <label> dom element, so the styling takes no effect.

It works if this would be the output: <label _ngcontent-qny-41="" class="md-input-placeholder md-empty md-float" for="md-input-0">Username</label>

What is the current behavior?

Shadow dom attribute is missing in <input md-input>'s generated <label> element (set via placeholder attribute).

Current generated output:

<md-input-container _ngcontent-qny-41="">
    <~~~>
        <input _ngcontent-qny-41="" class="md-input-element" md-input="" placeholder="Username" value="" ng-reflect-placeholder="Username" ng-reflect-id="md-input-0" id="md-input-0">
        <label class="md-input-placeholder md-empty md-float" for="md-input-0">Username</label>
    </~~~>
</md-input-container>

What are the steps to reproduce?

Plunker: http://plnkr.co/edit/7hut1LJlZtByTTuDFwSO?p=preview

What is the use-case or motivation for changing an existing behavior?

Customization

Which versions of Angular, Material, OS, browsers are affected?

Using Angular Material (SHA 592f33f) from 1/20/2017, 10:40 PM

Is there anything else we should know?

If there is a possibility to have a globally applied theme and defining another theme for a specific component which is used within that global them scope, then this way of styiling md controls wouldn't be necessary at all... (but I haven't found smth like that).

If someone has a workaround etc. let me know. Thank you in advance! :)

MA-Maddin avatar Jan 21 '17 16:01 MA-Maddin

that's the whole point of encapsulation.. styles don't leak out of the component scope.. you can get around it by using /deep/ http://plnkr.co/edit/vUMxXQYZScr8iVIvmPbX?p=preview

fxck avatar Jan 21 '17 19:01 fxck

The styling is defined in the component's stylesheet. The md controls are located in the component's template file.

Where does the style leak out of the component? It should NOT affect other components or the global style, what will be the case by prepending /deep/.

I am able to style the input (.md-input-element) through the component's stylesheet without problems, because the input is getting its _ngcontent-qny-41 attribute.

By adding the _ngcontent-qny-41 attribute manually via Chrome content explorer, the style will be applied successfully. And since every element that is automatically generated by md-input is getting that attribute, the <label> should get it as well.

Maybe this is an issue of one of the angular compilers and not md2 itself... I don't know.


As a workaround I have set the encapsulation meta property of the Component to ViewEncapsulation.Native now.

MA-Maddin avatar Jan 21 '17 20:01 MA-Maddin

Only the <input> element will get the _ngcontent-qny-41 attribute, because that element has been projected into the MdInputContainer component.

See the MdInputContainer template: here

The remaining elements in the MdInputContainer template will not receive any of the _ngcontent-qny-X attributes, because for all Material components the ViewEncapsulation is set to None.

This is per design, because it makes it easier for developers to overwrite specific styles of the Material components.

There are three options to overwrite the styles of the MdInputContainer.

  • Disable ViewEncapsulation on your component.
  • Use the /deep/ selector to target the child MdInputContainer component
  • Place the CSS outside of Angular to target the MdInputContainer everywhere.

devversion avatar Jan 22 '17 14:01 devversion

Thank you for the clarification. That makes sense to me now, somehow. :wink:

In my case there are actually these options to overwrite the style only within current component's scope:

  • ViewEncapsulation.Native on component.
  • :host /deep/ .md-input-placeholder (:host /deep/)
  • ViewEncapsulation.None + .my-target .md-input-placeholder

So this is more an issue of angular's emulated encapsulation...

MA-Maddin avatar Jan 23 '17 13:01 MA-Maddin

Yes, this is the way that view encapsulation works.

jelbourn avatar Jan 25 '17 00:01 jelbourn

https://github.com/angular/angular/issues/12215 is this the issue that is causing this?

MA-Maddin avatar Jan 25 '17 12:01 MA-Maddin

@jelbourn but from what @MA-Maddin mentioned in the 3 bullet points

2nd one - It is mentioned on angular.io that the shadow piercing descendant combinators i.e. /deep/ or ::ng-deep or >>> will be deprecated in the future angular releases. read more: https://angular.io/guide/component-styles#!#-deep-

3rd one - ViewEncapsulation.None would allow the generic styles specified on the elements like label/div etc or the classes like .mat-input-placeholder to propagate to the parent/child components of this component and mess up the styling there. wouldn't that defeat the whole purpose of using ViewEncapsulation.Emulated ?

please correct me if, I'm wrong.

rgantla avatar Jan 03 '18 23:01 rgantla

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.