Style individual bsTooltip (shinyBS) elements

假如想象 提交于 2021-01-28 12:18:33

问题


I am trying to add some tooltips to different action buttons in my shiny app through the bsTooltip() function of the shinyBS package, and I would like to modify the width just of a specific tooltip box. To do that, I can specify the HTML tags at the beginning of my UI and modify directly the CSS, but if I use the simple element .tooltip {...} I modify the width of every tooltip in my code:

Below you can find a minimal reproducible example with two different action buttons:

library(shiny)
library(shinyBS)

library(shiny)

ui <- fluidPage(

  tags$head(tags$style(HTML(".tooltip {width: 300px;}"))),

  br(),

  actionButton(inputId = "button1",
               label = "First"),
  bsTooltip(id = "button1",
            title = "Bonjour!",
            placement = "right",
            trigger = "hover"),

  br(),
  br(),

  actionButton(inputId = "button2",
               label = "Second"),
  bsTooltip(id = "button2",
            title = "Hello!",
            placement = "right",
            trigger = "hover")

)

server <- function(input, output, session) {

}

shinyApp(ui, server)

I've been in this situation before, for example when I had to modify the color of the placeholder of a specific textInput() widget. To do that, in my HTML() function I specified:

tags$head(tags$style(HTML("#textinput_ID::placeholder {color: #EEE1525;}")))

but this doesn't seem to work in this case.

Thanks a lot for your help!


回答1:


You can wrap your actionButton and bsTooltip in a div with an id. Now you can select this div by it's id and only style the tooltip inside.

library(shiny)
library(shinyBS)

ui <- fluidPage(

  tags$head(tags$style(HTML("#button1_div .tooltip {width: 300px;}"))),

  br(),
  div(id='button1_div',
      actionButton(inputId = "button1",
                   label = "First"),
      bsTooltip(title = "Bonjour!",
                placement = "right",
                trigger = "hover")),

  br(),
  br(),

  actionButton(inputId = "button2",
               label = "Second"),
  bsTooltip(id = "button2",
            title = "Hello!",
            placement = "right",
            trigger = "hover")

)

server <- function(input, output, session) {

}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/58320525/style-individual-bstooltip-shinybs-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!