简体   繁体   English

材质UI主题和Hyperstack

[英]Material UI Themes and Hyperstack

Material UI (React) uses a theaming solution where the theme object is created in JS then passed into the top level component. Material UI(React)使用一个游戏解决方案,其中主题对象在JS中创建,然后传递到顶级组件。 Creating the theme object in Opal can be complicated as the Material component expects a JS function to be passed in which actually creates the theme on the fly. 在Opal中创建主题对象可能很复杂,因为Material组件需要传递JS函数,实际上会动态创建主题。

Has anyone got an example of this working well? 有没有人得到这个运作良好的例子?

After some experimentation, I got this working well by mixing JS and Opal code so here is the solution in case anyone else comes up with this. 经过一些实验,我通过混合使用JS和Opal代码使这个工作得很好,所以这里有解决方案,以防其他人提出这个问题。 (There may be a better 'more Opal' solution, so if there is please do post an alternative answer, but the one below does work well. (可能有一个更好的'更Opal'解决方案,所以如果有,请发布一个替代答案,但下面的那个确实很好。

First import the JS libraries with webpack: 首先使用webpack导入JS库:

import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import indigo from '@material-ui/core/colors/indigo';
import pink from '@material-ui/core/colors/pink';
import red from '@material-ui/core/colors/red';
global.createMuiTheme = createMuiTheme;
global.MuiThemeProvider = MuiThemeProvider;
global.indigo = indigo;
global.pink = pink;
global.red = red;

Add the following to your Javascript assets: 将以下内容添加到您的Javascript资产:

// app/assets/javascripts/my_theme.js
const my_theme = createMuiTheme(
  { palette: {
    primary: { main: pink[500] },
    secondary: { main: indigo[500] }
  }
});

Then provide the theme in your top-level Component code: 然后在顶级组件代码中提供主题:

class MyRouter < HyperComponent
  include Hyperstack::Router

  render(DIV) do
    MuiThemeProvider(theme: `my_theme` ) do
      CssBaseline do
        ...
      end
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM