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
8 changes: 8 additions & 0 deletions .changepacks/changepack_log_react_native_css.json
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
- feat/react-native-metro
- feat/react-native-themes
- feat/react-native-responsive
- feat/react-native-pressed

# 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
12 changes: 6 additions & 6 deletions apps/expo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Box,
Button,
css,
Flex,
Image,
Input,
Expand All @@ -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)
Expand Down Expand Up @@ -69,10 +73,8 @@ export function App() {
<Button
_active={{ bg: '#4330d9' }}
bg="#5a44ff"
borderRadius="8px"
className={button}
onPress={() => setCount(count + 1)}
px={5}
py={3}
>
<Text color="#fff" fontWeight="600">
Pressed {count} times
Expand All @@ -81,10 +83,8 @@ export function App() {
<Button
_active={{ opacity: 0.6 }}
border="1px solid $border"
borderRadius="8px"
className={button}
onPress={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
px={5}
py={3}
>
<Text color="$text">
Switch to {theme === 'dark' ? 'light' : 'dark'}
Expand Down
74 changes: 58 additions & 16 deletions libs/extractor/src/native/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//! component its import is now bound to.

use oxc_ast::ast::{
ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, ExportDefaultDeclaration,
Expression, Function, FunctionBody, IdentifierReference, JSXAttribute, JSXAttributeItem,
JSXAttributeName, JSXAttributeValue, JSXChild, JSXElement, JSXElementName, JSXExpression,
LogicalOperator, ObjectExpression, ObjectPropertyKind, Program, TSType, UnaryOperator,
VariableDeclarator,
ArrayExpression, ArrayExpressionElement, ArrowFunctionExpression, CallExpression,
ExportDefaultDeclaration, Expression, Function, FunctionBody, IdentifierReference,
JSXAttribute, JSXAttributeItem, JSXAttributeName, JSXAttributeValue, JSXChild, JSXElement,
JSXElementName, JSXExpression, LogicalOperator, ObjectExpression, ObjectPropertyKind, Program,
TSType, TaggedTemplateExpression, UnaryOperator, VariableDeclarator,
};
use std::cell::Cell;
use std::fmt::Write;
Expand All @@ -30,6 +30,8 @@ use super::{NativePlatform, NativeTarget, REACT_NATIVE, RUNTIME, Replacement};
use crate::component::ExportVariableKind;
use crate::fresh_name::fresh_name;

mod css_call;

/// The changes compiling a file makes, or why it cannot compile
pub(super) struct Compiled {
pub replacements: Vec<Replacement>,
Expand Down Expand Up @@ -174,6 +176,7 @@ pub(super) fn compile(
target,
scoping,
components: &imports.components,
css: &imports.css,
sheet: Sheet::new(fresh_name("__s", code)),
units: fresh_name("__u", code),
units_used: Cell::new(false),
Expand All @@ -187,7 +190,7 @@ pub(super) fn compile(
replacements: imports.replacements,
errors: imports.errors,
};
if !visitor.components.is_empty() {
if !visitor.components.is_empty() || !visitor.css.is_empty() {
visitor.visit_program(program);
}
let Elements {
Expand Down Expand Up @@ -233,6 +236,8 @@ enum Prop {
As,
/// `style`, given whole
Style,
/// `className`, the styles `css()` gives
ClassName,
/// A style prop
Declares,
/// `_ios`, `_android` or `_web`: styles for one platform
Expand All @@ -259,10 +264,7 @@ fn classify(name: &str) -> Prop {
"_portrait" => Prop::When(State::Landscape(false)),
"_landscape" => Prop::When(State::Landscape(true)),
"_pressed" | "_active" => Prop::When(State::Pressed),
"className" => Prop::Unsupported(Problem::new(
"React Native has no class names",
"write the styles as props, or pass them in `style`",
)),
"className" => Prop::ClassName,
"typography" => Prop::Declares,
"styleOrder" | "styleVars" => Prop::Unsupported(Problem::new(
"React Native has no stylesheet order and no CSS variables",
Expand Down Expand Up @@ -310,6 +312,8 @@ struct Elements<'s> {
target: &'s NativeTarget,
scoping: &'s Scoping,
components: &'s FxHashMap<SymbolId, (ExportVariableKind, String)>,
/// The bindings of `css`
css: &'s FxHashSet<SymbolId>,
sheet: Sheet,
/// The name of the function converting values read at runtime
units: String,
Expand All @@ -336,6 +340,8 @@ struct Elements<'s> {
/// What the props of one element give its `style`
#[derive(Default)]
struct Styles {
/// The code of what `className` gives, before the style props
classes: Vec<String>,
choices: Vec<Choice>,
/// The styles of the theme, orientation and press blocks, which apply
/// after the other styles whatever the order of the props, as their
Expand All @@ -347,6 +353,9 @@ struct Styles {
removed: Vec<Span>,
/// The `style` prop, which leaves the element with the style props
style: Option<Span>,
/// The `style` prop's value compiled, which it keeps when the element
/// takes no other style
kept: Option<Replacement>,
/// A spread whose `style` cannot be read twice
opaque_spread: Option<Span>,
}
Expand All @@ -367,11 +376,35 @@ impl<'a> Visit<'a> for Elements<'_> {
"`{local}` can only be rendered as a JSX element on React Native: the build compiles its props into styles only there. Fix: render `<{local} />`, or use a React Native component here"
),
));
} else if self.is_css_reference(identifier) {
self.errors.push((
identifier.span.start,
format!(
"`{}` can only be called on React Native: the build compiles each call into styles. Fix: call it where the styles are used",
identifier.name
),
));
}
}

fn visit_ts_type(&mut self, _: &TSType<'a>) {}

fn visit_tagged_template_expression(&mut self, template: &TaggedTemplateExpression<'a>) {
if self.is_css(&template.tag) {
self.css_template(template);
} else {
walk::walk_tagged_template_expression(self, template);
}
}

fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
if self.is_css(&call.callee) {
self.css_call(call);
} else {
walk::walk_call_expression(self, call);
}
}

fn visit_variable_declarator(&mut self, declarator: &VariableDeclarator<'a>) {
if let (Some(name), Some(init)) = (declarator.id.get_identifier_name(), &declarator.init)
&& is_component_name(&name)
Expand Down Expand Up @@ -662,6 +695,7 @@ impl<'a> Elements<'_> {
return self.tag(attribute, local);
}
Prop::Style => self.given_style(attribute, span, local, styles),
Prop::ClassName => self.class_name(attribute, span, local, styles),
Prop::Declares => {
styles.removed.push(span);
match self.value_choice(name, attribute.value.as_ref()) {
Expand Down Expand Up @@ -724,11 +758,11 @@ impl<'a> Elements<'_> {
) {
match attribute_expression(attribute) {
Some(expression) => {
styles
.given
.push(expression.span().source_text(self.code).to_string());
let code = self.rewritten(expression);
let at = expression.span();
styles.kept = Some((at.start as usize, at.end as usize, code.clone()));
styles.given.push(code);
styles.style = Some(span);
self.visit_expression(expression);
}
None => self.error(
attribute.span.start,
Expand Down Expand Up @@ -941,7 +975,7 @@ impl<'a> Elements<'_> {
return self.state_block(Some(value), (key, state), offset, local);
}
Prop::Unsupported(problem) => self.error(offset, problem.on(local, key)),
Prop::As | Prop::Style | Prop::Pass => self.error(
Prop::As | Prop::Style | Prop::ClassName | Prop::Pass => self.error(
offset,
Problem::new(
"a block holds only style props",
Expand Down Expand Up @@ -1137,16 +1171,23 @@ impl<'a> Elements<'_> {
attributes_end: u32,
) {
let Styles {
classes,
mut choices,
blocks,
given,
mut removed,
style,
kept,
opaque_spread,
} = styles;
choices.extend(blocks);
let pressed = choices.iter().any(Choice::pressed);
let mut style_code = style_code(choices, given, &mut self.sheet, &self.runtime.vars());
let mut style_code = style_code(
choices,
(classes, given),
&mut self.sheet,
&self.runtime.vars(),
);
let reads = style_code
.as_ref()
.map_or_else(Reads::default, |style| style.reads);
Expand All @@ -1168,6 +1209,7 @@ impl<'a> Elements<'_> {
}
let Some(style_code) = style_code else {
self.remove(&removed);
self.replacements.extend(kept);
return;
};
let code = if wrapped {
Expand Down
Loading
Loading