rstudio / htmltools

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

Can't create a CSS variable with leading underscore(s) #397

Closed gadenbuie closed 1 year ago

gadenbuie commented 1 year ago

It's not possible to create CSS custom property names with css() that start with --_ or --__ because the underscores are converted into dashes.

library(htmltools)

css(
  "--_private-variable" = "private value",
)
#> [1] "---private-variable:private value"

This convention is used at times to indicate a locally-scoped or private custom CSS property, which is how we'd like to use css() in bslib.

One option would be to update the regex so that underscores that are proceeded by -- are not replaced with dashes.

Another option would be to add a locally-defined helper function called var() that works inside css(). It could help with creating CSS variables or with using them, e.g.

red <- "#FF0000"

css(
  var("_private-variable" = "private value"),
  color = var("text-color", red)
)
#> [1] "--_private-variable:private value;color:var(--text-color, #FF0000)"