Find the CSS to modify the highlight of a tabPanel with cicerone
the error is caused by making the position: fixed;
and looking at the fact that the navbar is the top most element i.e child of the body, and that position : absolute;
fixes this on way would be to modify the css
.navbar-fixed-top, .navbar-fixed-bottom{
position : absolute;
}
However this invalidates the effect of position : fixed;
. So a way to do this is by listening for user clicks on the next|previous
buttons, to toggle between position fixed and position absolute, this can be done using js
and to run js from shiny we need the shinyjs
package.
install.packages("shinyjs")
library(shiny)
library(shinyjs)
library(cicerone)
homeGuide <- Cicerone$
new(id = "homeGuide")$
step(
"[data-value='home']",
"Hello",
"Hello from tab 1",
is_id = FALSE
)$
step(
"[data-value='tab']",
"Tab :(",
"This is a tab",
is_id = FALSE
)$
step(
"[data-value='last']",
"Last",
"This is the last tab the one we check for",
is_id = FALSE
)$
step(
"home_secondary",
"Text",
"This is an input"
)
the UI
- call
useShinyjs
to import shinyjs javascript libs into the webpage. - the next line sets the style by default to position absolute.
ui <- tagList(
useShinyjs(),
tags$head(tags$style(HTML("
.navbar-fixed-top, .navbar-fixed-bottom{
position : absolute;
}
"))),
navbarPage(
"cicerone",
header = list(use_cicerone()),
id = "nav",
position = "fixed-top",
tabPanel(
"home",
h1("First tab", id = "home_primary"),
textInput("home_secondary", "Text")
),
tabPanel(
"tab",
h1("Second tab", id = "tab_primary"),
textInput("tab_secondary", "Text")
),
tabPanel(
"last",
h1("last tab", id = "tab_last"),
textInput("inp_text", "Text")
)
))
Server :
server <- function(input, output, session){
homeGuide$init()$start()
# if the user clicks the next button
observeEvent(input$homeGuide_cicerone_next, {
# check if the last highlighted element is the last tab
if(grepl("last",input$homeGuide_cicerone_next$highlighted) ) runjs("document.querySelector('.navbar').style.position = 'fixed'; document.querySelector('.navbar').style.position")
# because of some weird glitch you need to call the value of document.querySelector('.navbar').style.position in order for it to be changed took me a day to figure this out
})
# now for the previous button
observeEvent(input$homeGuide_cicerone_previous, {
# check if the before previous element is the last tab
# i don't know if this is the way it should behave but apparently shiny is called before the click is sent to js and before previous actually contains the value of the previous element
if(grepl("last",input$homeGuide_cicerone_previous$before_previous) ) runjs("document.querySelector('.navbar').style.position = 'absolute'; document.querySelector('.navbar').style.position")
})
}
shinyApp(ui, server)