/
FelaAdapter.ts
141 lines (123 loc) 路 3.9 KB
/
FelaAdapter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Adapter, ClassName, Ruleset, Sheet, FontFace } from 'aesthetic';
import { getStyleElements, purgeStyles, toArray } from 'aesthetic-utils';
import { combineRules, IRenderer } from 'fela';
import { render } from 'fela-dom';
import { NativeBlock, ParsedBlock } from './types';
export default class FelaAdapter extends Adapter<NativeBlock, ParsedBlock> {
fela: IRenderer;
keyframes: { [animationName: string]: ClassName } = {};
constructor(fela: IRenderer) {
super();
this.fela = fela;
this.syntax
.on('attribute', this.handleNested)
.on('css', this.handleCss)
.on('fallback', this.handleFallback)
.on('font-face', this.handleFontFace)
.on('global', this.handleGlobal)
.on('keyframe', this.handleKeyframe)
.on('media', this.handleMedia)
.on('property', this.handleProperty)
.on('pseudo', this.handleNested)
.on('selector', this.handleNested)
.on('support', this.handleSupport);
}
flushStyles() {
render(this.fela);
}
transformToClassName(styles: ParsedBlock[]): ClassName {
return this.fela.renderRule(combineRules(...styles.map(style => () => style)), {});
}
purgeStyles() {
purgeStyles(getStyleElements('data-fela-type'), true);
}
// http://fela.js.org/docs/api/fela/Renderer.html
private handleCss = (css: string) => {
this.fela.renderStatic(css);
};
// https://github.com/rofrischmann/fela/tree/master/packages/fela-plugin-fallback-value
private handleFallback = (
ruleset: Ruleset<NativeBlock>,
name: keyof NativeBlock,
value: unknown[],
) => {
ruleset.addProperty(name, [...value, ...toArray(ruleset.properties[name])]);
};
// http://fela.js.org/docs/basics/Fonts.html
// http://fela.js.org/docs/api/fela/Renderer.html
private handleFontFace = (
sheet: Sheet<NativeBlock>,
fontFaces: Ruleset<NativeBlock>[],
fontFamily: string,
srcPaths: string[][],
) => {
fontFaces.forEach((face, i) => {
const { local, ...style } = face.toObject() as FontFace;
this.fela.renderFont(fontFamily, srcPaths[i], {
...style,
localAlias: local,
});
});
};
// http://fela.js.org/docs/advanced/StaticStyle.html
// http://fela.js.org/docs/api/fela/Renderer.html#renderstaticstyle-selector
private handleGlobal = (
sheet: Sheet<NativeBlock>,
selector: string,
ruleset: Ruleset<NativeBlock>,
) => {
this.fela.renderStatic(ruleset.toObject(), selector);
};
// http://fela.js.org/docs/basics/Keyframes.html
// http://fela.js.org/docs/basics/Renderer.html#renderkeyframe
private handleKeyframe = (
sheet: Sheet<NativeBlock>,
keyframe: Ruleset<NativeBlock>,
animationName: string,
) => {
this.keyframes[animationName] = this.fela.renderKeyframe(
// @ts-ignore Annoying to type
() => keyframe.toObject(),
{},
);
};
// http://fela.js.org/docs/basics/Rules.html#3-media-queries
private handleMedia = (
ruleset: Ruleset<NativeBlock>,
query: string,
value: Ruleset<NativeBlock>,
) => {
ruleset.addNested(`@media ${query}`, value);
};
// http://fela.js.org/docs/basics/Rules.html#style-object
private handleNested = (
ruleset: Ruleset<NativeBlock>,
selector: string,
value: Ruleset<NativeBlock>,
) => {
ruleset.addNested(selector, value);
};
// http://fela.js.org/docs/basics/Rules.html
private handleProperty = (
ruleset: Ruleset<NativeBlock>,
name: keyof NativeBlock,
value: unknown,
) => {
if (name === 'animationName') {
ruleset.addProperty(
name,
this.syntax.injectKeyframes(String(value), this.keyframes).join(', '),
);
} else {
ruleset.addProperty(name, value);
}
};
// http://fela.js.org/docs/basics/Rules.html
private handleSupport = (
ruleset: Ruleset<NativeBlock>,
query: string,
value: Ruleset<NativeBlock>,
) => {
ruleset.addNested(`@supports ${query}`, value);
};
}