Using the shinymaterial
package, I'm attempting to check if a tab is active in an observer and "do something" i.e. print("Hello")
in below example. The tabs in shinymaterial::material_tabs()
do not have a shiny inputId associated with them, simply an href with class = "active"
if the tab is open.
Below is my feeble attempt of trying this with the shinyjs
package:
Reprex:
ui <- material_page(
useShinyjs(),
title = NULL,
# Define tabs
material_tabs(
tabs = c(
"First Tab" = "first_tab",
"Second Tab" = "second_tab"
)
),
# Define tab content
material_tab_content(
tab_id = "first_tab",
tags$h1("First Tab Content")
),
material_tab_content(
tab_id = "second_tab",
tags$h1("Second Tab Content")
)
)
server <- function(input, output, session) {
#Below does not work
# observe({
# if(session$sendCustomMessage(type = "shinymaterialJS", "$('li.tab a.active[href$=\"#second_tab\"]')")){
# print("Hello")
# }
#
# })
}
shinyApp(ui = ui, server = server)
There's probably an event triggered when the active tab changes but I'm not familiar with this package and I don't know.
Below is a solution using a MutationObserver (it does not use
shinyjs
).