【问题标题】:Create common style classes for JSS in material-ui在material-ui中为JSS创建通用样式类
【发布时间】:2019-02-14 04:44:17
【问题描述】:

我正在使用material-ui 的 JSS 实现来设置类的样式。

当涉及到组件的styles 时,我有很多重复的代码,因为我已经分离了我的组件。

例如,我的卡片都使用通用样式:

const styles = theme => ({
  cardContainer: {
    position: 'relative',
    width: '50%',
    padding: theme.spacing.unit / 2,
  },
  cardOuter: {
    height: '100%',
    width: '100%',
    textAlign: 'start',
  },
  card: {
    width: '100%',
    background: theme.palette.backgrounds.card.off,
  },
  cardOn: {
    background: theme.palette.backgrounds.card.on,
  },
  cardUnavailable: {
    background: theme.palette.backgrounds.card.disabled,
  },
  cardContent: {
    display: 'flex',
    flexWrap: 'wrap',
    minHeight: 98,
    height: 98,
    [theme.breakpoints.down('sm')]: {
      minHeight: 74,
      height: 74,
    },
    padding: `${theme.spacing.unit * 1.5}px !important`,
  },
});

我很少想扩展组件内的样式,但想将这些对象导入现有的styles 函数,因此我不必复制这些对象。

有人知道怎么做吗?

谢谢

【问题讨论】:

    标签: javascript reactjs material-design material-ui jss


    【解决方案1】:

    想通了。对于未来的观众:

    const styles = theme => ({
      ...card(theme),
      grid: {
        height: '100%',
        width: 'fit-content',
        paddingLeft: theme.spacing.unit * 2,
        paddingRight: theme.spacing.unit * 2,
        flexWrap: 'nowrap',
        overflowY: 'hidden',
      },
      name: {
        overflow: 'hidden',
        textOverflow: 'ellipsis',
        fontSize: '1.12rem',
        fontColor: theme.palette.text.main,
        [theme.breakpoints.down('sm')]: {
          fontSize: '0.9rem',
        }
      },
      state: {
        textOverflow: 'ellipsis',
        margin: '0 auto',
        marginTop: theme.spacing.unit / 2,
        fontSize: '1.0rem',
        fontColor: theme.palette.text.light,
        [theme.breakpoints.down('sm')]: {
          fontSize: '0.8rem',
        }
      },
      alarmArmedHome: {
        background: theme.palette.backgrounds.card.alarm.home,
      },
      alarmArmedAway: {
        background: theme.palette.backgrounds.card.alarm.away,
      },
      alarmTriggered: {
        background: theme.palette.backgrounds.card.alarm.triggered,
      },
      icon: {
        margin: '0 auto',
        color: theme.palette.text.icon,
        fontSize: '2.7rem',
        [theme.breakpoints.down('sm')]: {
          fontSize: '1.7rem',
        }
      },
    });
    

    card.js

    const styles = (theme) => ({
      cardContainer: {
        position: 'relative',
        width: '50%',
        padding: theme.spacing.unit / 2,
      },
      cardOuter: {
        height: '100%',
        width: '100%',
        textAlign: 'start',
      },
      card: {
        width: '100%',
        background: theme.palette.backgrounds.card.off,
      },
      cardOn: {
        background: theme.palette.backgrounds.card.on,
      },
      cardUnavailable: {
        background: theme.palette.backgrounds.card.disabled,
      },
      cardContent: {
        display: 'flex',
        flexWrap: 'wrap',
        minHeight: 98,
        height: 98,
        [theme.breakpoints.down('sm')]: {
          minHeight: 74,
          height: 74,
        },
        padding: `${theme.spacing.unit * 1.5}px !important`,
      },
    });
    
    export default styles;
    

    因此,如果需要,您几乎必须加入传递主题的对象:

    ...card(theme),
    

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 1970-01-01
      • 2021-10-10
      • 2019-04-29
      • 2021-05-09
      • 2019-04-23
      • 2019-06-01
      • 2021-05-13
      • 2018-11-05
      相关资源
      最近更新 更多