Maintain a consistent size for the material UI drawer, preventing it from resizing when the content size fluctuates

Incorporating Material UI into my project, I decided to use a drawer for navigation. However, within the drawer, there are Collapsible lists that expand when clicked. The issue arises when the list text items are lengthy, causing the drawer to unexpectedly widen. My goal is to limit the drawer's width to 30% of the window size. Despite trying to apply classes such as root or modal to the drawer component, the desired width adjustment remains elusive.

Below is an excerpt of the Drawer code:

<Drawer classes={drawerClasses} open={showStandardDrawer} anchor={"right"} onClose={closeDrawer}>
{Array.from(items).map((item, index) => {
return (
<List
key={`list-${index}`}
component="div"
aria-labelledby="nested-list-subheader"
subheader={
<ListSubheader component="div" id="nested-list-subheader">
{item.title}
</ListSubheader>
}
className={classes.root}
>
{ item.elements.map((el, index) => {
return (
<React.Fragment key={index}>
<ListItem key={index} button onClick={() => handleExpand(index)}>
<ListItemText primary={el.name} />
{open[index] ? <ExpandLess /> : <ExpandMore />}
</ListItem>
<Collapse in={open[index]} timeout="auto" unmountOnExit>
{ el.descriptions.map((description, index) => {
return (
<List key={`l-${index}`} component="div" disablePadding>
<ListItem button className={classes.nested} >
<ListItemIcon>
<StarBorder />
</ListItemIcon>
<ListItemText primary={description} primaryTypographyProps={{noWrap:true, width:'200px'} } />
</ListItem>
</List>
)})
}</Collapse>
</React.Fragment>
)
})}
</List>
)
})}
</Drawer>

The above snippet showcases the classes applied to the drawer ('drawerClasses'):

{
root: {
maxWidth: '200px',
minWidth: '50%',
width: '50%',
overflow: 'hidden'
},
modal: {
maxWidth: '50%',
minWidth: '50%',
width: '50%'
}
}

Although these styles may not align with my preferences, they serve as an attempt to constrain the Drawer's dimensions independently rather than conforming around its child elements.

Answer №1

Instead of using the modal class, opt for the paper class. The main visible container within the drawer is the Paper element. The root and modal classes are assigned to wrapper elements positioned in a way that their widths do not impact the Paper width.

Check out this code snippet from the Permanent drawer demo:

const useStyles = makeStyles(theme => ({
  drawer: {
    width: drawerWidth,
    flexShrink: 0,
  },
  drawerPaper: {
    width: drawerWidth,
  },
}));

...

      <Drawer
        className={classes.drawer}
        variant="permanent"
        classes={{
          paper: classes.drawerPaper,
        }}
        anchor="left"
      >

Explore the demonstration at https://codesandbox.io/s/zxljh

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

What steps do administrators (coaches) need to take in order to generate a new user (athlete) using Firebase Cloud Functions?

I am currently developing a web application designed for coaches and athletes. The main functionality I am working on is allowing coaches to add athletes to the platform. Once added, these athletes should receive an email containing a login link, and their ...

Client failed to receive the message that was sent through socket.on(...)

Upon checking, it appears that the message was successfully sent to the server: // Function used in ReactJS (client) const sendMessage = () => { const data = { room: room, content: { author: userName, message: message } } ...

The latest dev compile ran smoothly, however, the localhost webpage is not loading

Whenever I try running npm run dev, localhost just seems to keep loading endlessly. The screenshot of the response can be found in image 1: https://i.sstatic.net/HaUA7.png I initially suspected that a recent change might have caused this issue, but after ...

I would like to terminate the property when processing it on a mobile device

