【问题标题】:With a Styled Components Theme, how to a specify a design system's colors?使用 Styled Components Theme,如何设计特定的系统颜色?
【发布时间】:2018-04-23 21:58:19
【问题描述】:

在我的样式化组件主题中,我正在尝试执行以下操作:

theme.js

const colors = {    
  purples: {
    60: '#123123',
    50: '#123123',
    40: '#123123',
  },

  blues: {
    60: '#123123',
    50: '#123123',
    40: '#123123',
  },

  // Primary defined here to be easier
  purple: purples[50],
  blue: blues[50]

};

const theme = {
  colors,
};

export default theme;

我收到 blues 未定义错误。我这样做对吗?如何使用主题文件中的样式组件定义我的颜色?

目标是让它发挥作用:

  background: ${(props) => props.theme.colors.purple};
  background: ${(props) => props.theme.colors.purples[40};

【问题讨论】:

    标签: css reactjs styled-components


    【解决方案1】:

    你有:

    const colors = {
      // Primary defined here to be easier
      purple: purples[50],
      blue: blues[50]
      ...
    

    blues 尚未定义。你需要这样的东西:

    const purples = {
      60: '#123123',
      50: '#123123',
      40: '#123123',
    },
    
    const blues = {
      60: '#123123',
      50: '#123123',
      40: '#123123',
    },
    
    const colors = {
      purple: purples[50],
      blue: blues[50],
    };
    
    const theme = {
      colors,
    };
    
    export default theme;
    

    或者,您可以执行上述操作,但单独导出每个内容:

    export const purples = {
      60: '#123123',
      50: '#123123',
      40: '#123123',
    },
    
    export const blues = {
      60: '#123123',
      50: '#123123',
      40: '#123123',
    },
    
    export const theme = {
      purple: purples[50],
      blue: blues[50],
    };
    

    【讨论】:

    • 即使我把它移到最后,我仍然得到那个错误
    • 您需要将其移出对象。
    • 怎么,我只能定义一次颜色
    • 我更新了我的答案。您无需再次定义colors 即可。
    猜你喜欢
    • 2018-12-02
    • 2018-08-17
    • 2020-12-19
    • 2019-11-03
    • 2018-02-09
    • 1970-01-01
    • 2019-01-17
    • 2019-08-23
    • 2018-12-02
    相关资源
    最近更新 更多