Open DivadNojnarg opened 3 years ago
Since the parent (bs_accordion
) wants to query the final representation of it's children bs_according_item
, it seems the only way to do this is to 'resolve' bs_according_item
within bs_accordion
's render hook. In your example, it seems you can just do:
item <- as.tags(items[[i]])
tagQuery(item)$find(".card-header")
However, note that as.tags()
resolves only the render hooks attached to the top-level items[[i]]
(it doesn't resolve any hooks attached to it's children). In order to do that, you'd have to use htmltools:::tagify()
instead of as.tags()
(we've been thinking of possibly tagify()
, and this sort of use case might be reason enough to actually do it)
Thanks @cpsievert it works well. See below a reduced example:
options("theme_version" = "4")
my_wrapper_tag <- function(...) {
wrapper <- tags$div(class = "parent", ...)
items <- list(...)
tagAddRenderHook(wrapper, function(x) {
version <- getOption("theme_version")
if (!is.null(version)) {
if (version == "4") {
# resolve sub items
# items <- lapply(items, as.tags)
new_items <- tagQuery(items)$
find(".new-child")$
each(function(x, i) {
tagAppendAttributes(x, id = i)
})$
allTags()
x <- tagQuery(x)$
# replace accordion items processed above
empty()$
append(new_items)$
allTags()
}
}
x
})
}
my_nested_tag <- function() {
wrapper <- tags$div(
class = "nested",
tags$div(
tags$span()
)
)
tagAddRenderHook(wrapper, function(x) {
version <- getOption("theme_version")
if (!is.null(version)) {
x <- if (version == "4") {
new_child <- tags$div(class = "new-child")
tagQuery(x)$
prepend(new_child)$
allTags()
}
}
x
})
}
my_wrapper_tag(my_nested_tag(), my_nested_tag())
It requires to uncomment items <- lapply(items, as.tags)
to work well, as you suggested.
Will tagify()
be exported in that case?
Hi @schloerke!
I was finishing to review some chapter in the book and I found a weird result with
tagAddRenderHook()
. Typically, I created abs_accordion()
function which acceptsbs_accordion_item()
functions. Each function has a render hook to account for BS4 and BS5 differences. However, at some point, in thebs_accordion()
hook, I got a very strange output. Below is the HTML structure of the tag I want to modify:If I do a tagQuery round on this tag like:
tagQuery(item)$find(".card-header")
, I got nothing. If I dotagQuery(item)$find(".collapse")
, I get one selected result. If I check item$children, I get only:My question is what happens with:
Why is it ignored? Could it be a timing issue with the hooks?
My whole code:
Regarding best practices, I am not sure whether I should use only the hook on the top level or if having it in both functions is not an issue.