Add ysMap

This commit is contained in:
2023-10-17 21:14:04 +02:00
parent d6c7162480
commit ca7882ba73
6 changed files with 25 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
import Head from "next/head";
import Image from "next/image";
import { useYoshiki, Stylable, md } from "yoshiki/web";
import { useYoshiki, Stylable, md, ysMap } from "yoshiki/web";
import { ReactNode } from "react";
const Box = ({ children, ...props }: { children?: ReactNode } & Stylable) => {
@@ -69,7 +69,7 @@ export default function Home(props: Stylable) {
},
hover: {
text: {
color: { xs: "blue", md: "black" },
color: ysMap({ xs: "lue", md: "lack" }, (x) => `b${x}`),
},
},
focus: {

View File

@@ -1,6 +1,6 @@
{
"name": "yoshiki",
"version": "1.2.7",
"version": "1.2.8",
"author": "Zoe Roux <zoe.roux@sdg.moe> (https://github.com/zoriya)",
"license": "MIT",
"keywords": [

View File

@@ -32,3 +32,5 @@ export { sm, md, lg, xl } from "./type";
export const ThemeProvider = ({ theme, children }: { theme: Theme; children?: ReactNode }) => {
return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>;
};
export * from "../type";

View File

@@ -4,6 +4,7 @@
//
import { breakpoints, Theme } from "./theme";
import { isBreakpoints } from "./utils";
export type YoshikiStyle<Property> =
| Property
@@ -78,3 +79,19 @@ 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);
};

View File

@@ -25,3 +25,5 @@ export { useAutomaticTheme } from "./automatic-theme";
export const ThemeProvider = ({ theme, children }: { theme: Theme; children?: ReactNode }) =>
createElement(ThemeContext.Provider, { value: theme }, [children]);
export * from "../type";

View File

@@ -4,5 +4,6 @@
nodejs-16_x
nodePackages.yarn
nodePackages.expo-cli
nodePackages.eslint_d
];
}