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

When using nextjs, there is an issue that arises when attempting to define the property 'id' using context.params.id. This results in

Attempting to retrieve data from a JSON file (response.json) hosted on localhost:8000/response.json. Below is the code snippet from pages/test.js: import React from "react"; import Link from "next/link"; import Image from "next/ima ...

Press the [Submit] button to conceal DIV Container 1, reveal DIV Container 2, and populate DIVs discovered in a .PHP file within DIV Container 2

Trying to achieve the following steps: 1 - Press Submit Button 2 - Conceal DIV Container 1 3 - Reveal DIV Container 2 4 - Load and display all DIVs from "PricingDisclaimer.php" into Div Container 2 DIV Code snippet: <div id="MainContainer"> &l ...

Is there a way to retrieve two distinct values from an object?

Is there a way to retrieve two target values from an option using useState in react/nextjs? Here is a sample of my api: const movies = [ { id: 1, name: "Fight Club" }, { id: 2, name: "Titanic" }, { ...

ChakraUI ForwardRef in React not applying variant to child component

Encountering an issue with the combination of forwardRef and Input from ChakraUI. Attempting to create a generic input component with a flushed variant, but facing difficulty as Chakra keeps resetting it back to default. ModalInput Component import { for ...

Are Bootstrap Input groups inconsistent?

Hey there! I've been working on the sign-in example, but I seem to have hit a roadblock. In my local setup, the top image is what I see in my browser after running the code, while the desired layout that I found on the Bootstrap site is the one below ...

Examples of merging responsive design and equal height columns in CSS:

As a beginner in CSS, I am facing challenges in creating a responsive layout that adjusts based on the screen it is viewed on and achieving equal heights in columns. While I have been able to address these issues individually, combining them has proven to ...

Best practices for using Mock Service Worker to simulate fetch requests in a custom hook

My goal is to conduct testing on my application where users input a query that triggers a request to the Google Books API, followed by displaying the response. To achieve this, I've modularized the data fetching logic into a custom hook. The challenge ...

Why doesn't next-intl offer a feature to define default messages within components?

Why is it that next-intl (along with certain other react translation tools) choose not to offer a direct way to define default messages within components? For instance: The usage of next-intl looks like this: /* get the t function from a hook or elsewher ...

Editable Material Table in React with Date Picker and Column Warnings

I have been working on enhancing material-table functionality. My goal is to incorporate additional columns into the data table. One of the requirements is to introduce a date time picker in an editable column, while keeping another column non-editable. T ...

Modify the border color for the focus state in the Select Component of Material-UI library for React

I am currently attempting to modify the appearance of the Select Component when it is in focus, specifically from the Material-UI library. To clarify, I am referring to the appearance when an element within the dropdown has been clicked (the on-focus state ...

Is the absolute positioned element on the left or right based on its current location?

Is there a way to assign absolute positioning with left at 0px or right at 0px depending on whether the positioned div will go outside of its container? For example, when you right click in a browser and see the menu appear to the right of where you click ...

What is the purpose of using square brackets in a CSS style declaration?

I've recently taken over the management of a website and I came across some unfamiliar CSS syntax. Has anyone encountered this type of syntax before? Can someone explain what the square brackets inside a style represent? Thank you, Rob .trigger-tx ...

What is the best way to retrieve an API response after submitting data?

Currently working on a React website with a node.JS REST API, I'm facing a challenge that I need help with. During the login process, my React form sends a POST request to my API. Since this is a School Project and I am working locally, my React app ...

Utilize JavaScript to apply the CSS -moz-transition

Creating a web application and using CSS3 to transform a div, but encountering a challenge with Firefox. Able to make Chrome, Opera, and IE work properly, except for Firefox. Here's how I'm setting up the working browsers: obj.style.WebkitTrans ...

The true meaning of CSS3 transition is often misconstr

I'm trying to create a simple linear transition effect, but for some reason it's not working. Any suggestions on what might be causing the issue? HTML <div class="button-modern" style="background-color:#e73032"> <span style="color: ...

I am facing difficulties in adding dynamic content to my JSON file

I've encountered a challenge in appending new dynamic data to a JSON file. In my project, I receive the projectName from an input form on the /new page. My API utilizes node.js's fs module to generate a new JSON file, where I can subsequently add ...

What is the best way to adjust the spacing between components to prevent overlapping?

I'm looking to adjust the spacing between the textfield and button components so they don't overlap. I want to create some space between them but I'm not sure how to achieve this. I'd like to have at least 1-2 spaces added between the ...

The function getById() does not exist

Whenever I attempt to imitate a click, an error in the subject line pops up. This is my complete test file: import '@testing-library/jest-dom' import * as React from 'react'; import TestRenderer, {act} from 'react-test-renderer& ...

Flexbox - managing wrapping on legacy devices

Utilizing flexbox, I have successfully designed a 2 column layout with a div at the top spanning both columns for a menu format. While this works flawlessly in Chrome and iOS7, it appears to be ineffective in outdated versions of Safari. In this fiddle, yo ...

A guide on how to locate a specific marker in react-native-maps using its unique

I've been doing some digging, but I'm struggling to find a solution for extracting the coordinates of a react-native-maps marker based on the provided ID. Unfortunately, I keep encountering an error message that says "undefined is not an object." ...