-
Notifications
You must be signed in to change notification settings - Fork 30
/
styled.tsx
68 lines (60 loc) · 1.4 KB
/
styled.tsx
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
import React, { ReactType, ComponentType } from 'react';
import { StyleSheet, css, CSSProperties } from 'aphrodite/no-important';
type SheetDefinition = CSSProperties;
type Props = { [key: string]: any };
const domElements = [
'div',
'span',
'main',
'footer',
'section',
'h1',
'h2',
'h3',
'p',
'ul',
'ol',
'li',
'table',
'caption',
'th',
'td',
'a',
'button',
'img',
'em',
'strong',
'form',
'label',
];
interface StyleGenerator {
(def: SheetDefinition): ComponentType<any>;
attrs(props: Props): (def: SheetDefinition) => ComponentType<any>;
}
interface Styled {
(element: ReactType): StyleGenerator;
[node: string]: StyleGenerator;
}
const withProps = (propsToAdd: Props) => (component: ReactType) => {
const Component = component;
return (props: Props) => <Component {...propsToAdd} {...props} />;
};
const styled = ((element: ReactType) => {
const gen = (def: SheetDefinition) => {
const elementName = typeof element === 'string' ? element : element.name;
const styles = StyleSheet.create({
[elementName]: def,
});
const El = element;
return (props: Props) => (
<El className={css(styles[elementName])} {...props} />
);
};
gen.attrs = (props: Props) => (def: SheetDefinition) =>
withProps(props)(gen(def));
return gen;
}) as Styled;
domElements.forEach(el => {
styled[el] = styled(el);
});
export default styled;