-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
compileKeyframeCSS.ts
37 lines (30 loc) · 1.07 KB
/
compileKeyframeCSS.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { MakeStyles } from '../types';
import { compile, middleware, serialize, rulesheet, stringify, prefixer } from 'stylis';
import { cssifyObject } from './utils/cssifyObject';
export function compileKeyframeRule(keyframeObject: MakeStyles): string {
let css: string = '';
// eslint-disable-next-line guard-for-in
for (const percentage in keyframeObject) {
css += `${percentage}{${cssifyObject(keyframeObject[percentage])}}`;
}
return css;
}
/**
* Creates CSS rules for insertion from passed CSS.
*/
export function compileKeyframesCSS(keyframeName: string, keyframeCSS: string): string[] {
const cssRule = `@keyframes ${keyframeName} {${keyframeCSS}}`;
const rules: string[] = [];
serialize(
compile(cssRule),
middleware([
prefixer,
stringify,
// 💡 we are using `.insertRule()` API for DOM operations, which does not support
// insertion of multiple CSS rules in a single call. `rulesheet` plugin extracts
// individual rules to be used with this API
rulesheet(rule => rules.push(rule)),
]),
);
return rules;
}