diff --git a/examples/expo-example/app.json b/examples/expo-example/app.json index 00391a7..2be12af 100644 --- a/examples/expo-example/app.json +++ b/examples/expo-example/app.json @@ -27,9 +27,7 @@ }, "web": { "build": { - "babel": { - "include": ["@yoshiki/native", "@yoshiki/core"] - } + "bundler": "metro" }, "favicon": "./assets/favicon.png" } diff --git a/examples/expo-example/metro.config.js b/examples/expo-example/metro.config.js index f79958b..ad0672d 100644 --- a/examples/expo-example/metro.config.js +++ b/examples/expo-example/metro.config.js @@ -14,7 +14,10 @@ const workspaceRoot = path.resolve(projectRoot, "../.."); const config = getDefaultConfig(projectRoot); // 1. Watch all files within the monorepo -config.watchFolders = [workspaceRoot]; +config.watchFolders = [ + path.resolve(workspaceRoot, "examples/expo-example"), + path.resolve(workspaceRoot, "packages"), +]; // 2. Let Metro know where to resolve packages and in what order config.resolver.nodeModulesPaths = [ path.resolve(projectRoot, "node_modules"), diff --git a/examples/expo-example/src/app.tsx b/examples/expo-example/src/app.tsx index f125a12..511a73d 100644 --- a/examples/expo-example/src/app.tsx +++ b/examples/expo-example/src/app.tsx @@ -18,7 +18,7 @@ const CustomBox = ({ color, ...props }: { color: string }) => { const BoxWithoutProps = (props: object) => { return ( - + Text inside the box without props ); diff --git a/examples/expo-example/webpack.config.js b/examples/expo-example/webpack.config.js new file mode 100644 index 0000000..76d9329 --- /dev/null +++ b/examples/expo-example/webpack.config.js @@ -0,0 +1,18 @@ +// +// Copyright (c) Zoe Roux and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for details. +// +const createExpoWebpackConfigAsync = require("@expo/webpack-config"); + +module.exports = async function (env, argv) { + const config = await createExpoWebpackConfigAsync( + { + ...env, + babel: { + dangerouslyAddModulePathsToTranspile: ["@yoshiki"], + }, + }, + argv, + ); + return config; +}; diff --git a/packages/core/src/type.ts b/packages/core/src/type.ts index 70bfbd7..d42ce39 100644 --- a/packages/core/src/type.ts +++ b/packages/core/src/type.ts @@ -8,14 +8,20 @@ export type Theme = { spacing: string; }; +export const breakpoints = { + xs: 0, + sm: 600, + md: 900, + lg: 1200, + xl: 1600, +} + export type YoushikiStyle = | Property | ((theme: Theme) => Property) | Breakpoints; + export type Breakpoints = { - sm?: Property; - md?: Property; - lg?: Property; - xl?: Property; + [key in keyof (typeof breakpoints)]?: Property; }; diff --git a/packages/native/src/index.tsx b/packages/native/src/index.tsx index a8476e0..bc12dc7 100644 --- a/packages/native/src/index.tsx +++ b/packages/native/src/index.tsx @@ -3,21 +3,51 @@ // Licensed under the MIT license. See LICENSE file in the project root for details. // -import { ViewStyle, TextStyle, ImageStyle } from "react-native"; -import { YoushikiStyle } from "@yoshiki/core"; +import { ViewStyle, TextStyle, ImageStyle, useWindowDimensions } from "react-native"; +import { breakpoints, Breakpoints, YoushikiStyle } from "@yoshiki/core"; // TODO: shorhands type EnhancedStyle = { [key in keyof Properties]: YoushikiStyle; }; -export type CssObject = EnhancedStyle | EnhancedStyle | EnhancedStyle; +export type CssObject = + | EnhancedStyle + | EnhancedStyle + | EnhancedStyle; type Properties = ViewStyle | TextStyle | ImageStyle; +const useBreakpoint = (): number => { + const { width } = useWindowDimensions(); + const idx = Object.values(breakpoints).findIndex((x) => width <= x); + if (idx === -1) return 0; + return idx - 1; +}; + export const css = (css: CssObject, leftOvers?: { style?: Properties }) => { const { style, ...leftOverProps } = leftOvers ?? {}; + let breakpoint: number | undefined = undefined; + const ret: Properties = Object.fromEntries( + Object.entries(css) + .map(([key, value]) => { + if (typeof value === "object") { + if (!breakpoint) breakpoint = useBreakpoint(); + + const bpKeys = Object.keys(breakpoints); + for (let i = breakpoint; i >= 0; i--) { + if (bpKeys[i] in value) { + return [key, value[bpKeys[i]]]; + } + } + return undefined; + } + return [key, value]; + }) + .filter((x) => x !== undefined), + ); + return { - style: { ...css, ...style } as Properties, + style: { ...ret, ...style }, ...leftOverProps, }; }; diff --git a/packages/native/tsconfig.json b/packages/native/tsconfig.json index 32ca157..b65f7f9 100755 --- a/packages/native/tsconfig.json +++ b/packages/native/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "noErrorTruncation": true, "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "declaration": true,