最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - How to hide a bslib popover by clicking outside? - Stack Overflow

programmeradmin5浏览0评论

I was trying with the following example code

library(shiny)
library(bslib)

ui <- page_sidebar(
  title = "My dashboard",
  sidebar = "Sidebar",
  "Main content area",
  actionButton(
    "btn",
    "BTN"
  ) |>
    popover(
      "Popover message",
      title = "Popover title",
      selectInput(
        "test",
        "Test",
        letters[1:3],
        "a"
      ),
      options = list(trigger = "click focus")
    ),
  textOutput("text")
)

server <- function(input, output, session){
  output$text <- renderText({
    input$test
  })
}

shinyApp(ui, server)

I want to close the popover when I click outside of the popover, similar like we have in shiny modal with easyClose = TRUE.

I was trying with the following example code

library(shiny)
library(bslib)

ui <- page_sidebar(
  title = "My dashboard",
  sidebar = "Sidebar",
  "Main content area",
  actionButton(
    "btn",
    "BTN"
  ) |>
    popover(
      "Popover message",
      title = "Popover title",
      selectInput(
        "test",
        "Test",
        letters[1:3],
        "a"
      ),
      options = list(trigger = "click focus")
    ),
  textOutput("text")
)

server <- function(input, output, session){
  output$text <- renderText({
    input$test
  })
}

shinyApp(ui, server)

I want to close the popover when I click outside of the popover, similar like we have in shiny modal with easyClose = TRUE.

Share Improve this question edited Mar 24 at 18:57 Jan 9,9256 gold badges20 silver badges33 bronze badges asked Mar 20 at 14:28 user165673user165673 754 bronze badges 1
  • It's not as easy as adding an argument, or at least I am not aware of one. Besides adding some javascript to observe the events (i.e. clicks), I don't know if there's a way to do this. See below. – M-- Commented Mar 20 at 14:59
Add a comment  | 

1 Answer 1

Reset to default 2

Add options = list(trigger = "focus") to popover():

library(shiny)
library(bslib)

ui <- page_sidebar(
  title = "My dashboard",
  sidebar = "Sidebar",
  "Main content area",
  actionButton(
    "btn_pop", 
    "Click here for popover"
  ) |>
    popover(
      "Popover message",
      title = "Popover title",
      options = list(trigger = "focus")
    )
)

shinyApp(ui, function(input, output) {})

If the popover() contains an input (e.g. a selectInput() such as within the edited question), then we may use a JS approach as described in this answer:

library(shiny)
library(bslib)

ui <- page_sidebar(
  tags$head(
    tags$script(
      HTML(
        "$(document).ready(function () {",
        "  $('body').on('click', function (e) {",
        "    $('[data-bs-toggle=popover]').each(function () {",
        "      if (!$(this).is(e.target) &&",
        "          $(this).has(e.target).length === 0 &&",
        "          $('.popover').has(e.target).length === 0) {",
        "        $(this).popover('hide');",
        "      }",
        "    });",
        "  });",
        "})"
      )
    )
  ),
  title = "My dashboard",
  sidebar = "Sidebar",
  "Main content area",
  actionButton(
    "btn",
    "BTN"
  ) |>
    popover(
      "Popover message",
      title = "Popover title",
      selectInput(
        "test",
        "Test",
        letters[1:3],
        "a"
      )
    ),
  textOutput("text")
)

server <- function(input, output, session){
  output$text <- renderText({
    input$test
  })
}

shinyApp(ui, server)
发布评论

评论列表(0)

  1. 暂无评论