Skip to content

Refactor sidebar to use reactive Var operations instead of Python conditionals#6528

Draft
masenf wants to merge 1 commit into
mainfrom
claude/migrate-sidebar-to-memo-9bnis
Draft

Refactor sidebar to use reactive Var operations instead of Python conditionals#6528
masenf wants to merge 1 commit into
mainfrom
claude/migrate-sidebar-to-memo-9bnis

Conversation

@masenf
Copy link
Copy Markdown
Collaborator

@masenf masenf commented May 19, 2026

Type of change

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

Description

This PR refactors the sidebar component to use Reflex's reactive Var operations instead of Python-level conditionals, enabling better runtime reactivity and compilation to optimized React code.

Key changes:

  1. Type annotations: Updated function signatures to use rx.vars.StringVar[str], rx.vars.ArrayVar[list[int]], and rx.Var[bool] for reactive parameters instead of plain Python types.

  2. Reactive conditionals: Replaced Python if/else expressions with rx.cond() for dynamic UI rendering:

    • sidebar_leaf_guide(): Now uses rx.cond() for conditional rendering
    • sidebar_leaf(): Converted all conditional styling to rx.cond() calls
    • sidebar_category(): Uses rx.cond() for active state styling
    • sidebar_comp(): Replaced complex nested Python conditionals with reactive rx.cond() chains
  3. Reactive comparisons: Updated logic to use Var operations:

    • is_open calculation now uses index.length() > 0 and bitwise & operator
    • URL checks use .startswith() and .contains() methods on Var objects
    • Replaced string concatenation with direct integer indices
  4. Memoization: Added @rx._x.memo decorator to sidebar_comp() to compile the reactive tree into a single optimized React component that receives runtime props.

  5. Separation of concerns: The sidebar() wrapper function remains a regular Python function that handles non-reactive operations (URL normalization, index calculation), while sidebar_comp() handles all reactive rendering logic.

  6. Removed unused imports: Removed cli_ref from imports as it's no longer used.

  7. Documentation: Added comprehensive docstrings explaining the memoization strategy and the division between reactive and non-reactive logic.

Motivation

This refactoring enables the sidebar to respond to runtime URL and index changes without re-executing Python code, resulting in better performance and more idiomatic Reflex patterns. The memoized component compiles to a single React component that receives these values as props, rather than recalculating the entire tree on each change.

Testing

Existing sidebar functionality is preserved. The changes are internal refactoring to use reactive patterns rather than Python conditionals. All sidebar rendering behavior remains identical from the user's perspective.

https://claude.ai/code/session_013mppZsm9QZVz6DTPYokXR2

Convert sidebar_comp into an experimental memo component so the rendered
sidebar tree compiles to a single React component shared across every
docpage. The route-specific url and per-section indices are now passed
as runtime props, and the per-page JSX no longer re-inlines the entire
sidebar.

- Decorate sidebar_comp with @rx._x.memo; type all parameters as
  rx.Var subclasses (StringVar / ArrayVar) so the memo system can build
  placeholders and emit JSX with Var operations.
- Update helpers (sidebar_leaf_guide, sidebar_leaf, sidebar_item_comp,
  sidebar_category, create_sidebar_section) to accept Var-typed url and
  index parameters.
- Replace Python conditionals on Var-derived expressions with rx.cond.
- Convert index handling (bool(index), index[1:] if open else []) to
  Var operations (index.length(), rx.cond(is_open, index[1:], []).to(...)).
- Express startswith / contains URL checks via StringVar.startswith /
  StringVar.contains chained with Var __or__ / __and__ / __invert__.
- Drop the unused cli_ref_index and width parameters from sidebar_comp;
  sidebar() still accepts width for callers but no longer threads it
  through. normalize_url stays in the outer sidebar() since it relies
  on Python string ops not expressible as Var operations.

https://claude.ai/code/session_013mppZsm9QZVz6DTPYokXR2
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 19, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing claude/migrate-sidebar-to-memo-9bnis (5e22cb2) with main (048c7d8)

Open in CodSpeed

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