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_compat.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 `styled-components/native` and `@emotion/native` (part of #411): their `styled` (including `styled.View` and any other `styled.Name`) and `css` compile as Devup UI's with numbers as pixels, function parts receive `props.theme` from the runtime's `ThemeProvider`, and `ThemeProvider`, `useTheme` and `withTheme` import from the React Native runtime. Web output is unchanged.",
"date": "2026-11-21T00:00:00.000Z"
}
7 changes: 7 additions & 0 deletions .changepacks/changepack_log_react_native_compat_runtime.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"changes": {
"packages/react/package.json": "Patch"
},
"note": "`@devup-ui/react/native` adds `ThemeProvider`, `useStyledTheme` and `withTheme`, the theme context the build imports for styled-components and Emotion on React Native. They share the context and `withTheme` of the web compat entry. The runtime grows from 702 B to 1,185 B min+gzip.",
"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 @@ -15,6 +15,7 @@ on:
- feat/react-native-css
- feat/react-native-styled
- feat/react-native-states
- feat/react-native-animations

# 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
7 changes: 5 additions & 2 deletions apps/expo/check-bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ const RUNTIME_NAMES =
const APP_HOOKS = ['useThemeIndex', 'useBreakpoint', 'useLandscape', 'Focused']
/** Modules of @devup-ui/react, by the path Metro resolves */
const REACT_PACKAGE = /(^|[\\/])(@devup-ui[\\/]react|packages[\\/]react)[\\/]/
/** The React Native runtime among them */
/**
* The React Native runtime among them, with the theme context of
* styled-components and Emotion it shares with the web build
*/
const NATIVE_RUNTIME =
/(^|[\\/])(@devup-ui[\\/]react|packages[\\/]react)[\\/]dist[\\/]native[\\/]/
/(^|[\\/])(@devup-ui[\\/]react|packages[\\/]react)[\\/]dist[\\/](native[\\/]|utils[\\/](theme-context|with-theme)\.)/
const root = join(import.meta.dirname, 'dist', '_expo', 'static', 'js')
const failures = []

Expand Down
74 changes: 71 additions & 3 deletions libs/extractor/src/native/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use oxc_ast::ast::{
JSXElementName, JSXExpression, LogicalOperator, ObjectExpression, ObjectPropertyKind, Program,
TSType, TaggedTemplateExpression, UnaryOperator, VariableDeclarator,
};
use std::borrow::Cow;
use std::cell::Cell;
use std::fmt::Write;

Expand All @@ -28,9 +29,10 @@ use super::style::{
varied, when, without,
};
use super::theme::{DEFAULT_BREAKPOINTS, selector_theme};
use super::values::{Literal, NativeValue, ROOT_FONT_SIZE};
use super::values::{Literal, NativeValue, ROOT_FONT_SIZE, TIMED, number_code};
use super::{NativePlatform, NativeTarget, REACT_NATIVE, RUNTIME, Replacement};
use crate::component::ExportVariableKind;
use crate::extract_style::constant::is_maintain_value_property;
use crate::fresh_name::fresh_name;

