taotanapol commited on
Commit
36b762f
·
verified ·
1 Parent(s): 53dd0c1

Make the sidebar fixed

Browse files
Files changed (1) hide show
  1. streamlit_app.py +59 -10
streamlit_app.py CHANGED
@@ -343,24 +343,73 @@ st.markdown(
343
  section[data-testid="stSidebar"] {
344
  background: var(--cream-2);
345
  }
346
- /* Lock the sidebar open on desktop so Restaurant / Branch / Date
347
- filters are always one click away. Mobile keeps its slide-out
348
- toggle because the sidebar would otherwise eat the whole screen.
349
- Selectors cover several Streamlit versions of the collapse chevron
350
- when one matches the others are harmless no-ops. */
 
 
 
 
 
 
 
 
 
 
351
  @media (min-width: 769px) {
 
352
  button[data-testid="stSidebarCollapseButton"],
353
  button[data-testid="stSidebarCollapsedControl"],
354
  button[data-testid="collapsedControl"],
355
  [data-testid="stSidebarCollapseButton"],
356
- [data-testid="collapsedControl"] {
357
- display: none !important;
358
- }
359
- /* Belt and suspenders: also catch the chevron rendered inside
360
- the sidebar header on newer Streamlit builds. */
361
  section[data-testid="stSidebar"] > div:first-child > button {
362
  display: none !important;
363
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
  }
365
  .small-caption {
366
  color: var(--muted);
 
343
  section[data-testid="stSidebar"] {
344
  background: var(--cream-2);
345
  }
346
+ /* Desktop only (≥ 769 px):
347
+ (1) Hide the collapse chevron so the sidebar can't be closed.
348
+ (2) Pin the sidebar to the viewport with `position: fixed` so
349
+ the Language / Restaurant / Branch / Date / Source /
350
+ Sign-out section stays in place while the main content
351
+ scrolls. Selectors stack `html body` + the tag + the
352
+ `.stSidebar` class + the `[data-testid="stSidebar"]`
353
+ attribute — specificity (0,0,3,2) — which is high enough
354
+ to beat any Streamlit Emotion class (.st-emotion-cache-*)
355
+ that uses 2 classes (0,0,2,0) even when both !important.
356
+ (3) Match the sidebar width to Streamlit's actual rendered
357
+ width (336 px, observed via DevTools) and push the main
358
+ content right by the same amount so nothing slides under.
359
+ Mobile keeps its slide-out drawer behavior — locking the sidebar
360
+ open on a phone would consume the entire screen. */
361
  @media (min-width: 769px) {
362
+ /* (1) Hide collapse chevron. */
363
  button[data-testid="stSidebarCollapseButton"],
364
  button[data-testid="stSidebarCollapsedControl"],
365
  button[data-testid="collapsedControl"],
366
  [data-testid="stSidebarCollapseButton"],
367
+ [data-testid="collapsedControl"],
 
 
 
 
368
  section[data-testid="stSidebar"] > div:first-child > button {
369
  display: none !important;
370
  }
371
+
372
+ /* (2) Pin sidebar to viewport. */
373
+ html body section.stSidebar[data-testid="stSidebar"],
374
+ html body section[data-testid="stSidebar"].stSidebar,
375
+ html body section[data-testid="stSidebar"] {
376
+ position: fixed !important;
377
+ top: 0 !important;
378
+ left: 0 !important;
379
+ bottom: 0 !important;
380
+ height: 100vh !important;
381
+ max-height: 100vh !important;
382
+ width: 336px !important;
383
+ min-width: 336px !important;
384
+ max-width: 336px !important;
385
+ z-index: 999 !important;
386
+ overflow-y: auto !important;
387
+ transform: none !important;
388
+ }
389
+ /* Inner content of the sidebar also gets a fixed viewport
390
+ height + its own scroll so long sidebars don't break the
391
+ layout. */
392
+ section[data-testid="stSidebar"] > div,
393
+ [data-testid="stSidebarContent"],
394
+ [data-testid="stSidebarUserContent"] {
395
+ height: 100vh !important;
396
+ max-height: 100vh !important;
397
+ overflow-y: auto !important;
398
+ }
399
+
400
+ /* (3) Push the main content right by the sidebar's width. */
401
+ html body [data-testid="stMain"],
402
+ html body section[data-testid="stMain"],
403
+ html body .main,
404
+ html body div.main,
405
+ html body .main .block-container,
406
+ html body [data-testid="stAppViewContainer"] > .main {
407
+ margin-left: 336px !important;
408
+ }
409
+ /* Header bar already spans full width — leave it alone. */
410
+ [data-testid="stHeader"] {
411
+ margin-left: 0 !important;
412
+ }
413
  }
414
  .small-caption {
415
  color: var(--muted);