你可以从传递给 styled 组件的 CSS 对象中获取 CSS 属性吗?

huangapple go评论71阅读模式
英文:

Is there a way to get css attribute from passed css object inside styled component?

问题

Is there a way to get css attribute from passed css object inside styled component?

Works correctly if I do in this way.

const SquareWithProp = styled.div<{ size: number; }>`
    height: ${props => props.size}px; 
    width: ${props => props.size}px;
`

But I need something like this

// css object does not seem to exist here
const Square = styled.div`
    height: ${props => props.css.width}; 
    width: ${props => props.css.width};
`
const Component = () => {
    return (
        <>
            <SquareWithProp size={100}>test</SquareWithProp>
            <Square css={{ width: 100 }}>test</Square>
        </>
    )
}

Is there a way to retrieve css object inside styled component?

英文:

Is there a way to get css attribute from passed css object inside styled component?

Works correctly if I do in this way.

const SquareWithProp = styled.div&lt;{ size: number; }&gt;`
    height: ${props =&gt; props.size}px; 
    width: ${props =&gt; props.size}px;
`

But I need something like this

// css object does not seem to exist here
const Square = styled.div`
    height: ${props =&gt; props.css.width}; 
    width: ${props =&gt; props.css.width};
`
const Component = () =&gt; {
    return (
        &lt;&gt;
            &lt;SquareWithProp size={100}&gt;test&lt;/SquareWithProp&gt;
            &lt;Square css={{ width: 100 }}&gt;test&lt;/Square&gt;
        &lt;/&gt;
    )
}

Is there a way to retrieve css object inside styled component?

答案1

得分: 1

以下是翻译好的部分:

  • "The css value is certainly passed through to the props, but you're running into problems with the Typescript interface." 翻译为 "css`值确实传递给了props,但您在Typescript接口方面遇到了问题。"
  • "This is not necessarily a trivial problem – using Emotion and styled-components on the same component feels like it's going to cause more problems than it solves! – but it depends on what you're trying to do." 翻译为 "这并不一定是一个微不足道的问题 - 在同一个组件上同时使用Emotion和styled-components似乎会引发更多问题,而不是解决问题!但这取决于您试图做什么。"
  • "If you just want to pass through CSS-style properties in the css prop so that you can do fun things with them in your styled component, then this is easily done and doesn't involve Emotion at all. You just have to declare the css prop on your styled component:" 翻译为 "如果您只想通过css属性传递CSS样式属性,以便在样式化组件中进行有趣的操作,那么这很容易完成,而且根本不涉及Emotion。您只需要在样式化组件上声明css属性:"
  • "Instead of React.CSSProperties you can use the CSSObject type exported by Emotion – this is just familiar css-in-js property-value stuff." 翻译为 "您可以使用Emotion导出的CSSObject类型,而不是React.CSSProperties - 这只是熟悉的CSS-in-JS属性值内容。"
  • "If though you're wanting to pass through the output of Emotion's css function then things are a lot less helpful. css returns a serialized chunk of styles, so you will no longer be able to access the width without parsing it yourself:" 翻译为 "如果您想传递Emotion的css函数的输出,那么情况会变得不太有帮助。css返回一个序列化的样式块,因此您将不再能够在不自行解析的情况下访问宽度:"
  • "Here's a sandbox with examples of the different approaches." 翻译为 "这里是一个示例,展示了不同方法的示例。"
英文:

The css value is certainly passed through to the props, but you're running into problems with the Typescript interface.

This is not necessarily a trivial problem – using Emotion and styled-components on the same component feels like it's going to cause more problems than it solves! – but it depends on what you're trying to do.

If you just want to pass through CSS-style properties in the css prop so that you can do fun things with them in your styled component, then this is easily done and doesn't involve Emotion at all. You just have to declare the css prop on your styled component:

const Square = styled.div&lt;{ css: React.CSSProperties }&gt;`
    // e.g. { props: { css: { width: &quot;200px&quot; } } }
    height: ${props =&gt; props.css.width};
    width: ${props =&gt; props.css.width};
`;

Instead of React.CSSProperties you can use the CSSObject type exported by Emotion – this is just familiar css-in-js property-value stuff.

If though you're wanting to pass through the output of Emotion's css function then things are a lot less helpful. css returns a serialized chunk of styles, so you will no longer be able to access the width without parsing it yourself:

const SquareWithEmotionCss = styled.div&lt;{ css: SerializedStyles }&gt;`
  // e.g. { props: { css: { styles: &quot;width: 200px&quot; } } }
  ${(props) =&gt; props.css.styles};
`;

Here's a sandbox with examples of the different approaches.

huangapple
  • 本文由 发表于 2023年4月10日 20:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定