generated from garronej/ts-ci
-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
withStyles.tsx
60 lines (48 loc) · 1.85 KB
/
withStyles.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
import type { ReactComponent } from "./tools/ReactComponent";
import type { CSSObject, Css, Cx } from "./types";
import { createMakeStyles } from "./createMakeStyles";
export function createWithStyles<Theme>(params: {
useTheme(): Theme;
useCssAndCx(): { css: Css; cx: Cx };
}) {
const { useTheme, useCssAndCx } = params;
const { makeStyles } = createMakeStyles({ useTheme, useCssAndCx });
function withStyles<
Props extends Record<string, unknown>,
C extends ReactComponent<Props>,
>(
Component: C,
cssObjectOrGetCssObject:
| CSSObject
| ((theme: Theme, props: Props, css: Css) => CSSObject),
): C {
const useStyles = makeStyles<Props>()(
typeof cssObjectOrGetCssObject === "function"
? (theme: Theme, props: Props, css: Css) => ({
"root": cssObjectOrGetCssObject(theme, props, css),
})
: { "root": cssObjectOrGetCssObject },
);
const Out = function (props: Props) {
const { classes, cx } = useStyles(props);
const { className, ...rest } = props;
if (className !== undefined && typeof className !== "string") {
throw new Error();
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Component_: ReactComponent<any> = Component;
return (
<Component_ className={cx(classes.root, className)} {...rest} />
);
};
const { name } = Component;
if (typeof name === "string") {
Object.defineProperty(Out, "name", {
"value": `${name}WithStyles`,
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Out as any;
}
return { withStyles };
}