[英]Is there a way to theme with emotion without “styled”
With emotion
( https://github.com/emotion-js/emotion ) I know I can theme with css
and styled
together with something like: 有了emotion
( https://github.com/emotion-js/emotion ),我知道我可以使用css
主题,并使用类似以下内容的styled
:
const carouselCss = ({ theme }) => css`
.. css properties etc
`;
const CarouselDiv = styled('div')`
${carouselCss};
`;
Then in JSX: 然后在JSX中:
<ThemeProvider theme={{ stuff: 'blah' }}>
<CarouselDiv>
..
</CarouselDiv>
</ThemeProvider>
But is there any elegant way to theme with only css
- prefer not to use componentized styles
because I want to keep to semantic html elements in JSX (also have a massive code base and its easier to migrate from sass to emotion without having to use styled
) 但是,只有css
才能以任何优雅的方式来主题化主题-不希望使用组件化styles
因为我想保留JSX中的语义html元素(也具有庞大的代码库,并且无需使用styled
即可轻松地将其从Sass迁移到情感。 )
I know I can do something like: 我知道我可以做类似的事情:
const carouselCss = (theme) => css`
.. css properties etc
`;
then jsx: 然后是jsx:
<div className={carouselCss(this.props.theme)}>
..
</div>
But it means passing a theme prop all the time from the component - which is a little cumbersome 但这意味着始终从组件中传递主题道具-有点麻烦
Is there a better way to do this ? 有一个更好的方法吗 ? Somehow wrap css
with something so it has theme vars injected ? 以某种方式用某些东西包装css
,以便注入主题var?
You could use theme HoC . 您可以使用主题HoC 。 But if your concern is prop passing to slider, this HoC is basically doing the same, ie injects theme
into this.props.theme
. 但是,如果您关心的是道具传递到滑块,则此HoC基本上是在做同样的事情, this.props.theme
theme
注入this.props.theme
。 Personally, i would use ThemeProvider API if possible, maybe with function-style theme, as pointed out in the docs: 就个人而言,我将在可能的情况下使用ThemeProvider API ,如文档中指出的那样,可能使用函数样式的主题:
// function-style theme; note that if multiple <ThemeProvider> are used,
// the parent theme will be passed as a function argument
const adjustedTheme = ancestorTheme => ({ ...ancestorTheme, color: 'blue' });
ThemeProvider
will get you that. ThemeProvider
将为您提供。 Here is an example with both styled
and css
options shown : 这是显示styled
和css
选项的示例:
/** @jsx jsx */
import { jsx } from '@emotion/core'
import styled from '@emotion/styled'
import { ThemeProvider } from 'emotion-theming'
const theme = {
colors: {
primary: 'hotpink'
}
}
const SomeText = styled.div`
color: ${props => props.theme.colors.primary};
`
render(
<ThemeProvider theme={theme}>
<SomeText>some text</SomeText>
<div css={theme => ({ color: theme.colors.primary })}>
some other text
</div>
</ThemeProvider>
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.