
Update the active item in a service navigation component
Source:R/service_navigation.R
update_service_navigation.RdSends a message to the browser to update the highlighted active item in the service navigation bar.
When you need this function: when navigation is triggered
programmatically — for example, via a next / back button or a footer link
that points to a main page. In those cases the nav link itself is not
clicked, so the JavaScript binding does not fire and the active state does
not update automatically. Call update_service_navigation() alongside
your tab-switching call to keep them in sync.
When you don't need this function: when the user clicks a service
navigation link directly. The JavaScript binding updates the active state
automatically, so you only need to switch the tab panel in your
observeEvent().
inputId is cleaned up the same way as in service_navigation(), so you
can pass either the inputID you supplied there (e.g. "Page-Two") or its
cleaned form ("page_two").
See also
Other Govstyle navigation:
backlink_Input(),
navigate_to(),
service_navigation(),
service_navigation_server(),
update_page_title()
Examples
# Works the same with shiny::tabsetPanel() + shiny::updateTabsetPanel().
ui <- shinyGovstyle::gov_page(
shinyGovstyle::service_navigation(c("Page one", "Page two")),
bslib::navset_hidden(
id = "tabs",
bslib::nav_panel("page_one", shiny::actionButton("next_btn", "Next")),
bslib::nav_panel("page_two", "Page two content")
)
)
server <- function(input, output, session) {
# Nav link clicked — JS handles the active state, just switch the panel.
shiny::observeEvent(input$page_two, {
bslib::nav_select("tabs", "page_two")
})
# Programmatic navigation — the nav link is not clicked, so call
# update_service_navigation() explicitly.
shiny::observeEvent(input$next_btn, {
bslib::nav_select("tabs", "page_two")
shinyGovstyle::update_service_navigation(session, "page_two")
})
}
if (interactive()) shiny::shinyApp(ui = ui, server = server)