Remove border from Material UI Modal
Asked Answered
G

5

10

I am trying to use the React Material UI Modal but I always get a black border around the modal when it is focused on. I have removed the border when it is out of focus, but if the modal is focused, the border comes back. Any suggestions on how to remove it?

https://codesandbox.io/s/material-demo-kk0ux?file=/demo.js

const useStyles = makeStyles(theme => ({
  paper: {
    position: "absolute",
    width: 400,
    backgroundColor: theme.palette.background.paper,
    boxShadow: theme.shadows[5],
    padding: theme.spacing(2, 4, 3)
  },
  modal: {
    "&:focus": {
      outline: "none"
    }
  }
}));

export default function SimpleModal() {
  const classes = useStyles();
  // getModalStyle is not a pure function, we roll the style only on the first render
  const [modalStyle] = React.useState(getModalStyle);
  const [open, setOpen] = React.useState(false);

  const handleOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  const body = (
    <div style={modalStyle} className={classes.paper}>
      <h2 id="simple-modal-title">Text in a modal</h2>
      <p id="simple-modal-description">
        Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
      </p>
      <SimpleModal />
    </div>
  );

  return (
    <div>
      <button type="button" onClick={handleOpen}>
        Open Modal
      </button>
      <Modal
        className={classes.modal}
        open={open}
        onClose={handleClose}
        aria-labelledby="simple-modal-title"
        aria-describedby="simple-modal-description"
      >
        {body}
      </Modal>
    </div>
  );
}
Graphomotor answered 10/6, 2020 at 16:43 Comment(4)
Did you used google? Result 1 Result 2 Result 3 ?Monoxide
Does this answer your question? How to remove focused highlight in React material-ui Tab Component?Monoxide
Make sure you explicitly need to remove it; this comes up frequently during accessibility reviews.Whippet
@Monoxide yes I have googled and tried those results but nothing seemed to work. setting focus, hover, or active outline to none did not fix it. Adding disableAutoFocus also did not fix it.Graphomotor
D
32

Set the outline: 'none' to your paper instead. That will fix your problem.

Also, i think that you should be using <Dialog> instead, as recommended in docs. You will keep your behavior without that focus.

Desiccate answered 10/6, 2020 at 17:3 Comment(1)
Perfect thank you. I needed to apply the styling on paper. Will look into Dialog. CheersGraphomotor
A
6

Wrap the body of the Modal tag in a and provide outline: none as a style

<div style={{outline:'none'}}>     
    {body}    
    </div>
Apollyon answered 7/2, 2021 at 17:5 Comment(0)
I
1

add this to your makeStyles

modal:{
    "&:focus":{
    outline: 'none"
   }
 }
Inestimable answered 29/1, 2021 at 7:57 Comment(0)
S
0

Modal is a low level component to create a Dialog which should be preferred in most cases instead. The Modal container itself doesn't have a border by default, in case you copy the code from the first example here, you may want to remove the border and boxShadow properties if you don't want it:

const style = {
  position: 'absolute' as 'absolute',
  top: '50%',
  left: '50%',
  transform: 'translate(-50%, -50%)',
  width: 400,
  bgcolor: 'background.paper',
  // comment the 2 lines below
  // border: '2px solid #000',
  // boxShadow: 24,
  p: 4,
};

Live Demo

Codesandbox Demo

Swede answered 8/11, 2021 at 8:49 Comment(0)
C
0
const style = {
   position: 'absolute',
   top: '50%',
   left: '50%',
   transform: 'translate(-50%, -50%)',
   // width: 400,
   bgcolor: 'background.paper',
   boxShadow: 24,
   borderRadius: '20px',
   background: '#FFF',
   outline: 'none', // add this in your code 
};

<Modal
    open={modalStatus}
    onClose={modalHandleClose}
    aria-labelledby="modal-modal-title"
    aria-describedby="modal-modal-description"
  >
   /* add style here in the box */
    <Box sx={style}>Hello</Box>
  </Modal>
Cosette answered 8/1 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.