Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changepacks/changepack_log_metro_types.json
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 7 additions & 0 deletions .changepacks/changepack_log_react_native_types.json
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 6 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ on:
- feat/react-native-states
- feat/react-native-animations
- feat/react-native-compat
- feat/react-native-web

# Pull requests and every job below run read-only. Only the `release` and
# `deploy-pages` jobs get write permissions, and they run on pushes to main
Expand Down Expand Up @@ -930,7 +931,7 @@ jobs:
fi
files="$(git diff --name-only "$(git merge-base "$BASE" HEAD)" HEAD)"
echo "$files"
if grep -qE '^(libs/extractor/src/native/|libs/binding/|bindings/devup-ui-(binding|wasm)/|packages/(metro-plugin|plugin-utils)/|packages/react/src/native/|packages/react/src/utils/(theme-context|with-theme).|apps/expo/|\.github/workflows/publish\.yml$)' <<< "$files"; then
if grep -qE '^(libs/extractor/src/native/|libs/binding/|bindings/devup-ui-(binding|wasm)/|packages/(metro-plugin|plugin-utils)/|packages/react/src/(native|types)/|packages/react/src/utils/(theme-context|with-theme).|apps/expo/|\.github/workflows/publish\.yml$)' <<< "$files"; then
echo "changed=true" >> "$GITHUB_OUTPUT"
else
echo "changed=false" >> "$GITHUB_OUTPUT"
Expand Down Expand Up @@ -965,6 +966,10 @@ jobs:
- name: Check the bundles
working-directory: apps/expo
run: bun run check
# Metro wrote df/theme.d.ts and df/native.d.ts when it read the config
- name: Type-check the app against the theme and React Native props
working-directory: apps/expo
run: bun run typecheck
- *record-playwright-revisions
- *restore-playwright-cache
- *install-chromium
Expand Down
3 changes: 2 additions & 1 deletion apps/expo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"start": "expo start",
"export": "expo export --platform ios --platform android --platform web --no-bytecode --source-maps",
"check": "node check-bundle.mjs",
"check:web": "node check-web.mjs"
"check:web": "node check-web.mjs",
"typecheck": "bun ../../node_modules/@typescript/native/bin/tsc --noEmit"
},
"dependencies": {
"@devup-ui/react": "workspace:^",
Expand Down
20 changes: 20 additions & 0 deletions packages/metro-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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`
Expand Down
54 changes: 52 additions & 2 deletions packages/metro-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<typeof pluginUtils.loadCompiler>

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
Expand Down Expand Up @@ -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', () => {
Expand Down
16 changes: 15 additions & 1 deletion packages/metro-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
readSettings,
writeSettings,
} from './settings'
import { writeTypes } from './types'

export interface DevupUIMetroPluginOptions {
/**
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -159,6 +167,12 @@ export function withDevupUI<T extends MetroConfigLike>(
engine: options.engine,
}
writeSettings(settings)
writeTypes({
distDir: resolve(projectRoot, options.distDir ?? 'df'),
devupFile: settings.devupFile,
package: settings.package,
engine: settings.engine,
})
return {
...config,
transformer: {
Expand Down
44 changes: 44 additions & 0 deletions packages/metro-plugin/src/types.ts
Original file line number Diff line number Diff line change
@@ -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')
}
7 changes: 5 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -74,6 +74,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": [
Expand Down
4 changes: 2 additions & 2 deletions packages/react/rollup.types.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,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) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
39 changes: 39 additions & 0 deletions packages/react/src/native/types.d.ts
Original file line number Diff line number Diff line change
@@ -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 `<distDir>/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
}
}
20 changes: 18 additions & 2 deletions packages/react/src/types/props/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 React.JSX.IntrinsicElements> =
T extends keyof DevupElementProps
? DevupElementProps[T]
: React.ComponentProps<T>

// Component declarations put this overload first for fast resolution and last
// so Parameters<typeof Component> keeps describing the default element.
export type DevupDefaultComponentMergedProps<
T extends keyof React.JSX.IntrinsicElements,
> = Omit<React.ComponentProps<T>, DevupDefaultIntrinsicPropConflicts> & {
props?: FilterChildren<React.ComponentProps<T>>
> = Omit<DevupIntrinsicProps<T>, DevupDefaultIntrinsicPropConflicts> & {
props?: FilterChildren<DevupIntrinsicProps<T>>
} & DevupComponentProps<T>

export type DevupComponentBaseProps<T extends React.ElementType> =
Expand Down
Loading
Loading