Forked from Danilo-Araujo-Silva/Overriding Material UI styles with Styled Components.md
Last active
May 4, 2018 17:13
-
-
Save wilomgfx/6017dee8b45ac460bc7d28b143baee7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //For example, to override the AppBar (https://material-ui-next.com/api/app-bar/) root class we can do the following: | |
| // First method (override Material UI classnames): | |
| // 1 - Add the property classes in the AppBar component: | |
| <AppBar classes={{root: 'my-root-class'}} | |
| // 2 - Override the styles with the styled components: | |
| styled(AppBar)` | |
| &.my-root-class { | |
| z-index: 1500; | |
| } | |
| ` | |
| //Second method (force specificity): | |
| styled(AppBar)` | |
| && { | |
| z-index: 1500; | |
| } | |
| ` | |
| //Third method (use a custom classname): | |
| //1 - Add the classname in the classname property: | |
| <AppBar className={`my-class ${this.props.otherClassesFromPropertiesWithNeeded}`} | |
| // 2 - Override the styles with the styled components: | |
| styled(AppBar)` | |
| &.my-class { | |
| z-index: 1500; | |
| } | |
| `; | |
| //Forth method | |
| const StyledLink = styled(Link).attrs({ | |
| activeClassName: 'active' | |
| })` | |
| color: black; | |
| &.active { | |
| color: blue; | |
| } | |
| `; | |
| // or extract that magic classname: | |
| const activeClassName = 'active-link' | |
| const StyledLink = styled(Link).attrs({ | |
| activeClassName | |
| })` | |
| color: black; | |
| &.${activeClassName} { | |
| color: blue; | |
| } | |
| ` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment