rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
216 stars 70 forks source link

Use unnamed attributes with `tag()` #432

Closed JosiahParry closed 7 months ago

JosiahParry commented 7 months ago

<calcite-notice id="initial-note" open icon="information">

I am trying to wrap some custom components using the tag() function. However, I'm running into a situation where I need to be able to pass in a value to the tag without it being a key in a keyvalue pair. Is there any way to accomplish this with the existing infrastructure?

gadenbuie commented 7 months ago
tag(
  "calcite-notice",
  id = "initial-note",
  open = NA,
  icon = "information"
)

I've looked at this before and it's in our docs in a reasonable place but is still hard to find.

JosiahParry commented 7 months ago

Oh nice! Thank you! The below works

htmltools::tag(
  "calcite-notice",
  list(
    id = "initial-note",
    open = NA,
    icon = "information"
  )
)
wch commented 7 months ago

One more thing that might be useful to know: in HTML, an attribute with no value is semantically equivalent to one with the value "", like the two below:

<div open>
<div open="">

https://html-validate.org/rules/attribute-empty-style.html https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes

JosiahParry commented 7 months ago

@wch i did not know that! Thank you