TextInput上的RTL可在其他所有渲染器上运行

时间:2018-11-06 15:23:52

标签: javascript reactjs material-ui right-to-left

我对RTL有一个奇怪的问题,即似乎TextInput之外的所有内容都被正确翻转了-大约50%的时间都起作用。这是显示我在英语和希伯来语之间切换语言时出现的问题的gif:

(点击查看大图)

这是我包裹着树的主题组件。

import React, {useContext} from 'react'
import {create} from 'jss'
import jssRtl from 'jss-rtl'
import JssProvider from 'react-jss/lib/JssProvider'
import {
  createGenerateClassName,
  jssPreset,
  MuiThemeProvider,
  createMuiTheme
} from '@material-ui/core/styles'
import CssBaseline from '@material-ui/core/CssBaseline'
import {UserContext} from 'src/context/user.js'

const generateClassName = createGenerateClassName()
const plugins = [...jssPreset().plugins]
const dir = locale => {
  const base = locale.split('-') || []
  return ['he', 'ar'].includes(base[0]) ? 'rtl' : 'ltr'
}

const setDir = dir => {
  window.document &&
    window.document.getElementsByTagName('body')[0].setAttribute('dir', dir)
}

export default function Theme({children}) {
  const {locale} = useContext(UserContext)
  const direction = dir(locale)
  const theme = createMuiTheme({direction, useNextVariants: true})
  if (direction === 'rtl') plugins.push(jssRtl())
  setDir(direction)
  console.log('theme', {locale, direction})

  return (
    <MuiThemeProvider theme={theme}>
      <CssBaseline />
      {direction === 'rtl' ? (
        <JssProvider
          jss={create({plugins})}
          generateClassName={generateClassName}
        >
          {children}
        </JssProvider>
      ) : (
        children
      )}
    </MuiThemeProvider>
  )
}

1 个答案:

答案 0 :(得分:1)

疯狂的猜测:尝试一次添加jssRtl pugin。您正在jssRtl组件的每个渲染上添加Theme插件。这可能解释了它不可能在50%的时间内工作,因为每个偶数渲染都意味着翻转两次,这相当于不翻转。