How to change the styles of MUI TextField In React

How to change the styles of MUI TextField In React

MUI library is a great library that offers a comprehensive suite of UI tools that helps ship new features faster.

Sometimes we would like to customize the default look and feel of MUI components, which might be a bit not straightforward, This article will demonstrate how to change the styles of the MUI text field component.


Let’s get started!

There are a few options we can use in order to customize the styles of MUI components in React, some of them are:

In this article, we will cover one aspect of the styleOverrides options, which is to override the styles using the sx prop.

First, we need to create a new component for the text field, then we will pass the customized styles as props to this component so that we can re-use it for different use cases, let’s create CustomTextField.jsx file, and inside it, we will have the following implementation:

import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function CustomTextField({ label = "default label" }) {
  return (
    <TextField
      label={label}
    />
  );
}
How to change the styles of MUI TextField In React
How to change the styles of MUI TextField In React

As shown above, we’ve created the CustomTextField which wraps the MUI TextField component.


How to change the root styles of MUI TextField

To override the root styles of the TextField which has the class name MuiTextField-root, we need to define our custom styles through the sx prop on the root level, as shown below:

import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function InputField({ label = 'default label' }) {
  return (
    <TextField
      label={label}
      sx={{
        backgroundColor: '#ffd60a',
        border: '3px solid #001d3d',
      }}
    />
  );
}
How to change the root styles of MUI TextField
How to change the root styles of MUI TextField

That looks awesome! Let’s continue…


How to change the root styles of the MUI TextField label

We need to pass our custom styles using the sx prop through the prop InputLabelProps as the following:

import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function InputField({ label = 'default label' }) {
  return (
    <TextField
      label={label}
      sx={{
        backgroundColor: '#ffd60a',
        border: '3px solid #001d3d',
      }}
      InputLabelProps={{
        sx: {
          color: '#003566',
          textTransform: 'capitalize',
        },
      }}
    />
  );
}
How to change the root styles of the MUI TextField label
How to change the root styles of the MUI TextField label

Looks good! We have the label color changed, and the text transformed to the Capitalize form.


How to change the styles of the MUI TextField input

The same applied here, to change the styles of the MUI TextField input, we need to pass the custom styles using the sx prop through the inputProps prop, as the following:

import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function InputField({ label = 'default label' }) {
  return (
    <TextField
      label={label}
      sx={{
        backgroundColor: '#ffd60a',
        border: '3px solid #001d3d',
      }}
      InputLabelProps={{
        sx: {
          color: '#003566',
          textTransform: 'capitalize',
        },
      }}
      inputProps={{
        sx: {
          color: 'red',
          paddingLeft: '15px',
          fontSize: '20px',
        },
      }}
    />
  );
}

The above changes will result in the following UI:

How to change the styles of the MUI TextField input
How to change the styles of the MUI TextField input

How to change the styles of the MUI TextField Helper Text

MUI TextField helper text is usually used in case of communicating back to the user, such as reporting some input validations or errors.

We can use the property helperText to pass the message to the TextField, and by using the FormHelperTextProps we can pass our customized styles, as the following:

import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function InputField({ label = 'default label' }) {
  return (
    <TextField
      label={label}
      sx={{
        backgroundColor: '#ffd60a',
        border: '3px solid #001d3d',
      }}
      InputLabelProps={{
        sx: {
          color: '#003566',
          textTransform: 'capitalize',
        },
      }}
      inputProps={{
        sx: {
          color: 'red',
          paddingLeft: '15px',
          fontSize: '20px',
        },
      }}
      helperText="Please enter a valid input"
      FormHelperTextProps={{
        sx: {
          color: 'red',
        },
      }}
    />
  );
}
How to change the styles of the MUI TextField Helper Text
How to change the styles of the MUI TextField Helper Text

Now, let’s see how we can customize the styles on certain behaviors like hover, and focus.


How to change the border of the MUI TextField On Hover/On Focus

Between the input Default Label and the HelperText message, we have a border as shown below, and this border gets darker on hovering over it.

How to change the border of the MUI TextField On Hover/On Focus
How to change the border of the MUI TextField On Hover/On Focus

To customize the styles of this border onHover or onFocus we can do it by targeting the fieldset element from the InputProps prop (Note the difference between InputProps and inputProps), as the following:

  InputProps={{
    sx: {
      '&:hover fieldset': {
        border: '2px solid blue!important',
        borderRadius: 0,
      },
      '&:focus-within fieldset, &:focus-visible fieldset': {
        border: '4px solid red!important',
      },
    },
  }}
import * as React from 'react';
import TextField from '@mui/material/TextField';

