lukasoppermann / design-tokens

🎨 Figma plugin to export design tokens to json in an amazon style dictionary compatible format.
https://www.figma.com/community/plugin/888356646278934516/Design-Tokens
MIT License
964 stars 134 forks source link

json output: missing modes in referenced tokens #285

Open rganko opened 1 year ago

rganko commented 1 year ago

Having two modes for primitives collection ("Mode 1", "Mode 2"), I can see only one of defined modes for other collection, that uses primitives as reference.

Current exported json output:


  "primitives": {
    "mode 1": {
      "basic": {
        "01": {
          "type": "color",
          "value": "#fbfcfdff",
          "blendMode": "normal"
        }
      }
    },
    "mode 2": {
      "basic": {
        "01": {
          "type": "color",
          "value": "#ffffffff",
          "blendMode": "normal"
        }
      }
    }
  },
  "tokens": {
    "bg": {
      "default": {
        "type": "color",
        "value": "{primitives.mode 2.basic.01}"
      }
    }
  }
}```

Notive that `tokens.bg.default` reffers to `primitives.basic.01`.
Notice also, that path includes mode name `{primitives.mode 2.basic.01}` (it did not in previous version).

Since there is mode name in the token's value path now, I was expecting two entries for each `primitive's` mode.

In this scenario output json might look like:

```{
  "primitives": {
    "mode 1": {   ...    },
    "mode 2": {   ...    }
  },
  "tokens": {
     "mode 1": {
        "bg": {
          "default": {
             "type": "color",
             "value": "{primitives.mode 1.basic.01}"
           }
        }
     },
     "mode 2": {
        "bg": {
          "default": {
             "type": "color",
             "value": "{primitives.mode 2.basic.01}"
           }
        }
     },
  }
}```

Should I expect mode name in the value's path?

P.S. Is there a plan for dealing with spaces in mode's names, like "mode 1"?
lukasoppermann commented 1 year ago

Hey @rganko happy to accept a PR to deal with spaces. But I think it should be already working the way it does now, or do you experience a bug?

For the modes, the way you suggest is pretty uncommon. What we currently do is also not great. The best option would be to export one file per mode, but I am not sure if this is technically possible.

Mode should not be in the token name at all, as this breaks a lot of the magic of tokens, but for this we need multiple files e.g. dark.json and light.json with the same token (but different values) inside, e.g. tokens.bg (no mode reference.)

katywatkins commented 1 year ago

I'd love to better understand why one particular mode is included in the referenced name rather that removing it entirely. For a user with multiple modes for component tokens, won't there only ever be one mode with correct references? While the rest would always be wrong?

I'm running into something very similar to the problem mentioned above, except my primitive and component token collections both have a matching set of modes and there's still only one mode ever included in the referenced primitive names. I was also was hoping for the proposed output @rganko provided, but removing the mode from the name entirely seems like it would be almost equally helpful. Is the current functionality, where 'mode 2' (whichever Figma mode is listed last) is included in referenced token names regardless of their mode, the intended functionality for the foreseeable future? (Assuming there aren't significant changes made in how Figma does things.)

I'm fine writing some extra code to work around some of this, I was assuming I'd have to be using the JSON structure to make some of this work (top level as collections, second level as modes) - but also having to strip out one particular mode name from all referenced primitives feels awfully bad to me. I'm trying to figure out if my structure is fundamentally incompatible with this plugin and standard use cases, or if we're just in a temporary weird spot because variables are relatively new and things are likely to keep changing.

rganko commented 1 year ago

To be clear we are on the same page here let's compere JSON structure we've got. Based on today's results from figma using Design Tokens plugin.

Note: For single mode every thing works great (thank you for your work).

In case of having another mode (light, dark), json structure seams to consider only last one from all of the modes that are available. See json below - token's path only considers dark mode. Screenshot 2023-11-18 at 21 31 38

  "primitives": {
    "light": {
      "white-01": {
        "type": "color",
        "value": "#ffffffff",
        "blendMode": "normal"
      }
    },
    "dark": {
      "white-01": {
        "type": "color",
        "value": "#000000ff",
        "blendMode": "normal"
      }
    }
  },
  "tokens": {
    "header-bg": {
      "type": "color",
      "value": "{primitives.dark.white-01}"
    }
  }
}

It might look broken since there is no easy way to get primitives.light.white-01. The fact that only last one of used aliases are considered seems to be not that obvious.

And now the questions:

  1. Based on given results, can you please confirm if the result is correct (mainly primitives.dark.white-01) ?
  2. Is this plugin supposed to work only for single modes?
  3. @lukasoppermann you've mentioned creating two files for each mode separately. Is there (or will be) a possibility to choose which mode should be used while exporting?
0m4r commented 1 month ago

@rganko I am not sure if you wrote the code above as an example only, but if it is not I feel like you can solve your problem by restructuring your Figma variable setup. I'd recommend using the global collection without any mode, there you define all of your colors (and more in general, any other basic value for your setup).

After this you can create a different collection, there you can add as many modes as you like and use the variables you defined in the primitives collection to set up the different modes.

This should solve your problem, plus, allow you to use better semantical variable names (in your example, "dark" mode has a variable called "white-01" which is set to the opposite of white!).

so you'll get to have something like:

{
  "primitives": {
    "colors": {
      "white": "#fff",
      "black": "#000"
    }
  },
  "colors": {
    "light": {
      "primary": {
        "type": "color",
        "value": "{primitives.color.light}"
      },
      "secondary": {
        "type": "color",
        "value": "{primitives.color.dark}"
      }
    },
    "dark": {
      "primary": {
        "type": "color",
        "value": "{primitives.color.black}"
      },
      "secondary": {
        "type": "color",
        "value": "{primitives.color.light}"
      }
    }
  },
  "tokens": {
    "light": {
      "header-bg": {
        "type": "color",
        "value": "{primitives.primary}"
      }
    },
    "dark": {
      "header-bg": {
        "type": "color",
        "value": "{primitives.secondary}"
      }
    }
  }
}