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

FA
1d ago
+425-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 a Python mirror on rx.js for the members that have no Python equivalent:

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)(none: use an event trigger, or return the event from a handler)Dispatches events, as event triggers do.
reflex.event(name, payload)(none)Builds an event for addEvents.
rx.button("Read", on_click=rx.call_script("reflex.ref('search').current.value", callback=SearchState.set_query))


class SearchState(rx.State):
    @rx.event
    def read_search(self):
        return rx.run_script(
            rx.js.ref("search").current.value, callback=SearchState.set_query
        )

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. They hide page globals of the same names, which stay reachable as window.env and window.reflex; the docs say so.

Out of scope.

  • No reflex.library / rx.js.library. A JavaScript module imports npm packages directly, and nothing called them.
  • No focus, blur, set or scroll helpers: rx.set_focus, rx.blur_focus, rx.set_value and rx.scroll_to already do that, and the docs point to them.
  • No rx.js.add_events. From Python it only repeated an event trigger (rx.run_script(rx.js.add_events(State.h)) does what on_click=State.h or return State.h does, with an extra round trip through the browser). reflex.addEvents is for JavaScript that sends a value it computed back to Python.
  • 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.
  • Playwright test_call_script.py::test_reflex_api: reflex.ref and reflex.addEvents from string scripts; rx.js.ref read from the backend.
  • Coverage: 100% of the added Python lines and JS statements. ruff, pyright and npm run check are clean.

https://claude.ai/code/session_01LwJUCVxPh3ihrnaFn6CWnf