Code Monkey home page Code Monkey logo

Comments (3)

jorenbroekema avatar jorenbroekema commented on June 12, 2024

https://amzn.github.io/style-dictionary/#/formats?id=cssvariables you'll probably want to use options.outputReferences set to true

from style-dictionary.

lwnoble avatar lwnoble commented on June 12, 2024

@jorenbroekema I tried following @dbanksdesign guidance for issue #261 to convert this

{
  "data-background": {
    "voilet-50": {
      "type": "group";
      "background-color": {
        "value": "{mode-colors.default.background-color}",
        "type": "color",
        "$extensions": {
          "studio.tokens": {
            "modify": {
              "type": "mix",
              "value": "{mix-in}",
              "space": "lch",
              "color": "#ffffff"
            }
          }
        }
      },
      "alt-background-color": {
        "value": "{mode-colors.default.alt-background-color}",
        "type": "color"
      },
      "background-on-color": {
        "value": "{text-dark}",
        "type": "color",     
      }
    }
  }
}

to the following css:

[data-background=“violet-50”] {
--background: var(—mode-colors.violet-50.background-color);
--background-alt: var(—mode-colors.violet-50.alt-background-color);
--background-on-color: var(—text-dark);
}

I know there is more formatting to do to get my desired results, but I am getting the folllowing error:
"Build error: Cannot read properties of undefined (reading 'reduce')."
See console logs for more info."

when using the following code in the Style-Dictionary Configurator:


import StyleDictionary from 'style-dictionary';
import { formattedVariables } from 'style-dictionary/utils';
import { fileHeader } from 'style-dictionary/utils';
import { registerTransforms } from '@tokens-studio/sd-transforms';

// sd-transforms, 2nd parameter for options can be added
// See docs: https://github.com/tokens-studio/sd-transforms
registerTransforms(StyleDictionary);

const groups = ["font-style", "color-theme", "data-background"];

StyleDictionary.registerTransform({
  name: "attribute/group",
  type: 'attribute',
  matcher: function(prop) {
    return groups.includes(prop.path.slice(0,2).join('-'));
  },
  transformer: function(prop) {
    return {
      // Brittle logic, the group is the 3rd level of the object path
      group: true,
      groupName: prop.path.slice(0,3).join('-')
    }
  }
});

StyleDictionary.registerFilter({
  name: 'core-filter',
  matcher: (token) => token.filePath.endsWith('core.json')
});

StyleDictionary.registerFilter({
  name: 'base-filter',
  matcher: (token) => token.filePath.endsWith('base.json')
});

StyleDictionary.registerFilter({
  name: 'background-filter',
  matcher: (token) => token.filePath.endsWith('base.json')
});

StyleDictionary.registerTransform({
  name: 'css/boolean-to-display',
  type: 'value',
  transformer: (token, _, options) => {
    const tokenType = options.usesDtcg ? token.$type : token.type;
    const tokenValue = options.usesDtcg ? token.$value : token.value;
    if (tokenType === 'boolean') {
      return tokenValue === 'true' ? 'block' : 'hidden';
    }
    return tokenValue;
  }
});

// Custom CSS format that is basically a copy of the built-in,
// except for the fact that we pass formatting options -> commentPosition
StyleDictionary.registerFormat({
  name: 'custom/css/variables',
  formatter: async function ({ dictionary, options = {}, file }) {
    const selector = options.selector ? options.selector : `:root`;
    const { outputReferences, outputReferenceFallbacks, usesDtcg } = options;
    const header = await fileHeader({ file });
    return (
      header +
      `${selector} {\n` +
      formattedVariables({
        format: 'css',
        dictionary,
        outputReferences,
        outputReferenceFallbacks,
        usesDtcg,
        formatting: {
          commentPosition: 'above'
        }
      }) +
      `\n}\n`
    );
  },
});

StyleDictionary.registerFormat({
  name: 'css/group',
  formatter: function(dictionary, config) {
    const groupMap = dictionary.allProperties.reduce(function(prev,curr) {
      if (!prev[curr.attributes.groupName]) {
        prev[curr.attributes.groupName] = [];
      }
      prev[curr.attributes.groupName].push(curr);
      return prev;
    },{});
    
    return Object.keys(groupMap).map(function(key) {
      let props = groupMap[key];
      return `.${key} {
${props.map(function(prop) {
  return `  ${prop.name}: ${prop.value};`
}).join('\n')}
}`
    }).join('\n\n');
  }
});



Any ideas how to get the results I am looking for or how to solve the error?

from style-dictionary.

jorenbroekema avatar jorenbroekema commented on June 12, 2024

The dictionary.allProperties and dictionary.properties were deprecated in v3, and removed in v4 in favor of dictionary.allTokens and dictionary.tokens https://style-dictionary-v4.netlify.app/version-4/migration/#removed-deprecated-features

from style-dictionary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.