daattali / shinyjs

💡 Easily improve the user experience of your Shiny apps in seconds
https://deanattali.com/shinyjs
Other
736 stars 119 forks source link

Add support for Shiny JS events #168

Open aoles opened 6 years ago

aoles commented 6 years ago

Hi Dean, long time no see ;) I can't say that enough: your package is awesome! The only thing I'm currently missing is the support for Shiny events. Right now I'm implementing king of a busy indicator which can be easily set-up with a custom JS code as shown in the example. Yet, it would be really great if something along the following lines would be possible - what do you think?

Cheers, Andrzej

library(shinyjs)

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    ...
    div(id = "loader")
    ),
  server = function(input, output) {
    ...
    onshiny("busy", show("loader"))
    onshiny("idle", hide("loader"))
  }
)
daattali commented 6 years ago

That sounds like a good idea!

It would be similar to the onevent() function. The first argument would be the event to listen to, and the second argument could either be an expression or a callback function with a single argument. I don't have any time in the foreseeable future to work on this but I would gladly welcome a PR.

daattali commented 6 years ago

I started work on this in the feature-onshiny branch if anyone wants to continue. I don't know if I'll finish implementing this.

ch0c0l8ra1n commented 5 months ago

@daattali Your approach on the feature-onshiny works for all shiny events except shiny:busy, shiny:idle. Any expression called on shiny:idle will inadvertently make shiny busy and trigger shiny:busy. There's an infinite recursion. Likewise any expression that's to be called on shiny:busy will not be evaluated until after shiny is idle.

These should be handled on js itself as that won't make shiny busy and can be executed even when shiny is busy.

image