Add `reflex`, the API browser code reaches Reflex through

FA
7h ago
+722-11

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Description

First of a stacked series for ENG-9127 (safer script evaluation).

Problem. Code run by rx.call_script sees every export of utils/state.js, plus socket, navigate, params and token. Apps and Reflex itself come to depend on frontend internals by accident, and none of them can move.

Change. This PR adds one defined object, reflex (utils/reflex.js), and its Python mirror on rx.js:

JavaScriptPython (rx.js)What it does
reflex.ref(id)rx.js.ref(id)The ref of the component with that id. Its current is the element.
reflex.addEvents(events)rx.js.add_events(*events)Dispatches events, as event triggers do.
reflex.event(name, payload)(none)Builds an event for addEvents.
reflex.library(name)rx.js.library(name)Returns a promise of an npm library bundled for dynamic components.
rx.button("Focus", on_click=rx.call_script("reflex.ref('search').current.focus()"))


class SearchState(rx.State):
    @rx.event
    def clear_later(self):
        return rx.run_script(rx.js.add_events(SearchState.clear))

Why the name reflex. Comparable frameworks name their browser API after the framework: Shiny's Shiny, htmx's htmx, Livewire's $wire, Dash's dash_clientside. A generic name collides with an app's own names. An earlier draft called it env, which shadowed the env from env.json that rx.get_upload_url renders (getBackendURL(env.UPLOAD)). That made upload URLs in backend scripts silently wrong. Scripts now see both names: env (the config, as in v0.11) and reflex.

Errors. Each one states the problem, its cause and the fix:

  • rx.js.add_events with a handler that needs arguments fails at definition time. The message says which handler failed and how to pass the arguments.
  • reflex.library rejects Reflex internals ($/...), a library the app does not bundle (the message says to call bundle_library(...)), and calls made before the app has mounted (the message says to call it from an event such as on_mount).

Out of scope.

  • reflex exposes no state and no queueEvents; those need a concrete use first.
  • ref_<id> registry keys stay private.
  • Restricting what a script can see is a later PR. This one changes no name a script could already reach.

Tests

  • vitest tests/reflex.test.js: each member, with exact values and error texts; late binding; the object is frozen; load order across the import cycle with state.js.
  • vitest run_script.test.js: the names a backend script sees, plus a regression test for getBackendURL(env.UPLOAD) → …/_upload/a.png.
  • tests/units/reflex_base/test_js.py: the rendered expressions and imports, and the exact error for a handler that needs arguments.
  • Playwright test_call_script.py::test_reflex_api: reflex from a string script; rx.js from an event trigger and from a backend handler; ref read from the backend; library from on_mount.
  • Coverage: 100% of the added Python lines and JS statements. ruff, pyright and npm run check are clean.

https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf