diff --git a/.changepacks/changepack_log_react_native_css.json b/.changepacks/changepack_log_react_native_css.json new file mode 100644 index 000000000..77bab2bf9 --- /dev/null +++ b/.changepacks/changepack_log_react_native_css.json @@ -0,0 +1,8 @@ +{ + "changes": { + "bindings/devup-ui-binding/package.json": "Patch", + "bindings/devup-ui-wasm/package.json": "Patch" + }, + "note": "`codeExtractNative` compiles `css()` for React Native (part of #411): a call compiles to the style it describes, the value React Native's `style` takes, and `className` on a Devup UI element passes it on before the element's style props. `css()` takes objects of style props with responsive values, theme tokens and blocks, conditions choosing between objects, what another `css()` gives, and CSS text whose interpolations are whole values. A `css()` whose styles differ by theme, breakpoint or orientation must be called in a function component or a hook. A `style` prop holding Devup UI elements or `css()` calls now compiles them too. Web output is unchanged.", + "date": "2026-11-21T00:00:00.000Z" +} diff --git a/.changepacks/changepack_log_react_native_styled.json b/.changepacks/changepack_log_react_native_styled.json new file mode 100644 index 000000000..f07bf4032 --- /dev/null +++ b/.changepacks/changepack_log_react_native_styled.json @@ -0,0 +1,8 @@ +{ + "changes": { + "bindings/devup-ui-binding/package.json": "Patch", + "bindings/devup-ui-wasm/package.json": "Patch" + }, + "note": "`codeExtractNative` compiles `styled()` for React Native (part of #411): `styled(Target)(...parts)`, `` styled(Target)`css text` `` and `styled(Target, styles)` compile to a function component rendering its target with the styles it describes, before the `style` it is given. It takes the parts `css()` takes; a function in them is called with the component's props, as a value or as a part giving what `css()` gives. A Devup UI target keeps the styles it declares first, and a styled `Box` with a press style becomes a `Pressable`. The hooks its styles read are called in the component. Web output is unchanged.", + "date": "2026-11-21T00:00:00.000Z" +} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2bf0a8564..af9dc3598 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,6 +11,8 @@ on: - feat/react-native-metro - feat/react-native-themes - feat/react-native-responsive + - feat/react-native-pressed + - feat/react-native-css # 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 diff --git a/apps/expo/src/App.tsx b/apps/expo/src/App.tsx index 624128e25..6d8174a5f 100644 --- a/apps/expo/src/App.tsx +++ b/apps/expo/src/App.tsx @@ -1,6 +1,7 @@ import { Box, Button, + css, Flex, Image, Input, @@ -12,6 +13,9 @@ import { useState } from 'react' import { Badge } from './Badge' +/** The shape both buttons share */ +const button = css({ borderRadius: '8px', px: 5, py: 3 }) + export function App() { const [name, setName] = useState('') const [count, setCount] = useState(0) @@ -69,10 +73,8 @@ export function App() { + ) +} +``` + +`css()` takes what it does on the web: objects of style props (with +responsive values, theme tokens and the theme, platform and orientation +blocks), conditions choosing between them, what another `css()` gives, and +CSS text (`` css`padding: 8px; background-color: $primary;` ``, each +interpolation a whole value). The element's style props apply after its +`className`, and its `style` prop last. + +A `css()` whose styles differ by theme, breakpoint or orientation reads the +runtime, so it must be called in a function component or a hook. One that does +not can be called anywhere, such as at the top of the file. These are build +errors: + +- a class name (`className="p-4"`, `css('p-4')`) +- a press style in `css()`: give `_active` to the element itself +- selectors, nesting and at-rules in CSS text + +`css()` gives a style rather than a class on React Native, so pass it to +`className`, not to the `style` of a React Native component, which TypeScript +types as a string. + +## `styled()` + +`styled()` compiles to a function component that renders its target with the +styles it describes, before the `style` the component is given: + +```tsx +const Card = styled(Box)({ bg: '$surface', p: [2, null, 4] }) +const Title = styled(Text)` + color: ${(props) => props.color}; + font-size: 18px; +` +// compiles to +const Card = (props) => { + const t = useThemeIndex(), b = useBreakpoint() + return +} +``` + +It takes the parts `css()` takes, as `styled(Target)(...parts)`, +`` styled(Target)`css text` `` or `styled(Target, styles)`. A function in +them is called with the component's props: a value (`w: (p) => p.width`, or +an interpolation in CSS text), or a part giving what `css()` gives +(`(p) => p.active && css({ opacity: 0.5 })`). The target is a Devup UI +component, which keeps the styles it declares first (`Flex` its row), or any +React Native component. A press style makes a styled `Box` a `Pressable`. +These are build errors: + +- a tag (`styled('div')`, `styled.div`) +- a function of the props giving style props (`(p) => ({ bg: p.color })`): + return what `css()` gives instead +- a press style on a component other than a `Box` or a `Button` + +The component reads style props only through `styled()`: on React Native, +`` passes `p` on to the target, so give it `style` or +`className` instead. + ## Limits of this version Only styles the build can decide are supported: literals, values read at runtime, theme tokens, responsive values, conditions between them and the -platform, theme and orientation blocks. These are build errors, reported by -Metro at the file, line and column: +platform, theme, orientation and press blocks, `css()` and `styled()`. These +are build errors, reported by Metro at the file, line and column: - interaction states other than `_active`/`_pressed` (`_hover`, `_focus`, ...) -- `css()`, `styled()`, `globalCss()`, `keyframes()` and `Grid` +- `globalCss()`, `keyframes()` and `Grid` - any prop React Native has no style for - with `runtime: false`, a value that differs by theme or breakpoint, `_theme*` with more than one theme, `_portrait`, `_landscape`, and