Skip to content
This repository was archived by the owner on Aug 25, 2020. It is now read-only.

Commit effab99

Browse files
committed
feat: @Keyframes support
1 parent db4ac50 commit effab99

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,25 @@ TBD
231231

232232
TBD
233233

234-
### Raw Styles
235-
236-
Add styles to the global scope by defining selector:
234+
### @keyframes
237235

238236
```typescript
239-
constructor(private stylerService: StylerService) {
240-
this.stylerService.setRaw('body', {
241-
background: '#222222',
242-
color: '#eeeeee',
237+
constructor(private styler: StylerComponent,
238+
private stylerService: StylerService) {
239+
this.styler.register({
240+
host: {
241+
animationDuration: '1s',
242+
animationName: this.stylerService.keyframes({
243+
'0%': {
244+
transform: 'rotate(0deg)',
245+
},
246+
'100%': {
247+
transform: 'rotate(360deg)',
248+
},
249+
}),
250+
animationTimingFunction: 'linear',
251+
animationIterationCount: 'infinite',
252+
},
243253
});
244254
}
245255
```

package/src/compiler/compiler.service.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { DOCUMENT } from '@angular/common';
12
import { Inject, Injectable } from '@angular/core';
2-
import { DOCUMENT, ɵSharedStylesHost as SharedStylesHost } from '@angular/platform-browser';
3+
import { ɵSharedStylesHost as SharedStylesHost } from '@angular/platform-browser';
34
import { processAutoPx } from '../helpers/process-auto-px';
45
import { autoPx } from '../meta/compiler';
56
import { StylerHashService } from '../meta/hash';
@@ -184,4 +185,15 @@ export class StylerCompilerService {
184185
// update hash in unit
185186
unit.hash.next(newHash);
186187
}
188+
189+
addKeyframes(def: any): string {
190+
let css = '';
191+
for (const key in def) {
192+
css += `${key}{${this.compileProps(def[key])}}`
193+
}
194+
const selector = `kf-${this.hash.hash(css)}`;
195+
// @todo impr performace by hash-caching
196+
this.sharedStylesHost.addStyles([`@keyframes ${selector}{${css}}`]);
197+
return selector;
198+
}
187199
}

package/src/styler.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ export class StylerService {
2424
});
2525
this.compiler.render();
2626
}
27+
28+
keyframes(def: any): string {
29+
return this.compiler.addKeyframes(def);
30+
}
2731
}

0 commit comments

Comments
 (0)