From 7a6e0a9cb509da231882b0e414ed075bd519e963 Mon Sep 17 00:00:00 2001 From: Juan Andrade Date: Wed, 15 May 2024 15:49:17 -0400 Subject: [PATCH 01/22] add css-modules integration, View as css-module --- build-settings/rollup.config.js | 5 + package.json | 1 + .../src/components/view.module.css | 15 + .../src/components/view.tsx | 53 +- packages/wonder-blocks-core/src/util/types.ts | 5 +- packages/wonder-blocks-tokens/src/tokens.css | 91 +++ types/assets.d.ts | 6 + yarn.lock | 621 +++++++++++++++++- 8 files changed, 764 insertions(+), 33 deletions(-) create mode 100644 packages/wonder-blocks-core/src/components/view.module.css create mode 100644 packages/wonder-blocks-tokens/src/tokens.css diff --git a/build-settings/rollup.config.js b/build-settings/rollup.config.js index 57d0e73ebd..408f602130 100644 --- a/build-settings/rollup.config.js +++ b/build-settings/rollup.config.js @@ -5,6 +5,7 @@ import path from "path"; import autoExternal from "rollup-plugin-auto-external"; import babel from "rollup-plugin-babel"; import resolve from "@rollup/plugin-node-resolve"; +import postcss from "rollup-plugin-postcss"; const {presets, plugins} = require("./babel.config")({env: () => false}); @@ -50,6 +51,10 @@ const createConfig = (pkgName) => { autoExternal({ packagePath: `packages/${pkgName}/package.json`, }), + postcss({ + modules: true, + extract: false, + }), ], }; }; diff --git a/package.json b/package.json index be2e96cfaf..4be46dba03 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,7 @@ "rollup": "^2.79.1", "rollup-plugin-auto-external": "^2.0.0", "rollup-plugin-babel": "^4.0.0-beta.2", + "rollup-plugin-postcss": "^4.0.2", "storybook": "^8.0.0", "storybook-addon-pseudo-states": "^2.1.2", "typescript": "^4.9.5", diff --git a/packages/wonder-blocks-core/src/components/view.module.css b/packages/wonder-blocks-core/src/components/view.module.css new file mode 100644 index 0000000000..792e4429da --- /dev/null +++ b/packages/wonder-blocks-core/src/components/view.module.css @@ -0,0 +1,15 @@ +.default { + align-items: stretch; + border-width: 0; + border-style: solid; + box-sizing: border-box; + display: flex; + flex-direction: column; + margin: 0; + padding: 0; + position: relative; + z-index: 0; + /* fix flexbox bugs */ + min-height: 0; + min-width: 0; +} \ No newline at end of file diff --git a/packages/wonder-blocks-core/src/components/view.tsx b/packages/wonder-blocks-core/src/components/view.tsx index c2213d4b02..499321af1a 100644 --- a/packages/wonder-blocks-core/src/components/view.tsx +++ b/packages/wonder-blocks-core/src/components/view.tsx @@ -1,12 +1,16 @@ // WARNING: If you modify this file you must update view.js.flow. -import * as React from "react"; import {StyleSheet} from "aphrodite"; +import * as React from "react"; + +// import addStyle from "../util/add-style"; -import addStyle from "../util/add-style"; +import type {StyleType, TextViewSharedProps} from "../util/types"; -import type {TextViewSharedProps} from "../util/types"; +import {processStyleList} from "../util/util"; -const styles = StyleSheet.create({ +import styles from "./view.module.css"; + +const stylesOld = StyleSheet.create({ // https://github.com/facebook/css-layout#default-values default: { alignItems: "stretch", @@ -33,11 +37,11 @@ type Props = TextViewSharedProps & { tag?: ValidViewTags; }; -const StyledDiv = addStyle("div", styles.default); -const StyledArticle = addStyle("article", styles.default); -const StyledAside = addStyle("aside", styles.default); -const StyledNav = addStyle("nav", styles.default); -const StyledSection = addStyle("section", styles.default); +// const StyledDiv = addStyle("div", styles.default); +// const StyledArticle = addStyle("article", styles.default); +// const StyledAside = addStyle("aside", styles.default); +// const StyledNav = addStyle("nav", styles.default); +// const StyledSection = addStyle("section", styles.default); /** * View is a building block for constructing other components. `View` roughly @@ -66,31 +70,44 @@ const StyledSection = addStyle("section", styles.default); * ``` */ +const isStyleString = (style: string | StyleType) => + style && typeof style === "string"; + const View: React.ForwardRefExoticComponent< Props & React.RefAttributes > = React.forwardRef(function View(props, ref) { - const {testId, tag = "div", ...restProps} = props; + const {style, testId, tag = "div", ...restProps} = props; + + let className = styles.default; + let aphroditeStyle = null; + + if (isStyleString(style)) { + className += ` ${style}`; + // StyleType + } else { + aphroditeStyle = processStyleList([stylesOld.default, style]); + } + const commonProps = { ...restProps, // Note: this matches the default test id that Testing Library uses! "data-testid": testId, + className: isStyleString(style) ? className : aphroditeStyle?.className, + style: aphroditeStyle?.style, } as const; switch (tag) { case "article": - return ; + return
; case "aside": - return ; + return