英文:
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<{ 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?
答案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 thecss
prop on your styled component:" 翻译为 "如果您只想通过css
属性传递CSS样式属性,以便在样式化组件中进行有趣的操作,那么这很容易完成,而且根本不涉及Emotion。您只需要在样式化组件上声明css
属性:" - "Instead of
React.CSSProperties
you can use theCSSObject
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<{ css: React.CSSProperties }>`
// e.g. { props: { css: { width: "200px" } } }
height: ${props => props.css.width};
width: ${props => 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<{ css: SerializedStyles }>`
// e.g. { props: { css: { styles: "width: 200px" } } }
${(props) => props.css.styles};
`;
Here's a sandbox with examples of the different approaches.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论