Skip to content

Commit c17bc4b

Browse files
committed
Add loads of code comments
1 parent 9ee5a1e commit c17bc4b

File tree

4 files changed

+227
-99
lines changed

4 files changed

+227
-99
lines changed

api.js

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,82 @@
11
import React from 'react';
22

3-
let css = (rawCSSWithProps) => (target, name, descriptor) => ({
3+
/*
4+
This get's used as decorator @css
5+
6+
Get's the template literal passed to it argument.
7+
This contains references to props as well
8+
9+
Method decorators expect a function to be returned,
10+
this function gets the parent class, name of the function -
11+
render in this case and the descriptor for the function.
12+
*/
13+
let css = (rawCSS) => (parentClass, name, descriptor) => ({
414
...descriptor,
515
value: function () {
6-
let props;
7-
let giveMeProps = (object) => {
8-
props = object.props;
16+
let originalProps;
17+
18+
/* Totally stealing props by fake rendering the component */
19+
let getProps = (object) => {
20+
originalProps = object.props;
921
return object;
1022
};
11-
let rendered = descriptor.value.apply(giveMeProps(this), arguments);
23+
let rendered = descriptor.value.apply(getProps(this), arguments);
1224

13-
let rawCSS = fillProps(rawCSSWithProps[0], props)
14-
let style = parseCss(rawCSS);
15-
let newProps = {...props, style};
25+
/* Replace props and return realCSS™ */
26+
let realCSS = fillProps(rawCSS, originalProps)
1627

28+
/* Convert real CSS to javascripty CSS */
29+
let style = parseCss(realCSS);
30+
31+
/* Merge styles into original props */
32+
let newProps = {...originalProps, style};
33+
34+
/*
35+
Pass on a clone of the rendered component
36+
with our merged props.
37+
This overrides the original render function
38+
*/
1739
return React.cloneElement(rendered, newProps, rendered.props.children);
1840
}
1941
});
2042

21-
let camelCase = (key) => key.replace(/(\-[a-z])/g, $1 => $1.toUpperCase().replace('-',''));
43+
/*
44+
Replace props with actual values
2245
23-
let fillProps = (rawCSSWithProps, props) => {
46+
Uses regex pattern to match references to props
47+
48+
Supports direct usage
49+
color: {this.props.color}
50+
51+
Does not evaluate conditions (yet)
52+
color: {this.props.color || 'blue'}
53+
*/
54+
let fillProps = (rawCSS, props) => {
55+
rawCSS = rawCSS[0]; // template literal = array
2456
let re = /{this.props.*}/g;
25-
let matches = rawCSSWithProps.match(re);
57+
let matches = rawCSS.match(re);
2658
if (matches && matches.length) {
2759
for (let match of matches) {
2860
let keyword = match;
2961
keyword = keyword.replace('{this.props.', '');
30-
keyword = keyword.substring(0, keyword.length-1); // }
62+
keyword = keyword.substring(0, keyword.length-1); // remove }
3163
keyword = keyword.trim();
32-
33-
rawCSSWithProps = rawCSSWithProps.replace(match, props[keyword]);
64+
rawCSS = rawCSS.replace(match, props[keyword]);
3465
}
3566
}
36-
return rawCSSWithProps;
67+
return rawCSS;
3768
}
3869

39-
let parseCss = (rawCSS) => {
70+
/*
71+
Convert realCSS to javascripty CSS
72+
73+
Split on semi-colon
74+
Trim the whitespace
75+
Camel case keys
76+
*/
77+
let parseCss = (realCSS) => {
4078
let styles = {};
41-
let rules = rawCSS.trim().split(';');
79+
let rules = realCSS.trim().split(';');
4280
for (let rule of rules) {
4381
let [key, value] = rule.trim().split(':');
4482
if (key && value) {
@@ -50,4 +88,6 @@ let parseCss = (rawCSS) => {
5088
return styles;
5189
}
5290

91+
let camelCase = (key) => key.replace(/(\-[a-z])/g, $1 => $1.toUpperCase().replace('-',''));
92+
5393
export default css;

css-constructor.js

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)