-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtags.R
140 lines (121 loc) · 3.5 KB
/
tags.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
#' Create HTML tags
#'
#' Create an HTML tag to place in a Dash app layout. All tags are available
#' in the `html` list, and some common tags have shortcuts as functions for
#' convenience (e.g. `h1()` produces `<h1>` and is equivalent to `html$h1()`).
#'
#' @name tags
#' @param ... Any named arguments become tag attributes, and any unnamed
#' arguments become children. A named argument with a value of `NULL` will
#' be removed, and a named argument with a value of `NA` will be rendered
#' as a boolean argument. See 'Special attributes' below for more information.
#' @param tag_name The name of the HTML tag.
#' @param content List of attributes and children.
#'
#' @section Special attributes:
#' There are a few HTML attributes that are treated in a special way:
#' - To add a `class` attribute, use the `className` parameter
#' - To add a `for` attribute, use the `htmlFor` parameter
#' - The `style` attribute is not provided as a string. Instead, it's provided
#' as a named list, where the name and value of each element correspond to the
#' CSS property and value. Each CSS property should be written in camelCase.
#' - A special property `n_clicks` is automatically added to every HTML tag.
#' This property represents the number of times that this element has been
#' clicked on. If not explicitly initialized to a certain integer, its default
#' value is `NULL` initially.
#'
#' @examples
#' if (interactive()) {
#' app <- dash_app()
#' app %>% set_layout(
#' html$div(
#' h1(
#' "title",
#' style = list(
#' "color" = "red",
#' "backgroundColor" = "blue"
#' )
#' ),
#' "some text",
#' button(
#' "can't click me",
#' disabled = NA,
#' className = "mybtn"
#' )
#' )
#' )
#' app %>% run_app()
#'}
NULL
#' @rdname tags
#' @format NULL
#' @export
html <- lapply(all_tags, function(tag_name) {
rlang::new_function(
args = alist(... = ),
body = rlang::expr({
dash_tag(!!tag_name, list(...))
}),
env = asNamespace("dash")
)
})
#' @rdname tags
#' @export
h1 <- html$h1
#' @rdname tags
#' @export
h2 <- html$h2
#' @rdname tags
#' @export
h3 <- html$h3
#' @rdname tags
#' @export
h4 <- html$h4
#' @rdname tags
#' @export
div <- html$div
#' @rdname tags
#' @export
span <- html$span
#' @rdname tags
#' @export
p <- html$p
#' @rdname tags
#' @export
strong <- html$strong
#' @rdname tags
#' @export
br <- html$br
#' @rdname tags
#' @export
button <- html$button
#' @rdname tags
#' @export
a <- html$a
#' @rdname tags
#' @export
img <- html$img
#' @rdname tags
#' @export
dash_tag <- function(tag_name, content = list()) {
content_names <- rlang::names2(content)
content_named_idx <- nzchar(content_names)
attributes <- remove_empty(content[content_named_idx])
children <- unname(content[!content_named_idx])
# Try to match the exact level of nesting of children as original {dash}
if (length(children) == 0) {
children <- NULL
} else if (length(children) == 1) {
children <- children[[1]]
}
# Support boolean attributes
attributes[is.na(attributes)] <- names(attributes[is.na(attributes)])
attributes[attributes == ""] <- names(attributes[attributes == ""])
tag_params <- attributes
tag_params[["children"]] <- children
dash_html_fx <- paste0("html", toupper(substring(tag_name, 1, 1)), substring(tag_name, 2))
if (tag_name %in% c("map", "object")) {
dash_html_fx <- paste0(dash_html_fx, "El")
}
do.call(dash_html_fx, tag_params)
}