mui / mui/material-ui

Extended styled-component styles get overwritten with SSR

Open
#27,412 5 comments 0 reactions 1 assignee View on GitHub

@mnajdova is already working on this.

Since Jul 23, 2021.

package: styled-engine-sc scope: system
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

When using styled-components with V5 + NextJS, when extending core components the styles are overwritten by defaults.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When using this theme:

const theme = createTheme({
  palette: {
    primary: {
      main: '#f00',
    },
    secondary: {
      main: '#00f',
    },
  },
});

With these components

import { ThemeProvider, Box, Button } from '@material-ui/core';
import styled from 'styled-components';

const Example = styled(Box)`
  ${({ theme }) => {
    return {
      backgroundColor: theme.palette.primary.main,
      width: '200px',
      height: '200px',
    }
  }}
`;

const Example2 = styled(Button)`
  border: none;
`;

<ThemeProvider>
  <Example bgcolor="secondary.main" />
  <Example2 variant="outlined">Test</Example2>
</ThemeProvider>

I get this result:

image

The problem happens with <StyledEngineProvider injectFirst /> and without.

Expected Behavior 🤔

The styles given in the styled() definitions should take priority and be injected after the defaults.

Steps to Reproduce 🕹

Using the new nextjs styled-components example:
https://codesandbox.io/s/festive-dew-kh20g (content in pages/_app.js)

Steps:

  1. Clone the example repo
  2. Create a styled component that extends off a core component with base styles

Context 🔦

This has happened when I was trying to change a button border from within styled-components. I'm aware this is possible through the theme but I needed some extra customisation.

Your Environment 🌎

`npx @material-ui/envinfo`
  Chrome 91.0.4472.164
  System:
    OS: macOS 11.2.3
  Binaries:
    Node: 12.22.1 - ~/.nvm/versions/node/v12.22.1/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v12.22.1/bin/yarn
    npm: 6.14.12 - ~/.nvm/versions/node/v12.22.1/bin/npm
  Browsers:
    Chrome: 91.0.4472.164
    Edge: Not Found
    Firefox: 89.0.2
    Safari: 14.0.3
  npmPackages:
    @material-ui/core: next => 5.0.0-beta.1
    @material-ui/private-theming:  5.0.0-beta.1
    @material-ui/styled-engine:  5.0.0-beta.1
    @material-ui/styled-engine-sc: next => 5.0.0-beta.1
    @material-ui/system:  5.0.0-beta.1
    @material-ui/types:  6.0.1
    @material-ui/unstyled:  5.0.0-alpha.40
    @material-ui/utils:  5.0.0-beta.0
    @types/react: latest => 17.0.14
    react: latest => 17.0.2
    react-dom: latest => 17.0.2
    styled-components: latest => 5.3.0
    typescript: latest => 4.3.5

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.