Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Material-UI theme with styled-components? #10098

Closed
danielkarlsson opened this issue Jan 30, 2018 · 19 comments
Closed

How to use Material-UI theme with styled-components? #10098

danielkarlsson opened this issue Jan 30, 2018 · 19 comments
Labels
support: question Community support but can be turned into an improvement support: Stack Overflow Please ask the community on Stack Overflow

Comments

@danielkarlsson
Copy link

How can I access the mui theme with the example shown in the documentation on how to use the styled components API?

@mbrookes

This comment has been minimized.

@mbrookes mbrookes added the support: Stack Overflow Please ask the community on Stack Overflow label Jan 30, 2018
@support
Copy link

support bot commented Jan 30, 2018

👋 Thanks for using Material-UI!

We use the issue tracker exclusively for bug reports and feature requests, however,
this issue appears to be a support request or question. Please ask on StackOverflow where the
community will do their best to help. There is a "material-ui" tag that you can use to tag your
question.

If you would like to link from here to your question on SO, it will help others find it.
If your issues is confirmed as a bug, you are welcome to reopen the issue using the issue template.

@support support bot closed this as completed Jan 30, 2018
@danielkarlsson

This comment has been minimized.

@mbrookes

This comment has been minimized.

@einarpersson

This comment has been minimized.

@nicolasiensen

This comment has been minimized.

@bsherrill480

This comment has been minimized.

@eps1lon

This comment has been minimized.

@plnichols

This comment has been minimized.

@obedparla

This comment has been minimized.

@0xlkda

This comment has been minimized.

@ahmedsaab

This comment has been minimized.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 14, 2020

The recommended approach is to use two theme provider:

import React from 'react';
import styled, { ThemeProvider } from 'styled-components';
import {
  StylesProvider,
  ThemeProvider as MuiThemeProvider,
  createMuiTheme,
  darken,
  fade,
} from '@material-ui/core';

const myTheme = createMuiTheme();

const StyledButton = styled.button`
  ${({ theme }) => `
  padding: ${theme.spacing(1, 2)};
  border: 1px solid;
  cursor: pointer;
  outline: 0;
  border-radius: ${theme.shape.borderRadius}px;
  color: ${theme.palette.primary.contrastText};
  background-color: ${theme.palette.primary.main};
  border-color: ${theme.palette.primary.main};
  transition: ${theme.transitions.create(['background-color', 'box-shadow'])};
  &:hover {
    background-color: ${darken(theme.palette.primary.main, 0.1)};
    border-color: ${darken(theme.palette.primary.main, 0.2)};
  }
  font-size: 18px;
  ${theme.breakpoints.up('md')} {
    font-size: 16px;
  }
  `}
`;

export default function App() {
  return (
    <StylesProvider injectFirst>
      <MuiThemeProvider theme={myTheme}>
        <ThemeProvider theme={myTheme}>
          <StyledButton>Styled Components</StyledButton>
        </ThemeProvider>
      </MuiThemeProvider>
    </StylesProvider>
  );
}

@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Jan 14, 2020
@oliviertassinari oliviertassinari changed the title How to use mui theme with styled components API How to use Material-UI theme with styled-components? Jan 14, 2020
@malik-sahab
Copy link

malik-sahab commented Jan 29, 2020

The recommended approach is to use two theme provider:

@oliviertassinari will this approach cause performance issues?

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 29, 2020

will this approach cause performance issues?

@malik-sahab None that I'm aware of.

@R-Oscar
Copy link

R-Oscar commented Mar 20, 2020

@oliviertassinari is there any way to use single theme provider? I would like to have determenistic classnames

@oliviertassinari
Copy link
Member

oliviertassinari commented Mar 20, 2020

@R-Oscar the class names of styled-components aren't deterministic, there is no way to make them so. Stick to JSS in this case.

@R-Oscar
Copy link

R-Oscar commented Mar 20, 2020

@oliviertassinari I mean classes of @material-ui/core (such as MuiSvgIcon-root). According to the spec there should be only one theme provider in order to achieve that, but I would like to keep styled components in my project

@oliviertassinari
Copy link
Member

#10098 (comment) will do the task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement support: Stack Overflow Please ask the community on Stack Overflow
Projects
None yet
Development

No branches or pull requests