<div class="col-sm-24 embed-responsive"> <iframe width="100%" src="http://www.youtube.com/embed/${item.snippet.resourceId.videoId}"></iframe> </div> .embed-responsive iframe { height: 185px; margin-top: -4%; paddin ...

React navigator appears stuck and unable to navigate between screens

I encounter an issue where my app closes when I press the switch screen button, but there is no error output displayed. I have checked for any version discrepancies and found no problem. The function I implemented for the button is functioning as expected ...

What is the best way to make a JavaFX WebView the same size as the entire scene?

My goal is to have a JavaFX WebView that automatically resizes to fit the scene upon startup. Currently, I am focused on setting the initial size properly. @Override public void start(Stage stage) { try { Scene scene = new Scene(createWebViewP ...

Scrolling in Bootstrap 4 Cards conceals the fixed Header

Here's some HTML code featuring Bootstrap 4 elements. It showcases a fixed Header and Footer with scrollable Bootstrap Cards in between. When scrolling, the Headers may be hidden by the Cards. How can you adjust the layout so that the Cards scroll "be ...

What methods can I use to create a navigation bar similar to this design?

Lately, I've noticed a growing trend in navigation bars that are designed to resemble waves. Wavey Navbar I'm eager to incorporate a similar navbar into my website, but I'm struggling to figure out how to do it. I'm working with react ...

HTML5, canvas covering every element

Below is the code that I am working with: <html> <head> <title>canvas</title> </head> <body> <canvas width="1745" height="569" style="width: 1730px; height: 1680px; position: absolute; z-index: 1"></canvas> ...

Avoid using CSS browser prefixes when selecting plugins or addons

I tried searching on Google for a solution, but I couldn't find anything! Is there a trick or library available to avoid repeating CSS extensions for browsers? Instead of using -webkit-, -moz-, and -o- prefixes? -webkit-animation: NAME-YOUR-ANIMATI ...

What are some alternatives to using fetch for making API calls in Next.js?

In my NextJS project, I have an express backend where data fetching is required in getServerSideProps. The issue arises when using await fetch('/api/anyApi'), as it does not work with relative paths, and calling to the absolute path await fetch( ...

How to ensure an image or ad stays in a fixed position on the screen

Is there a way to keep a part of the screen fixed, regardless of what other elements are in that space? Do I need to merge code with the HTML code of the ad or image to achieve this? ...

Javascript code creates a blur effect on webpage bodies

On my webpage, I have multiple elements, including a "change BG" button. When this button is clicked, I want to hide all other content on the page except for the button using JavaScript. Alternatively, clicking the button could blur out all other elements ...

Do not proceed with the form submission if the fields are left blank

I am facing a challenge with organizing two sets of data, "heat" and "cold", obtained from an external provider. The data is messy and I have trimmed down the code to focus on the main issue at hand. Both "heat" and "cold" have properties that users need t ...

Issues persist while attempting to save sass (.scss) files using Atom on a Mac

When attempting to work with sass files, I encountered an error message every time I tried to save the file: Command failed: sass "/Users/bechara/Desktop/<path of the file>/test.scss" "/Users/bechara/Desktop/<path of the file>/test.css" Errno: ...

Issue encountered during file transfer from React to Flask

I'm encountering an issue while trying to upload a file using React and sending it to the Flask backend via a post request. The error message I keep getting is Failed to load resource: the server responded with a status of 400 (BAD REQUEST). I've ...

What is the method for aligning text to the right side within a ListItem?

Currently, I have these elements where the "YoYo" text is aligned to the left by default. However, I am looking to adjust the positioning of some "YoYo" texts so that they can appear on the right side. I attempted to provide a style object with justifyCon ...

Room.kt - The Database_Impl you are looking for cannot be found

I've been facing issues while trying to implement a Database in my new app. Despite numerous searches, I couldn't find a solution, so I'm reaching out here with my problem: FATAL EXCEPTION: main (Ask Gemini) Process: com.basic.app, PID: ...

Gradually bringing a tag into view, gently fading it away, and then altering the text before beginning the cycle anew

I have a situation where an tag's content is being dynamically changed with jQuery and then faded in and out using the Velocity JS library along with the setInterval function. Initially, everything works smoothly for about 30 seconds. However, after ...

Tips for using props as a className with makeStyles

Exploring the world of React for the first time, I am embarking on creating a website using this technology. Within a functional component, I have arranged an image and text content in a 6-column grid layout. The challenge I face is swapping these six colu ...