javascript - 有没有一种方法可以在没有 "styled"的情况下以情感为主题

标签 javascript css reactjs emotion

有了 emotion ( https://github.com/emotion-js/emotion ),我知道我可以使用 cssstyled 进行主题化,例如:

const carouselCss = ({ theme }) => css`
 .. css properties etc
`;

const CarouselDiv = styled('div')`
  ${carouselCss};
`;

然后在 JSX 中:

<ThemeProvider theme={{ stuff: 'blah' }}>
  <CarouselDiv>
        ..
  </CarouselDiv>
</ThemeProvider>

但是有没有什么优雅的方法可以仅使用 css 进行主题化 - 不喜欢使用组件化 styles 因为我想保留 JSX 中的语义 html 元素(也有一个庞大的代码库,更容易从 sass 迁移到情感,而无需使用 styled)

我知道我可以做这样的事情:

const carouselCss = (theme) => css`
 .. css properties etc
`;

然后是 jsx:

<div className={carouselCss(this.props.theme)}>
..
</div>

但这意味着始终从组件传递主题 Prop - 这有点麻烦

有更好的方法吗?以某种方式用一些东西包裹 css 以便它注入(inject)主题变量?

最佳答案

ThemeProvider 将为您提供。这是一个同时显示 styledcss 选项的示例:

/** @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>
)

关于javascript - 有没有一种方法可以在没有 "styled"的情况下以情感为主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52149199/

相关文章:

reactjs - 是的:如何仅在字段存在时验证字段?

reactjs - GatsbyJS 网站非页面组件中的 GraphQL 查询投诉

javascript - document.write ('<div id="容器"') 在 IE7 中出现问题,它会自动关闭

javascript - Google map 标记为 SVG(内部带有图像 png 标签)

javascript - 将图像缩小到鼠标点击位置的坐标? (jQuery)

css - 如何在dataTable中的每一行之间添加空格

ReactJS Datepicker 多个日期选择

javascript - Google 应用程序脚本 - for 循环损坏

javascript - 在一种条件下定位多个表单元素

html - 为什么我在设计 View 中看不到图像,但在浏览器中可以看到?