loadCSS function

void loadCSS(
  1. String cssClassPrefix,
  2. Map<String, CSSValueBase>? css
)

Loads css dynamically.

cssClassPrefix Prefix for each class in css Map. css Map of CSS classes.

Implementation

void loadCSS(String cssClassPrefix, Map<String, CSSValueBase>? css) {
  var loadedThemes = _loadedThemesByPrefix[cssClassPrefix];

  if (loadedThemes == null) {
    _loadedThemesByPrefix[cssClassPrefix] = loadedThemes = {};
  }

  if (loadedThemes[css] != null) return;
  loadedThemes[css] = true;

  var id = '__dom_tools__dynamic_css__$cssClassPrefix';

  var styleElement = StyleElement()..id = id;

  var prev = document.head!.querySelector('#$id');
  if (prev != null) {
    prev.remove();
  }

  document.head!.append(styleElement);

  var sheet = styleElement.sheet as CssStyleSheet?;

  for (var key in css!.keys) {
    var val = css[key]!;
    var rule = '.$cssClassPrefix$key { ${val.cssValue()} }\n';
    sheet!.insertRule(rule, 0);
    print(rule);
  }
}