Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Won't fix] Investigate styling components rendered using cxComponentWrapper #2084

Closed
LTiger14 opened this issue Apr 18, 2019 · 4 comments
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@LTiger14
Copy link

LTiger14 commented Apr 18, 2019

Following a discussion on the Spartacus Slack, it was noted that styling components dynamically rendered using cxComponentWrapper was causing issues.

Steps to Reproduce

in test-parent.component.html

<div class="container">
  <div class="test">
    <div class="column" *ngFor="let column of (categories.component | columnGroup: 3)">
      <ng-container *ngFor="let category of column"
        [cxComponentWrapper]="{
          uid: category.uid,
          flexType: category.typeCode
        }"
      ></ng-container>
    </div>
  </div>
</div>

In the code example above cxComponentWrapper renders an element of type test-child.

Within test-parent.component.scss, these elements should be styleable using:

test-child {
  width: 100%;
  margin-bottom: 27px;
}

Observed Results

The styling is not applied to the test-child component

Expected Results

The styling should be applied

Possible reason

This could be caused by the way Angular builds the components and applies the styles in their emulated view encapsulation.

For more info https://spartacus-storefront.slack.com/archives/CD16V16FR/p1555588176034300

@LTiger14 LTiger14 added the bug Something isn't working label Apr 18, 2019
@Xymmer Xymmer added this to the TBD milestone Apr 25, 2019
@Xymmer Xymmer changed the title Investigate styling components rendered using cxComponentWrapper [spike needed] Investigate styling components rendered using cxComponentWrapper Apr 25, 2019
@Xymmer
Copy link
Contributor

Xymmer commented Apr 25, 2019

Louis says: I would like to have some time to investigate before beta. Wouldn’t be very long to try it out maybe half day

@Xymmer Xymmer modified the milestones: TBD, 1.0 Beta-0 Apr 25, 2019
@Xymmer Xymmer modified the milestones: 1.0 Beta-0, 1.0 RC-0 May 29, 2019
@Xymmer Xymmer added the BETA label May 31, 2019
@Xymmer Xymmer changed the title [spike needed] Investigate styling components rendered using cxComponentWrapper ike needed] Investigate styling components rendered using cxComponentWrapper May 31, 2019
@Xymmer Xymmer changed the title ike needed] Investigate styling components rendered using cxComponentWrapper Investigate styling components rendered using cxComponentWrapper May 31, 2019
@LTiger14 LTiger14 removed the BETA label Jun 3, 2019
@LTiger14 LTiger14 modified the milestones: 1.0 Beta-0, 1.0 RC-0 Jun 3, 2019
@LTiger14
Copy link
Author

LTiger14 commented Jun 3, 2019

After looking into this issue I was able to identify the cause of it and find a few ways around it. The issue is caused by the Angular default view encapsulation : ViewEncapsulation.Emulated. This has the merit of limiting the component style to a single component and not being applied to other components. This is achieved though adding _ngcontent-c# to the elements within a component and limiting the styles to only be applied to the elements with these labels. This is a known issue angular/angular#12215.

There are different workarounds for this issue:

If possible most style should be applied to the child component (the one rendered by the cxComponentWrapper)

In the case where the styling has to be on the parent e.g. alignment four are three ways around this issue:

  1. Add a wrapper div with a class around the cxComponentWrapper element and apply the style on that parent class.
  2. Change the encapsulation property in the parent to ViewEncapsulation.Native (might not be compatible with all browsers) or ViewEncapsulation.None this will cause the component to be affected by other styles than just the component specific style.
  3. Move the style affecting the child out of the parent stylesheet into a more global stylesheet. For example moving the style to styles.scss renders the appropriate result.
test-parent {
  test-child {
    width: 100%;
    margin-bottom: 27px;
  }
}
  1. Not recommended add /deep/ to bypass view encapsulation (https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep)
/deep/ test-child {
    width: 100%;
    margin-bottom: 27px;
  }

@LTiger14
Copy link
Author

LTiger14 commented Jun 3, 2019

A fix for this can be seen in angular/angular#12215, view related PRs. TLDR: manually add the parent _ngContent-c# in the child component. View jss project PR 191.

@LTiger14
Copy link
Author

LTiger14 commented Jun 14, 2019

After working a bit on the solution, I was able to make it work in the Angular default view encapsulation with some fancy tricks. The tricks are perhaps a bit too fancy so after discussion with Tobias we wont go with this solution.

An alternative way to the three that were stated in an earlier comment is to set the default view encapsulation to none throughout the app. This can be achieved by adding this to the main.ts of the application:

{defaultEncapsulation: ViewEncapsulation.None}

which makes the configuration look like this:

document.addEventListener('DOMContentLoaded', () => {
  platformBrowserDynamic().bootstrapModule(AppModule, {
    defaultEncapsulation: ViewEncapsulation.None,
  });
});

@LTiger14 LTiger14 changed the title Investigate styling components rendered using cxComponentWrapper [Won't fix] Investigate styling components rendered using cxComponentWrapper Jun 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants