Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sfc-gh-nbellante
Copy link
Contributor

@sfc-gh-nbellante sfc-gh-nbellante commented Jul 3, 2025

Describe your changes

This change fixes the bug linked below and adds a bunch of testing to ensure this doesn't happen again without us catching it. Additionally, it was discovered that there were some tests that were locking in this buggy behavior, so those have been updated as well.

GitHub Issue Link (if applicable)

This PR fixes #11848

Testing Plan

Added a new e2e test file as I felt it was merited to ensure this functionality doesn't regress. The test injects some javascript to look for any transient flickers that might occur that could be missed by playwright.


Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

@snyk-io
Copy link
Contributor

snyk-io bot commented Jul 3, 2025

🎉 Snyk checks have passed. No issues have been found so far.

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

@github-actions
Copy link
Contributor

github-actions bot commented Jul 3, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-11861/streamlit-1.46.1-py3-none-any.whl
🕹️ Preview app pr-11861.streamlit.app (☁️ Deploy here if not accessible)

@sfc-gh-nbellante sfc-gh-nbellante added security-assessment-completed Security assessment has been completed for PR change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Jul 3, 2025
… sidebar behavior

- Removed unused useRef for width initialization.
- Integrated innerWidth from useWindowDimensionsContext to determine sidebar collapse state.
- Updated useLayoutEffect dependencies to include innerWidth for better responsiveness.
@sfc-gh-nbellante sfc-gh-nbellante changed the title taking a TDD approach to this bugfix. these should fail (at least some of them) [fix] No more sidebar flickering when initial_sidebar_state="collapsed" Jul 3, 2025
@sfc-gh-nbellante sfc-gh-nbellante requested a review from Copilot July 3, 2025 23:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a sidebar flickering issue when initial_sidebar_state="collapsed" by refactoring how sidebar collapse state is initialized and updated, and it bolsters coverage with both unit and end-to-end tests to prevent regressions.

  • Refactor AppView to remove useLayoutEffect/useRef in favor of useExecuteWhenChanged and simplify state initialization.
  • Add comprehensive unit tests in AppView.test.tsx for various sidebar initial states and transitions.
  • Introduce new E2E tests in e2e_playwright that inject monitoring scripts to catch transient flickers during page load.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
frontend/app/src/components/AppView/AppView.tsx Refactored sidebar-collapse logic: removed legacy hooks, added useExecuteWhenChanged.
frontend/app/src/components/AppView/AppView.test.tsx Added unit tests covering sidebar render and collapse/expand behavior.
e2e_playwright/st_sidebar_flicker_test.py New E2E tests monitoring aria-expanded changes to detect sidebar flicker.
e2e_playwright/st_sidebar_flicker.py Demo Streamlit app that drives the flicker tests via set_page_config.
e2e_playwright/multipage_apps_v2/mpa_v2_top_nav_test.py Updated mobile navigation test to assert aria-expanded instead of visibility.
e2e_playwright/hello_app_test.py Moved viewport setup calls to top of tests to stabilize sidebar state during resizing.
Comments suppressed due to low confidence (2)

frontend/app/src/components/AppView/AppView.test.tsx:1288

  • This test currently sets up the scenario but lacks assertions after simulating script finish; add checks or simulate scriptFinishedHandler to verify the sidebar override logic is applied as intended.
      // Since we can't easily trigger the script finished handler in the test,

frontend/app/src/components/AppView/AppView.tsx:206

  • [nitpick] Consider initializing isSidebarCollapsed with the result of shouldCollapse(initialSidebarState, parseInt(activeTheme.emotion.breakpoints.md, 10), innerWidth) instead of a hardcoded true, so the UI won't briefly flash the wrong sidebar state before the effect runs.
  const [isSidebarCollapsed, setSidebarIsCollapsed] = useState<boolean>(true)

@sfc-gh-nbellante sfc-gh-nbellante marked this pull request as ready for review July 3, 2025 23:34
)

const hasInitializedWidthRef = useRef(false)
const [isSidebarCollapsed, setSidebarIsCollapsed] = useState<boolean>(true)
Copy link
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby Jul 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is some logic outside of this PR I am missing, but if we initialize this as collapsed and update below to only update the collapsed state when the innerWidth changes, user clicks expand button, initial state changes, or theme changes, does this break the case that the initial state was expanded?
Or perhaps it doesn't break it because the innerWidth will always change as part of rendering? If this is right, it seems not ideal to rely on that inner width change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this logic definitely has room for improvement. I've filed a tech debt item to refine this behavior and clean it up. In the short term, I have this initialized to true because I didn't want to put the initialization logic that would be flawed upon first render when considering the inner width of 0. We could instead add conditional checks and initialize this as undefined if you feel that would be clearer.

Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@sfc-gh-nbellante sfc-gh-nbellante merged commit 4a9ad33 into develop Jul 8, 2025
38 checks passed
@sfc-gh-nbellante sfc-gh-nbellante deleted the nico/bugfix-11848 branch July 8, 2025 17:37
sfc-gh-nbellante added a commit that referenced this pull request Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sidebar briefly pops up when initial_sidebar_state="collapsed"

4 participants