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_styled.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()` 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"
}
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
- 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
Expand Down
28 changes: 13 additions & 15 deletions apps/expo/src/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Text } from '@devup-ui/react'
import { styled, Text } from '@devup-ui/react'

/** Styled the same in every theme, so it compiles without the runtime */
const Pill = styled(Text)({
bg: '#5a44ff',
borderRadius: '999px',
color: '#fff',
fontSize: '12px',
fontWeight: '600',
px: 2,
py: 1,
})

/** Styles the same in every theme, so it compiles without the runtime */
export function Badge({ label }: { label: string }) {
return (
<Text
bg="#5a44ff"
borderRadius="999px"
color="#fff"
fontSize="12px"
fontWeight="600"
px={2}
py={1}
>
{label}
</Text>
)
return <Pill>{label}</Pill>
}
46 changes: 37 additions & 9 deletions libs/extractor/src/native/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use crate::component::ExportVariableKind;
use crate::fresh_name::fresh_name;

mod css_call;
mod styled;

use styled::is_function;

/// The changes compiling a file makes, or why it cannot compile
pub(super) struct Compiled {
Expand Down Expand Up @@ -177,6 +180,9 @@ pub(super) fn compile(
scoping,
components: &imports.components,
css: &imports.css,
styled: &imports.styled,
props: fresh_name("__props", code),
in_styled: false,
sheet: Sheet::new(fresh_name("__s", code)),
units: fresh_name("__u", code),
units_used: Cell::new(false),
Expand All @@ -190,7 +196,7 @@ pub(super) fn compile(
replacements: imports.replacements,
errors: imports.errors,
};
if !visitor.components.is_empty() || !visitor.css.is_empty() {
if !visitor.components.is_empty() || !visitor.css.is_empty() || !visitor.styled.is_empty() {
visitor.visit_program(program);
}
let Elements {
Expand Down Expand Up @@ -314,6 +320,13 @@ struct Elements<'s> {
components: &'s FxHashMap<SymbolId, (ExportVariableKind, String)>,
/// The bindings of `css`
css: &'s FxHashSet<SymbolId>,
/// The bindings of `styled`
styled: &'s FxHashSet<SymbolId>,
/// The parameter holding the props of a component `styled()` gives
props: String,
/// Whether the styles of a `styled()` component are being read, where a
/// function is called with its props
in_styled: bool,
sheet: Sheet,
/// The name of the function converting values read at runtime
units: String,
Expand Down Expand Up @@ -376,7 +389,7 @@ 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) {
} else if self.is_css_reference(identifier) || self.is_styled_reference(identifier) {
self.errors.push((
identifier.span.start,
format!(
Expand All @@ -392,6 +405,8 @@ impl<'a> Visit<'a> for Elements<'_> {
fn visit_tagged_template_expression(&mut self, template: &TaggedTemplateExpression<'a>) {
if self.is_css(&template.tag) {
self.css_template(template);
} else if let Some(target) = self.styled_target(&template.tag) {
self.styled_template(template, target);
} else {
walk::walk_tagged_template_expression(self, template);
}
Expand All @@ -400,6 +415,10 @@ impl<'a> Visit<'a> for Elements<'_> {
fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
if self.is_css(&call.callee) {
self.css_call(call);
} else if let Some(target) = self.styled_target(&call.callee) {
self.styled_call(call, target);
} else if self.is_styled(&call.callee) {
self.styled_direct(call);
} else {
walk::walk_call_expression(self, call);
}
Expand Down Expand Up @@ -549,13 +568,7 @@ impl<'a> Elements<'_> {

/// Call the hooks reading what `reads` names at `at`
fn call_hooks(&mut self, at: HookAt, reads: Reads) {
self.runtime.hooks_used = self.runtime.hooks_used.union(reads);
let calls: Vec<String> = self
.runtime
.variables(reads)
.map(|(hook, variable)| format!("const {variable} = {}();", self.runtime.hooks[hook]))
.collect();
let call = calls.join(" ");
let call = self.hook_calls(reads);
match at {
HookAt::Body(at) => self.replacements.push((at, at, format!("\n{call}"))),
HookAt::Expression(start, end) => {
Expand All @@ -566,6 +579,17 @@ impl<'a> Elements<'_> {
}
}

/// The statements calling the hooks reading what `reads` names
fn hook_calls(&mut self, reads: Reads) -> String {
self.runtime.hooks_used = self.runtime.hooks_used.union(reads);
let calls: Vec<String> = self
.runtime
.variables(reads)
.map(|(hook, variable)| format!("const {variable} = {}();", self.runtime.hooks[hook]))
.collect();
calls.join(" ")
}

/// Whether the element being compiled is in a function component or a
/// hook, which then calls the hooks reading what `reads` names first
fn read_in_component(&mut self, reads: Reads) -> bool {
Expand Down Expand Up @@ -1128,6 +1152,10 @@ impl<'a> Elements<'_> {
/// The code of `expression`
fn source(&self, expression: &Expression<'_>) -> String {
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
return format!("({code})({})", self.props);
}
if matches!(
expression.without_parentheses(),
Expression::SequenceExpression(_)
Expand Down
18 changes: 17 additions & 1 deletion libs/extractor/src/native/element/css_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use oxc_ast_visit::Visit;
use oxc_span::{GetSpan, Span};
use std::fmt::Write;

use super::styled::{gives_style_props, is_function};
use super::{Elements, Prop, Styles, attribute_expression, classify};
use crate::native::errors::Problem;
use crate::native::style::{Choice, combined, style_code};
Expand Down Expand Up @@ -154,7 +155,7 @@ impl<'a> Elements<'_> {
/// One argument of `css()`: an object of styles, a condition choosing
/// objects, or a style it passes on whole, such as what another `css()`
/// gives
fn css_part(&mut self, expression: &Expression<'a>, choices: &mut Vec<Choice>) {
pub(super) fn css_part(&mut self, expression: &Expression<'a>, choices: &mut Vec<Choice>) {
match expression.without_parentheses() {
Expression::ObjectExpression(object) => {
let object_choices = self.object_choices(object, CSS);
Expand Down Expand Up @@ -185,6 +186,21 @@ impl<'a> Elements<'_> {
self.error(expression.span().start, class_problem(CSS, "a part"));
}
expression if is_nothing(expression) => {}
expression if self.in_styled && is_function(expression) => {
if gives_style_props(expression) {
self.error(
expression.span().start,
Problem::new(
"a function of the props gives style props, which React Native cannot read at runtime",
"return what `css()` gives, or write each value as a function of the props",
)
.call("styled()"),
);
return;
}
let code = format!("({})({})", self.rewritten(expression), self.props);
choices.push(Choice::Given(code));
}
expression => {
let code = self.rewritten(expression);
choices.push(Choice::Given(code));
Expand Down
Loading
Loading