-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c01c0d9
commit a2868d7
Showing
3 changed files
with
64 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters