Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ You will need to import the following files to use the tech-carbon-estimator:

- main.js
- polyfill.js
- styles.css
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed likely this needed to be removed, but I haven't seen it used - Dan, I guess you will when you try embedding it in the website.

With the style isolation, we are changing our ask of consumers from "include this in your global styles somewhere" to "put this file at /styles.css for us". Which is very rigid.

Perhaps we should allow the styles url to be given as a component attribute - including any path and cache-busting suffixes.

Having the whole bundled styles within the js bundle (so we can import it as a string rather than from a URL path) would eliminate the issue altogether, but then we're back into custom build system/tooling territory.

Why is it necessary to have separate sl-style and package-style? If we could have package-style be always used, we could specify it in styleUrl and simplify things a lot, and then do something else (e.g. style tag) for adaptations the demo page requires.


These files can be found under dist/tech-carbon-estimator when developing locally.

Expand Down
27 changes: 24 additions & 3 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"assets": ["src/favicon.ico"],
"styles": ["src/styles.css", "src/sl-styles.css"],
"scripts": []
"styles": [
{
"input": "src/styles.css",
"bundleName": "styles",
"inject": false
},
{
"input": "src/sl-styles.css",
"bundleName": "styles",
"inject": false
}
]
},
"configurations": {
"production": {
Expand All @@ -43,7 +53,18 @@
"npm-package": {
"index": {"input": "src/package-index.html", "output": "index.html"},
"assets": [],
"styles": ["src/styles.css", "src/package-styles.css"]
"styles": [
{
"input": "src/styles.css",
"bundleName": "styles",
"inject": false
},
{
"input": "src/package-styles.css",
"bundleName": "styles",
"inject": false
}
]
},
"development": {
"optimization": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ describe('TechCarbonEstimatorComponent', () => {
it('should show the form by default', () => {
fixture.detectChanges();

const formElement = fixture.nativeElement.querySelector('carbon-estimator-form');
const assumptionsElement = fixture.nativeElement.querySelector('assumptions-and-limitation');
const formElement = fixture.nativeElement.shadowRoot.querySelector('carbon-estimator-form');
const assumptionsElement = fixture.nativeElement.shadowRoot.querySelector('assumptions-and-limitation');

expect(formElement).toBeTruthy();
expect(assumptionsElement).toBeFalsy();
Expand Down
24 changes: 21 additions & 3 deletions src/app/tech-carbon-estimator/tech-carbon-estimator.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectorRef, Component, ElementRef, Input, ViewChild } from '@angular/core';
import { ChangeDetectorRef, Component, ElementRef, Input, ViewChild, ViewEncapsulation } from '@angular/core';
import { CarbonEstimatorFormComponent } from '../carbon-estimator-form/carbon-estimator-form.component';
import { CarbonEstimationComponent } from '../carbon-estimation/carbon-estimation.component';
import { CarbonEstimation, EstimatorValues } from '../types/carbon-estimator';
Expand All @@ -24,6 +24,9 @@ import { TabItemComponent } from '../tab/tab-item/tab-item.component';
TabItemComponent,
],
templateUrl: './tech-carbon-estimator.component.html',

// Protect against style interference by the hosting page
encapsulation: ViewEncapsulation.ShadowDom
})
export class TechCarbonEstimatorComponent {
@Input() public extraHeight?: string;
Expand All @@ -35,8 +38,23 @@ export class TechCarbonEstimatorComponent {

constructor(
private estimationService: CarbonEstimationService,
private changeDetector: ChangeDetectorRef
) {}
private changeDetector: ChangeDetectorRef,
private ref: ElementRef
) {
this.insertShadowStylesLink()
}

private insertShadowStylesLink() {
// Reasons for this approach:
// 1. Angular global injection would insert the tag in the page root, so we disabled it.
// 2. Component `styleUrl` wouldn't allow us to vary stylesheets based on build configurations.

const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'styles.css'; // This is the `bundleName` of the concatenated styles file

this.ref.nativeElement.shadowRoot.appendChild(link);
}

public handleFormSubmit(formValue: EstimatorValues) {
this.formValue = formValue;
Expand Down
Loading