-
Notifications
You must be signed in to change notification settings - Fork 3
/
react.tsx
135 lines (117 loc) Β· 3.89 KB
/
react.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
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
import React from 'react';
export const css: any = () => {
throw new Error(`Looks like you didn't setup foliage/babel-plugin`);
};
export const keyframes: any = () => {
throw new Error(`Looks like you didn't setup foliage/babel-plugin`);
};
export const createGlobalStyle: any = () => {
throw new Error(`Looks like you didn't setup foliage/babel-plugin`);
};
export const assertKeyframe = (input: any) => {
if (input.keyframes) {
return input;
}
throw new Error(
'Looks like you passed wrong element to an "animation" property',
);
};
export const assertVariable = (input: any) => {
return input;
// throw new Error('Looks like you passed wrong element as css-custom-property');
};
interface BlockCSS {
content: string;
css: string;
}
interface ComponentConfig {
component?: React.FC;
children?: React.FC;
mapVariants?: Record<string, (value: any) => any>;
variants?: Record<string, Record<string, BlockCSS>>;
compound?: Array<{ css: BlockCSS } & Record<string, any>>;
defaults?: Record<string, any>;
}
interface ComponentProps {
className?: string;
}
export const Global: React.FC<{ styles: any[] | any }> = ({ styles }) => {
React.useEffect(() => {
if (Array.isArray(styles)) {
styles.forEach((style) => add(style));
} else {
add(styles);
}
}, [styles]);
return null;
};
export function component(
tag: string, // eslint-disable-line @typescript-eslint/naming-convention
styles: BlockCSS | BlockCSS[],
{
component: _c = () => null,
children: _h = () => null,
mapVariants = {},
variants = {},
compound = [],
defaults = {},
}: ComponentConfig = {},
): React.FC<ComponentProps & Record<string, unknown>> {
const styleClasses = toArray(styles).map((style) => style.css);
return ({ className, children, ...props }) => {
const mainRef = React.useRef<HTMLElement>(null);
React.useEffect(() => {
toArray(styles).forEach((block) => add(block));
}, [...styleClasses]);
React.useEffect(() => {
if (isString(className)) mainRef.current?.classList.add(className);
styleClasses.forEach((css) => mainRef.current?.classList.add(css));
}, [className]);
React.useEffect(() => {
Object.keys(variants).forEach((variantName) => {
const propValue = props[variantName] ?? defaults[variantName];
const mapper = mapVariants[variantName] ?? id;
const variantCase = mapper(propValue);
const cssBlock = variants[variantName][variantCase];
if (cssBlock) {
mainRef.current?.classList.add(cssBlock.css);
add(cssBlock);
}
});
}, [variants, props, defaults, mapVariants]);
React.useEffect(() => {
compound.forEach(({ css: cssBlock, ...matchers }) => {
const isMatched = Object.keys(matchers).every((variantName) => {
const expectedVariantCase = matchers[variantName];
const propValue = props[variantName] ?? defaults[variantName];
const mapper = mapVariants[variantName] ?? id;
const variantCase = mapper(propValue);
return expectedVariantCase === variantCase;
});
if (isMatched) {
mainRef.current?.classList.add(cssBlock.css);
add(cssBlock);
}
});
}, [compound, props, defaults, mapVariants]);
const Tag = tag as unknown as React.FC<{ className?: string; ref?: any }>;
return <Tag ref={mainRef}>{children}</Tag>;
};
}
function toArray<T>(maybe: T | T[]): T[] {
return Array.isArray(maybe) ? maybe : [maybe];
}
function id<T>(value: T): T {
return value;
}
function isString(value: unknown): value is string {
return typeof value === 'string';
}
function add(cssBlock: BlockCSS) {
if (!document.querySelector(`[data-foliage="${cssBlock.css}"]`)) {
const style = document.createElement('style');
style.dataset.foliage = cssBlock.css;
style.textContent = cssBlock.content;
document.head.append(style);
}
}