Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation suggestion: Import JSON objects (not from files) #101

Open
rootwork opened this issue May 21, 2021 · 0 comments
Open

Documentation suggestion: Import JSON objects (not from files) #101

rootwork opened this issue May 21, 2021 · 0 comments

Comments

@rootwork
Copy link

I wasn't entirely sure if I could use JSON objects that already existed in my code without having to save them to a file first. Using the Importing *.js Files documentation I was able to do it, but I thought it might be worth adding documentation that makes that explicit.

I can create a PR for this if you agree. Here's the basic version:


  1. Provide an object of JSON values. You can do this directly like so:
const theme = {
  "colors": {
    "primary": "#900",
    "secondary": "#060"
  }
}

or you can use JSON.stringify to transform any objects you already have into JSON-formatted objects.

  1. If you have nested objects, it appears that you can't access them through Sass's map-get() function. So with the above example data, you couldn't do color: map-get($theme.colors, 'primary'). Instead, you have to create objects of the nested objects, e.g.:
const colors = theme.colors
  1. Then, as in the "Importing *.js Files" example, export your objects:
module.exports = {
  colors
}
  1. Finally, call the JS file you've just created and use the map-get() function:
@import 'theme.js';

a {
  color: map-get($colors, 'primary');
}

My particular use case was providing a way for people to define theme components in a YAML file, then using js-yaml to read them in. So it seemed really silly to then have to export the settings back to a JSON file just for consumption by the JSON importer! Doing it with the objects alone made me much happier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant