Hướng dẫn css-in js material ui
You can leverage our styling solution, even if you are not using our components. Show
Material-UI aims to provide strong foundations for building dynamic UIs. For the sake of simplicity, we expose our styling solution to users. You can use it, but you don't have to. This styling solution is interoperable with all the other major solutions. Material-UI's styling solutionIn previous versions, Material-UI has used LESS, then a custom inline-style solution to write the style of the components, but these approaches have proven to be limited. Most recently, we have moved toward a CSS-in-JS solution. It unlocks many great features (theme nesting, dynamic styles, self-support, etc.). We think that it's the future:
So, you may have noticed in the demos what CSS-in-JS looks like. We use the higher-order component created by
JSSMaterial-UI's styling solution uses JSS at its core. It's a high performance JS to CSS compiler which works at runtime and server-side. It is about 8 kB (minified and gzipped) and is extensible via a plugins API. If you end up using this styling solution in your codebase, you're going to need to learn the API. The best place to start is by looking at the features that each plugin provides. Material-UI uses few of
them. You can always add new plugins if needed with the If you wish to build your own instance of Sheets registryWhen rendering on the server, you will need to get all rendered styles as a CSS string. The Sheets managerThe sheets manager uses a reference counting algorithm in order to attach and detach the style sheets only once per (styles, theme) couple. This technique provides an important performance boost when re-rendering instances of a component. When only rendering on the client, that's not something you need to be aware of. However, when rendering on the server you do. You can read more about Server Rendering. Class namesYou may have noticed that the class names generated by our styling solution are non-deterministic, so you can't rely on them to stay the same. The following CSS won't work:
Instead, you have to use the
If you don't like this default behavior, you can change it. JSS relies on the concept of class name generator. Global CSSWe provide a custom implementation of the class name generator for Material-UI needs:
⚠️ Be cautious when using
⚠️ When using
CSS injection orderThe CSS injected by Material-UI to style a component has the highest specificity possible as
the You might, however, also want to override these styles, for example with styled-components. If you are experiencing a CSS injection order issue, JSS provides a mechanism to handle this situation. By adjusting the placement of the HTML commentThe simplest approach is to add an HTML comment that determines where JSS will inject the styles:
Other HTML elementCreate React App strips HTML comments when creating the production build. To get around the issue, you can provide a DOM element (other than a comment) as the JSS insertion point. For example, a
JS createCommentcodesandbox.io prevents the access to the
JssProviderreact-jss exposes a
PluginsJSS uses the concept of plugins to extend its core, allowing people to cherry-pick the features they need. You pay the performance overhead for only what's you are using.
Given
It's a subset of jss-preset-default. Of course, you are free to add a new plugin. We have one example for the APIwithStyles(styles, [options]) => higher-order componentLink a style sheet with a component. It does not modify the component passed to it; instead, it returns a new component with a Some implementation details that might be interesting to being aware of:
Arguments
Returns
Examples
Also, you can use as decorators like so:
createGenerateClassName([options]) => class name generatorA function which returns a class name generator function. Arguments
Returns
Examples
Alternative APIsDo you think that higher-order components are the new mixins? Rest assured we don't, however because Render props API (+11 lines)The term “render prop” refers to a simple technique for sharing code between React components using a prop whose value is a function.
You can access the theme the same way you would do it with
@jedwards1211 Has taken the time to move this module into a package: material-ui-render-props-styles. Feel free to use it. styled-components API (+15 lines)styled-components's API removes the mapping between components and styles. Using components as a low-level styling construct can be simpler.
You can access the theme the same way you would do it with |