Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i remove the outline on focus in DataGrid

Tags:

material-ui

I'm trying to remove the outline that appears when you focus on a cell in Material UI's DataGrid component.

I'm trying to remove the outline that appears when you focus on a cell in Material UI's DataGrid component.

None of these methods work:

const useStyles = makeStyles((theme) => ({

// Method 1:
  '@global': {
    '.MuiDataGrid-cell:focus': {
      outline: 0,
    },
  },

// Method 2:
  cell: {
    '& .MuiDataGrid-cell:focus': {
      outline: 0,
    },
  },

// Method 3:
  cell: {
    ':focus': { outline: 0 },
  },

const classes = useStyles()

const dataGridColumns: GridColumns = [
    {
      ...other,
      cellClassName: classes.cell,
    }]

Edit:

'@global': {
        '.MuiDataGrid-root .MuiDataGrid-cell:focus': {
          outline: 0,
        },
      },

worked for me, but I would prefer not to use a global css adjustment.

like image 353
jess Avatar asked Mar 12 '21 20:03

jess


People also ask

Can I override the classes used by the DataGrid?

You can override many of the classes used by the datagrid, but you'll need to make sure that the selector being used is more specific in order for it to take precedence. Many of their classes are documented here in the CSS section.

When to use outline 0 vs outline none?

Note: On focus Use outline:0 in spite of outline:none. It’s valid and better practice.

Is there a way to remove the highlight from a modal?

You can remove it, though: You may want to add some other way for users to know what element has keyboard focus though for usability. Chrome will also apply highlighting to other elements such as DIV’s used as modals. To prevent the highlight on those and all other elements as well, you can do:

Is there a way to remove the keyboard focus on elements?

You can remove it, though: You may want to add some other way for users to know what element has keyboard focus though for usability. Chrome will also apply highlighting to other elements such as DIV’s used as modals.


5 Answers

You can modify the MuiDataGrid-cell class by using Material UI's useStyles() function like the following (no need to declare globally):

import { makeStyles } from '@material-ui/core/styles';
import { DataGrid } from '@material-ui/data-grid';

const useStyles = makeStyles({
    root: {
        '&.MuiDataGrid-root .MuiDataGrid-cell:focus': {
            outline: 'none',
        },
    }
});

const MyComponent = () => {
    const classes = useStyles();

    return (
        <DataGrid
           className={classes.root}
           // ...other table setup props
        />   
    );
}

export default MyComponent;

Resources:

  • https://material-ui.com/styles/advanced/#with-material-ui-core
  • https://material-ui.com/api/data-grid/#css
  • https://github.com/mui-org/material-ui-x/issues/420

Edit: Updated for 4.0.0-alpha.29

const useStyles = makeStyles({
    root: {
        '&.MuiDataGrid-root .MuiDataGrid-columnHeader:focus, &.MuiDataGrid-root .MuiDataGrid-cell:focus': {
            outline: 'none',
        },
    }
});
like image 93
bryyytl Avatar answered Oct 19 '22 04:10

bryyytl


Just for completion sake this is how I styled the DataGrid with styled components and it worked.

const StyledDataGrid = styled(DataGrid)`
  &.MuiDataGrid-root .MuiDataGrid-columnHeader:focus,
  &.MuiDataGrid-root .MuiDataGrid-cell:focus {
    outline: none;
  }
`;
like image 6
Michael Neumann Avatar answered Oct 19 '22 03:10

Michael Neumann


const useStyles = makeStyles(
      () => createStyles({
        root: {
          '& .MuiDataGrid-columnHeader:focus-within, & .MuiDataGrid-cell:focus-within': {
            outline: 'none',
          },

          '& .MuiDataGrid-columnHeader:focus, & .MuiDataGrid-cell:focus': {
            outline: 'none',
          },
        },
      }),
    );
like image 2
msniezynski Avatar answered Oct 19 '22 04:10

msniezynski


If you're using MUI >= 5, you can simply do the follow:

<DataGrid
   sx={{
      "&.MuiDataGrid-root .MuiDataGrid-cell:focus-within": {
         outline: "none !important",
      },
   }}
   ...
/>
like image 3
William Zimmermann Avatar answered Oct 19 '22 04:10

William Zimmermann


I just needed to do this as well for a project I'm working on.

You can override many of the classes used by the datagrid, but you'll need to make sure that the selector being used is more specific in order for it to take precedence.

Many of their classes are documented here in the CSS section.

The below snippet worked for me.

const useStyles = makeStyles((theme) => ({
  root: {
    "& .MuiDataGrid-root": {
      "& .MuiDataGrid-colCell:focus": {
        outline: "none"
      },
    },
  },
}));
like image 1
Kevin Avatar answered Oct 19 '22 04:10

Kevin