In the code snippet below, I have set up the useState hook so that when a ListItem is clicked on, the selected state becomes true. My goal is to apply specific styling when an item is selected using the sx prop instead of creating a styled component for such simple styling requirements. What I want to achieve is to change the color of the ListItemText to blue and the background color of the ListItem to a lighter shade of blue when it is selected.
import { useState } from 'react'
import { Box, Drawer, CssBaseline, AppBar, Toolbar, Avatar, List } from '@mui/material';
import Typography from '@mui/material/Typography';
import ListItem from '@mui/material/ListItem';
import ListItemButton from '@mui/material/ListItemButton';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import HomeOutlinedIcon from "@mui/icons-material/HomeOutlined"
import PeopleOutlinedIcon from "@mui/icons-material/PeopleOutlined"
import PersonOutlinedIcon from "@mui/icons-material/PersonOutlined"
import CalendarTodayOutlinedIcon from '@mui/icons-material/CalendarTodayOutlined';
import HelpOutlinedIcon from "@mui/icons-material/HelpOutlined"
import PieChartOutlineOutlinedIcon from "@mui/icons-material/PieChartOutlineOutlined"
import TimelineOutlinedIcon from "@mui/icons-material/TimelineOutlined"
import FolderSharedOutlinedIcon from '@mui/icons-material/FolderSharedOutlined';
import { Link as RouterLink } from 'react-router-dom'
import { useTheme } from '@mui/material'
import { ColorModeContext, tokens } from "./../theme"
import { useContext } from 'react';
import Logo from "./../assets/code2.jpg"
const SideBar = () => {
const theme = useTheme();
const colorMode = useContext(ColorModeContext);
const colors = tokens(theme.palette.mode);
const [selected, setSelected] = useState("Dashboard")
const handleListItemClick = (text) => {
setSelected(text)
}
const Item = ({ icon, text, to, selected }) => {
console.log(selected === text);
return (
<ListItem key={text}
selected={selected === text}
disablePadding
onClick={() => handleListItemClick(text)}
sx={{
"&$selected": {
"& .MuiListItem.Mui-selected": {
backgroundColor: "blue",
}
},
"&$selected:hover": {
backgroundColor: "white",
'& .MuiListItem-root': {
color: "white"
}
}
}}>
<ListItemButton component={RouterLink} to={to}>
<ListItemIcon>
{icon}
</ListItemIcon>
<ListItemText selected={selected === text} primary={text}
sx={{
"&$selected": {
"& .MuiListItemText.Mui-selected": {
color: "blue",
}
}}} />
</ListItemButton>
</ListItem>)
}
return (
<Drawer sx={{
width: 240,
flexShrink: 0,
'& .MuiDrawer-paper': {
p: 2,
width: 240,
boxSizing: 'border-box',
backgroundColor: `${colors.primary[500]}`,
}
}} variant="permanent" anchor="left">
<Box textAlign="center" m="0 0 15px 0">
<Typography variant="h3" color={colors.grey[100]} fontWeight="bold" sx={{ m: "10px 0 0 0" }}> ADMINIS</Typography>
</Box>
<Box mb="10px">
<Box display="flex" justifyContent="center" alignItems="center">
<Avatar alt="logo" src={Logo} sx={{ width: 100, height: 100, cursor: "pointer"}} />
</Box>
</Box>
<Box textAlign="center">
<Typography variant="h2" color={colors.grey[100]} fontWeight="bold" sx={{ m: "10px 0 0 0" }}> Huvon Goodridge</Typography>
<Typography variant="h5" color={colors.greenAccent[500]}>VP Fancy Admin</Typography>
</Box>
<List>
<Item selected={selected} icon={<HomeOutlinedIcon />} text={"Dashboard"} to={'dashboard'} />
</List>
<Typography variant="h6" color={colors.grey[300]} sx={{ m: "15px 0px 0px 5px" }}>
Data
</Typography>
<List>
<Item selected={selected} icon={<PeopleOutlinedIcon />} text={"Team"} to={'team'} />
<Item selected={selected} icon={<FolderSharedOutlinedIcon />} text={"Projects"} to={"projects"} />
</List>
<Typography variant="h6" color={colors.grey[300]} sx={{ m: "15px 0px 0px 5px" }}>
Pages
</Typography>
<List>
<Item selected={selected} icon={<PersonOutlinedIcon />} text={"Profile"} to={'profile'} />
<Item selected={selected} icon={<CalendarTodayOutlinedIcon />} text={"Calendar"} to={'calendar'} />
<Item selected={selected} icon={<HelpOutlinedIcon />} text={"FAQ"} to={'faq'} />
</List>
<Typography variant="h6" color={colors.grey[300]} sx={{ m: "15px 0px 0px 5px" }}>
Charts
</Typography>
<List>
<Item selected={selected} icon={<PieChartOutlineOutlinedIcon />} text={"Pie Chart"} to={'piechart'} />
<Item selected={selected} icon={<TimelineOutlinedIcon />} text={"Timeline"} to={'timeline'} />
</List>
</Drawer>)
}
export default SideBar;
I've made numerous attempts to target the classes of the components I need to style, but none have been successful. I expected the changes in background color and text color when using the provided CSS properties, but unfortunately, it did not work as intended. Despite consulting the material UI website documentation, I was unable to find a solution to this issue.