Skip to content

Commit

Permalink
Fix codegen for non-grouped typography tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzasse committed Jul 22, 2022
1 parent f550548 commit 7321166
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export default class Codegen {
const tokenName = this.formatTokenName(k, config.tokenCase);
let tokenValue = v;

// Format token value object keys for Figma variable groups
// Eg. `typography.native/web` or `colors.light/dark`
if (
typeof v === "object" &&
(name === "colors" || name === "typography")
(name === "colors" || (name === "typography" && !("fontSize" in v)))
) {
// Format token value object keys for Figma variable groups
// Eg. `typography.native/web` or `colors.light/dark`
tokenValue = Object.entries(v).reduce((acc, entry) => {
const _tokenName = this.formatTokenName(
entry[0],
Expand Down
12 changes: 5 additions & 7 deletions src/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from "fs";
import axios from "axios";
import log from "./log";
import { optimizeSvg } from "./svgo";
import { rgbToHex, roundToDecimal } from "./utils";
import { rgbToHex, roundToDecimal, toFixed } from "./utils";

export default class Tokenizer {
constructor({ config, figmaAPI, onlyNew }) {
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class Tokenizer {
let color = "";

if (typeof fill.opacity === "number") {
const alpha = parseFloat(fill.opacity.toFixed(2));
const alpha = toFixed(fill.opacity, 2);
color = `rgba(${Math.round(r * 255)}, ${Math.round(g * 255)}, ${Math.round(b * 255)}, ${alpha})`; // prettier-ignore
} else {
color = rgbToHex(
Expand Down Expand Up @@ -127,10 +127,8 @@ export default class Tokenizer {
fontWeight: textStyle.fontWeight,
fontSize: textStyle.fontSize,
textTransform: textStyle.textCase === "UPPER" ? "uppercase" : "none",
letterSpacing: textStyle.letterSpacing,
lineHeight: parseFloat(
(textStyle.lineHeightPx / textStyle.fontSize).toFixed(3)
),
letterSpacing: toFixed(textStyle.letterSpacing, 2),
lineHeight: toFixed(textStyle.lineHeightPx / textStyle.fontSize, 3),
};

if (style.group) {
Expand All @@ -150,7 +148,7 @@ export default class Tokenizer {
// SHADOWS -------------------------------------------------------------
function getShadow(shadow) {
const { r, g, b, a } = shadow.color;
const alpha = parseFloat(a.toFixed(2));
const alpha = toFixed(a, 2);
const rgba = `rgba(${Math.round(r * 255)}, ${Math.round(g * 255)}, ${Math.round(b * 255)}, ${alpha})`; // prettier-ignore
const hex = rgbToHex(
Math.round(r * 255),
Expand Down
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const rgbToHex = (r, g, b) => {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};

export const roundToDecimal = (n) => Math.abs(Math.round(n * 10) / 10);
export const roundToDecimal = (num) => Math.abs(Math.round(num * 10) / 10);

export const toFixed = (num, dec) => parseFloat(num.toFixed(dec));

export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

0 comments on commit 7321166

Please sign in to comment.