diff --git a/.changepacks/changepack_log_metro_expo_web.json b/.changepacks/changepack_log_metro_expo_web.json new file mode 100644 index 000000000..bd3611187 --- /dev/null +++ b/.changepacks/changepack_log_metro_expo_web.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/metro-plugin/package.json": "Patch" + }, + "note": "Document Expo web (part of #411): Metro compiles the web build through react-native-web with the same output as iOS and Android, which the Expo example now exports and checks in Chromium.", + "date": "2026-11-21T00:00:00.000Z" +} diff --git a/.changepacks/changepack_log_metro_types.json b/.changepacks/changepack_log_metro_types.json new file mode 100644 index 000000000..f255a5bc0 --- /dev/null +++ b/.changepacks/changepack_log_metro_types.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/metro-plugin/package.json": "Patch" + }, + "note": "Write `df/theme.d.ts` (the tokens of devup.json) and `df/native.d.ts` (React Native props for each component) when Metro reads the config, with a `distDir` option, as the web plugins do.", + "date": "2026-11-21T00:00:00.000Z" +} diff --git a/.changepacks/changepack_log_react_native_types.json b/.changepacks/changepack_log_react_native_types.json new file mode 100644 index 000000000..ac8e96aa9 --- /dev/null +++ b/.changepacks/changepack_log_react_native_types.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/react/package.json": "Patch" + }, + "note": "Add `@devup-ui/react/native/types`, which types each component as the React Native component it renders (`Box` takes `ViewProps`, `Button` takes `PressableProps`, ...) through the new `DevupElementProps` augmentation point. Web types are unchanged.", + "date": "2026-11-21T00:00:00.000Z" +} diff --git a/apps/expo/app.json b/apps/expo/app.json index ad7952e54..c9389260f 100644 --- a/apps/expo/app.json +++ b/apps/expo/app.json @@ -3,6 +3,6 @@ "name": "Devup UI Expo", "slug": "devup-ui-expo", "version": "0.1.0", - "platforms": ["ios", "android"] + "platforms": ["ios", "android", "web"] } } diff --git a/apps/expo/check-bundle.mjs b/apps/expo/check-bundle.mjs index 81297af7a..704d8d76f 100644 --- a/apps/expo/check-bundle.mjs +++ b/apps/expo/check-bundle.mjs @@ -114,7 +114,7 @@ function runtimeUsers(code, locate) { return users } -for (const platform of ['ios', 'android']) { +for (const platform of ['ios', 'android', 'web']) { const directory = join(root, platform) const bundles = readdirSync(directory).filter((file) => file.endsWith('.js')) if (bundles.length === 0) failures.push(`${platform}: no JavaScript bundle`) diff --git a/apps/expo/check-web.mjs b/apps/expo/check-web.mjs new file mode 100644 index 000000000..2d28949c7 --- /dev/null +++ b/apps/expo/check-web.mjs @@ -0,0 +1,102 @@ +// Opens the web export (`expo export --platform web` wrote dist/) in Chromium +// and checks that react-native-web renders the styles Devup UI compiled: the +// card takes the theme's color and switches with the theme, the input takes +// its focus style through the runtime's `Focused`, and the button, a +// `Pressable`, counts a press. +import { createReadStream, existsSync, statSync } from 'node:fs' +import { createServer } from 'node:http' +import { extname, join, normalize } from 'node:path' + +import { chromium } from '@playwright/test' + +const root = join(import.meta.dirname, 'dist') +const TYPES = { + '.html': 'text/html', + '.js': 'text/javascript', + '.json': 'application/json', + '.png': 'image/png', +} + +const server = createServer((request, response) => { + const path = join(root, normalize(new URL(request.url, 'http://x').pathname)) + if (!path.startsWith(root)) { + response.writeHead(404).end() + return + } + // A path that is no file is the app's page + const file = + existsSync(path) && statSync(path).isFile() + ? path + : join(root, 'index.html') + response.writeHead(200, { + 'content-type': TYPES[extname(file)] ?? 'application/octet-stream', + }) + createReadStream(file).pipe(response) +}) +await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) +const url = `http://127.0.0.1:${server.address().port}/` + +const failures = [] +/** Fail with `message` unless `actual` is `expected` */ +function expectEqual(actual, expected, message) { + if (actual !== expected) { + failures.push(`${message}: expected ${expected}, got ${actual}`) + } +} + +const browser = await chromium.launch() +try { + const page = await browser.newPage({ colorScheme: 'light' }) + const errors = [] + page.on('pageerror', (error) => errors.push(error.message)) + await page.goto(url) + const card = page.getByTestId('card') + const name = page.getByTestId('name') + const count = page.getByTestId('count') + await card.waitFor() + /** The computed style `property` of `locator` */ + const style = (locator, property) => + locator.evaluate( + (element, property) => getComputedStyle(element)[property], + property, + ) + + expectEqual( + await style(card, 'backgroundColor'), + 'rgb(255, 255, 255)', + 'the card in the light theme', + ) + expectEqual( + await style(count, 'backgroundColor'), + 'rgb(90, 68, 255)', + 'the count button', + ) + await name.focus() + expectEqual( + await style(name, 'borderTopColor'), + 'rgb(90, 68, 255)', + 'the focused input', + ) + await count.click() + expectEqual( + (await count.textContent())?.trim(), + 'Pressed 1 times', + 'the count after a press', + ) + await page.getByTestId('theme').click() + expectEqual( + await style(card, 'backgroundColor'), + 'rgb(31, 31, 46)', + 'the card in the dark theme', + ) + for (const error of errors) failures.push(`page error: ${error}`) +} finally { + await browser.close() + server.close() +} + +if (failures.length > 0) { + console.error(failures.join('\n')) + process.exit(1) +} +console.info('web: react-native-web renders the compiled styles') diff --git a/apps/expo/package.json b/apps/expo/package.json index c9662f3d0..0f4dea58c 100644 --- a/apps/expo/package.json +++ b/apps/expo/package.json @@ -5,15 +5,20 @@ "main": "index.ts", "scripts": { "start": "expo start", - "export": "expo export --platform ios --platform android --no-bytecode --source-maps", - "check": "node check-bundle.mjs" + "export": "expo export --platform ios --platform android --platform web --no-bytecode --source-maps", + "check": "node check-bundle.mjs", + "check:web": "node check-web.mjs", + "typecheck": "bun ../../node_modules/@typescript/native/bin/tsc --noEmit" }, "dependencies": { "@devup-ui/react": "workspace:^", + "@expo/metro-runtime": "~58.0.17", "expo": "~58.0.26", "react": "^19.3.1", + "react-dom": "^19.3.1", "react-native": "0.88.1", "react-native-reanimated": "~4.7.1", + "react-native-web": "~0.21.4", "react-native-worklets": "0.13.0" }, "devDependencies": { diff --git a/apps/expo/src/App.tsx b/apps/expo/src/App.tsx index 56d362fdd..e6318fa87 100644 --- a/apps/expo/src/App.tsx +++ b/apps/expo/src/App.tsx @@ -59,6 +59,7 @@ export function App() { boxShadow="$raised" maxW={[null, null, '480px']} p={[4, null, 6]} + testID="card" w="100%" > @@ -72,6 +73,7 @@ export function App() { onChangeText={setName} px={3} py={2} + testID="name" value={name} /> @@ -88,6 +90,7 @@ export function App() { bg="#5a44ff" className={button} onPress={() => setCount(count + 1)} + testID="count" > Pressed {count} times @@ -98,6 +101,7 @@ export function App() { border="1px solid $border" className={button} onPress={() => setTheme(theme === 'dark' ? 'light' : 'dark')} + testID="theme" > Switch to {theme === 'dark' ? 'light' : 'dark'} diff --git a/apps/landing/src/app/(detail)/docs/LeftMenu.tsx b/apps/landing/src/app/(detail)/docs/LeftMenu.tsx index db5adc836..402cd2450 100644 --- a/apps/landing/src/app/(detail)/docs/LeftMenu.tsx +++ b/apps/landing/src/app/(detail)/docs/LeftMenu.tsx @@ -44,6 +44,7 @@ export function LeftMenu() { {t('features')} {t('limitations')} {t('buildErrors')} + React Native **Experimental (0.x).** React Native support is in progress +> ([#411](https://github.com/dev-five-git/devup-ui/issues/411)) and +> `@devup-ui/metro-plugin` is not published yet. Its API may change. + +Devup UI compiles for React Native in Metro, the bundler of React Native and +Expo. You keep importing `Box`, `Text`, `css`, `styled`, ... from +`@devup-ui/react`. At build time each element becomes a React Native component, +and its style props become style objects hoisted into one `StyleSheet.create` +per file. CSS is never parsed at runtime. + +## Setup + +```bash +npm install @devup-ui/react @devup-ui/metro-plugin +``` + +```js +// metro.config.js (Expo) +const { getDefaultConfig } = require('expo/metro-config') +const { withDevupUI } = require('@devup-ui/metro-plugin') + +module.exports = withDevupUI(getDefaultConfig(__dirname)) +``` + +```js +// metro.config.js (React Native CLI) +const { getDefaultConfig } = require('@react-native/metro-config') +const { withDevupUI } = require('@devup-ui/metro-plugin') + +module.exports = withDevupUI(getDefaultConfig(__dirname)) +``` + +```tsx +import { Box, Flex, Text } from '@devup-ui/react' + +export function Card() { + return ( + + + Hello + + + + ) +} +``` + +Metro writes `df/theme.d.ts` and `df/native.d.ts` when it reads the config. +With them, theme tokens complete in your editor, and each component takes the +props of the React Native component it renders (`onPress`, `onChangeText`, +`testID`, ...). + +## Elements + +| Element | React Native | +| --------------------------------- | -------------------------------- | +| `Box`, `Flex`, `VStack`, `Center` | `View` | +| `Text` | `Text` | +| `Image` | `Image` (`src` becomes `source`) | +| `Input` | `TextInput` | +| `Button` | `Pressable` | + +## Differences from the web + +The same component code builds for both, but React Native has no CSS, so some +things work differently. + +| | Web | React Native | +| ------------------------------------------ | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Output | Atomic CSS classes in a stylesheet | Style objects in one `StyleSheet.create` per file | +| Runtime | None | None for static styles. A small runtime (about 1.2 kB gzip) reads the theme, the window and press, hover and focus states, but only for the styles that use them. With `runtime: false`, those styles are build errors instead. | +| Numbers | `p={4}` is `16px` | `p={4}` is `16`: density-independent pixels, with the same scale as the web | +| Units | Any CSS unit | Pixels, `rem` and `em` (16 pixels each), percentages and `auto`. `vw`, `vh` and other units are build errors. | +| Themes | CSS variables, switched by `data-theme` | The runtime picks the style object of the current theme. `setTheme`, `useTheme` and `getTheme` work, and the system appearance picks `light` or `dark`. | +| Breakpoints | Media queries | The window width, with the `breakpoints` of devup.json. A component renders again only when the window crosses into another breakpoint. | +| `_active`, `_hover`, `_focus`, `_disabled` | CSS pseudo-classes | `Pressable` tells its style function whether it is pressed, hovered or focused: a `Button` is one, and a `Box` with these styles becomes one. `_focus` on an `Input` works through the runtime. `_disabled` reads the `disabled` prop. Hover and focus happen only where there is a pointer or keyboard focus, such as the web. | +| Other selectors | `_before`, `_focusVisible`, `selectors`, ... | Build errors: React Native has no selectors | +| `_ios`, `_android`, `_web` | — | Apply on that platform only | +| `_portrait`, `_landscape` | Media queries | The window's orientation | +| `className={css(...)}` | A class | The build turns it into the element's styles | +| `keyframes`, `animation*`, `transition*` | CSS animations | [react-native-reanimated](https://docs.swmansion.com/react-native-reanimated/): an element that animates renders Reanimated's animated component. Without Reanimated installed, these are build errors. | +| `globalCss`, `Grid` | Supported | Build errors | +| styled-components and Emotion | `styled-components`, `@emotion/styled`, ... | `styled-components/native` and `@emotion/native` compile the same way. Their numbers are pixels, as those packages read them. | +| Unsupported properties | — | A property React Native has no style for (`gridTemplateColumns`, `float`, ...) is a build error | + +Build errors point at the file, line and column of the style, as on the web. + +## Expo web + +On the web, Expo bundles the app with Metro through +[react-native-web](https://necolas.github.io/react-native-web/), and the same +native output turns into CSS there. A site that is not an Expo app keeps using +the [Vite, Next.js, Webpack, Rsbuild or Bun plugin](/docs/installation), which +output atomic CSS instead. + +See the +[`@devup-ui/metro-plugin` README](https://github.com/dev-five-git/devup-ui/tree/main/packages/metro-plugin) +for every option and each feature in detail. diff --git a/bun.lock b/bun.lock index 699ed7003..62ce0b80a 100644 --- a/bun.lock +++ b/bun.lock @@ -27,10 +27,13 @@ "version": "0.1.0", "dependencies": { "@devup-ui/react": "workspace:^", + "@expo/metro-runtime": "~58.0.17", "expo": "~58.0.26", "react": "^19.3.1", + "react-dom": "^19.3.1", "react-native": "0.88.1", "react-native-reanimated": "~4.7.1", + "react-native-web": "~0.21.4", "react-native-worklets": "0.13.0", }, "devDependencies": { @@ -1262,6 +1265,8 @@ "@expo/metro-file-map": ["@expo/metro-file-map@58.0.6", "", { "dependencies": { "2g": "^1.0.1", "debug": "^4.3.4", "fb-watchman": "^2.0.2", "invariant": "^2.2.4", "jest-worker": "^29.7.0", "walker": "^1.0.8" } }, "sha512-+kPFI328BSUxpK7OO0kM31NQlYG/z7WOTISbPEuY4fMZxmN/nLnmJIDNG8NTtM5ZHXlNPqgjRvdukxtsBEkvuw=="], + "@expo/metro-runtime": ["@expo/metro-runtime@58.0.17", "", { "dependencies": { "@expo/log-box": "^58.0.15", "anser": "^1.4.9", "pretty-format": "^29.7.0", "stacktrace-parser": "^0.1.10", "whatwg-fetch": "^3.0.0" }, "peerDependencies": { "expo": "*", "react": "*", "react-dom": "*", "react-native": "*" }, "optionalPeers": ["react-dom"] }, "sha512-onJvBi90uwLj69F1EaVPmPeds/ndxc2/vNc0Z5n8rqcMKToMyLxGaL3Be6yf7OVjPhGvoDj+LWQ/IcbpN1hnoA=="], + "@expo/modules-macros-linux-arm64": ["@expo/modules-macros-linux-arm64@0.33.0", "", { "os": "linux", "cpu": "arm64", "libc": "glibc" }, "sha512-5F4L/IJThOBq3oRo9NB5BI4+Od8K2YDTbU+2IHgsnXsrTc5icQtOlLwHOEfExghjo0PHCXGap4zprfOKvtANxw=="], "@expo/modules-macros-linux-x64": ["@expo/modules-macros-linux-x64@0.33.0", "", { "os": "linux", "cpu": "x64", "libc": "glibc" }, "sha512-G3kxfmMpH7BDtoeBVeJA4vh5O3kQ3wmjmSfDremRBWENHKwHJXTOsyerLQqkJC39oPKCuXgXhzvp+/5qy+DyvA=="], @@ -2502,6 +2507,8 @@ "cosmiconfig": ["cosmiconfig@7.1.0", "", { "dependencies": { "@types/parse-json": "^4.0.0", "import-fresh": "^3.2.1", "parse-json": "^5.0.0", "path-type": "^4.0.0", "yaml": "^1.10.0" } }, "sha512-AdmX6xUzdNASswsFtmwSt7Vj8po9IuqXm0UXz7QKPuEUmPB4XyjGfaAr2PSuELMwkRMVH1EpIkX5bTZGRB3eCA=="], + "cross-fetch": ["cross-fetch@3.2.0", "", { "dependencies": { "node-fetch": "^2.7.0" } }, "sha512-Q+xVJLoGOeIMXZmbUK4HYk+69cQH6LudR0Vu/pRm2YlU/hDV9CiS0gKUMaWY5f2NeUH9C1nV3bsTlCo0FsTV1Q=="], + "cross-spawn": ["cross-spawn@7.0.6", "", { "dependencies": { "path-key": "^3.1.0", "shebang-command": "^2.0.0", "which": "^2.0.1" } }, "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA=="], "css-background-parser": ["css-background-parser@0.1.0", "", {}, "sha512-2EZLisiZQ+7m4wwur/qiYJRniHX4K5Tc9w93MT3AS0WS1u5kaZ4FKXlOTBhOjc+CgEgPiGY+fX1yWD8UwpEqUA=="], @@ -2512,6 +2519,8 @@ "css-gradient-parser": ["css-gradient-parser@0.0.17", "", {}, "sha512-w2Xy9UMMwlKtou0vlRnXvWglPAceXCTtcmVSo8ZBUvqCV5aXEFP/PC6d+I464810I9FT++UACwTD5511bmGPUg=="], + "css-in-js-utils": ["css-in-js-utils@3.1.0", "", { "dependencies": { "hyphenate-style-name": "^1.0.3" } }, "sha512-fJAcud6B3rRu+KHYk+Bwf+WFL2MDCJJ1XG9x137tJQ0xYxor7XziQtuGFbWNdqrvF4Tk26O3H73nfVqXt/fW1A=="], + "css-mediaquery": ["css-mediaquery@0.1.2", "", {}, "sha512-COtn4EROW5dBGlE/4PiKnh6rZpAPxDeFLaEEwt4i10jpDMFt2EhQGS79QmmrO+iKCHv0PU/HrOWEhijFd1x99Q=="], "css-to-react-native": ["css-to-react-native@3.2.0", "", { "dependencies": { "camelize": "^1.0.0", "css-color-keywords": "^1.0.0", "postcss-value-parser": "^4.0.2" } }, "sha512-e8RKaLXMOFii+02mOlqwjbD00KSEKqblnpO9e++1aXS1fPQOpS1YoqdVHBqPjHNoxeF2mimzVqawm2KCbEdtHQ=="], @@ -2744,6 +2753,10 @@ "fb-watchman": ["fb-watchman@2.0.4", "", { "dependencies": { "bser": "2.1.2" } }, "sha512-tGQtaoYisPDriJBN0mT5yZSyNETnJGA1gTYxaVRAE5KUO3LI9kPUc9WIu8JXcxaIci+faIKlzXqXyPgrmubtBQ=="], + "fbjs": ["fbjs@3.0.5", "", { "dependencies": { "cross-fetch": "^3.1.5", "fbjs-css-vars": "^1.0.0", "loose-envify": "^1.0.0", "object-assign": "^4.1.0", "promise": "^7.1.1", "setimmediate": "^1.0.5", "ua-parser-js": "^1.0.35" } }, "sha512-ztsSx77JBtkuMrEypfhgc3cI0+0h+svqeie7xHbh1k/IKdcydnvadp/mUaGgjAOXQmQSxsqgaRhS3q9fy+1kxg=="], + + "fbjs-css-vars": ["fbjs-css-vars@1.0.2", "", {}, "sha512-b2XGFAFdWZWg0phtAWLHCk836A1Xann+I+Dgd3Gk64MHKZO44FfoD1KxyvbSh0qZsIoXQGGlVztIY+oitJPpRQ=="], + "fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" }, "optionalPeers": ["picomatch"] }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="], "fetch-nodeshim": ["fetch-nodeshim@0.4.10", "", {}, "sha512-m6I8ALe4L4XpdETy7MJZWs6L1IVMbjs99bwbpIKphxX+0CTns4IKDWJY0LWfr4YsFjfg+z1TjzTMU8lKl8rG0w=="], @@ -2848,6 +2861,8 @@ "husky": ["husky@9.1.7", "", { "bin": { "husky": "bin.js" } }, "sha512-5gs5ytaNjBrh5Ow3zrvdUUY+0VxIuWVL4i9irt6friV+BqdCfmV11CQTWMiBYWHbXhco+J1kHfTOUkePhCDvMA=="], + "hyphenate-style-name": ["hyphenate-style-name@1.1.0", "", {}, "sha512-WDC/ui2VVRrz3jOVi+XtjqkDjiVjTtFaAGiW37k6b+ohyQ5wYDOGkvCZa8+H0nx3gyvv0+BST9xuOgIyGQ00gw=="], + "icu-minify": ["icu-minify@4.15.2", "", { "dependencies": { "@formatjs/icu-messageformat-parser": "^3.4.0" } }, "sha512-IsgBR7/1RBkYWUqP2wS+vE9BBBfH4HjNjDbSPF3uK2Jjlzlsd7R+loUhjEEUySU6tjGmUIuft7w3qp7PI0KkXA=="], "ignore": ["ignore@5.3.2", "", {}, "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g=="], @@ -2868,6 +2883,8 @@ "inline-style-parser": ["inline-style-parser@0.2.9", "", {}, "sha512-IttN8BqKLxzUrnTqHI+/hwAJQItBAUjdoHERCDv+oOuX48VP6bzW8+QjOXSfoevzq3YrYAwX1uLHp8TqO9yIeg=="], + "inline-style-prefixer": ["inline-style-prefixer@7.0.1", "", { "dependencies": { "css-in-js-utils": "^3.1.0" } }, "sha512-lhYo5qNTQp3EvSSp3sRvXMbVQTLrvGV6DycRMJ5dm2BLMiJ30wpXKdDdgX+GmJZ5uQMucwRKHamXSst3Sj/Giw=="], + "intl-messageformat": ["intl-messageformat@12.2.0", "", { "dependencies": { "@formatjs/fast-memoize": "3.1.7", "@formatjs/icu-messageformat-parser": "3.5.23" } }, "sha512-zFzW80iYSSJm4V185g3ItJQ96lBZ2RDO8O8qmRiPAAVKYXJeHfDQN2ITdtLlV+CjSi9Lo7zVW9xGt9CPCx7J6A=="], "invariant": ["invariant@2.2.4", "", { "dependencies": { "loose-envify": "^1.0.0" } }, "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA=="], @@ -3228,6 +3245,8 @@ "node-addon-api": ["node-addon-api@7.1.1", "", {}, "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ=="], + "node-fetch": ["node-fetch@2.7.0", "", { "dependencies": { "whatwg-url": "^5.0.0" }, "peerDependencies": { "encoding": "^0.1.0" }, "optionalPeers": ["encoding"] }, "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A=="], + "node-forge": ["node-forge@1.4.3", "", {}, "sha512-P6wzfh6t70avmV8FrVEAd/36sOPJQ+hkaJVX1Laap9J/77m7M0eyozDvc8SwnDf4P7MP94gwbAAo0rg3YVXT+g=="], "node-int64": ["node-int64@0.4.0", "", {}, "sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw=="], @@ -3404,6 +3423,8 @@ "react-native-reanimated": ["react-native-reanimated@4.7.2", "", { "dependencies": { "react-native-is-edge-to-edge": "^1.3.1", "semver": "^7.7.3" }, "peerDependencies": { "react": "*", "react-native": "0.86 - 0.88", "react-native-worklets": "0.13.x" } }, "sha512-dlPMG+dKKbcumR8k2C8wApqVsrCevINxEki3I6t0S/6LPqa639unbwRn85yu1yhtC9oJI94xk+ycdGuEGdH4Gw=="], + "react-native-web": ["react-native-web@0.21.4", "", { "dependencies": { "@babel/runtime": "^7.18.6", "@react-native/normalize-colors": "^0.74.1", "fbjs": "^3.0.4", "inline-style-prefixer": "^7.0.1", "memoize-one": "^6.0.0", "nullthrows": "^1.1.1", "postcss-value-parser": "^4.2.0", "styleq": "^0.1.3" }, "peerDependencies": { "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" } }, "sha512-77B6KlalB4osNIck91TNFixPkYPsTbuNEvw8P7IV+4v4/5wZSBRKZnw0Zhx6YjuauDTz71gc8IW+fVhcBMjr5A=="], + "react-native-worklets": ["react-native-worklets@0.13.0", "", { "dependencies": { "@babel/generator": "^7.27.1", "@babel/plugin-transform-arrow-functions": "^7.27.1", "@babel/plugin-transform-class-properties": "^7.28.6", "@babel/plugin-transform-classes": "^7.28.6", "@babel/plugin-transform-nullish-coalescing-operator": "^7.28.6", "@babel/plugin-transform-optional-chaining": "^7.28.6", "@babel/plugin-transform-shorthand-properties": "^7.27.1", "@babel/plugin-transform-template-literals": "^7.27.1", "@babel/plugin-transform-unicode-regex": "^7.27.1", "@babel/preset-typescript": "^7.28.5", "@babel/traverse": "^7.27.1", "@babel/types": "^7.27.1", "@jridgewell/remapping": "^2.3.5", "convert-source-map": "^2.0.0", "semver": "^7.7.4", "whatwg-fetch": "^3.6.20" }, "peerDependencies": { "@babel/core": "*", "@react-native/metro-config": "*", "react": "*", "react-native": "0.86 - 0.88" } }, "sha512-1OrhAeAKaZF4XrBa4YbPQbKzNOpjh52HDlId8e6ete1OCHNdAlrQHzG4gznJ225EAj3dgQXHViJvK51M9B52jQ=="], "react-refresh": ["react-refresh@0.14.2", "", {}, "sha512-jCvmsr+1IUSMUyzOkRcvnVbX3ZYC6g9TDrDbFuFmRDq7PD4yaGbLKNQL6k2jnArV8hjYxh7hVhAZB6s9HDGpZA=="], @@ -3516,6 +3537,8 @@ "set-blocking": ["set-blocking@2.0.0", "", {}, "sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw=="], + "setimmediate": ["setimmediate@1.0.5", "", {}, "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA=="], + "setprototypeof": ["setprototypeof@1.2.0", "", {}, "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="], "sharp": ["sharp@0.35.5", "", { "dependencies": { "@img/colour": "^1.1.0", "detect-libc": "^2.1.2", "semver": "^7.8.5" }, "optionalDependencies": { "@img/sharp-darwin-arm64": "0.35.5", "@img/sharp-darwin-x64": "0.35.5", "@img/sharp-freebsd-wasm32": "0.35.5", "@img/sharp-libvips-darwin-arm64": "1.3.4", "@img/sharp-libvips-darwin-x64": "1.3.4", "@img/sharp-libvips-linux-arm": "1.3.4", "@img/sharp-libvips-linux-arm64": "1.3.4", "@img/sharp-libvips-linux-ppc64": "1.3.4", "@img/sharp-libvips-linux-riscv64": "1.3.4", "@img/sharp-libvips-linux-s390x": "1.3.4", "@img/sharp-libvips-linux-x64": "1.3.4", "@img/sharp-libvips-linuxmusl-arm64": "1.3.4", "@img/sharp-libvips-linuxmusl-x64": "1.3.4", "@img/sharp-linux-arm": "0.35.5", "@img/sharp-linux-arm64": "0.35.5", "@img/sharp-linux-ppc64": "0.35.5", "@img/sharp-linux-riscv64": "0.35.5", "@img/sharp-linux-s390x": "0.35.5", "@img/sharp-linux-x64": "0.35.5", "@img/sharp-linuxmusl-arm64": "0.35.5", "@img/sharp-linuxmusl-x64": "0.35.5", "@img/sharp-webcontainers-wasm32": "0.35.5", "@img/sharp-win32-arm64": "0.35.5", "@img/sharp-win32-ia32": "0.35.5", "@img/sharp-win32-x64": "0.35.5" }, "peerDependencies": { "@types/node": "*" }, "optionalPeers": ["@types/node"] }, "sha512-Ywn4OnzGukp7CDMrp08RQ50YKmuwG47brZgIVPTvBaaAfQlRlygrRqSrxdCiL9M+LlzLBiJ68IR1QqvzHyjC7g=="], @@ -3592,7 +3615,7 @@ "styled-jsx": ["styled-jsx@5.1.6", "", { "dependencies": { "client-only": "0.0.1" }, "peerDependencies": { "@babel/core": "*", "babel-plugin-macros": "*", "react": ">= 16.8.0 || 17.x.x || ^18.0.0-0 || ^19.0.0-0" }, "optionalPeers": ["@babel/core", "babel-plugin-macros"] }, "sha512-qSVyDTeMotdvQYoHWLNGwRFJHC+i+ZvdBRYosOFgC+Wg1vx4frN2/RG/NA7SYqqvKNLf39P2LSRA2pu6n0XYZA=="], - "styleq": ["styleq@0.2.1", "", {}, "sha512-L0TR0NQb+X4/ktDEKmjWyp27gla+LUYi/by5k5SjKXf6/pvZP7wbwEB5J+tqxdFVPgzbsuz+d4RTScO/QZquBw=="], + "styleq": ["styleq@0.1.3", "", {}, "sha512-3ZUifmCDCQanjeej1f6kyl/BeP/Vae5EYkQ9iJfUm/QwZvlgnZzyflqAsAWYURdtea8Vkvswu2GrC57h3qffcA=="], "stylis": ["stylis@4.2.0", "", {}, "sha512-Orov6g6BB1sDfYgzWfTHDOxamtX1bE/zo104Dh9e6fqJ3PooipYyfJ0pUmrZO2wAvO8YbEyeFrkV91XTsGMSrw=="], @@ -3634,6 +3657,8 @@ "toqr": ["toqr@0.1.1", "", {}, "sha512-FWAPzCIHZHnrE/5/w9MPk0kK25hSQSH2IKhYh9PyjS3SG/+IEMvlwIHbhz+oF7xl54I+ueZlVnMjyzdSwLmAwA=="], + "tr46": ["tr46@0.0.3", "", {}, "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="], + "trim-lines": ["trim-lines@3.0.1", "", {}, "sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg=="], "trough": ["trough@2.2.0", "", {}, "sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw=="], @@ -3664,6 +3689,8 @@ "typescript-eslint": ["typescript-eslint@8.73.0", "", { "dependencies": { "@typescript-eslint/eslint-plugin": "8.73.0", "@typescript-eslint/parser": "8.73.0", "@typescript-eslint/typescript-estree": "8.73.0", "@typescript-eslint/utils": "8.73.0" }, "peerDependencies": { "eslint": "^8.57.0 || ^9.0.0 || ^10.0.0", "typescript": ">=4.8.4 <6.1.0" } }, "sha512-+EnKy9rkYPun4l+kG2bDuYWQ6y1tGVgdR/tY1U9AC6h21CjEwOoc7gsjwe0FXB8uWFzk4QiCFbuRWgX758PiAg=="], + "ua-parser-js": ["ua-parser-js@1.0.41", "", { "bin": { "ua-parser-js": "script/cli.js" } }, "sha512-LbBDqdIC5s8iROCUjMbW1f5dJQTEFB1+KO9ogbvlb3nm9n4YHa5p4KTvFPWvh2Hs8gZMBuiB1/8+pdfe/tDPug=="], + "ufo": ["ufo@1.6.4", "", {}, "sha512-JFNbkD1Svwe0KvGi8GOeLcP4kAWQ609twvCdcHxq1oSL8svv39ZuSvajcD8B+5D0eL4+s1Is2D/O6KN3qcTeRA=="], "undici-types": ["undici-types@8.9.0", "", {}, "sha512-KTDyRTYX8sWmKXAikPHHSyc63CRPETMctyjKFupcC6OBLXT3xsN0e9aF7m+mIXutFWpUXuedtowG7iLOzp0kQg=="], @@ -3780,6 +3807,8 @@ "web-vitals": ["web-vitals@4.2.4", "", {}, "sha512-r4DIlprAGwJ7YM11VZp4R884m0Vmgr6EAKe3P+kO0PPj3Unqyvv59rczf6UiGcb9Z8QxZVcqKNwv/g0WNdWwsw=="], + "webidl-conversions": ["webidl-conversions@3.0.1", "", {}, "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="], + "webpack": ["webpack@5.112.2", "", { "dependencies": { "@types/estree": "^1.0.8", "@types/json-schema": "^7.0.15", "@webassemblyjs/ast": "^1.14.1", "@webassemblyjs/wasm-edit": "^1.14.1", "@webassemblyjs/wasm-parser": "^1.14.1", "browserslist": "^4.28.1", "chrome-trace-event": "^1.0.2", "enhanced-resolve": "^5.26.0", "es-module-lexer": "^2.1.0", "events": "^3.2.0", "graceful-fs": "^4.2.11", "mime-db": "^1.54.0", "minimizer-webpack-plugin": "^5.13.0", "schema-utils": "^4.8.1", "tapable": "^2.3.0", "watchpack": "^2.6.3", "webpack-sources": "^3.6.0" }, "peerDependencies": { "webpack-cli": "*" }, "optionalPeers": ["webpack-cli"], "bin": { "webpack": "bin/webpack.js" } }, "sha512-B2tp1LI9D8GVpPDqprdt12ukTkXXahj9MzqSWJq4faSPgOUM7CdZaGAB6HrL/8E6UwbBpdj4vzWDbKoFnVoU6g=="], "webpack-sources": ["webpack-sources@3.7.1", "", {}, "sha512-mcxs7A2LsEdwsNqQburqS4QHPR2Ca3NGplUakn6J5GPQvnVZ4oD4IM4FJcT3f8AVN2e2EsjuIVxKj4rorHvyuQ=="], @@ -3790,6 +3819,8 @@ "whatwg-mimetype": ["whatwg-mimetype@3.0.0", "", {}, "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q=="], + "whatwg-url": ["whatwg-url@5.0.0", "", { "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" } }, "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw=="], + "whatwg-url-minimum": ["whatwg-url-minimum@0.2.0", "", {}, "sha512-BtjXU1caNcCFfETP2DhqSeVPfRDl2MTUitI107RE34rJXjR/izfUtR5IoTFOK1WqjNLXbbeqaCH/mQAOLjpc8g=="], "which": ["which@2.0.2", "", { "dependencies": { "isexe": "^2.0.0" }, "bin": { "node-which": "./bin/node-which" } }, "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA=="], @@ -4406,6 +4437,8 @@ "@stylexjs/postcss-plugin/@babel/core": ["@babel/core@7.29.13", "", { "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.13", "@babel/helper-compilation-targets": "^7.29.13", "@babel/helper-module-transforms": "^7.29.13", "@babel/helpers": "^7.29.13", "@babel/parser": "^7.29.13", "@babel/template": "^7.29.7", "@babel/traverse": "^7.29.13", "@babel/types": "^7.29.11", "@jridgewell/remapping": "^2.3.5", "convert-source-map": "^2.0.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", "json5": "^2.2.3", "semver": "^6.3.1" } }, "sha512-j1nvOQCZYSgp7HLmisnCXFTpVvFODiijQ9QHkORfGKWH6YDtskTEeCJS7eV545GZ9UBzrq0e2xsOd5QQ5PjIqw=="], + "@stylexjs/stylex/styleq": ["styleq@0.2.1", "", {}, "sha512-L0TR0NQb+X4/ktDEKmjWyp27gla+LUYi/by5k5SjKXf6/pvZP7wbwEB5J+tqxdFVPgzbsuz+d4RTScO/QZquBw=="], + "@tailwindcss/oxide-wasm32-wasi/@emnapi/core": ["@emnapi/core@1.11.3", "", { "dependencies": { "@emnapi/wasi-threads": "1.2.3", "tslib": "^2.4.0" }, "bundled": true }, "sha512-zLpS5asjEb7lq8jYLq37N6XKaE41DIexlY1rF/z4/tIl3wo13Sqm28fRyfIsKZD+NZ8mM5RoKkpW/rBcuoSZSg=="], "@tailwindcss/oxide-wasm32-wasi/@emnapi/runtime": ["@emnapi/runtime@1.11.3", "", { "dependencies": { "tslib": "^2.4.0" }, "bundled": true }, "sha512-Xz4Tpyki7XyrpbUK1jR1AhdAdaXyhhY4lZ3neLodmhpuWfy2PAQN5B46sAiU4liOXGLkHypn/qU+jvfWSCYYLA=="], @@ -4490,6 +4523,8 @@ "fast-glob/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="], + "fbjs/promise": ["promise@7.3.1", "", { "dependencies": { "asap": "~2.0.3" } }, "sha512-nolQXZ/4L+bP/UGlkfaIujX9BKxGwmQ9OT4mOt5yvy8iK1h3wqTEJCijzGANTCCl9nWjY41juyAn2K3Q1hLLTg=="], + "finalhandler/debug": ["debug@2.6.9", "", { "dependencies": { "ms": "2.0.0" } }, "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA=="], "finalhandler/encodeurl": ["encodeurl@1.0.4", "", {}, "sha512-ImAQpWtQZKdkUxvj9CMNzB8M4IGg0DkRTZzmOHqgVFrf71JDmUacbt+iKljfIpD2yHcxI+HR76d8bwvdvtSvGA=="], @@ -4608,6 +4643,10 @@ "react-native/ws": ["ws@7.5.14", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": "^5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-u9Oi2nQ8SZnTUAf6spLLKF5TyqoG3Nbbrd4ai6Sjo4P24bycDewhQqtNMIg4bFpnpH/TD4qk4IuAVhT0HIIqug=="], + "react-native-web/@react-native/normalize-colors": ["@react-native/normalize-colors@0.74.89", "", {}, "sha512-qoMMXddVKVhZ8PA1AbUCk83trpd6N+1nF2A6k1i6LsQObyS92fELuk8kU/lQs6M7BsMHwqyLCpQJ1uFgNvIQXg=="], + + "react-native-web/memoize-one": ["memoize-one@6.0.0", "", {}, "sha512-rkpe71W0N0c0Xz6QD0eJETuWAJGnJ9afsl1srmwPrI+yBCkge5EycXXbYRyvL29zZVUWQCY7InPRCv3GDXuZNw=="], + "recast/source-map": ["source-map@0.6.1", "", {}, "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="], "redent/strip-indent": ["strip-indent@3.0.0", "", { "dependencies": { "min-indent": "^1.0.0" } }, "sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ=="], @@ -6228,6 +6267,8 @@ "@stylexjs/nextjs-plugin/@stylexjs/babel-plugin/@stylexjs/stylex": ["@stylexjs/stylex@0.11.1", "", { "dependencies": { "css-mediaquery": "^0.1.2", "invariant": "^2.2.4", "styleq": "0.2.1" } }, "sha512-1OofsiCP2DYV+Cw/iIuHYTAJRy34TtxQt0FDuQGTnNH915hb6NkPmX6iPa++9t4KP3HWR9oVRmAHkpP58BIYbw=="], + "@stylexjs/open-props/@stylexjs/stylex/styleq": ["styleq@0.2.1", "", {}, "sha512-L0TR0NQb+X4/ktDEKmjWyp27gla+LUYi/by5k5SjKXf6/pvZP7wbwEB5J+tqxdFVPgzbsuz+d4RTScO/QZquBw=="], + "@stylexjs/postcss-plugin/@babel/core/@babel/code-frame": ["@babel/code-frame@7.29.7", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.29.7", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" } }, "sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw=="], "@stylexjs/postcss-plugin/@babel/core/@babel/helper-compilation-targets": ["@babel/helper-compilation-targets@7.29.13", "", { "dependencies": { "@babel/compat-data": "^7.29.7", "@babel/helper-validator-option": "^7.29.7", "browserslist": "^4.24.0", "lru-cache": "^5.1.1", "semver": "^6.3.1" } }, "sha512-8WxJ4/Vmes3ghacj8VOJy3NW8eyHiFY/4thU/LSBuBE470+igjR/j1ZTNn8fV5VdNrLx6iXUwig526UdOvfP0w=="], @@ -7114,6 +7155,8 @@ "@stylexjs/nextjs-plugin/@babel/core/@babel/helper-compilation-targets/lru-cache": ["lru-cache@5.1.1", "", { "dependencies": { "yallist": "^3.0.2" } }, "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w=="], + "@stylexjs/nextjs-plugin/@stylexjs/babel-plugin/@stylexjs/stylex/styleq": ["styleq@0.2.1", "", {}, "sha512-L0TR0NQb+X4/ktDEKmjWyp27gla+LUYi/by5k5SjKXf6/pvZP7wbwEB5J+tqxdFVPgzbsuz+d4RTScO/QZquBw=="], + "@stylexjs/postcss-plugin/@babel/core/@babel/code-frame/js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="], "@stylexjs/postcss-plugin/@babel/core/@babel/helper-compilation-targets/browserslist": ["browserslist@4.29.6", "", { "dependencies": { "baseline-browser-mapping": "^2.11.37", "caniuse-lite": "^1.0.30001826", "electron-to-chromium": "^1.5.486", "node-releases": "^2.0.64", "update-browserslist-db": "^1.4.0" }, "bin": { "browserslist": "cli.js" } }, "sha512-CmVVIQ3RVc5HxYEc9X84GvxK/C8C1wH8TlKAiO7uTxF8PDwt7r8t9Qk3J9NkMOVzX5O/HbLv3yzxZg9HPrnI9w=="], diff --git a/e2e/exported-routes.ts b/e2e/exported-routes.ts index 855e4015a..e336225ab 100644 --- a/e2e/exported-routes.ts +++ b/e2e/exported-routes.ts @@ -89,6 +89,7 @@ const ENGLISH_EXPORTED_ROUTES = [ '/docs/migration/vanilla-extract', '/docs/overview', '/docs/quick-start', + '/docs/react-native', '/showcase', '/team', ] as const diff --git a/packages/metro-plugin/README.md b/packages/metro-plugin/README.md index 62ce77ca8..fda3842db 100644 --- a/packages/metro-plugin/README.md +++ b/packages/metro-plugin/README.md @@ -67,6 +67,8 @@ withDevupUI(config, { package: '@devup-ui/react', // devup.json, from the project root. Default: 'devup.json' devupFile: 'devup.json', + // Where the theme and React Native types are written. Default: 'df' + distDir: 'df', // Packages in node_modules that ship uncompiled Devup UI. Default: [] include: [], // Whether styles that need the React Native runtime may import it. @@ -80,6 +82,24 @@ withDevupUI(config, { }) ``` +## TypeScript + +When Metro reads the config, the plugin writes two declaration files into +`df` (the `distDir` option), which ignores itself in git: + +- `theme.d.ts`: the tokens of devup.json, as the web plugins write them, so + `bg="$primary"` and `typography="title"` complete and check. +- `native.d.ts`: it imports `@devup-ui/react/native/types`, so each + component takes the props of the React Native component it renders: + `Box`, `Flex`, `VStack`, `Center` and `Grid` take `View`'s (`onLayout`, + `testID`, ...), `Text` takes `Text`'s, `Image` takes `Image`'s, `Input` takes + `TextInput`'s (`onChangeText`, ...) and `Button` takes `Pressable`'s + (`onPress`, ...). Each also takes `className`, for `css()`. HTML props that + React Native lacks, such as `href` or `onInput`, become type errors. + +A tsconfig that names its files (`include`) must list `df/*.d.ts`. Expo's +default tsconfig includes every file of the project. + ## Themes `$` tokens of devup.json (colors, `length` and `shadow`) and `typography` @@ -310,6 +330,19 @@ const Card = styled.View({ padding: 8, backgroundColor: '$surface' }) `runtime: false` they are build errors. - Any other import stays on the package. +## Expo web + +On the web, Expo bundles the app with Metro through +[react-native-web](https://necolas.github.io/react-native-web/), and this +plugin compiles it as for iOS and Android: the same `StyleSheet` objects, +which react-native-web turns into CSS. `_web` blocks apply there, and +`_hover` and `_focus` follow the mouse and the keyboard. Install the web +dependencies Expo asks for (`npx expo install react-dom react-native-web +@expo/metro-runtime`) and add `"web"` to the `platforms` of app.json. + +A site that is not an Expo app keeps using the Vite, Next.js, Webpack, +Rsbuild or Bun plugin, which output atomic CSS instead. + ## Limits of this version Only styles the build can decide are supported: literals, values read at diff --git a/packages/metro-plugin/src/__tests__/plugin.test.ts b/packages/metro-plugin/src/__tests__/plugin.test.ts index 55ecaa4e4..f8fbee424 100644 --- a/packages/metro-plugin/src/__tests__/plugin.test.ts +++ b/packages/metro-plugin/src/__tests__/plugin.test.ts @@ -1,8 +1,24 @@ -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from 'node:fs' import { tmpdir } from 'node:os' import { dirname, join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it, spyOn } from 'bun:test' +import * as pluginUtils from '@devup-ui/plugin-utils' +import { + afterEach, + beforeEach, + describe, + expect, + it, + type Mock, + spyOn, +} from 'bun:test' import { REANIMATED_PACKAGE, @@ -11,17 +27,22 @@ import { withDevupUI, } from '../plugin' import { readSettings, SETTINGS_ENV } from '../settings' +import { NATIVE_TYPES } from '../types' +const wasm = pluginUtils.loadCompiler('wasm') let root: string let previous: string | undefined +let selection: Mock beforeEach(() => { root = mkdtempSync(join(tmpdir(), 'devup-metro-plugin-')) previous = process.env[SETTINGS_ENV] delete process.env[SETTINGS_ENV] + selection = spyOn(pluginUtils, 'loadCompiler').mockReturnValue(wasm) }) afterEach(() => { + selection.mockRestore() rmSync(root, { recursive: true, force: true }) if (previous === undefined) delete process.env[SETTINGS_ENV] else process.env[SETTINGS_ENV] = previous @@ -106,6 +127,35 @@ describe('withDevupUI', () => { devupFile: join(root, 'theme', 'devup.json'), engine: 'wasm', }) + expect(selection.mock.calls).toEqual([['wasm']]) + }) + + it('writes the types of the theme and of React Native into df', () => { + write( + 'devup.json', + JSON.stringify({ theme: { colors: { default: { primary: '#000' } } } }), + ) + withDevupUI({ + projectRoot: root, + transformer: { babelTransformerPath: '/up.js' }, + }) + expect(readFileSync(join(root, 'df', 'theme.d.ts'), 'utf-8')).toContain( + 'primary', + ) + expect(readFileSync(join(root, 'df', 'native.d.ts'), 'utf-8')).toBe( + NATIVE_TYPES, + ) + expect(readFileSync(join(root, 'df', '.gitignore'), 'utf-8')).toBe('*') + }) + + it('writes the types into the distDir option', () => { + withDevupUI( + { projectRoot: root, transformer: { babelTransformerPath: '/up.js' } }, + { distDir: 'types/devup' }, + ) + expect(existsSync(join(root, 'types', 'devup', 'theme.d.ts'))).toBe(true) + expect(existsSync(join(root, 'types', 'devup', 'native.d.ts'))).toBe(true) + expect(existsSync(join(root, 'df'))).toBe(false) }) it('resolves the upstreamTransformer option from the project root', () => { diff --git a/packages/metro-plugin/src/plugin.ts b/packages/metro-plugin/src/plugin.ts index 062ea148e..70bac22e6 100644 --- a/packages/metro-plugin/src/plugin.ts +++ b/packages/metro-plugin/src/plugin.ts @@ -13,6 +13,7 @@ import { readSettings, writeSettings, } from './settings' +import { writeTypes } from './types' export interface DevupUIMetroPluginOptions { /** @@ -24,6 +25,12 @@ export interface DevupUIMetroPluginOptions { package?: string /** Path of devup.json, from the project root. Defaults to `devup.json`. */ devupFile?: string + /** + * Where the TypeScript declarations are written, from the project root: + * the theme's tokens and the React Native props of each component. + * Defaults to `df`, as in the other plugins. + */ + distDir?: string /** * Packages in `node_modules` whose files are compiled as well: a library * that ships Devup UI components uncompiled. Each name matches that whole @@ -127,7 +134,8 @@ function upstreamTransformer( * Compile Devup UI for React Native in Metro: the config's Babel transformer * becomes Devup UI's, which compiles each file that imports Devup UI and then * hands the code to the transformer the config had (Expo's or React - * Native's). + * Native's). The TypeScript declarations of the theme and of the React Native + * props are written into `distDir`. * * ```js * // metro.config.js @@ -159,6 +167,12 @@ export function withDevupUI( engine: options.engine, } writeSettings(settings) + writeTypes({ + distDir: resolve(projectRoot, options.distDir ?? 'df'), + devupFile: settings.devupFile, + package: settings.package, + engine: settings.engine, + }) return { ...config, transformer: { diff --git a/packages/metro-plugin/src/types.ts b/packages/metro-plugin/src/types.ts new file mode 100644 index 000000000..3013e8349 --- /dev/null +++ b/packages/metro-plugin/src/types.ts @@ -0,0 +1,44 @@ +import { mkdirSync, writeFileSync } from 'node:fs' +import { join } from 'node:path' + +import { + createThemeInterfaceArgs, + type DevupEngine, + loadCompiler, + loadDevupConfigSync, +} from '@devup-ui/plugin-utils' + +/** + * `native.d.ts`: it imports the declarations that type each component of + * `@devup-ui/react` as the React Native component it renders + */ +export const NATIVE_TYPES = "import '@devup-ui/react/native/types'\n" + +/** What the types of a project are written from */ +export interface TypesOptions { + /** The directory written, absolute */ + readonly distDir: string + /** devup.json, absolute */ + readonly devupFile: string + /** The package Devup UI is imported from */ + readonly package: string + readonly engine?: DevupEngine +} + +/** + * Write the TypeScript declarations of the project into `distDir`, as the web + * plugins do: `theme.d.ts`, the tokens of devup.json, and `native.d.ts`, which + * types the components for React Native. The directory ignores itself in git. + */ +export function writeTypes(options: TypesOptions): void { + const compiler = loadCompiler(options.engine) + compiler.registerTheme(loadDevupConfigSync(options.devupFile).theme ?? {}) + mkdirSync(options.distDir, { recursive: true }) + writeFileSync(join(options.distDir, '.gitignore'), '*', 'utf-8') + writeFileSync( + join(options.distDir, 'theme.d.ts'), + compiler.getThemeInterface(...createThemeInterfaceArgs(options.package)), + 'utf-8', + ) + writeFileSync(join(options.distDir, 'native.d.ts'), NATIVE_TYPES, 'utf-8') +} diff --git a/packages/react/package.json b/packages/react/package.json index 6d2b468b0..759ae00b0 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -21,8 +21,8 @@ "scripts": { "lint": "eslint && bun ../../node_modules/@typescript/native/bin/tsc --noEmit && bun run test:types", "build": "bun run test:types && bun ../../node_modules/@typescript/native/bin/tsc && vite build && rollup -c rollup.types.config.mjs && bun run test:published-types", - "test:types": "bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/tsconfig.json", - "test:published-types": "bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/published/tsconfig.bundler.json && bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/published/tsconfig.node16.json" + "test:types": "bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/tsconfig.json && bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/native/tsconfig.json", + "test:published-types": "bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/published/tsconfig.bundler.json && bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/published/tsconfig.node16.json && bun ../../node_modules/@typescript/native/bin/tsc -p type-tests/published/tsconfig.native.json" }, "publishConfig": { "access": "public" @@ -79,6 +79,9 @@ "types": "./dist/native/index.d.ts", "import": "./dist/native/index.js", "require": "./dist/native/index.cjs" + }, + "./native/types": { + "types": "./dist/native/types.d.ts" } }, "files": [ diff --git a/packages/react/rollup.types.config.mjs b/packages/react/rollup.types.config.mjs index ca2bcc427..c54a74e35 100644 --- a/packages/react/rollup.types.config.mjs +++ b/packages/react/rollup.types.config.mjs @@ -20,8 +20,8 @@ const entries = { } const shared = '_shared' -/** Declarations written by hand, which ambiently declare modules and stay as they are */ -const ambient = /^compat\/(?!index\.)[^/]+\.d\.ts$/ +/** Declarations written by hand, which declare or augment modules and stay as they are */ +const ambient = /^(compat\/(?!index\.)[^/]+|native\/types)\.d\.ts$/ function files(directory) { return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index 1de5d171a..1afc6989f 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -9,7 +9,7 @@ export { Text } from './components/Text' export { ThemeScript } from './components/ThemeScript' export { VStack } from './components/VStack' export { useTheme } from './hooks/use-theme' -export type { DevupProps } from './types/props' +export type { DevupElementProps, DevupProps } from './types/props' export type { ResponsiveValue } from './types/responsive-value' export type { DevupCustomShorthands } from './types/shorthand' export type { DevupTheme } from './types/theme' diff --git a/packages/react/src/native/types.d.ts b/packages/react/src/native/types.d.ts new file mode 100644 index 000000000..eb30b594f --- /dev/null +++ b/packages/react/src/native/types.d.ts @@ -0,0 +1,39 @@ +/** + * Types `@devup-ui/react` for React Native: each component takes the props of + * the React Native component the build renders it as, instead of those of the + * HTML element it renders on the web. `@devup-ui/metro-plugin` imports this + * file from `/native.d.ts`; to opt in by hand, import it from a + * declaration file of the project: + * + * ```ts + * import '@devup-ui/react/native/types' + * ``` + */ +import type { + ImageProps, + PressableProps, + TextInputProps, + TextProps, + ViewProps, +} from 'react-native' + +/** What every element takes besides its component's props */ +interface DevupNativeElementProps { + /** The class of `css()`, whose styles the build gives the element */ + className?: string +} + +declare module '@devup-ui/react' { + interface DevupElementProps { + /** `Box`, `Flex`, `VStack`, `Center` and `Grid`: a `View` */ + div: ViewProps & DevupNativeElementProps + /** `Text`: a `Text` */ + span: TextProps & DevupNativeElementProps + /** `Image`: an `Image` */ + img: ImageProps & DevupNativeElementProps + /** `Input`: a `TextInput` */ + input: TextInputProps & DevupNativeElementProps + /** `Button`: a `Pressable` */ + button: PressableProps & DevupNativeElementProps + } +} diff --git a/packages/react/src/types/props/index.ts b/packages/react/src/types/props/index.ts index c2646b9b5..043bedb09 100644 --- a/packages/react/src/types/props/index.ts +++ b/packages/react/src/types/props/index.ts @@ -63,12 +63,28 @@ type DevupDefaultIntrinsicPropConflicts = | 'width' | keyof DevupCustomShorthands +/** + * The props a component takes, by the HTML element it renders on the web, in + * place of that element's: empty on the web, and filled by + * `@devup-ui/react/native/types` with the props of the React Native component + * each renders on React Native (`div` a `View`, `span` a `Text`, ...). + */ +// biome-ignore lint/suspicious/noEmptyInterface: public module augmentation point for React Native. +// eslint-disable-next-line @typescript-eslint/no-empty-object-type +export interface DevupElementProps {} + +/** The props of the element `T` a component renders */ +export type DevupIntrinsicProps = + T extends keyof DevupElementProps + ? DevupElementProps[T] + : React.ComponentProps + // Component declarations put this overload first for fast resolution and last // so Parameters keeps describing the default element. export type DevupDefaultComponentMergedProps< T extends keyof React.JSX.IntrinsicElements, -> = Omit, DevupDefaultIntrinsicPropConflicts> & { - props?: FilterChildren> +> = Omit, DevupDefaultIntrinsicPropConflicts> & { + props?: FilterChildren> } & DevupComponentProps export type DevupComponentBaseProps = diff --git a/packages/react/type-tests/native/app.tsx b/packages/react/type-tests/native/app.tsx new file mode 100644 index 000000000..118bdb76d --- /dev/null +++ b/packages/react/type-tests/native/app.tsx @@ -0,0 +1,28 @@ +// The components typed for React Native (`@devup-ui/react/native/types`): +// each takes the props of the React Native component it renders, and its +// style props as on the web +import { Box, Button, css, Flex, Image, Input, Text } from '@devup-ui/react' + +export const app = ( + {}} testID="card"> + + {}}> + title + + + text} + secureTextEntry + /> + + + +) + +// @ts-expect-error a View has no href +export const link = +// @ts-expect-error a TextInput reports its text with onChangeText +export const input = {}} /> diff --git a/packages/react/type-tests/native/tsconfig.json b/packages/react/type-tests/native/tsconfig.json new file mode 100644 index 000000000..ecdf8f0c4 --- /dev/null +++ b/packages/react/type-tests/native/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "paths": { + "@devup-ui/react": ["../../src/index.ts"], + "@devup-ui/react/*": ["../../src/*"] + } + }, + "files": ["../../src/native/types.d.ts", "app.tsx"], + "include": [] +} diff --git a/packages/react/type-tests/published/native.tsx b/packages/react/type-tests/published/native.tsx new file mode 100644 index 000000000..a6e001e12 --- /dev/null +++ b/packages/react/type-tests/published/native.tsx @@ -0,0 +1,16 @@ +// Type-checks a React Native project against the published declarations: the +// import `@devup-ui/metro-plugin` writes types each component as the React +// Native component it renders +import '@devup-ui/react/native/types' + +import { Box, Button, Input } from '@devup-ui/react' + +export const app = ( + {}} p={2}> + text} /> +