Skip to content

Commit

Permalink
feat(core): introduce styling service
Browse files Browse the repository at this point in the history
  • Loading branch information
trotyl committed Sep 21, 2019
1 parent 8d1fb11 commit 62ae3ce
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/core/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './fast-iterable-differ/index';
export * from './iterable-differs/index';
export * from './key-value-differs/index';
export * from './renderer-extension/index';
export * from './styling/index';
34 changes: 34 additions & 0 deletions projects/core/src/styling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Styling

Styling utility.

## Type

**Service**

## Provenance

None.

## NgModule

`@angular-contrib/core#ContribStylingModule`

## Usage

```typescript
@Component()
class MyComponent implements OnDestroy {
private disposeGlobalStyle: () => void;

constructor(
styling: Styling,
) {
this.disposeGlobalStyle = styling.addGlobalStyle(`body { color: red; }`);
}

ngOnDestroy(): void {
this.disposeGlobalStyle();
}
}
```
2 changes: 2 additions & 0 deletions projects/core/src/styling/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './styling';
export * from './styling.module';
7 changes: 7 additions & 0 deletions projects/core/src/styling/styling.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NgModule } from '@angular/core';
import { Styling } from './styling';

@NgModule({
providers: [Styling],
})
export class ContribStylingModule {}
29 changes: 29 additions & 0 deletions projects/core/src/styling/styling.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { DOCUMENT } from '@angular/common';
import { TestBed } from '@angular/core/testing';
import { Styling } from './styling';

describe('Styling', () => {
let service: Styling;

beforeEach(() => {
service = TestBed.get(Styling);
});

it('should add global style', () => {
const document: Document = TestBed.get(DOCUMENT);
const previousLength = document.head.childNodes.length;

const dispose = service.addGlobalStyle('body { color: red; }');

const length = document.head.childNodes.length;
expect(length).toBe(previousLength + 1);

const lastChild = document.head.childNodes[length - 1];
expect(lastChild instanceof HTMLStyleElement).toBe(true);

dispose();

const postDisposeLength = document.head.childNodes.length;
expect(postDisposeLength).toBe(previousLength);
});
});
29 changes: 29 additions & 0 deletions projects/core/src/styling/styling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable, Renderer2, Inject, RendererFactory2 } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable({
providedIn: 'root',
})
export class Styling {
private renderer: Renderer2;

constructor(
rendererFactory: RendererFactory2,
@Inject(DOCUMENT) private document: any,
) {
this.renderer = rendererFactory.createRenderer(null, null);
}

addGlobalStyle(style: string): () => void {
const styleElement = this.renderer.createElement('style');
this.renderer.setAttribute(styleElement, 'type', 'text/css');
this.renderer.setProperty(styleElement, 'textContent', style);

const doc = this.document as Document;
this.renderer.appendChild(doc.head, styleElement);

return () => {
this.renderer.removeChild(doc.head, styleElement);
};
}
}

0 comments on commit 62ae3ce

Please sign in to comment.