Skip to content

Update the SizePX format for the “dimension” token when using the DTCG format with a unit specification. #1501

@thierryc

Description

@thierryc

Update the SizePX format for the “dimension” token when using the DTCG format with a unit specification.

{
   "$value": {
       "value": 16,
       "unit": "px"
    }
}

My suggestion is to add a filter to prevent the conversion of the value if the unit is not a PX, allowing another transform to handle the other unit.

Below is a suggested update to the code of the sizePx transform function.

Please let me know your feedback. @jorenbroekema

I use the 5v branch.

https://github.com/amzn/style-dictionary/compare/v5...thierryc:style-dictionary:transform-unit-value-object?expand=1

/**
   * Adds 'px' to the end of the number. Does not scale the number
   *
   * ```js
   * // Matches: token.type === 'dimension'
   * // Returns:
   * "10px"
   * ```
   *
   * @memberof Transforms
   */
  [transforms.sizePx]: {
    type: value,
    filter: (token, options) => {
      const rawValue = options.usesDtcg ? token.$value : token.value;
      if (typeof rawValue === 'object' && rawValue !== null && 'value' in rawValue && 'unit' in rawValue) {
        return rawValue.unit === 'px' && (isDimension(token, options) || isFontSize(token, options));
      }
      return isDimension(token, options) || isFontSize(token, options);
    },
    transform: function (token, _, options) {
      const rawValue = options.usesDtcg ? token.$value : token.value;
      if (typeof rawValue === 'object' && rawValue !== null && 'value' in rawValue && 'unit' in rawValue) {
        const parsedVal = parseFloat(rawValue.value);
        if (isNaN(parsedVal)) throwSizeError(token.name, rawValue.value, rawValue.unit || 'px');
        return parsedVal + (rawValue.unit || 'px');
      } else {
        const parsedVal = parseFloat(rawValue);
        if (isNaN(parsedVal)) throwSizeError(token.name, rawValue, 'px');
        return parsedVal + 'px';
      }
    },
  },

other issue about object and unit DTCG format : #1471

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions