Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: typescript error #76

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions src/core/hoc.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
import React, { useContext, useMemo } from "react";
import { useContext, useMemo } from "react";
import { OsmiContext } from "./context";
import { applyHelper } from "./apply";
import { colorHelper } from "./color";

// Types
import type { FC, ComponentType } from "react";
import type { NamedStyles, ApplyInstance } from "../types/osmi.types";

export const withStyles =
<P extends ApplyInstance = ApplyInstance>(
Component: React.ComponentType<P>
): React.FC<Omit<P, keyof ApplyInstance>> =>
(props) => {
const themeContext = useContext(OsmiContext);
Component: ComponentType<P>
): FC<Omit<P, keyof ApplyInstance>> =>
(props: Omit<P, keyof ApplyInstance>) => {
const themeContext = useContext(OsmiContext);

if (themeContext === null) {
throw new Error("You must wrap your components in a OsmiProvider.");
}
if (themeContext === null) {
throw new Error("You must wrap your components in a OsmiProvider.");
}

const { scaleWidth, scaleHeight, switchMode } = themeContext;
const { scaleWidth, scaleHeight, switchMode } = themeContext;

const apply = useMemo(
() =>
<T extends NamedStyles<T> | NamedStyles<any>>(...args: (string | boolean | undefined)[]) =>
applyHelper(...args)(themeContext)
,
[themeContext]
);
const apply = useMemo(
() =>
<T extends NamedStyles<T> | NamedStyles<any>>(
...args: (string | boolean | undefined)[]
) =>
applyHelper(...args)(themeContext),
[themeContext]
);

const colors = useMemo(
() =>
(...args: (string | boolean | undefined)[]): string | string[] => {
if (args.length === 1) {
const arg = args[0];
return arg !== false && arg !== undefined ? colorHelper(arg)(themeContext) : "";
} else if (args.length === 2) {
return args.map(arg => arg !== false && arg !== undefined ? colorHelper(arg)(themeContext) : "");
}
const colors = useMemo(
() =>
<T extends string | string[]>(
...args: (string | boolean | undefined)[]
): T => {
if (args.length === 1) {
return colorHelper(args[0] as string)(themeContext) as T;
} else if (args.length === 2) {
return args.map((syntax) =>
colorHelper(syntax as string)(themeContext)
) as T;
} else {
throw Error("Invalid color syntax");
}
,
[themeContext]
);
},
[themeContext]
);

return (
<Component
{...(props as P)}
apply={apply}
colors={colors}
scaleWidth={scaleWidth}
scaleHeight={scaleHeight}
switchTheme={switchMode}
/>
);
};
return (
<Component
{...(props as P)}
apply={apply}
colors={colors}
scaleWidth={scaleWidth}
scaleHeight={scaleHeight}
switchTheme={switchMode}
/>
);
};
6 changes: 3 additions & 3 deletions src/core/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export const useStyles = (): ApplyInstance => {

const colors = useMemo(
() =>
(...args: (string | boolean | undefined)[]): string | string[] => {
<T extends string | string[]>(...args: (string | boolean | undefined)[]): T => {
if (args.length === 1) {
return colorHelper(args[0] as string)(themeContext);
return colorHelper(args[0] as string)(themeContext) as T;
} else if (args.length === 2) {
return args.map((syntax) =>
colorHelper(syntax as string)(themeContext)
);
) as T;
} else {
throw Error("Invalid color syntax");
}
Expand Down
27 changes: 16 additions & 11 deletions src/processor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const customProcessor = (
customTheme: CustomThemeType
): Record<string, string | object> => {
const processors: Record<
string,
keyof CustomThemeType,
(themeValue: any) => Record<string, unknown>
> = {
colors: customColors,
Expand All @@ -39,14 +39,19 @@ export const customProcessor = (
shadow: customShadows,
};

return Object.entries(processors).reduce((finalObject, [key, processor]) => {
const themeValue = customTheme[key as keyof CustomThemeType];
if (themeValue) {
finalObject = {
...finalObject,
...processor(themeValue),
};
}
return finalObject;
}, {} as Record<string, unknown>);
return Object.entries(processors).reduce<Record<string, any>>(
(finalObject, [key, processor]) => {
if (key in customTheme) {
const themeValue = customTheme[key as keyof CustomThemeType];
if (themeValue) {
finalObject = {
...finalObject,
...processor(themeValue),
};
}
}
return finalObject;
},
{}
);
};
Loading