r - How to change the collapse breakpoints of a bslib navbar? - Stack Overflow
I'm working with page_navbar
from the bslib
package in a Shiny app in R. I want the navigation bar to collapse at a different breakpoint than it currently does (992px which is associated with .navbar-expand-lg
). The navigation bar expands when there isn't enough space, and since my navbar is frozen with position = "fixed_top"
per the docs, it overlaps the content.
I tried to edit the media query in devtools and add custom CSS as mentioned in this post and this post, but I haven't gotten anything to work. Ideally, I'd be able to either change the breakpoint for .navbar-expand-lg
or replace it with the other ones that come with bs5. Here's a basic app to demonstrate the problem:
library(shiny)
library(bslib)
ui <- page_navbar(
theme = bs_theme(version = 5),
title = "Dashboard",
underline = TRUE,
inverse = TRUE,
fillable = FALSE,
position = "fixed-top",
tags$style(type = "text/css", "body {padding-top: 50px;}"),
nav_panel(
title = "Extremely Long First Page Title",
layout_columns(
card(
card_header("Histogram"),
plotOutput(outputId = "distPlot"),
height = "1000px"
)
)
),
nav_panel(title = "Even Longer Extremely Long Second Page Title"),
nav_panel(title = "Even Longer, Even Longer Extremely Long Third Page Title"),
nav_spacer(),
nav_menu(
title = "Links",
align = "right"
),
nav_menu(
title = "Help",
align = "right"
)
)
server <- function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(1000, 50, 5))
})
}
shinyApp(ui, server)
I'm working with page_navbar
from the bslib
package in a Shiny app in R. I want the navigation bar to collapse at a different breakpoint than it currently does (992px which is associated with .navbar-expand-lg
). The navigation bar expands when there isn't enough space, and since my navbar is frozen with position = "fixed_top"
per the docs, it overlaps the content.
I tried to edit the media query in devtools and add custom CSS as mentioned in this post and this post, but I haven't gotten anything to work. Ideally, I'd be able to either change the breakpoint for .navbar-expand-lg
or replace it with the other ones that come with bs5. Here's a basic app to demonstrate the problem:
library(shiny)
library(bslib)
ui <- page_navbar(
theme = bs_theme(version = 5),
title = "Dashboard",
underline = TRUE,
inverse = TRUE,
fillable = FALSE,
position = "fixed-top",
tags$style(type = "text/css", "body {padding-top: 50px;}"),
nav_panel(
title = "Extremely Long First Page Title",
layout_columns(
card(
card_header("Histogram"),
plotOutput(outputId = "distPlot"),
height = "1000px"
)
)
),
nav_panel(title = "Even Longer Extremely Long Second Page Title"),
nav_panel(title = "Even Longer, Even Longer Extremely Long Third Page Title"),
nav_spacer(),
nav_menu(
title = "Links",
align = "right"
),
nav_menu(
title = "Help",
align = "right"
)
)
server <- function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(1000, 50, 5))
})
}
shinyApp(ui, server)
Share
Improve this question
edited yesterday
Jan
8,4996 gold badges17 silver badges31 bronze badges
asked yesterday
krebokrebo
231 silver badge3 bronze badges
New contributor
krebo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2 Answers
Reset to default 3Use bs_add_variables()
for overwriting the Bootstrap Sass variable $grid-breakpoints
with .where = "declarations"
such that it is defined after Bootstrap (see Theming variables within the bslib
docs):
theme = bs_theme() |>
bs_add_variables(
"grid-breakpoints" = # here e.g. with lg: 800px;
"(xs: 0, sm: 576px, md: 768px, lg: 800px, xl: 1200px, xxl: 1400px)",
.where = "declarations"
)
Result:
The theme bs_theme(5)
uses a bootstrap.min.css
which defines the breakpoints. These have to be overwritten in order to change the collapsing behaviour.
Secondly, you complained about the navbar overlapping your plot. I fixed that by adding some custom CSS to make the navbar positioned relative
rather than fixed
.
library(shiny)
library(bslib)
theme <- bs_theme()
# Overwrite grid breakpoints of 'bootstrap.min.css'
theme <- bs_add_variables(
theme,
"grid-breakpoints" = "(
xs: 0,
sm: 200px,
md: 300px,
lg: 500px, /* change the breakpoints of bootstrap.min.css here!. Make sure, that they are in ascending order */
xl: 1800px
)"
)
ui <- page_navbar(
theme = theme,
title = "Dashboard",
underline = TRUE,
inverse = TRUE,
fillable = FALSE,
position = "fixed-top",
# Custom CSS to modify navbar overlapping behavior
tags$head(
tags$style(HTML("
/* Make the navbar not overlap the plot :) */
.fixed-top, .navbar-fixed-top {
position: relative !important;
top: 0;
right: 0;
left: 0;
z-index: 1030;
}
"))
),
nav_panel(
title = "Extremely Long First Page Title",
layout_columns(
card(
card_header("Histogram"),
plotOutput(outputId = "distPlot"),
height = "1000px"
)
)
),
nav_panel(title = "Even Longer Extremely Long Second Page Title"),
nav_panel(title = "Even Longer, Even Longer Extremely Long Third Page Title"),
nav_spacer(),
nav_menu(
title = "Links",
align = "right"
),
nav_menu(
title = "Help",
align = "right"
)
)
server <- function(input, output, session) {
output$distPlot <- renderPlot({
hist(rnorm(1000, 50, 5))
})
}
shinyApp(ui, server)
It will look like this:
- 2015年世界科技发展回顾:信息技术
- Windows 30岁生快 看他如何改变世界
- 苹果允许预装软件可卸载 或因遭受安卓手机阵型冲击
- .NET开源:微软"云为先"战略的全面铺开
- 导航APP软件免费“醉翁之意不在酒”
- 软件定义网络:正在进行的网络变革
- 谷歌无人驾驶汽车或将带来下一轮失业大潮
- java - ChatResponse how to get the history of the ToolResponseMessage Spring ai - Stack Overflow
- Docker environment for aya rust ebpf compilation - Stack Overflow
- docker compose - Issues with Using CapSolver Extension in Selenium Grid for reCaptcha v2 - Stack Overflow
- Issue with Ruby in CICD environment on Linux: usrbinenv: 'ruby.exe': No such file or directory - Stack Overflow
- reactjs - Why can't AWS Amplify find my components from a working version of my React App? - Stack Overflow
- c# - Loading data to dataGridView freezes the window during loading, why threading does not work here? - Stack Overflow
- c# - Getting username of logged in user with NegotiateWindows domain credentials - Stack Overflow
- wpf - Installation of an upgrade version starting at ResumeDlg - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- apache - is it possible to graceful single process in passenger rails app? - Stack Overflow