mirror of
https://github.com/zoriya/yoshiki.git
synced 2026-06-08 04:32:03 +00:00
Allow ysMap to map to anoter type
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "yoshiki",
|
||||
"version": "1.2.8",
|
||||
"version": "1.2.9",
|
||||
"author": "Zoe Roux <zoe.roux@sdg.moe> (https://github.com/zoriya)",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
|
||||
@@ -34,3 +34,4 @@ export const ThemeProvider = ({ theme, children }: { theme: Theme; children?: Re
|
||||
};
|
||||
|
||||
export * from "../type";
|
||||
export * from "../utils";
|
||||
|
||||
@@ -79,19 +79,3 @@ export const assignChilds = <Style>(
|
||||
}
|
||||
return target;
|
||||
};
|
||||
|
||||
export const ysMap = <Property>(
|
||||
value: YoshikiStyle<Property>,
|
||||
f: (p: Property) => Property,
|
||||
): YoshikiStyle<Property> => {
|
||||
if (typeof value === "function") {
|
||||
// @ts-ignore too hard to type right
|
||||
return (theme) => ysMap(value(theme), f);
|
||||
}
|
||||
if (isBreakpoints(value)) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).map(([bp, bpValue]) => [bp, ysMap(bpValue, f)]),
|
||||
);
|
||||
}
|
||||
return f(value);
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//
|
||||
|
||||
import { breakpoints } from "./theme";
|
||||
import { Breakpoints } from "./type";
|
||||
import { Breakpoints, YoshikiStyle } from "./type";
|
||||
|
||||
export const isBreakpoints = <T>(value: unknown): value is Breakpoints<T> => {
|
||||
if (typeof value !== "object" || !value) return false;
|
||||
@@ -26,3 +26,19 @@ export const forceBreakpoint = <T extends Record<string, unknown>>(
|
||||
Object.entries(value).map(([key, value]) => [key, { [breakpoint]: value }]),
|
||||
) as any;
|
||||
};
|
||||
|
||||
export const ysMap = <Property, Mapped>(
|
||||
value: YoshikiStyle<Property>,
|
||||
f: (p: Property) => Mapped,
|
||||
): YoshikiStyle<Mapped> => {
|
||||
if (typeof value === "function") {
|
||||
// @ts-ignore too hard to type right
|
||||
return (theme) => ysMap(value(theme), f);
|
||||
}
|
||||
if (isBreakpoints(value)) {
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).map(([bp, bpValue]) => [bp, ysMap(bpValue, f)]),
|
||||
);
|
||||
}
|
||||
return f(value);
|
||||
};
|
||||
|
||||
@@ -27,3 +27,4 @@ export const ThemeProvider = ({ theme, children }: { theme: Theme; children?: Re
|
||||
createElement(ThemeContext.Provider, { value: theme }, [children]);
|
||||
|
||||
export * from "../type";
|
||||
export * from "../utils";
|
||||
|
||||
Reference in New Issue
Block a user