10

我有一个使用 Styled Components 创建的导航栏组件。我想创建一些改变背景颜色和/或文本颜色的道具。

例如: <Navbar dark>应该有以下 CSS:

background: #454545;
color: #fafafa;

<Navbar light>应该相反:

background: #fafafa;
color: #454545;

但是,如果两个道具都没有使用,那么我想要一个默认的背景和文本颜色——比如(出于演示目的),如下所示:

background: #eee;
color: #333;

现在,我的问题是如何在 Styled Components 中进行设置。

我可以执行以下操作:

background: ${props => props.dark ? #454545 : '#eee'}
background: ${props => props.dark ? #fafafa : '#eee'}
background:  #eee;

和颜色类似的东西。

但这是多余的,不是很优雅。我想要某种 if/else 语句:

background: ${ props => { 
  if (props.dark) { #454545 }
  elseif (props.light) { #fafafa }
  else { #eee }
}

但我不知道如何在 Styled Components 中设置类似的东西。

有什么建议么?

提前致谢。

4

7 回答 7

49

保持传入的prop名称相同。然后你可以使用一个switch/case语句。例如,传入一个colorprop 并将其用作 atype以匹配 a case

工作示例

编辑简单样式的组件


例如:

<Button color="primary">Example</Button>

组件/按钮

import styled from "styled-components";

const handleColorType = color => {
  switch (color) {
    case "primary":
      return "#03a9f3";
    case "danger":
      return "#f56342";
    default:
      return "#fff";
  }
};

const Button = styled.button`
  display: block;
  cursor: pointer;
  border: 0;
  margin: 5px 0;
  background: #000;
  font-size: 20px;
  color: ${({ color }) => handleColorType(color)};

  &:focus {
    outline: 0;
  }
`;

export default Button;

如果您有多个属性(例如 acolor和一background对),则使用与上述相同的概念,更改handleColorType以返回string带有属性的 a 并调用带样式属性的handleColorType函数。

例如:

<MultiButton color="primary">Example</MultiButton>

组件/MultiButton

import styled from "styled-components";

const handleColorType = color => {
  switch (color) {
    case "primary":
      return "color: #03a9f3; background: #000;";
    case "danger":
      return "color: #fff; background: #f56342;";
    default:
      return "color: #000; background: #eee;";
  }
};

const MultiButton = styled.button`
  display: block;
  margin: 5px 0;
  cursor: pointer;
  border: 0;
  font-size: 20px;
  ${({ color }) => handleColorType(color)};

  &:focus {
    outline: 0;
  }
`;

export default MultiButton;
于 2019-05-08T21:45:44.727 回答
16

这是我最终使用的解决方案:

export const Navbar = styled.nav`
  width: 100%;

  ...  // rest of the regular CSS code

  ${props => {
    if (props.dark) {
      return `
        background: ${colors.dark};
        color: ${colors.light};
    `
    } else if (props.light) {
      return `
        background: ${colors.light};
        color: ${colors.dark};
    `
    } else {
      return `
        background: ${colors.light};
        color: ${colors.dark};
    `
    }
  }}
`
于 2019-05-12T09:53:06.440 回答
1

更优雅(我猜)和现代的东西是解构道具和使用 switch 语句的组合,例如:

const Button = styled.button`
  ${({primary, secondary}) => {
      switch(true) {
        case primary:
          return `background-color : green`
        case secondary:
          return `background-color : red`
      }
    }
  }
`
于 2020-04-07T01:34:56.203 回答
1

样式化的组件还接受一个功能,您可以在其中读取道具。此外,如果您选择传递一个主题道具,您还可以为您的主题定义一个对象。


const themes = {
  dark: css `
     background: ${colors.dark};
     color: ${colors.light};
  `,
  light: css`
     background: ${colors.light};
     color: ${colors.dark};
  `
}
export const Navbar = styled.nav(({theme})=>`
  width: 100%;
  background: ${colors.light};
  color: ${colors.dark};
  ... // rest of the css

  ${theme?themes[theme]:''}

  `)

<Navbar theme="dark" />
于 2020-05-02T14:29:33.353 回答
0

使用Styled-tools 的解决方案

在您的具体情况下

由于您只有两个主题,一个是默认主题,一个是lightdark可以切换到的主题,您只需要检查它是否为暗模式。

import {ifProp} from "styled-tools";

export const Navbar = styled.nav`
  ${ifProp("dark",
    css`
      background: ${colors.dark};
      color: ${colors.light};
    `,
    css`
      background: ${colors.light};
      color: ${colors.dark};
    `,

  )}
`;

并用<Navbar $dark={isDarkMode} />

更通用的解决方案

当然,您仍然希望使用暗/亮等主题方法。设置变体会使事情变得更容易。例如,您可以在单独的枚举中跟踪您想要的不同主题。像这样:

enum Themes {
    DARK = "dark",
    LIGHT = "light",
}

稍后在您的样式中,您可以指定:

import {switchProp} from "styled-tools";

export const Navbar = styled.nav`
  ${switchProp({
    dark: css`
      background: ${colors.dark};
      color: ${colors.light};
    `,
    light: css`
      background: ${colors.light};
      color: ${colors.dark};
    `,

  })}
`;

然后渲染<Navbar variant={Theme.LIGHT} />或者<Navbar variant={Theme.DARK} />

参考

于 2021-12-09T12:40:33.943 回答
0

你也可以这样做:


 const colorType= {
   dark: '#454545',
   light: '#0a0a0a',
   normal: '#dedede'
};



export const Navbar= styled.nav`
   background: ${({color}) => colorType[color] || `${color}`};

`;

你在这里:

<Navbar color="primary" />
<Navbar color="#FFFFFF" />

于 2020-02-28T18:06:09.073 回答
0

关于什么:

const StyledButton = styled.button`
    padding: 8px 16px;
    border-radius: ${props => props.rounded ? '10px' : '0px'};
    ${props => {
        switch (props.type) {
            case 'primary':
                return `
                    background-color : #000;
                    color: #fff;
                    border: 1px solid #000000;
                `;
            case 'secondary':
                return `
                    background-color : #DEDEDE;
                    border: 1px solid #999;
                `;
        }
    }
}`;
于 2022-02-24T02:27:06.220 回答