Is there a way for me to implement my custom CSS design within the Material UI Textfield component?

I have a project in Next.js where I need to create a registration form with custom styles. The issue I'm facing is that I'm struggling to customize a textField using my own CSS. I attempted to use the makeStyles function, but encountered a problem as it requires passing objects and some properties like background-color and border-radius are not recognized.

Here's the CSS I want to implement:

.Name {
  width: 535px;
  height: 61px;
  flex-grow: 0;
  margin: 40px 100px 20px 0;
  padding: 19px 329px 20px 20px;
  border-radius: 8px;
  border: solid 1px var(--select-border);
  background-color: #ffffff;
}

I saved this CSS in a file, imported it into my component as styles, and applied it using className like this:

<TextField
    className={styles.Name}
    placeholder='Restaurant Name'
>

I also tried using makeStyles in this way:

const useStyles = makeStyles({
nameStyles: {
width: '535px',
height: '61px',
flexGrow: '0',
margin: '40px 100px 20px 0',
padding: '19px 329px 20px 20px',
border: '8px',
border: 'solid 1px var(--select-border)',
backgroundColor: '#ffffff',
})

export default function RestaurantRegistration() {
   const classes = useStyles()

   return (
    <React.Fragment>
  
   <TextField
    className={classes.nameStyles}
    placeholder='Restaurant Name'
   >

   </TextField>
 )
 }

When using the second method, I had to change properties containing '-' to camelCase due to syntax errors. For example, I changed flex-grow to flexGrow and background-color to backroundColor. I'm unsure if this is the correct approach. Can anyone provide assistance?

Answer №1

In order to effectively utilize the className attribute, it is necessary to enclose your component within the StylesProvider component and include the property injectFirst.

Furthermore, by referencing the TextField API, you can make use of the classes prop, which accepts an object allowing for override or extension of styles. Utilizing the root rule name allows you to apply custom styles to TextField.

Utilizing css modules and StylesProvider:

  <StylesProvider injectFirst> 
    <TextField
        className={styles.Name}
        placeholder='Restaurant Name'
       >
  </StylesProvider>

Using makeStyles:

const useStyles = makeStyles({
  root: {
    width:'535px',
    height:'61px',
    flexGrow:'0',
    margin:'40px 100px 20px 0',
    padding: '19px 329px 20px 20px',
    border: '8px',
    border: 'solid 1px var(--select-border)',
    backgroundColor:'#ffffff',
}})
   
export default function RestaurantRegistration() {
   const classes = useStyles()

   return (
    <TextField
      classes={classes}
      placeholder='Restaurant Name'
    />
 )
}

https://codesandbox.io/s/so-textfields-pxv9z?file=/src/TextFields/index.js

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Filtering React component names and their corresponding values

I am looking to apply filters to React components based on their name and values. Below is the array that needs to be filtered: let filteredArray: any[] = [] const filteredItems: any[] = eventList.filter( (event) => event.printEvent.labels = ...

Files with extensions containing wildcards will trigger a 404 error when accessed from the public folder in NextJS

I have successfully set up my public folder to serve static files, however I am encountering an issue with files that have a leading dot in their filename (.**). Specifically, I need to host the "well-known" text file for apple-pay domain verification, wh ...

Tips for combining several Launchdarkly initiatives into a unified React application

Discovering how to implement feature flags in multiple Launchdarkly projects from a single provider is explored in the React web docs. The example below illustrates setting up feature flags using different clientSideID values within the same project. impor ...

Unusual occurrence: unexpected positioning issue with iOS and Chrome (Windows)

My website looks perfect on Firefox, but unfortunately, it's not displaying correctly on Safari (iOS) and some Chrome devices. The Menu-Bar, which should be position fixed, is not showing properly. I'm not sure what the issue is. Screenshots: ...

Ways to position an image in the middle of a Div

I am currently working with PHP and Smarty, attempting to display a slideshow's images in the center of a specific div, but I am encountering difficulties achieving this. Below you will find the code snippet. Can anyone help me figure out what I migh ...

Leverage the data populated by useEffect to conditionally use the SWR hook

Currently trying to resolve the issue raised in this query, and progress has been made. The API is returning the correct data and updating as expected. However, upon initial loading, useSWR is making requests to the API with all data being null. The data ...

Why Does my Overlay Keep Bouncing when Using JQuery to slideDown / slideUp Text on Image?

Can anyone help me replicate the text overlay effect that is seen when hovering over an image on The Guardian website? I have almost got it working, but whenever my mouse moves over the trail-text area, it starts bouncing (slideDown slideUp) repeatedly. I ...

When using next.js, a warning may be encountered that states: "A value of NaN was received for the `children` attribute. To resolve this, ensure the value is cast as

I am currently tackling my first Next.js project and have created a file called myCart.js. Below is the code snippet: function orderCard(arr1) { //Code here... } let noRefresh; function makeGetRequest() { //Code here... } export default makeGetReques ...

Tips for distinguishing between elements in React

I am facing an issue with zooming images on mouse hover using CSS. I have a code snippet that works well, but it zooms both images simultaneously. How can I differentiate between mouse movements over different elements in ReactJS? Here is the code: .styl ...

Ways to efficiently update styles dynamically using css and/or javascript

I am trying to update the styles of a button when it is clicked. I initially set the style for the first element on page load, but now I need to remove those styles from the first element and apply them to the clicked button instead. I am having trouble fi ...

Deleting records in Drizzle ORM with multiple conditions in the WHERE clause can be accomplished by using the delete function along

Below is a function that I have: export async function deleteFavoriteTrack({profileId, trackId}) { await db.delete(favoriteTracks).where(eq(favoriteTracks.profileId, profileId)); } I am only able to use one "eq" in this function. How can I achieve a s ...

How can I resolve the issue of <td> being repeatedly displayed five times instead of just twice in PHP?

Can someone assist me with fixing this for loop issue? I am trying to display controls next to each item in the row, but it is showing 5 sets of controls instead of just 2. <tbody> <?php //retrieve list of supplies $numOfRows = 0; $result = my ...

Creating a customized component using unique styles in Material UI version 1

Currently, I am facing a challenge in styling my Card component with a width of 75 pixels while using Redux and Material UI V1. Despite following the example provided by Material UI on custom styling through withStyles and connect, I have not been able to ...

Obtaining values from keys in React list items

getEmployeeCredits(id) { if (this.state.company_roles) { return this.state.company_roles.map(function (cr, i) { if (cr.id === id) { return cr.assigned_credits } }.bind(thi ...

What is the best way to design a Global Navigation menu for websites?

For example, I am looking to integrate a Navigation menu into my website using just one file. I have considered using PHP or creating an HTML frame, but I am wondering what the current industry standard is for professionals. Any insights? ...

Error: There was an issue registering the component as the target container is not recognized as a valid DOM element

Upon executing the React code below, I encountered the following error: import React from 'react'; import ReactDOM from 'react-dom'; ReactDOM.render( <div id="root"> <h1>Hello, world!</h1></div>, document ...

Exploring Meta Tag Functionality with ReactJS v18 Using react-helmet: Understanding Google's Approach to Managing Dynamic Meta Tags in Client

I'm currently developing a complete ReactJS application (without Next.JS, so it's entirely Client Side Rendering) and I've encountered some challenges (or at least questions) regarding SEO and dynamic meta tags. The foundation of ReactJS is ...

Combining Cognito and Lambda: How to Match User Login Data with a PUT Parameter?

Presented here is my current application setup: - Sensors sending data to Amazon RDS (MySQL) - User Interface built with React.js, connected to Lambda for accessing data in Amazon RDS ! All user authentication is handled through Cognito. Ther ...

Steps for transferring data from one flatlist to another (an array of objects) within the same page featuring identical data:

const DATA = [ { car_name: 'Ford', model_number: '2021 . 68 - 1647', full_tank: false, suggestion: [ { price: '$2.50', type: 'Oil Top up&apos ...

CSS / JavaScript Navigation Menu overshadowing Flash content in Firefox

On my website, I have a drop-down menu that was created using CSS and JavaScript. This menu drops down over a Flash animation. Interestingly, in Internet Explorer 6 and 7, the drop-down menus successfully appear over the Flash animation. However, in Mozill ...