Skip to content

Add React Native device APIs to @effect/ui-native/kit - #542

Merged
tim-smart merged 21 commits into
mainfrom
agent/nelson-effect-bot/eabedc3bb763
Nov 28, 2026
Merged

tim-smart merged 21 commits into
mainfrom
agent/nelson-effect-bot/eabedc3bb763

Conversation

@effect-bot

@effect-bot effect-bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Adds React Native's device APIs to @effect/ui-native/kit. They call the native modules and RCTDeviceEventEmitter directly, so none of them loads React Native's JavaScript wrappers.

Closes EFF-4575

What's in it

The kit gets KeyboardAvoidingView as a component, and these namespaces: AccessibilityInfo, Alert, AppState, Appearance, BackHandler, Dimensions (with PixelRatio's helpers), I18nManager, Keyboard, Linking, PermissionsAndroid, PlatformColor (with DynamicColorIOS as PlatformColor.dynamic), Share, StatusBar and Vibration.

Each module exposes up to three forms:

  • Effects, such as Linking.openURL(url) and Dimensions.get. When native can reject one, it fails with Native.DeviceError.
  • Command factories for update, such as Keyboard.Dismiss and Alert.Alert. A fallible one answers with Result<A, Native.DeviceError>.
  • Subscriptions and Streams of device events, such as Dimensions.onChange and Keyboard.changes. The Subscriptions are built on Subscription.direct, so they send the current value when they start and then each change.

Every module reads its device from Native.Device, a Context.Reference that NativeApp.register provides. The reference's default is an inert device, so code without a device (a test, say) gets safe values: a 0×0 window at scale 1, the "active" app state, a hidden keyboard, no permissions, and so on.

Shared changes

  • @effect/ui: Subscription.direct's attach can return { close, start }. start runs after the Subscription starts, outside render, which is how a source sends its current value first.
  • Native: Native.Device, Native.DeviceError, and Native.Color widened with a branded NativeColor for PlatformColor. Native.Host gains accessibilityEvent, which AccessibilityInfo.focus uses.
  • NativeApp: setSurfaceProps is no longer a no-op. Surface.initialProps is renamed to props and follows native's updates, Surface.onProps listens for them, and mount re-runs an Effect props when they change. It listens before the first run, and an update that arrives during it replaces that run, and each run gets the same services as the first. This rename is breaking. The package is unreleased.
  • NativeHost tracks the focused input, so Keyboard.dismiss can blur it.

Choices worth a look

  • PlatformColor.make({ ios, android, fallback }) builds one object with both semantic and resource_paths. Each platform's C++ parser reads its own key plus fallback, so one value works on both platforms. PlatformColor.dynamic adds resource_paths: [] and an Android fallback, either android or light. android is required by the type when light is itself a native colour. Without that, Android would get no colour at all.
  • BackHandler.onPress keeps a stack per device, and the latest handler gets each press first. A handler that returns undefined passes the press on. When nothing takes it, Android's default runs. The README explains how this works alongside React Native's own BackHandler.
  • StatusBar.configure is a Subscription that works like RN's <StatusBar> stack: for each field, the latest one wins, and stopping restores the one before. setStyle and setHidden set the value that shows when no configure sets that field.
  • Alert buttons carry IDs, and Alert.alert returns the ID of the button that was pressed (undefined on a dismissal). The result type includes the default "ok" unless the options' type requires buttons with at least one button.
  • KeyboardAvoidingView's padding behavior adds the inset to the consumer's own bottom padding. The inset is computed from the view's bottom edge minus whatever movement of that edge the adjustment itself caused (at most the change in padding, height or position), so it settles after one layout however the parent places the view. PermissionsAndroid.Permission is a union of string literals. Off Android, check gives false and request gives "denied", and neither calls native.
  • Share.share requires a message or a url in its type, and fails with DeviceError without either (iOS never answers then). Its tintColor is a colour string or number, since iOS's sheet takes only a processed number.
  • Following React Native's emitters, a module's addListener/removeListeners are only called on iOS.

Reviewing

There's one commit per module after the first. The first commit holds the shared runtime changes and is the one to read closely. The module commits follow the same shape and mostly stand alone. kit/internal/device.ts, which arrives with Dimensions, holds the shared helpers: source, observe, subscription, promise and callback.

Verification

  • Each module has tests in packages/ui-native/test/kit, written against a fake device (test/utils/FakeDevice.ts). pnpm check, the tests tsconfig, dprint, oxlint and vitest for ui and ui-native all pass (1571 tests).
  • Fake-native runtime: I built a Metro bundle per platform that runs NativeApp.register and the kit in a bare JS context. The context has fake TurboModules and React Native's real RCTDeviceEventEmitter, TurboModuleRegistry, Platform and processColor. On both Android and iOS I checked:
    • Initial values and changes for Dimensions, Appearance, AppState and Keyboard.
    • BackHandler: the default action runs once with no handler, never while a handler takes the press, and once again after the handler closes.
    • Calls go to IntentAndroid on Android and LinkingManager on iOS.
    • StatusBar passes the animation arguments to iOS only.
    • PermissionsAndroid doesn't call native off Android.
    • AccessibilityInfo uses the right module per platform.
    • PlatformColor values pass through processColor unchanged, and iOS resolves dynamic to numbers.
    • Nothing logs a warning.
  • Metro graph: no kit module pulls in a single react-native file, React, the renderer or InitializeCore. Each module is 0.6 to 1.6 KB minified, plus 2.7 KB of internals they all share. These figures are before Vite's tree-shaking.
  • The fake device's modules can enforce how many arguments a method takes, which the AccessibilityInfo tests use.
  • I haven't run anything on a device or simulator, so the native side of each call is only checked against React Native's specs and its C++ colour parsers.

@effect-bot
effect-bot force-pushed the agent/nelson-effect-bot/eabedc3bb763 branch 3 times, most recently from 3be4200 to 64588d5 Compare November 27, 2026 12:25
@github-actions

Copy link
Copy Markdown

Bundle size

Production build, gzip level 9 per chunk. Effect-only client: 26780 B gzip. Full report

App Chunks Raw bytes Gzip bytes Added gzip B Δ raw bytes Δ gzip bytes
counter 1 175849 59960 33180 0 0
features 1 185333 63357 36577 0 0
routing 2 352950 120802 94022 0 0

@effect-bot
effect-bot force-pushed the agent/nelson-effect-bot/eabedc3bb763 branch 4 times, most recently from c8daca8 to 6024322 Compare November 28, 2026 06:27
@effect-bot
effect-bot force-pushed the agent/nelson-effect-bot/eabedc3bb763 branch from 6024322 to 41b6f19 Compare November 28, 2026 06:53
@tim-smart
tim-smart merged commit 82a1750 into main Nov 28, 2026
13 checks passed
@tim-smart
tim-smart deleted the agent/nelson-effect-bot/eabedc3bb763 branch November 28, 2026 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants