Emotion CSS-in-JS - 如何基于组件道具添加条件CSS?

Zan*_*der 5 javascript css emotion

我想要一个用Emotion设计的组件,它采用最终控制样式的道具.例如,考虑GridCol具有各种道具的组件,这些道具可以更改填充和宽度(宽度可以在不同的视口宽度上更改).

我想使用这样的API:

<GridCol gutter size="2>

// or alternatively, like this:

<GridCol gutter size={{
  m: 2,
  l: 4
}}>
Run Code Online (Sandbox Code Playgroud)

这里发生了三件事:

  • gutter是一个布尔道具,可以为列添加一些水平填充
  • 所述size支柱可以是字符串或一个对象.如果它是一个字符串,我们只是添加几行CSS并且我们很好,但是,如果它是一个对象,我们需要根据在别处设置的断点对象插入一些媒体查询.

Emotion的文档不清楚如何处理这种性质的样式,至少我还没有看到它,所以我希望找到一个共同的解决方案.

对于gutter道具,它是微不足道的:

const GridCol = props => styled('div')`
  display: block;
  box-sizing: border-box;
  flex: 1 0 0;
  min-width: 0;
  padding: ${props.gutter ? `0 10px` : '0'};
`
Run Code Online (Sandbox Code Playgroud)

对于size道具,它变得更复杂,我希望得到的CSS看起来像这样:

const GridCol = props => styled('div')`
  display: block;
  box-sizing: border-box;
  flex: 1 0 0;
  min-width: 0;
  padding: ${props.gutter ? `0 10px` : '0'};

  /* styles here if  `size` is a string */
  width: 10%;

  /* styles here if  `size` is an object */
  @media screen and (min-width: 500px) {
      width: 20%;
  }


  @media screen and (min-width: 800px) {
      width: 30%;
  }


  @media screen and (min-width: 1100px) {
      width: 40%;
  }
`
Run Code Online (Sandbox Code Playgroud)

这些width值将由prop的键确定,该键对应于breakpoints对象中的值,这部分并不简单,但我不知道如何动态生成所需的css.

我确信我可以添加更多信息,我已经做了一些尝试,但目前还没有任何工作.我的感觉是我应该创建一个无状态功能组件,为每个条件生成css,然后在最后加入CSS.

tkh*_*h44 16

这是一个很好的问题.首先,避免这种模式.

const GridCol = props => styled('div')`
  display: block;
  box-sizing: border-box;
  flex: 1 0 0;
  min-width: 0;
  padding: ${props.gutter ? `0 10px` : '0'};
`
Run Code Online (Sandbox Code Playgroud)

在此示例中,在每个渲染上创建一个新的样式组件,这对于性能很糟糕.

任何表达式或插值都可以是一个函数.此函数将接收2个参数:propscontext

const GridCol = styled('div')`
  display: block;
  box-sizing: border-box;
  flex: 1 0 0;
  min-width: 0;
  padding: ${props => props.gutter ? `0 10px` : '0'};
`
Run Code Online (Sandbox Code Playgroud)

至于size你的例子中的道具,我会使用以下模式.

import { css } from 'emotion'

const sizePartial = (props) => typeof props.size === 'string' ?
  css`width: 10%;` :
  css`
   @media screen and (min-width: 500px) {
      width: 20%;
   }


   @media screen and (min-width: 800px) {
      width: 30%;
   }


   @media screen and (min-width: 1100px) {
      width: 40%;
   }
 `
Run Code Online (Sandbox Code Playgroud)

然后,您可以像表达式中出现的任何其他函数一样使用partial.

const GridCol = styled('div')`
  display: block;
  box-sizing: border-box;
  flex: 1 0 0;
  min-width: 0;
  padding: ${props => props.gutter ? `0 10px` : '0'};
  ${sizePartial};
`
Run Code Online (Sandbox Code Playgroud)

这是一个非常强大的模式,可用于组合项目中可重用的动态样式.

如果您对利用此模式的其他库感兴趣,请查看 https://github.com/emotion-js/facepainthttps://github.com/jxnblk/styled-system

  • 关于在每个渲染上创建新样式的组件,是的,它仍然适用。正如您不想在另一个组件的渲染函数中创建任何其他组件一样,您也不希望在每个渲染上创建样式化组件。 (2认同)

Sea*_*ean 5

如果您正在寻找一种使用 Emotion 的css道具实现条件样式的方法,您可以执行以下操作:

import { css } from '@emotion/core';

const styles = ({ isSelected }) => css`
  border: solid 1px black;
  border-radius: 10px;
  padding: 16px;
  cursor: pointer;
  ${isSelected === true &&
  `
    background-color: #413F42;
    color: white;
  `}
`;

const ConditionalComponent = () => {
  const [isSelected, setIsSelected] = useState(false);
  return (
    <div css={styles({ isSelected })} onClick={() => setIsSelected(!isSelected)}>
      Click here to change styles.
    </div>
  );
};
Run Code Online (Sandbox Code Playgroud)

此处有更多详细信息:https : //seanconnolly.dev/emotion-conditionals