Skip to content

ViewEncapsulation.Emulated/ViewEncapsulation.Native behave differently on child components #20449

Closed

Description

@SteveVanOpstal

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

In ViewEncapsulation.Native mode: parent styles are applied to the parent and child components.
In ViewEncapsulation.Emulated mode: parent styles are applied only to the parent component.

Expected behavior

As ViewEncapsulation.Emulated is an emulated version of ViewEncapsulation.Native, I expect both modes to have the same behaviour.
So I expect in both modes that the CSS is applied to both parent and child components.

Minimal reproduction of the problem with instructions

The example and code should be self explaining:
http://plnkr.co/edit/DoEdigJs54QRl7d2Qa9c

Environment


Angular version: 5.0.1

But I've seen the issue in earlier versions of Angular too.


Browser:
- [x] Chrome (desktop) version 62.0.3202.62 (Official Build) (64-bit)
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

Activity

  1. frederikprijck commented on Nov 15, 2017

    @frederikprijck
    Contributor

    @SteveVanOpstal: According to this commit: ae28e7a the result is expected and it doesn't look like a bug.

    The docs also mention:

    The styles specified in @component metadata apply only within the template of that component.

    For more info, check: https://angular.io/guide/component-styles#style-scope & https://angular.io/guide/component-styles#view-encapsulation

    If you want to apply them to childs (note: it's deprecated to use, but still works):

    You can explicitly apply the style to inner component by using >>> or /deep/: :host >>> .foo

    See: http://plnkr.co/edit/mtizmMnNx7VpfswFAyi4?p=preview


    Note: I do find it odd that the behavior differs from .native ... @vicb any reason this differs from native ? Or has the native behavior changed since the time this commit was merged? Coz it looks like the docs are also incorrect when you look at them from a Native perspective.

    The styles specified in @component metadata apply only within the template of that component.

    It looks like this is not true for Native viewEncapsulation.

  2. frederikprijck commented on Nov 15, 2017

    @frederikprijck
    Contributor

    Apparently the behavior is only different when u add a emulated directive inside a native one, which probably isn't a supported use case I guess?

  3. SteveVanOpstal commented on Nov 15, 2017

    @SteveVanOpstal
    Author

    I've created a plunker (http://plnkr.co/edit/6x4d8eAZMNo6nmgOzUuB?p=preview) which explicitely states that the child component has the same ViewEncapsulation as their parent. This results in the behaviour that is described in the docs (CSS is not applied to the child).
    However, I expected the child to take on the ViewEncapsulation of the parent by default (which is not the case).

  4. added this to the Backlog milestone on Feb 8, 2018
  5. vicb commented on Feb 9, 2018

    @vicb
    Contributor

    The example and code should be self explaining:

    We have 1800 bugs opened - it really helps to add explanation.

  6. SteveVanOpstal commented on Feb 9, 2018

    @SteveVanOpstal
    Author

    I made the assumption that a child component would use the same ViewEncapsulation of the parent.
    So if you would set the parent to Native that automatically the child will also be Native.
    The docs however https://angular.io/api/core/Component#encapsulation clearly states that Emulated is always the default

  7. SteveVanOpstal commented on Feb 9, 2018

    @SteveVanOpstal
    Author

    The actual issue is that when a parent component has ViewEncapsulation.Native and the child component has ViewEncapsulation.Emulated. The styles of the parent, in the case of the plunker :host p { color: blue; }, will make the child blue. In case both parent and child are Emulated the child will not become blue

    I changed the plunker to be a bit more readable: http://plnkr.co/edit/G4i6T3d03H12fs8d1FVJ?p=info

  8. modified the milestones: Backlog, needsTriage on Feb 26, 2018
  9. added
    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
    and removed on Oct 1, 2020
  10. AndrewKushnir commented on Oct 15, 2020

    @AndrewKushnir
    Contributor

    Closing this ticket since ViewEncapsulation.Native is deprecated and will be removed in v11 (see deprecations guide).

  11. angular-automatic-lock-bot commented on Nov 15, 2020

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

  12. locked and limited conversation to collaborators on Nov 15, 2020
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

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: coreIssues related to the framework runtimecore: CSS encapsulationfreq1: lowtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions