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

98.61% Statements 71/72
87.5% Branches 49/56
100% Functions 4/4
98.57% Lines 69/70

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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                                              1952x 1952x 1952x 1952x   1952x                                           1946x       1946x 1946x 1946x 1946x   1946x 1946x     1946x   1946x 1866x   1866x 1864x 1864x 778x   1086x 1086x 1086x           80x 80x 46x         1946x   1946x   521x   521x 521x 521x 373x 373x   148x 148x 148x 148x       1425x   1346x   1346x 1344x 1344x 414x 414x       930x     930x   20x 20x 20x 20x       910x 910x 910x 910x         79x   16x 16x 16x 16x         1946x 26x     1946x                             97x 97x   97x 1935x                 1935x 1935x     97x    
/**
 * 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,
  isDeprecatedToken,
  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,
}) {
  Iif (shouldSkipToken(token)) {
    return { lightColor: null, darkColor: null };
  }
 
  const { tokenValue, originalValue, originalDarkValue, darkValue } = extractColorValues(token);
  const propName = getPropName(token.path);
  const description = getTokenDescription(token);
  const deprecated = isDeprecatedToken(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, token, deprecated };
      } else {
        const resolved = resolveColor(tokenValue, allTokens);
        Eif (resolved) {
          lightColor = { name: propName, color: resolved, description, token, deprecated };
        }
      }
    }
  } else {
    // Not a reference, resolve directly
    const resolved = resolveColor(tokenValue, allTokens);
    if (resolved) {
      lightColor = { name: propName, color: resolved, description, token, deprecated };
    }
  }
 
  // 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, token, deprecated };
        darkColorAdded = true;
      } else {
        const resolved = resolveColor(darkValue, allTokens);
        Eif (resolved) {
          darkColor = { name: propName, color: resolved, description, token, deprecated };
          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, token, deprecated };
        darkColorAdded = true;
      } else {
        // Check if referenced token has a dark mode extension
        const refDarkValue =
          refToken.$extensions?.[MODE_EXTENSION_KEY]?.dark?.$value ||
          refToken.original?.$extensions?.[MODE_EXTENSION_KEY]?.dark?.$value;
 
        if (refDarkValue) {
          // Use the referenced token's dark value
          const resolved = resolveColor(refDarkValue, allTokens);
          Eif (resolved) {
            darkColor = { name: propName, color: resolved, description, token, deprecated };
            darkColorAdded = true;
          }
        } else {
          // Fall back to token's own dark value or light value
          const resolved = resolveColor(darkValue || tokenValue, allTokens);
          Eif (resolved) {
            darkColor = { name: propName, color: resolved, description, token, deprecated };
            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, token, deprecated };
      darkColorAdded = true;
    }
  }
 
  // Apply fallback if enabled and no dark color was added
  if (useDarkFallback && !darkColorAdded && lightColor) {
    darkColor = { name: propName, color: lightColor.color, description, token, deprecated };
  }
 
  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 };
}