All files / packages/design-tokens/src/style-dictionary/utils mobile-color-helpers.js

100% Statements 65/65
90% Branches 45/50
100% Functions 4/4
100% Lines 63/63

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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171                                            1321x 1321x 1321x 1321x   1321x                                           1317x 2x     1315x 1315x 1315x   1315x 1315x     1315x   1315x 1258x   1258x 1256x 1256x 222x   1034x 1034x 1034x           57x 57x 39x         1315x   1315x   513x   513x 513x 513x 85x 85x   428x 428x 428x 428x       802x   746x   746x 744x 744x 146x 146x   598x 598x 598x 598x       56x   12x 12x 12x 12x         1315x 23x     1315x                             95x 95x   95x 1307x                 1307x 1307x     95x    
/**
 * Shared color processing utilities for mobile platforms (Android/iOS)
 *
 * This module contains common logic for processing color tokens with light/dark modes
 * to reduce duplication between Android and iOS format files.
 */
 
import {
  getRawValueUnitValue,
  isTokenReference,
  findTokenByReference,
  shouldSkipToken,
  getTokenDescription,
} from './mobile-helpers.js';
import { MODE_EXTENSION_KEY } from './constants.js';
 
/**
 * Extract color values from a token (light and dark modes)
 * @param {object} token - The token to process
 * @returns {object} - { tokenValue, originalValue, originalDarkValue, darkValue }
 */
export function extractColorValues(token) {
  const tokenValue = getRawValueUnitValue(token);
  const originalValue = token.original?.$value;
  const originalDarkValue = token.original?.$extensions?.[MODE_EXTENSION_KEY]?.dark?.$value;
  const darkValue = token.$extensions?.[MODE_EXTENSION_KEY]?.dark?.$value || originalDarkValue;
 
  return { tokenValue, originalValue, originalDarkValue, darkValue };
}
 
/**
 * Process a single color token for both light and dark modes
 * @param {object} config - Configuration object
 * @param {object} config.token - The token to process
 * @param {Array} config.allTokens - All tokens for reference resolution
 * @param {Function} config.getPropName - Function to get property name from token
 * @param {Function} config.getRefProperty - Function to get reference property string (refToken, isDark) => string | null
 * @param {Function} config.resolveColor - Function to resolve color to final format (colorValue, tokens) => string | null
 * @param {boolean} config.useDarkFallback - Whether to use light value as fallback for dark mode
 * @returns {object} - { lightColor, darkColor } objects with { name, color } or null
 */
export function processColorToken({
  token,
  allTokens,
  getPropName,
  getRefProperty,
  resolveColor,
  useDarkFallback = true,
}) {
  if (shouldSkipToken(token)) {
    return { lightColor: null, darkColor: null };
  }
 
  const { tokenValue, originalValue, originalDarkValue, darkValue } = extractColorValues(token);
  const propName = getPropName(token.path);
  const description = getTokenDescription(token);
 
  let lightColor = null;
  let darkColor = null;
 
  // Process light value
  const lightRef = isTokenReference(originalValue) ? originalValue : null;
 
  if (lightRef) {
    const refToken = findTokenByReference(lightRef, allTokens);
 
    if (refToken) {
      const refProp = getRefProperty(refToken, false); // false for light mode
      if (refProp) {
        lightColor = { name: propName, color: refProp, description };
      } else {
        const resolved = resolveColor(tokenValue, allTokens);
        Eif (resolved) {
          lightColor = { name: propName, color: resolved, description };
        }
      }
    }
  } else {
    // Not a reference, resolve directly
    const resolved = resolveColor(tokenValue, allTokens);
    if (resolved) {
      lightColor = { name: propName, color: resolved, description };
    }
  }
 
  // Process dark value
  let darkColorAdded = false;
 
  if (originalDarkValue && isTokenReference(originalDarkValue)) {
    // Has explicit dark value reference
    const refToken = findTokenByReference(originalDarkValue, allTokens);
 
    Eif (refToken) {
      const refProp = getRefProperty(refToken, true); // true for dark mode
      if (refProp) {
        darkColor = { name: propName, color: refProp, description };
        darkColorAdded = true;
      } else {
        const resolved = resolveColor(darkValue, allTokens);
        Eif (resolved) {
          darkColor = { name: propName, color: resolved, description };
          darkColorAdded = true;
        }
      }
    }
  } else if (lightRef) {
    // No explicit dark value, but light value is a reference
    const refToken = findTokenByReference(lightRef, allTokens);
 
    if (refToken) {
      const refProp = getRefProperty(refToken, true); // true for dark
      if (refProp) {
        darkColor = { name: propName, color: refProp, description };
        darkColorAdded = true;
      } else {
        const resolved = resolveColor(darkValue || tokenValue, allTokens);
        Eif (resolved) {
          darkColor = { name: propName, color: resolved, description };
          darkColorAdded = true;
        }
      }
    }
  } else if (darkValue) {
    // Dark value exists but is not a reference
    const resolved = resolveColor(darkValue, allTokens);
    Eif (resolved) {
      darkColor = { name: propName, color: resolved, description };
      darkColorAdded = true;
    }
  }
 
  // Apply fallback if enabled and no dark color was added
  if (useDarkFallback && !darkColorAdded && lightColor) {
    darkColor = { name: propName, color: lightColor.color, description };
  }
 
  return { lightColor, darkColor };
}
 
/**
 * Process an array of color tokens into light and dark arrays
 * @param {object} config - Configuration object
 * @param {Array} config.tokens - Tokens to process
 * @param {Array} config.allTokens - All tokens for reference resolution
 * @param {Function} config.getPropName - Function to get property name from token
 * @param {Function} config.getRefProperty - Function to get reference property string
 * @param {Function} config.resolveColor - Function to resolve color to final format
 * @param {boolean} config.useDarkFallback - Whether to use light value as fallback for dark mode
 * @returns {object} - { lightColors: [], darkColors: [] }
 */
export function processColorTokens(config) {
  const lightColors = [];
  const darkColors = [];
 
  config.tokens.forEach((token) => {
    const { lightColor, darkColor } = processColorToken({
      token,
      allTokens: config.allTokens,
      getPropName: config.getPropName,
      getRefProperty: config.getRefProperty,
      resolveColor: config.resolveColor,
      useDarkFallback: config.useDarkFallback,
    });
 
    if (lightColor) lightColors.push(lightColor);
    if (darkColor) darkColors.push(darkColor);
  });
 
  return { lightColors, darkColors };
}