-
Notifications
You must be signed in to change notification settings - Fork 1
#219 Isolate styling with shadow dom #253
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
Changes from all commits
b41c8f6
7f859dc
12422ce
b37993c
a45c4e6
827c503
e00ae8c
a3e05a1
531a3e9
b8e6af7
2bdfd2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,23 @@ | |
| "polyfills": ["zone.js"], | ||
| "tsConfig": "tsconfig.app.json", | ||
| "assets": ["src/favicon.ico"], | ||
| "styles": ["src/styles.css", "src/sl-styles.css"], | ||
| "scripts": [] | ||
| "styles": [ | ||
| { | ||
| "input": "src/tce-global-styles.css", | ||
| "bundleName": "tce-global-styles", | ||
| "inject": true | ||
| }, | ||
| { | ||
| "input": "src/styles.css", | ||
| "bundleName": "styles", | ||
| "inject": false | ||
| }, | ||
| { | ||
| "input": "src/sl-styles.css", | ||
| "bundleName": "styles", | ||
| "inject": false | ||
| } | ||
| ] | ||
| }, | ||
| "configurations": { | ||
| "production": { | ||
|
|
@@ -43,7 +58,33 @@ | |
| "npm-package": { | ||
| "index": {"input": "src/package-index.html", "output": "index.html"}, | ||
| "assets": [], | ||
| "styles": ["src/styles.css", "src/package-styles.css"] | ||
| "styles": [ | ||
| { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is off |
||
| "input": "src/tce-global-styles.css", | ||
| "bundleName": "tce-global-styles", | ||
| "inject": true | ||
| }, | ||
| { | ||
| "input": "src/styles.css", | ||
| "bundleName": "styles", | ||
| "inject": false | ||
| }, | ||
| { | ||
| "input": "src/package-styles.css", | ||
| "bundleName": "styles", | ||
| "inject": false | ||
| } | ||
| ], | ||
| "optimization": { | ||
| "scripts": true, | ||
| "styles": { | ||
| "minify": true, | ||
| "inlineCritical": false | ||
| }, | ||
| "fonts": true | ||
| }, | ||
| "extractLicenses": true, | ||
| "sourceMap": false | ||
| }, | ||
| "development": { | ||
| "optimization": false, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,26 +2,28 @@ | |
| <span class="tce-chart-total-colour"></span> | ||
| <span class="apexcharts-legend-text" id="tce-chart-total-text">Total Emissions Estimate {{ totalEmissions() }}</span> | ||
| <!--span><button (click)="toggleMassPercentages()">toggle</button></span--> | ||
| <span class="tce-switch-container" *ngIf="shouldShowUnitsSwitch()"> | ||
| <label class="tce-switch-label" [class.tce-switch-active]="isMass()"> | ||
| <input | ||
| type="radio" | ||
| name="massPercentSwitch" | ||
| [checked]="isMass()" | ||
| (change)="isMass.set(true)" | ||
| class="tce-switch-input" /> | ||
| kg | ||
| </label> | ||
| <label class="tce-switch-label" [class.tce-switch-active]="!isMass()"> | ||
| <input | ||
| type="radio" | ||
| name="massPercentSwitch" | ||
| [checked]="!isMass()" | ||
| (change)="isMass.set(false)" | ||
| class="tce-switch-input" /> | ||
| % | ||
| </label> | ||
| </span> | ||
| @if(shouldShowUnitsSwitch()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume this and the other html changes in files are fixing consequences of changing the styles approach |
||
| <span class="tce-switch-container"> | ||
| <label class="tce-switch-label" [class.tce-switch-active]="isMass()"> | ||
| <input | ||
| type="radio" | ||
| name="massPercentSwitch" | ||
| [checked]="isMass()" | ||
| (change)="isMass.set(true)" | ||
| class="tce-switch-input" /> | ||
| kg | ||
| </label> | ||
| <label class="tce-switch-label" [class.tce-switch-active]="!isMass()"> | ||
| <input | ||
| type="radio" | ||
| name="massPercentSwitch" | ||
| [checked]="!isMass()" | ||
| (change)="isMass.set(false)" | ||
| class="tce-switch-input" /> | ||
| % | ||
| </label> | ||
| </span> | ||
| } | ||
| </div> | ||
| <figure [attr.aria-label]="emissionAriaLabel()" tabindex="0"> | ||
| <apx-chart | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,6 +152,11 @@ export class CarbonEstimationComponent implements OnInit, OnDestroy { | |
| // for our purposes monthly estimate is just the annual divided equally | ||
| // beteween 12 months | ||
|
|
||
| // if no estimation, return undefined | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appreciate I asked for this, but in hindsight it's clear - dealing with null so the below doesn't have to. The comment though just restates what the code says, could remove. |
||
| if(this.carbonEstimation() == null) { | ||
dmoorhouse-scottlogic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return undefined; | ||
| } | ||
|
|
||
| const copy = JSON.parse(JSON.stringify(this.carbonEstimation())); | ||
|
|
||
| copy.values.totalEmissions = copy.values.totalEmissions/12; | ||
|
|
||
| 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, OnInit, 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'; | ||
|
|
@@ -24,9 +24,13 @@ 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 { | ||
| export class TechCarbonEstimatorComponent implements OnInit { | ||
| @Input() public extraHeight?: string; | ||
| @Input({ alias: 'assets-base-path' }) public assetsBasePath?: string; | ||
|
|
||
| public formValue: EstimatorValues | undefined; | ||
| public carbonEstimation: CarbonEstimation | null = null; | ||
|
|
@@ -35,9 +39,40 @@ export class TechCarbonEstimatorComponent { | |
|
|
||
| constructor( | ||
| private estimationService: CarbonEstimationService, | ||
| private changeDetector: ChangeDetectorRef | ||
| private changeDetector: ChangeDetectorRef, | ||
| private ref: ElementRef | ||
| ) {} | ||
|
|
||
| ngOnInit() { | ||
| 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. | ||
|
|
||
| let stylesLink = this.createShadowStylesLink('styles.css'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why let rather than const? |
||
| let googleFontsLink = this.createShadowStylesLink('https://fonts.googleapis.com/icon?family=Material+Icons+Outlined'); | ||
|
|
||
| this.ref.nativeElement.shadowRoot.appendChild(googleFontsLink); | ||
| this.ref.nativeElement.shadowRoot.appendChild(stylesLink); | ||
| } | ||
|
|
||
| private createShadowStylesLink(styleHref: string) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this was |
||
|
|
||
| const isAbsoluteUrl = styleHref.startsWith('http://') || styleHref.startsWith('https://'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may well be simpler to deal with this path wrangling in the calling method - it's only our own styles that needs anything |
||
| const basePath = this.assetsBasePath && !isAbsoluteUrl ? this.assetsBasePath.replace(/\/?$/, '/') : ''; | ||
|
|
||
| const stylesPath = `${basePath}${styleHref}`; | ||
|
|
||
| let stylesLink = document.createElement('link'); | ||
| stylesLink.rel = 'stylesheet'; | ||
| stylesLink.type = 'text/css'; | ||
| stylesLink.href = stylesPath; | ||
| return stylesLink; | ||
kellis-sl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public handleFormSubmit(formValue: EstimatorValues) { | ||
| this.formValue = formValue; | ||
| this.carbonEstimation = this.estimationService.calculateCarbonEstimation(this.formValue); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to say "your" bundle