-
Notifications
You must be signed in to change notification settings - Fork 0
/
tippy_utils.R
72 lines (67 loc) · 2.08 KB
/
tippy_utils.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
#' Generate a random class name.
#'
#' This function generates a random class name for the tooltip
#'
#' @return A character string representing the random class name.
#'
make_class <- function() {
glue::glue("tip-{stri_rand_strings(1, 8, '[a-z0-9]')}")
}
#' Create a tippy tooltip element.
#'
#' This function creates a tippy tooltip on an html element
#'
#' @param element The HTML element to be wrapped with the tooltip.
#' @param tooltip The content of the tooltip.
#' @param arrow Boolean for whether to show arrow or not
#' @param placement The placement position for the tippy tooltip. Options:
#' 'auto', 'top', 'bottom', 'left', 'right'. You can also add '-start' or '-end'
#' to each of the above.
#' @return An HTML div element with the tooltip attached.
#'
make_tippy <- function(
element,
tooltip,
arrow = TRUE,
placement = "bottom") {
selector <- make_class()
tippy_script <- glue::glue(
"makeTippyHandler('.{selector}', '{tooltip}', {tolower(arrow)}, '{placement}');"
)
div(
class = selector,
tags$script(tippy_script),
element
)
}
#' Activate the tippy tooltip.
#'
#' This function generates the JavaScript code for activating the tippy tooltip
#'
#' @return A character string representing the JavaScript code for activating the tooltip.
#'
activate_tippy <- function() {
"showWithTooltipHandler();"
}
#' Display an element with a tooltip inside an htmlwidget
#'
#' @param value The value to be wrapped with the tooltip.
#' @param tooltip The content of the tooltip.
#' @param arrow Boolean for whether to show arrow or not
#' @param placement The placement position for the tippy tooltip. Options:
#' 'auto', 'top', 'bottom', 'left', 'right'. You can also add '-start' or '-end'
#' to each of the above.
#' @return An HTML div element with the specified class and value.
#'
show_with_tippy <- function(
value,
tooltip,
arrow = TRUE,
placement = "bottom") {
div(
`data-tippy-text` = tooltip,
`data-tippy-arrow` = tolower(arrow),
`data-tippy-placement` = placement,
value
)
}