-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
73 lines (64 loc) · 1.86 KB
/
index.js
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
import * as React from "react";
export const createStitchesPropertyName = (variantName) =>
`--stitches-dynamic-${variantName}`;
export const createDynamicStyled = (_css) => {
return (type, styleConfig) => {
const { dynamicVariants, ...restOfStyleConfig } = styleConfig;
const cssComponent = _css(restOfStyleConfig);
const dVarToClassMap = {};
const dynamicVariantsMap = {};
for (const variantName in dynamicVariants) {
dynamicVariantsMap[variantName] = dynamicVariants[variantName](
`var(${createStitchesPropertyName(variantName)})`
);
}
return React.forwardRef(({ css, style, as, ...props }, ref) => {
const {
props: { className, ..._fProps }
} = cssComponent(props);
const Component = as || type;
const { dynamicVariantStyles, classNames } = processDynamicVariantsProps(
className,
dynamicVariantsMap,
props,
dVarToClassMap,
cssComponent,
_fProps,
as
);
return (
<Component
{..._fProps}
style={{ ...style, ...dynamicVariantStyles }}
className={classNames.join(" ")}
ref={ref}
/>
);
});
};
};
function processDynamicVariantsProps(
className,
dynamicVariantsMap,
props,
dVarToClassMap,
cssComponent,
_fProps
) {
const dynamicVariantStyles = {};
const classNames = className ? [className] : [];
for (const key in dynamicVariantsMap) {
if (key in props) {
dynamicVariantStyles[createStitchesPropertyName(key)] = props[key];
if (!dVarToClassMap[key]) {
dVarToClassMap[key] = String(
cssComponent({ css: dynamicVariantsMap[key] })
).split(" ")[1];
}
classNames.push(dVarToClassMap[key]);
_fProps[key] = undefined;
}
}
_fProps["as"] = undefined;
return { dynamicVariantStyles, classNames };
}