-
Notifications
You must be signed in to change notification settings - Fork 2
/
tippy.R
210 lines (191 loc) · 4.46 KB
/
tippy.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#' Add a Tooltip to an Element
#'
#' Add tooltips to an element.
#'
#' @param element Shiny, or htmltools element, or character string.
#' @param content Content of the tooltip.
#' @param ... Any other options from \href{https://atomiks.github.io/tippyjs/v6/all-props/}{the official documentation}.
#'
#' @examples
#' if(FALSE){
#' tippy("Hover me!", content = "Hi, I'm the tooltip!")
#' tippy(h3("Hello"), content = "World")
#' }
#'
#' @seealso \href{https://atomiks.github.io/tippyjs/}{official documentation}
#'
#' @importFrom htmltools span browsable tagAppendAttributes HTML
#' @importFrom htmltools tagList html_print
#' @importFrom jsonlite toJSON
#'
#' @name tippy
#' @export
tippy <- function(element, content, ...) {
if(missing(content))
stop("Must pass content", call. = FALSE)
if(missing(element))
stop("Must pass element", call. = FALSE)
if(inherits(element, "shiny.tag.list"))
stop("Cannot assign tooltip to `tagList`", call. = FALSE)
if(!inherits(element, "shiny.tag"))
element <- span(HTML(element[1]), id = random_id())
id <- element$attribs$id
if(is.null(id)){
id <- random_id()
element <- tagAppendAttributes(element, id = id)
}
script <- .tippy_this(id, content = content, ...)
el <- tagList(
useTippy(),
element,
script
)
structure(el, class = c("tippy", class(el)))
}
#' @export
print.tippy <- function(x, ...){
html_print(x)
invisible(x)
}
#' Bind Tooltip to a Selector
#'
#' Binds a tooltip to a valid CSS selector.
#'
#' @inheritParams tippy
#' @param selector A CSS selector, e.g.: `#id` or
#' `.class`. If it is a bare selector (e.g.: `sth`)
#' then it is assumed to be an id and processed as
#' `#sth`. Set `.is_tag` to `TRUE` to disable that.
#' @param .is_tag Whether the selector is a tag,
#' e.g.: `<p>`.
#'
#' @examples
#' library(shiny)
#'
#' ui <- fluidPage(
#' tippy(h3("Hello"), "World"),
#' h4("World", id = "theId"),
#' tippyThis("theId", "A tooltip")
#' )
#'
#' server <- function(input, output) {}
#'
#' if(FALSE){
#' shinyApp(ui, server)
#' }
#'
#' @name tippyThis
#' @export
tippyThis <- function(selector, content = NULL, ..., .is_tag = FALSE){
if(missing(selector))
stop("must pass selector", call. = FALSE)
tagList(
useTippy(),
.tippy_this(
selector = selector,
content = content,
...,
.is_tag = .is_tag
)
)
}
#' Methods
#'
#' Call tippy methods on tooltips.
#'
#' @param selectors Selectors of tooltips to
#' apply the method. If `NULL` then the method
#' is applied to _all tooltips._
#' @param .session A valid shiny session.
#' @param .is_tag Whether `selectors` are tags,
#' e.g.: `<p>`.
#'
#' @examples
#' library(shiny)
#' library(tippy)
#'
#' ui <- fluidPage(
#' useTippy(),
#' h5("plot tooltip", class = "tip"),
#' p("Another tooltip", class = "tip"),
#' tippyThis(
#' ".tip",
#' "This is indeed a tooltip"
#' ),
#' actionButton("disable", "Disable"),
#' actionButton("enable", "Enable")
#' )
#'
#' server <- function(input, output) {
#' observeEvent(input$disable, {
#' tippy_disable()
#' });
#'
#' observeEvent(input$enable, {
#' tippy_enable()
#' })
#' }
#'
#' if(FALSE) {
#' shinyApp(ui, server)
#' }
#'
#' @name methods
#'
#' @export
tippy_disable <- function(
selectors = NULL,
.session = shiny::getDefaultReactiveDomain(),
.is_tag = FALSE
){
send_method(selectors, "disable", .session = .session)
}
#' @rdname methods
#' @export
tippy_enable <- function(
selectors = NULL,
.session = shiny::getDefaultReactiveDomain(),
.is_tag = FALSE
) {
send_method(selectors, "enable", .session = .session)
}
#' @rdname methods
#' @export
tippy_destroy <- function(
selectors = NULL,
.session = shiny::getDefaultReactiveDomain(),
.is_tag = FALSE
) {
send_method(selectors, "destroy", .session = .session)
}
#' @rdname methods
#' @export
tippy_show <- function(
selectors = NULL,
.session = shiny::getDefaultReactiveDomain(),
.is_tag = FALSE
) {
send_method(selectors, "show", .session = .session)
}
#' @rdname methods
#' @export
tippy_hide <- function(
selectors = NULL,
.session = shiny::getDefaultReactiveDomain(),
.is_tag = FALSE
) {
send_method(selectors, "hide", .session = .session)
}
# Send data to method
send_method <- function(selectors, method, ..., .session){
.session$sendCustomMessage(
"tippy-method",
list(
method = method,
selectors = as.list(
make_selectors(selectors)
),
opts = list(...)
)
)
}