英文:
Selecting a tab inside nav_menu toggles the dropdown
问题
我对bslib
中的nav_menu
和nav_select
有一个问题。
当我尝试选择嵌套在nav_menu
内的选项卡时,页面会导航到新选项卡,但它还会打开nav_menu
的下拉列表,就好像被点击了一样,而不是从服务器中选择。然后,下拉列表不会关闭,直到再次点击导航菜单按钮。在页面的其他地方点击不会切换它。
有人见过这个问题吗?这是否是预期行为?有没有解决方法?谢谢!
一个可重现的示例:
library(shiny)
library(bslib)
library(rlang)
nav_items <- function(prefix) {
list(
nav("a", tagList(
paste(prefix, ": tab a content"),
actionButton(inputId = "go_to_tab_c", label = "Go to tab C")
)),
nav("b", paste(prefix, ": tab b content")),
nav_item(
tags$a(icon("github"), "Shiny", href = "https://github.com/rstudio/shiny", target = "_blank")
),
nav_spacer(),
nav_menu(
value = "ya",
title = "Other links",
align = "right",
nav("c", paste(prefix, ": tab c content")),
nav_item(
tags$a(
icon("r-project"),
"RStudio",
href = "https://rstudio.com",
target = "_blank"
)
)
)
)
}
ui <- page_fluid(
shinyjs::useShinyjs(),
page_navbar(
id = "main_nav",
title = "Page",
bg = "#0062cc",
!!!nav_items(prefix = "page_navbar")
)
)
server <- function(input, output, session) {
observeEvent(input$go_to_tab_c, {
nav_select(id = "main_nav", selected = "c")
})
}
shinyApp(ui, server)
英文:
I have a question about nav_menu
and nav_select
from bslib
.
When I try to select a tab that is nested inside a nav_menu
, the page
navigates to the new tab, but it also opens the nav_menu
dropdown list as it
were clicked, instead of selected from the server. Then, the dropdown does not
close until the nav menu button is clicked again. Clicking elsewhere on the page
does not toggle it.
Has anyone seen this? Is it expected behavior? Any ways around it? Thanks!
A reprex:
library(shiny)
library(bslib)
library(rlang)
nav_items <- function(prefix) {
list(
nav("a", tagList(
paste(prefix, ": tab a content"),
actionButton(inputId = "go_to_tab_c", label = "Go to tab C")
)),
nav("b", paste(prefix, ": tab b content")),
nav_item(
tags$a(icon("github"), "Shiny", href = "https://github.com/rstudio/shiny", target = "_blank")
),
nav_spacer(),
nav_menu(
value = "ya",
title = "Other links",
align = "right",
nav("c", paste(prefix, ": tab c content")),
nav_item(
tags$a(
icon("r-project"),
"RStudio",
href = "https://rstudio.com",
target = "_blank"
)
)
)
)
}
ui <- page_fluid(
shinyjs::useShinyjs(),
page_navbar(
id = "main_nav",
title = "Page",
bg = "#0062cc",
!!!nav_items(prefix = "page_navbar")
)
)
server <- function(input, output, session) {
observeEvent(input$go_to_tab_c, {
nav_select(id = "main_nav", selected = "c")
})
}
shinyApp(ui, server)
答案1
得分: 1
请阅读bslib
存储库上的此线程,了解发生这种情况的根本原因。简而言之,这是由于Bootstrap 5
的更改传播到bslib
,然后传播到shiny
引起的。该线程还包括一些示例代码,说明如何使用来自shiny
服务器的一些JS
关闭nav_menu
。
英文:
For anyone arriving here, please read through this thread on the bslib
repo about the underlying reason why this happens. In short, it's a change in Bootstrap 5
that propagates through bslib
and then shiny
. The thread also includes some example code how to close the nav_menu
using some JS
from the shiny
server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论