I want to change styles of list/dropdown (not the input) of Autocomplete component in Material-UI. I'm using material-styles for styling.
I would like this list to be more visible from the background so maybe increase the box-shadow.
How can I do this?
The Material-UI Autocomplete component and its Popper component can be styled by targeting built-in MUI styling APIs. The Popper is also called a dropdown or listbox. The difference is that Popper is the positioning element while Listbox is the visible list element.
To get the value in the React Material-UI autocomplete, we can get the selected values from the onChange callback. We add the Autocomplete from the @material-ui/lab module. And we set the options prop to the top5films array to add the options for the autocomplete.
Luckily, Material-UI provides a solution for this: makeStyles . Using makeStyles , you can add CSS in JS without making your code look messy. First, you need to import makeStyles in your app. Next, pass all the CSS you need in your app as an object to makeStyles and store it inside a variable, useStyles .
Material-UI is simply a library that allows us to import and use different components to create a user interface in our React applications. This saves a significant amount of time since the developers do not need to write everything from scratch.
If you want to style the Autocomplete’s text color, text decoration, font, or control a variety of other styling options, the Popper component is really what you need to target. Also, if you want to set the height of the box containing the Autocomplete options, make sure to set minHeight instead of setting height.
Learn how to customize Material UI components by taking advantage of different strategies for specific use cases. Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: 1. One-off customization
The sx prop is the best option for adding style overrides to a single instance of a component in most cases. It can be used with all Material UI components. To customize a specific part of a component, you can use the class name provided by Material UI inside the sx prop.
Material UI provides several different ways to customize a component's styles. Your specific context will determine which one is ideal. From narrowest to broadest use case, here are the options: 1. One-off customization To change the styles of one single instance of a component, you can use one of the following options:
One way of doing this is by adjusting the elevation
of the Paper
component used by Autocomplete
. The default elevation is 1. The example below uses a value of 8 by specifying that in a custom Paper
component (CustomPaper
) which is then provided via the PaperComponent
prop of Autocomplete
.
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import Paper from "@material-ui/core/Paper";
const CustomPaper = (props) => {
return <Paper elevation={8} {...props} />;
};
export default function ComboBox() {
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
PaperComponent={CustomPaper}
renderInput={(params) => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
/>
);
}
If the elevation
prop does not give you the look you want, you can customize styles of the Paper component via the classes
prop of Autocomplete
as in the example below which uses a very ugly border for demonstration purposes, but you can make whatever CSS change you want to make using the same approach.
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import { makeStyles } from "@material-ui/core/styles";
const useStyles = makeStyles({
paper: {
border: "5px solid black"
}
});
export default function ComboBox() {
const classes = useStyles();
return (
<Autocomplete
id="combo-box-demo"
options={top100Films}
getOptionLabel={(option) => option.title}
style={{ width: 300 }}
classes={{ paper: classes.paper }}
renderInput={(params) => (
<TextField {...params} label="Combo box" variant="outlined" />
)}
/>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With