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_react_native_focused_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 `Focused` (internal), which renders a `TextInput` with the style the build compiles for whether it has focus, following its `onFocus` and `onBlur` and still calling the ones it is given. The runtime grows from 630 B to 702 B min+gzip.",
"date": "2026-11-21T00:00:00.000Z"
}
8 changes: 8 additions & 0 deletions .changepacks/changepack_log_react_native_states.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 `_hover`, `_focus` and `_disabled` for React Native (part of #411): `_hover` and `_focus` read the `hovered` and `focused` that `Pressable` gives its style function, an `Input` with `_focus` renders through the runtime's `Focused`, and `_disabled` applies while the element's `disabled` prop is true. In `css()` these and `_active` are build errors. 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 @@ -13,6 +13,7 @@ on:
- feat/react-native-responsive
- feat/react-native-pressed
- feat/react-native-css
- feat/react-native-styled

# 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
11 changes: 6 additions & 5 deletions apps/expo/check-bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
// the breakpoint and the orientation, so it calls each of those hooks of the
// runtime; src/Badge.tsx is styled the same in every theme and at every
// width, so it must not use the runtime. Its buttons take press styles, which
// React Native's `Pressable` applies through a function for `style`.
// React Native's `Pressable` applies through a function for `style`, and its
// input a focus style, which the runtime's `Focused` applies.
import { readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'

const PLACEHOLDER = 'Cannot run on the runtime'
/** Names of the runtime that only code using it holds, as minified code keeps them */
const RUNTIME_NAMES =
/\b(?:defineThemes|useThemeIndex|useBreakpoint|useLandscape)\b/g
/** The hooks src/App.tsx must call */
const APP_HOOKS = ['useThemeIndex', 'useBreakpoint', 'useLandscape']
/\b(?:defineThemes|useThemeIndex|useBreakpoint|useLandscape|Focused)\b/g
/** What src/App.tsx must use from the runtime */
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 */
Expand Down Expand Up @@ -144,7 +145,7 @@ for (const platform of ['ios', 'android']) {
for (const hook of APP_HOOKS) {
const callers = [...(users.get(hook) ?? [])]
if (!callers.some((source) => /src[\\/]App\.tsx$/.test(source))) {
failures.push(`${platform}/${file}: src/App.tsx does not call ${hook}`)
failures.push(`${platform}/${file}: src/App.tsx does not use ${hook}`)
}
}
const all = [
Expand Down
1 change: 1 addition & 0 deletions apps/expo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function App() {
Your name
</Text>
<Input
_focus={{ borderColor: '#5a44ff' }}
border="1px solid $border"
borderRadius="8px"
color="$text"
Expand Down
143 changes: 106 additions & 37 deletions libs/extractor/src/native/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use super::errors::{Problem, RuntimeFeature};
use super::imports::{self, native_component};
use super::props::{Declaration, Tokens, declarations, dynamic_keys, literal_levels};
use super::sheet::Sheet;
use super::style::{Choice, Reads, State, Vars, cascade, in_theme, style_code, varied, when};
use super::style::{
Choice, INTERACTIONS, Reads, State, Vars, cascade, in_theme, interaction_keys, style_code,
varied, when, without,
};
use super::theme::{DEFAULT_BREAKPOINTS, selector_theme};
use super::values::{Literal, NativeValue, ROOT_FONT_SIZE};
use super::{NativePlatform, NativeTarget, REACT_NATIVE, RUNTIME, Replacement};
Expand Down Expand Up @@ -53,6 +56,13 @@ struct Runtime {
/// The variable holding whether the element is pressed, in the function
/// `Pressable` calls for its style
pressed: String,
/// The variables holding whether the pointer is over the element and
/// whether it has focus
hovered: String,
focused: String,
/// The component telling a `TextInput` whether it has focus
focused_wrapper: String,
focused_used: bool,
/// The hooks reading each, by what they read
hooks: [String; 3],
/// What the hooks the file calls read
Expand Down Expand Up @@ -80,6 +90,10 @@ impl Runtime {
breakpoint: fresh_name("__b", code),
landscape: fresh_name("__l", code),
pressed: fresh_name("__p", code),
hovered: fresh_name("__h", code),
focused: fresh_name("__f", code),
focused_wrapper: fresh_name("__Focused", code),
focused_used: false,
hooks: HOOKS.map(|hook| fresh_name(&format!("__{hook}"), code)),
hooks_used: Reads::default(),
wrapper: fresh_name("__Themed", code),
Expand All @@ -97,6 +111,9 @@ impl Runtime {
breakpoint: &self.breakpoint,
landscape: &self.landscape,
pressed: &self.pressed,
hovered: &self.hovered,
focused: &self.focused,
disabled: "false",
}
}

Expand All @@ -112,7 +129,7 @@ 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 || theme_api) {
if !(self.hooks_used.any() || self.wrapper_used || self.focused_used || theme_api) {
return None;
}
let reads = self.hooks_used.union(self.wrapper_reads);
Expand All @@ -123,6 +140,9 @@ impl Runtime {
if self.wrapper_used {
specifiers.push(format!("Themed as {}", self.wrapper));
}
if self.focused_used {
specifiers.push(format!("Focused as {}", self.focused_wrapper));
}
let theme = &target.theme;
let mut calls = String::new();
if reads.theme || theme_api {
Expand Down Expand Up @@ -270,6 +290,9 @@ fn classify(name: &str) -> Prop {
"_portrait" => Prop::When(State::Landscape(false)),
"_landscape" => Prop::When(State::Landscape(true)),
"_pressed" | "_active" => Prop::When(State::Pressed),
"_hover" => Prop::When(State::Hovered),
"_focus" => Prop::When(State::Focused),
"_disabled" => Prop::When(State::Disabled),
"className" => Prop::ClassName,
"typography" => Prop::Declares,
"styleOrder" | "styleVars" => Prop::Unsupported(Problem::new(
Expand Down Expand Up @@ -366,6 +389,11 @@ struct Styles {
removed: Vec<Span>,
/// The `style` prop, which leaves the element with the style props
style: Option<Span>,
/// The code of the element's `disabled` prop, which `_disabled` reads
disabled: Option<String>,
/// Whether `Focused` renders the element, which tells whether it has
/// focus: a `TextInput` taking a focus style
focus_wrapped: bool,
/// The `style` prop's value compiled, which it keeps when the element
/// takes no other style
kept: Option<Replacement>,
Expand Down Expand Up @@ -687,8 +715,17 @@ impl<'a> Elements<'_> {
}
let retagged = tag.is_some();
let press = self.press.take();
if let Some(press) = press.filter(|_| styles.blocks.iter().any(Choice::pressed)) {
tag = self.pressable(press, kind, local, tag);
let held: Vec<State> = INTERACTIONS
.into_iter()
.filter(|state| styles.blocks.iter().any(|choice| choice.holds(*state)))
.collect();
if let Some(press) = press.filter(|_| !held.is_empty()) {
if *kind == ExportVariableKind::Input && held == [State::Focused] && tag.is_none() {
// A `TextInput` tells its focus only through its events
styles.focus_wrapped = true;
} else {
tag = self.pressable(press, kind, local, tag);
}
}
self.finish(element, styles, (tag, local), previous_end);
self.children(element, kind, local, retagged);
Expand All @@ -709,6 +746,12 @@ impl<'a> Elements<'_> {
return None;
};
let name = name.name.as_str();
if name == "disabled" {
styles.disabled = Some(
attribute_expression(attribute)
.map_or_else(|| "true".to_string(), |value| self.source(value)),
);
}
if name == "src" && *kind == ExportVariableKind::Image {
self.image_source(attribute, local);
return None;
Expand Down Expand Up @@ -914,11 +957,12 @@ impl<'a> Elements<'_> {
offset: u32,
local: &str,
) -> Vec<Choice> {
// `Pressable` tells whether it is pressed; only the orientation needs
// the runtime
if state == State::Pressed {
// `Pressable` tells whether it is pressed, hovered or focused, and
// `disabled` whether it is disabled: only the orientation needs the
// runtime
if INTERACTIONS.contains(&state) {
self.press.get_or_insert_with(|| (offset, name.to_string()));
} else if !self.target.runtime {
} else if !self.target.runtime && state != State::Disabled {
self.error(
offset,
RuntimeFeature::Orientation.problem().on(local, name),
Expand Down Expand Up @@ -1207,50 +1251,75 @@ impl<'a> Elements<'_> {
style,
kept,
opaque_spread,
disabled,
focus_wrapped,
} = styles;
choices.extend(blocks);
let pressed = choices.iter().any(Choice::pressed);
let mut style_code = style_code(
choices,
(classes, given),
&mut self.sheet,
&self.runtime.vars(),
);
if disabled.is_none() {
// `_disabled` applies only to an element given `disabled`
choices = choices
.into_iter()
.map(|choice| without(choice, State::Disabled))
.collect();
}
let disabled = disabled.unwrap_or_default();
let vars = Vars {
disabled: &disabled,
..self.runtime.vars()
};
let keys = interaction_keys(&choices, &vars);
let mut style_code = style_code(choices, (classes, given), &mut self.sheet, &vars);
let reads = style_code
.as_ref()
.map_or_else(Reads::default, |style| style.reads);
if let Some(style) = style_code.as_mut().filter(|_| pressed) {
let focus_wrapped = focus_wrapped && style_code.is_some();
if let Some(style) = style_code
.as_mut()
.filter(|_| !keys.is_empty() && !focus_wrapped)
{
// `Pressable` calls a function for its style with its state
style.code = format!(
"({{ pressed: {} }}) => {}",
self.runtime.pressed, style.code
);
style.code = format!("({{ {} }}) => {}", keys.join(", "), style.code);
}
let wrapped = reads.any() && !self.read_in_component(reads);
if wrapped {
self.runtime.wrapper_used = true;
let in_component = !reads.any() || self.read_in_component(reads);
if !in_component {
self.runtime.wrapper_reads = self.runtime.wrapper_reads.union(reads);
let wrapper = self.runtime.wrapper.clone();
self.rename(element, &wrapper);
} else if let Some(tag) = &tag {
self.rename(element, tag);
}
let wrapper = if focus_wrapped {
self.runtime.focused_used = true;
Some(self.runtime.focused_wrapper.clone())
} else if in_component {
None
} else {
self.runtime.wrapper_used = true;
Some(self.runtime.wrapper.clone())
};
if let Some(name) = wrapper.as_ref().or(tag.as_ref()) {
self.rename(element, name);
}
let Some(style_code) = style_code else {
self.remove(&removed);
self.replacements.extend(kept);
return;
};
let code = if wrapped {
// The wrapper gives the theme, the breakpoint and the orientation,
// in this order: the style takes them up to the last it reads
let read = self.runtime.variables(reads).last().map_or(0, |(at, _)| at);
let parameters = [
let code = if wrapper.is_some() {
// `Focused` gives whether the element has focus first; a wrapper
// gives the theme, the breakpoint and the orientation, in this
// order, to an element outside a component: the style takes them
// up to the last it reads
let focus = focus_wrapped.then_some(self.runtime.focused.as_str());
let read = self.runtime.variables(reads).last().map(|(at, _)| at);
let runtime = [
&self.runtime.index,
&self.runtime.breakpoint,
&self.runtime.landscape,
][..=read]
.iter()
.map(|name| name.as_str())
];
let parameters = focus
.into_iter()
.chain(
runtime[..read.filter(|_| !in_component).map_or(0, |at| at + 1)]
.iter()
.map(|name| name.as_str()),
)
.collect::<Vec<_>>()
.join(", ");
format!(
Expand Down Expand Up @@ -1290,12 +1359,12 @@ impl<'a> Elements<'_> {
return Some(self.pressable.clone());
}
(_, Some(_)) => Problem::new(
"a press style makes the element React Native's `Pressable`, which cannot render the component `as` gives",
"a press, hover or focus style makes the element React Native's `Pressable`, which cannot render the component `as` gives",
"remove `as`, or pass a function reading `pressed` to the `style` of a `Pressable`",
),
(native, None) => Problem::new(
format!(
"a press style needs React Native's `Pressable`, and the element renders React Native's `{native}`"
"a press, hover or focus style needs React Native's `Pressable`, and the element renders React Native's `{native}`"
),
"wrap it in a `Box` or a `Button` with `_pressed`",
),
Expand Down
14 changes: 12 additions & 2 deletions libs/extractor/src/native/element/css_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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};
use crate::native::style::{Choice, State, combined, style_code};
use crate::native::values::Literal;

/// The name `css()` errors call the call by
Expand Down Expand Up @@ -49,7 +49,7 @@ impl<'a> Elements<'_> {
self.error(
offset,
Problem::new(
"a press style applies while an element is pressed, which `css()` cannot tell",
"a press, hover or focus style applies while the element is in that state, which `css()` cannot tell",
format!("give `{name}` to the element"),
)
.on(CSS, &name),
Expand All @@ -67,6 +67,16 @@ impl<'a> Elements<'_> {

/// Put the style `choices` describe in place of the `css` at `span`
fn css_code(&mut self, span: Span, choices: Vec<Choice>) {
if choices.iter().any(|choice| choice.holds(State::Disabled)) {
self.error(
span.start,
Problem::new(
"`_disabled` applies while the element is disabled, which `css()` cannot tell",
"give `_disabled` to the element",
)
.call(CSS),
);
}
let code = style_code(
choices,
(Vec::new(), Vec::new()),
Expand Down
Loading
Loading