export default function InputField({ label = 'default label' }) {
  return (
    <TextField
      label={label}
      sx={{
        backgroundColor: '#ffd60a',
        border: '3px solid #001d3d',
      }}
      InputLabelProps={{
        sx: {
          color: '#003566',
          textTransform: 'capitalize',
        },
      }}
      InputProps={{
        sx: {
          '&:hover fieldset': {
            border: '2px solid blue!important',
            borderRadius: 0,
          },
          '&:focus-within fieldset, &:focus-visible fieldset': {
            border: '4px solid red!important',
          },
        },
      }}
      inputProps={{
        sx: {
          color: 'red',
          paddingLeft: '15px',
          fontSize: '20px',
        },
      }}
      helperText="Please enter a valid input"
      FormHelperTextProps={{
        sx: {
          color: 'red',
        },
      }}
    />
  );
}

Now once the user moves the mouse cursor over the input field, we can see the following styles applied:

How to change the border of the MUI TextField On Hover
How to change the border of the MUI TextField On Hover

And once the input is focused:

How to change the border of the MUI TextField On Focus
How to change the border of the MUI TextField On Focus

That’s It! If you want to customize more, you can target more elements in the same way we did in this article (like the input disabled state).


How to make MUI TextField re-usable

In order to achieve the re-usability, we can extract the styles into their variables and pass them as the default props to the TextField, and we can allow the users of the CustomTextField to pass their own styles, as the following:

import * as React from 'react';
import TextField from '@mui/material/TextField';

// 1- Default styles
const rootStyles = {
  backgroundColor: '#ffd60a',
  border: '3px solid #001d3d',
};

const inputLabelStyles = {
  color: '#003566',
  textTransform: 'capitalize',
};

const rootInputStyles = {
  '&:hover fieldset': {
    border: '2px solid blue!important',
    borderRadius: 0,
  },
  '&:focus-within fieldset, &:focus-visible fieldset': {
    border: '4px solid red!important',
  },
};

const inputStyles = {
  color: 'red',
  paddingLeft: '15px',
  fontSize: '20px',
};

const helperTextStyles = {
  color: 'red',
};

export default function InputField({
  label = 'default label',
  // 2- User custom styles
  customRootStyles,
  customInputLabelStyles,
  customRootInputStyles,
  customInputStyles,
  customHelperTextStyles,
}) {
  return (
    <TextField
      label={label}
      helperText="Please enter a valid input"
      sx={{ ...rootStyles, ...customRootStyles }}
      InputLabelProps={{
        sx: {
          ...inputLabelStyles,
          ...customInputLabelStyles,
        },
      }}
      InputProps={{
        sx: {
          ...rootInputStyles,
          ...customRootInputStyles,
        },
      }}
      inputProps={{
        sx: {
          ...inputStyles,
          ...customInputStyles,
        },
      }}
      FormHelperTextProps={{
        sx: {
          ...helperTextStyles,
          ...customHelperTextStyles,
        },
      }}
    />
  );
}

Resources:

That’s It! Hope this article helps you in achieving your custom styles in MUI.

Photo from Unsplash.

Related Posts

How to Capture Screenshots with Puppeteer In NodeJS

How to Capture Screenshots with Puppeteer In NodeJS

To Capture Screenshots with Puppeteer: Launch a Browser Instance Navigate to the Web Page Capture the Screenshot Introduction: Puppeteer is a powerful Node.js library that allows developers…

How to Minimize Puppeteer Browser Window To Tray

How to Minimize Puppeteer Browser Window To Tray

Puppeteer is a powerful tool for automating tasks in headless or non-headless web browsers using JavaScript. While Puppeteer is often used to perform actions within a browser,…

Intercepting Responses in Node.js with Puppeteer

Intercepting Responses in Node.js with Puppeteer

Introduction: Puppeteer is a powerful Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It’s widely used for web scraping, automated testing,…

Mastering React Component Re-rendering in Jest

Mastering React Component Re-rendering in Jest

In this hands-on guide, we’ll explore the art of optimizing React component re-rendering within Jest tests. By combining theory with practical coding examples, you’ll gain a deep…

Eliminating Nesting Loops in React Rendering

Eliminating Nesting Loops in React Rendering

React has ushered in a new era of web application development with its component-based structure, promoting code reusability and maintainability. But as projects evolve, achieving optimal performance…

Exploring Type and Interface Usage in TypeScript

Exploring Type and Interface Usage in TypeScript

TypeScript has gained immense popularity by bridging the gap between dynamic JavaScript and static typing. Two of its fundamental features, “Type” and “Interface,” play pivotal roles in…

This Post Has One Comment

  1. Thank you for writing this up. I found it very helpful in trying to figure out the MUI styling for this component.

Leave a Reply

%d bloggers like this: