【问题标题】:Gatsby and Theme-UI ColorModeProviderGatsby 和 Theme-UI ColorModeProvider
【发布时间】:2020-08-10 14:39:28
【问题描述】:

我正在用 Gatsby 试验各种字体加载策略,最终解决了 gatsby-plugin-theme-ui 的来源问题,因此我可以在客户端加载所有字体后更新 theme.fonts 值(默认情况下,theme.fonts 设置为系统字体)。

完成所有这些后,我安装了ColorModeProvider 并将其添加到ThemeProvider 内部的wrap-root-element.js,但它并没有完全正常工作。只有textbackground 颜色在更新,但primarysecondarymuted 颜色没有变化(使用默认颜色)。我可以确认每次更改主题时 CSS 变量都会更新。

请让我知道我在这里缺少什么?

  • Demo(代码块颜色不应更改,因为它们由另一个提供程序处理)
  • The actual PR

主题提供者:

/** @jsx jsx */
import { jsx, ThemeProvider } from 'theme-ui'
import { useState, useEffect, useCallback, Fragment } from 'react'
import theme from '../gatsby-plugin-theme-ui'
import components from '../gatsby-plugin-theme-ui/components'
import useEventListener from '../hooks/use-event-listener'

const themeUI = { ...theme }
const { fonts: { safe: safeFonts } } = { ...theme }
const safeFontsTheme = {
  ...Object.assign(
    {},
    theme,
    { fonts: safeFonts }
  )
}

const ThemeUIProvider = ({ element }) => {
  const [theme, setTheme] = useState(safeFontsTheme)

  const updateTheme = useCallback(
    () => {
      setTheme(themeUI)
      document.documentElement.classList.remove(
        `font-loading-stage-1`,
        `font-loading-stage-2`
      )
    },
    [setTheme],
  )

  useEventListener(
    typeof window !== 'undefined' && window,
    'FONTS_ARE_LOADED',
    updateTheme
  )

  useEffect(() => {
    updateTheme()
    sessionStorage.getItem('areFontsLoaded')
  }, [updateTheme])

  return (
    <Fragment>
      {jsx(ThemeProvider, {
        theme,
        components,
      },
        element
      )}
    </Fragment>
  )
}

export default ThemeUIProvider

wrap-root-element.js:

/** @jsx jsx */
import { jsx } from 'theme-ui'
import { ColorModeProvider } from '@theme-ui/color-modes'
import PrismThemeProvider from './code-block/prism-theme-provider'
import ThemeUIProvider from './theme-ui-provider'

export const wrapRootElement = ({ element }) => {
  return (
    <PrismThemeProvider>
      <ThemeUIProvider element={element}>
        <ColorModeProvider>
          {element}
        </ColorModeProvider>
      </ThemeUIProvider>
    </PrismThemeProvider>
  )
}

颜色模式按钮:

/** @jsx jsx */
import { jsx, IconButton, useColorMode } from 'theme-ui'

const ColorModeButton = props => {
  const colorModes = [`default`, `dark`, `deep`, `swiss`]
  const [colorMode, setColorMode] = useColorMode()

  return (
    <IconButton
      {...props}
      aria-label='Toggle website theme'
      onClick={() => {
        const index = colorModes.indexOf(colorMode)
        const next = colorModes[(index + 1) % colorModes.length]
        setColorMode(next)
      }}
      sx={{
        cursor: 'pointer',
        padding: 0,
        width: 40,
        height: 40,
        marginX: 1,
      }}
    >
      <svg
        width='24'
        height='24'
        viewBox='0 0 32 32'
        fill='currentcolor'
        sx={{
          display: 'flex',
          margin: '0 auto',
          transition: 'transform 400ms ease',
        }}
      >
        <circle
          cx='16'
          cy='16'
          r='14'
          fill='none'
          stroke='currentcolor'
          strokeWidth='4'
        ></circle>
        <path d='M 16 0 A 16 16 0 0 0 16 32 z'></path>
      </svg>
    </IconButton>
  )
}

export default ColorModeButton

我还在SpectrumGithub issues 上问过这个问题,以防其他人有兴趣查看这些线程。

【问题讨论】:

    标签: reactjs gatsby emotion css-in-js theme-ui


    【解决方案1】:

    这里的问题是原始的gatsby-theme-ui-plugin 没有从package.jsongatsby-config.js 中删除。

    【讨论】:

    • 我遇到了同样的问题,您的回答对我有帮助,但我认为其他人可能会发现一些额外的上下文有用。 gatsby-theme-ui 已重命名为 gatsby-plugin-theme-ui 原始版本仍然存在,因此如果您安装它,那么您的主题将不适用。
    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 2020-05-25
    • 1970-01-01
    • 2020-04-10
    • 2020-07-25
    • 2018-10-26
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多