All files / utils/src/styles index.ts

100% Statements 15/15
90% Branches 9/10
100% Functions 3/3
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    1x       18x 18x 18x     18x     1x       7x 7x 7x   7x 90x 10x   10x 10x     90x      
import { isDefined } from '../guards';
 
export const getCssVar = (
  name: string,
  element?: Element,
): string | undefined => {
  const targetElement = element ?? document.documentElement;
  const varName = `--${name.replace(/^--/, '')}`;
  const value = getComputedStyle(targetElement)
    .getPropertyValue(varName)
    .trim();
  return value || undefined;
};
 
export const getAllCssVars = (
  prefix: string,
  element?: Element,
): Record<string, string> => {
  const targetElement = element ?? document.documentElement;
  const styles = getComputedStyle(targetElement);
  const searchPrefix = `--${prefix.replace(/^--/, '')}`;
 
  return Array.from(styles).reduce<Record<string, string>>((acc, property) => {
    if (property.startsWith(searchPrefix)) {
      const value = getCssVar(property, element);
 
      Eif (isDefined(value)) {
        acc[property] = value;
      }
    }
    return acc;
  }, {});
};