material-ui jss-rtl - 使用jss-rtl之后我的页面仍然是LTR

时间:2018-04-12 20:29:07

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

我在material-ui项目中使用nextjss-rtl以及react,但是存在问题 使用ltr

后,该页面仍为<JssProvider ...>

-rtl组件代码:

import React from "react";
import { create } from 'jss';
import rtl from 'jss-rtl';
import JssProvider from 'react-jss/lib/JssProvider';
import { createGenerateClassName, jssPreset } from 'material-ui/styles';

// Configure JSS
const jss = create({ plugins: [...jssPreset().plugins, rtl()] });

// Custom Material-UI class name generator.
const generateClassName = createGenerateClassName();

function RTL(props) {
  return (
    <JssProvider jss={jss} generateClassName={generateClassName}>
      {props.children}
    </JssProvider>
  );
}
export default RTL;

-index.js代码:

import React from 'react';
import ReactDOM from 'react-dom';
import {createBrowserHistory} from "history";
import {Router, Route, Switch} from 'react-router-dom';
import {MuiThemeProvider, createMuiTheme} from 'material-ui/styles';
import r8 from "./components/r8";
import RTL from "./components/RTL";
import appRoutes from "./routes/app";
import "./assets/css/App.css";
import "./assets/fonts/fonts.css";

const hist = createBrowserHistory();
const theme = createMuiTheme({
  typography: {
    fontFamily: "PhpPlus"
  },
  pallete: {
    primary: {
      light: r8.color.indigo[300],
      main: r8.color.indigo[500],
      dark: r8.color.indigo[700],
    },
    secondary: {
      light: r8.color.pink.A200,
      main: r8.color.pink.A400,
      dark: r8.color.pink.A700,
    },
    error: {
      light: r8.color.red[300],
      main: r8.color.red[500],
      dark: r8.color.red[700],
    }
  }
});
const Rendering = <RTL>
  <MuiThemeProvider theme={theme}>
    <Router history={hist}>
      <Switch>
        {
          appRoutes.map((route, key) => {
            return <Route {...route} key={key}/>;
          })
        }
      </Switch>
    </Router>
  </MuiThemeProvider>
</RTL>;

ReactDOM.render(Rendering, document.getElementById('R8'));

[注意]控制台中没有错误

我测试过:

const theme = createMuiTheme({ direction: "rtl", ...});

//no change , still ltr

<body dir="rtl">
//page direction is rtl but paddings or margins and floats and other css codes not flipped!

有人知道什么是错的吗? 请注意,此代码在Windows 10的早期版本的项目中工作。当我将操作系统更改为Linux Ubuntu 16.04(LTS)并安装了Nodejs时,它不再起作用了...... (抱歉打字不好英语 - 我是波斯语!:))

2 个答案:

答案 0 :(得分:4)

我按照以下顺序行事:

  1. 创建与您相同的JSS Provider
  2. 创建一个包含JSS Provider的组件
  3. 我认为您需要向packagejson文件添加一些软件包,或者更新reactmaterial-ui

    这是我的:

    // pagckage.json
    ...
    "dependencies": {
        "@material-ui/core": "^1.0.0",
        "@material-ui/icons": "^1.0.0",
        "jss-rtl": "^0.2.3",
        "material-ui": "^1.0.0-beta.47",
        "react": "^16.3.2",
        "react-dom": "^16.3.2",
        "react-jss": "^8.4.0",
        "react-scripts": "^1.1.4",
    }
    ...
    
    
    // the RTL component in separate rtl.jsx file
    import React from 'react';
    import {create} from 'jss';
    import rtl from 'jss-rtl';
    import JssProvider from 'react-jss/lib/JssProvider';
    import {createGenerateClassName, jssPreset} from '@material-ui/core/styles';
    
    const jss = create({plugins: [...jssPreset().plugins, rtl()]});
    
    const generateClassName = createGenerateClassName();
    
    export default props => (
        <JssProvider jss={jss} generateClassName={generateClassName}>
            {props.children}
        </JssProvider>
    );
    
    
    // Sample for your root component
    import React from 'react';
    import {MuiThemeProvider, createMuiTheme} from '@material-ui/core/styles';
    import TextField from '@material-ui/core/TextField';
    import RTL from './app/public/rtl';
    
    const theme = createMuiTheme({
        direction: 'rtl',
    });
    
    const RootComponent = () => (
        <RTL>
            <MuiThemeProvider theme={theme}>
                <div>
                    <TextField label="name"/>
                </div>
            </MuiThemeProvider>
        </RTL>
    );
    
    export default RootComponent;
    

    在这些配置之后,我在项目中获得了rtl个样式。

答案 1 :(得分:0)

谢谢@ ali-ghafoori的回答,它对我有帮助:),

顺便说一句,material-ui docs内部有一个解决方案

import { create } from 'jss';
import { StylesProvider, jssPreset } from '@material-ui/core/styles';
import rtl from 'jss-rtl'

const jss = create({
  plugins: [...jssPreset().plugins, rtl()],
});

export default function App() {
  return (
    <StylesProvider jss={jss}>
      ...
    </StylesProvider>
  );
}