mod animation;
Expand Down Expand Up @@ -66,6 +68,9 @@ struct Runtime {
/// The component telling a `TextInput` whether it has focus
focused_wrapper: String,
focused_used: bool,
/// The hook giving the theme of a compat `ThemeProvider`
styled_theme: String,
styled_theme_used: bool,
/// The hooks reading each, by what they read
hooks: [String; 3],
/// What the hooks the file calls read
Expand Down Expand Up @@ -97,6 +102,8 @@ impl Runtime {
focused: fresh_name("__f", code),
focused_wrapper: fresh_name("__Focused", code),
focused_used: false,
styled_theme: fresh_name("__useStyledTheme", code),
styled_theme_used: false,
hooks: HOOKS.map(|hook| fresh_name(&format!("__{hook}"), code)),
hooks_used: Reads::default(),
wrapper: fresh_name("__Themed", code),
Expand Down Expand Up @@ -132,7 +139,12 @@ impl Runtime {
/// The import of the runtime and the calls telling it the theme names
/// and the breakpoints, when the file uses it
fn prelude(&self, target: &NativeTarget, theme_api: bool) -> Option<String> {
if !(self.hooks_used.any() || self.wrapper_used || self.focused_used || theme_api) {
if !(self.hooks_used.any()
|| self.wrapper_used
|| self.focused_used
|| self.styled_theme_used
|| theme_api)
{
return None;
}
let reads = self.hooks_used.union(self.wrapper_reads);
Expand All @@ -143,6 +155,9 @@ impl Runtime {
if self.wrapper_used {
specifiers.push(format!("Themed as {}", self.wrapper));
}
if self.styled_theme_used {
specifiers.push(format!("useStyledTheme as {}", self.styled_theme));
}
if self.focused_used {
specifiers.push(format!("Focused as {}", self.focused_wrapper));
}
Expand Down Expand Up @@ -204,6 +219,10 @@ pub(super) fn compile(
components: &imports.components,
css: &imports.css,
styled: &imports.styled,
compat_bindings: &imports.compat,
compat: false,
props_called: Cell::new(false),
natives: Vec::new(),
keyframes: &imports.keyframes,
animation: None,
animated: Animated::new(code),
Expand Down Expand Up @@ -237,6 +256,7 @@ pub(super) fn compile(
pressable_used,
runtime,
animated,
natives,
mut replacements,
errors,
..
Expand All @@ -250,6 +270,17 @@ pub(super) fn compile(
"import {{ Pressable as {pressable} }} from \"{REACT_NATIVE}\";"
);
}
if !natives.is_empty() {
let specifiers: Vec<String> = natives
.iter()
.map(|(name, local)| format!("{name} as {local}"))
.collect();
let _ = writeln!(
prelude,
"import {{ {} }} from \"{REACT_NATIVE}\";",
specifiers.join(", ")
);
}
prelude.push_str(&animated.prelude());
if !sheet.is_empty() {
prelude.push_str(&sheet_prelude(&sheet, code));
Expand Down Expand Up @@ -357,6 +388,17 @@ struct Elements<'s> {
css: &'s FxHashSet<SymbolId>,
/// The bindings of `styled`
styled: &'s FxHashSet<SymbolId>,
/// The bindings of `styled` and `css` imported from a compat package
compat_bindings: &'s FxHashSet<SymbolId>,
/// Whether the styles read are a compat package's, whose numbers are
/// pixels as React Native reads them
compat: bool,
/// Whether a function was called with `props`, the props of the
/// `styled()` component being read
props_called: Cell<bool>,
/// The React Native components `styled.Name` renders: each name and the
/// local name it is imported under
natives: Vec<(String, String)>,
/// The bindings of `keyframes`
keyframes: &'s FxHashSet<SymbolId>,
/// The offset and the name of the first animation or transition of the
Expand Down Expand Up @@ -657,6 +699,12 @@ impl<'a> Elements<'_> {
/// breakpoint it is paired with on: one style object, or one a
/// breakpoint and a theme where the styles differ by them
fn static_choice(&self, name: &str, items: &[(usize, Literal)]) -> Result<Choice, Problem> {
// A number of a compat package is pixels, as React Native reads it
let items: Cow<[(usize, Literal)]> = if self.compat {
Cow::Owned(in_pixels(name, items))
} else {
Cow::Borrowed(items)
};
let theme = &self.target.theme;
let themes = match self.theme_scope {
Some(index) => index..index + 1,
Expand All @@ -676,7 +724,7 @@ impl<'a> Elements<'_> {
for index in themes.clone() {
row.push(cell(
name,
items,
&items,
&Tokens {
theme,
index,
Expand Down Expand Up @@ -1218,6 +1266,8 @@ impl<'a> Elements<'_> {

/// `code`, converted at runtime when its numbers take `scale`
fn converted(&self, code: &str, scale: Option<u32>) -> String {
// A number of a compat package is pixels, as React Native reads it
let scale = scale.map(|scale| if self.compat { 1 } else { scale });
scale.map_or_else(
|| code.to_string(),
|scale| {
Expand All @@ -1232,6 +1282,7 @@ impl<'a> Elements<'_> {
let code = expression.span().source_text(self.code);
if self.in_styled && is_function(expression) {
// A function in the styles of a `styled()` component takes its props
self.props_called.set(true);
return format!("({code})({})", self.props);
}
if matches!(
Expand Down Expand Up @@ -1585,3 +1636,20 @@ fn text_child(child: &JSXChild<'_>) -> Option<Span> {
_ => None,
}
}

/// `items`, the literals of the style prop `name`, with each number of a
/// length as pixels, as styled-components and Emotion read it
fn in_pixels(name: &str, items: &[(usize, Literal)]) -> Vec<(usize, Literal)> {
let unitless = css::disassemble_property(name).into_iter().any(|property| {
is_maintain_value_property(&property) || TIMED.contains(&property.as_ref())
});
items
.iter()
.map(|(level, literal)| match literal {
Literal::Number(number) if !unitless => {
(*level, Literal::Text(format!("{}px", number_code(*number))))
}
literal => (*level, literal.clone()),
})
.collect()
}
38 changes: 29 additions & 9 deletions libs/extractor/src/native/element/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,28 @@ impl<'a> Elements<'_> {
tag: Option<String>,
interactive: bool,
) -> Option<String> {
if !self.reanimated(animation, on) {
if tag.is_some() && !interactive {
// The component `as` gives animates when it is animated
self.reanimated(animation, on);
return tag;
}
self.animated_native(animation, (native_component(kind), local), on, interactive)
.or(tag)
}

/// Reanimated's animated component of the React Native component
/// `native`, which `local` is bound to, for an element taking an
/// animation
pub(super) fn animated_native(
&mut self,
animation: &(u32, String),
(native, local): (&str, &str),
on: &str,
interactive: bool,
) -> Option<String> {
if !self.reanimated(animation, on) {
return None;
}
if interactive {
self.error(
animation.0,
Expand All @@ -109,24 +128,25 @@ impl<'a> Elements<'_> {
)
.on(on, &animation.1),
);
return tag;
return None;
}
// The component `as` gives animates when it is animated
tag.or_else(|| Some(self.animated_component(kind, local)))
Some(self.animated_component(native, local))
}

/// Reanimated's animated component of the React Native component `local`
/// is bound to
fn animated_component(&mut self, kind: &ExportVariableKind, local: &str) -> String {
/// Reanimated's animated component of the React Native component
/// `native`, which `local` is bound to
fn animated_component(&mut self, native: &str, local: &str) -> String {
self.animated.used = true;
let native = native_component(kind);
if ANIMATED.contains(&native) {
return format!("{}.{native}", self.animated.name);
}
if let Some((_, name)) = self.animated.created.iter().find(|(of, _)| of == local) {
return name.clone();
}
let name = fresh_name(&format!("__Animated{local}"), self.code);
let name = fresh_name(
&format!("__Animated{}", local.trim_start_matches('_')),
self.code,
);
self.animated
.created
.push((local.to_string(), name.clone()));
Expand Down
10 changes: 9 additions & 1 deletion libs/extractor/src/native/element/css_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ impl<'a> Elements<'_> {

/// Compile a call of `css` to the style it describes
pub(super) fn css_call(&mut self, call: &CallExpression<'a>) {
let compat = self.is_compat(&call.callee);
let outer = std::mem::replace(&mut self.compat, compat);
let mut choices = Vec::new();
for argument in &call.arguments {
match argument.as_expression() {
Expand All @@ -60,12 +62,16 @@ impl<'a> Elements<'_> {
self.reanimated(&animation, CSS);
}
self.css_code(call.span, choices);
self.compat = outer;
}

/// Compile `css` tagged with CSS text, such as `` css`padding: 8px;` ``,
/// to the style it describes
pub(super) fn css_template(&mut self, template: &TaggedTemplateExpression<'a>) {
let compat = self.is_compat(&template.tag);
let outer = std::mem::replace(&mut self.compat, compat);
let choices = self.css_text(&template.quasi, template.span.start);
self.compat = outer;
self.css_code(template.span, choices);
}

Expand Down Expand Up @@ -204,7 +210,8 @@ impl<'a> Elements<'_> {
}
expression if is_nothing(expression) => {}
expression if self.in_styled && is_function(expression) => {
if gives_style_props(expression) {
// A compat package's function gives React Native styles
if !self.compat && gives_style_props(expression) {
self.error(
expression.span().start,
Problem::new(
Expand All @@ -216,6 +223,7 @@ impl<'a> Elements<'_> {
return;
}
let code = format!("({})({})", self.rewritten(expression), self.props);
self.props_called.set(true);
choices.push(Choice::Given(code));
}
expression => {
Expand Down
Loading
Loading