Material UI Styles Not Rendering
Asked Answered
B

3

2

Im attempting to build a website using Material-UI and React. When attempting to use Material-UI's styling via the Hook API, it works online in codesandbox.io but does not work locally when I run it. The border radius property does not seem to update, nor do any of the properties in the button or instruction object

What I see when I run the same code locally

import React from 'react';
import { makeStyles } from '@material-ui/styles';
import Stepper from '@material-ui/core/Stepper';
import Step from '@material-ui/core/Step';
import StepLabel from '@material-ui/core/StepLabel';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';

const useStyles = makeStyles({
  root: {
    width: "100%"
  },
  button: {
    marginRight: 10,
    borderRadius: 100,
    fontSize: 20,
  },
  instructions: {
    marginTop: 2,
    marginBottom: 5
  }
});

function getSteps() {
  return ['Select campaign settings', 'Create an ad group', 'Create an ad'];
}

function getStepContent(step) {
  switch (step) {
    case 0:
      return 'Select campaign settings...';
    case 1:
      return 'What is an ad group anyways?';
    case 2:
      return 'This is the bit I really care about!';
    default:
      return 'Unknown step';
  }
}

function HorizontalLinearStepper() {
  const classes = useStyles();
  const [activeStep, setActiveStep] = React.useState(0);
  const [skipped, setSkipped] = React.useState(new Set());
  const steps = getSteps();

  function isStepOptional(step) {
    return step === 1;
  }

  function isStepSkipped(step) {
    return skipped.has(step);
  }

  function handleNext() {
    let newSkipped = skipped;
    if (isStepSkipped(activeStep)) {
      newSkipped = new Set(newSkipped.values());
      newSkipped.delete(activeStep);
    }

    setActiveStep(prevActiveStep => prevActiveStep + 1);
    setSkipped(newSkipped);
  }

  function handleBack() {
    setActiveStep(prevActiveStep => prevActiveStep - 1);
  }

  function handleSkip() {
    if (!isStepOptional(activeStep)) {
      // You probably want to guard against something like this,
      // it should never occur unless someone's actively trying to break something.
      throw new Error("You can't skip a step that isn't optional.");
    }

    setActiveStep(prevActiveStep => prevActiveStep + 1);
    setSkipped(prevSkipped => {
      const newSkipped = new Set(prevSkipped.values());
      newSkipped.add(activeStep);
      return newSkipped;
    });
  }

  function handleReset() {
    setActiveStep(0);
  }

  return (
    <div className={classes.root}>
      <Stepper activeStep={activeStep}>
        {steps.map((label, index) => {
          const stepProps = {};
          const labelProps = {};
          if (isStepOptional(index)) {
            labelProps.optional = <Typography variant="caption">Optional</Typography>;
          }
          if (isStepSkipped(index)) {
            stepProps.completed = false;
          }
          return (
            <Step key={label} {...stepProps}>
              <StepLabel {...labelProps}>{label}</StepLabel>
            </Step>
          );
        })}
      </Stepper>
      <div>
        {activeStep === steps.length ? (
          <div>
            <Typography className={classes.instructions}>
              All steps completed - you&apos;re finished
            </Typography>
            <Button onClick={handleReset} className={classes.button}>
              Reset
            </Button>
          </div>
        ) : (
          <div>
            <Typography className={classes.instructions}>{getStepContent(activeStep)}</Typography>
            <div>
              <Button disabled={activeStep === 0} onClick={handleBack} className={classes.button}>
                Back
              </Button>
              {isStepOptional(activeStep) && (
                <Button
                  variant="contained"
                  color="primary"
                  onClick={handleSkip}
                  className={classes.button}
                >
                  Skip
                </Button>
              )}
              <Button
                variant="contained"
                color="primary"
                onClick={handleNext}
                className={classes.button}
              >
                {activeStep === steps.length - 1 ? 'Finish' : 'Next'}
              </Button>
            </div>
          </div>
        )}
      </div>
    </div>
  );
}

export default HorizontalLinearStepper;

You can view the expected results here: https://98m6j7m314.codesandbox.io in which the buttons border is circular after applying the borderRadius property

Barrier answered 8/1, 2019 at 1:44 Comment(3)
In inspect element is the style being overwritten by something else or is it not getting rendered at all ?Rheumatic
@Rheumatic it seems like its being overwritten, but im not too sure, it has a class associated with it by the name of .MuiButton-root-107 and that class is setting the border radius to 4px. This is not the same on the working version although it is the same code and package versionsBarrier
Did you try on different browsers ? Also try using the !important tag.Rheumatic
C
2

When using @material-ui/styles with @material-ui/core you need to follow the installation step https://v3.material-ui.com/css-in-js/basics/#migration-for-material-ui-core-users.

Here's your codesandbox link working: https://codesandbox.io/s/material-demo-rv2w1

Campbellite answered 8/1, 2019 at 21:20 Comment(6)
Hello Josh, thank you very much, I did not perform the installation step correctly! It now works after importing the bootstrap.js, although I had imported the install from @material-ui/styles, but it was below the import of ReactBarrier
@Barrier Can you plz share how do imported bootstrap.js to index.js file?Rival
@Oshik, what's the point of importing styles if you then still need to make styles?Guarded
Codesandbox is not working. That is why we request always to add code example in the answer along with the link.Stride
@Josh Wooding My app won't compile following this with error = Attempted import error: 'install' is not exported from '@material-ui/styles'. Any idea why this would occur?Burlie
@Burlie If you are using v4 the install step is no longer needed.Campbellite
S
1

Web browsers use cache and in some cases your changes are not reloaded. Refershing using Ctrl+f5, clearing or disabling cache in your settings may be useful.

Please attempt to see your localhost web page using another web browser & in incognito

Sniff answered 8/1, 2019 at 2:19 Comment(1)
hey my friend, just tried the incognito, it still doesn't render the Button with the borderRadius, ill try the ctrl-f5Barrier
W
0

Here is the very simple answer for this -

You just need to change the Injection Order of MUI CSS. Check the website for the solution

https://mui.com/material-ui/guides/interoperability/#css-injection-order-3

It worked for me 🔥🔥

Words answered 8/4, 2023 at 13:29 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Neptunium

© 2022 - 2024 — McMap. All rights reserved.