Creating a full-width menu with Material UI - a step-by-step guide

I am currently working on creating a mobile menu that spans the full width of the screen.

Despite my efforts, it seems like I might be misunderstanding something as my CSS rules are not being applied correctly.

Here is a live code example for reference: https://stackblitz.com/edit/react-ucvbgt-sur6ne?file=demo.js

https://i.stack.imgur.com/7d5pQ.png Any help or guidance would be greatly appreciated. Thanks!

Answer №1

Although this question is not recent, I encountered the same issue and managed to find a effective solution which I would like to share here.

The code you are currently using is missing two crucial elements: the marginThreshold property and a maxWidth style. The marginThreshold determines how close to the window edge the popover can be displayed. Additionally, including the maxWidth style ensures that the menu stretches across the full width as intended.

Below is the updated version of your menu component:

  <Menu
    position="relative"
    id="basic-menu"
    anchorEl={anchorEl}
    open={open}
    onClose={handleClose}
    marginThreshold={0}
    slotProps={{
      paper: {
        sx: {
          color: 'red',
          width: '100%',
          maxWidth: '100%',
          left: '0px',
          right: '0px',
        },
      }
    }}
    MenuListProps={{
      'aria-labelledby': 'basic-button',
    }}
  >

In addition to addressing the above issues, it's worth noting that the PaperProps attribute has been deprecated. As a replacement, I've switched to utilizing the new slotProps pattern.

Answer №2

Consider switching your menu layout to a drawer format, specifically with the anchor set to 'top' position.

To deepen your understanding, check out this resource: https://example.com/drawer-layout-guide

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

React Markdown Editor Failing to Display Line Breaks Properly

I am currently utilizing the @mdxeditor/editor to modify a markdown file. I have successfully set up automatic saving of changes to the database every second. Everything is functioning as expected. However, there is an issue: After saving a note in the da ...

Box Pattern with Pop-Up Modal

Check out the grid of squares I've coded below: <div class='square-container'> <div class='square'></div> <div class='square'></div> <div class='square'></div& ...

Combining an image and a link in HTML CSS: Tips for stacking elements on top of each other

I am currently working on adding a hyperlink to an image, so that when the image is clicked it will navigate to the specified link. However, I am having trouble with the formatting of the hyperlink as it is appearing smaller than the actual image. Below ...

What is the best way to ensure that my text always aligns perfectly to the end of the line?

I've been struggling to create a column layout where the text perfectly aligns with the end of the line, but so far, I haven't had any success. Despite my efforts to find a solution online, I have come up empty-handed. What I'm aiming for i ...

Adjust the image's placement and reduce its size as the screen size decreases, without using any media queries

Essentially, I encountered an issue where I had a table row with 2 cells - the left cell containing text and the right one containing an image. As the screen size decreased, I needed the image to move below the text. After some investigation, I delved into ...

Techniques for transferring child properties and values to parent components upon the screen being initialized

Is there a way to pass property values from a child component to a parent component when the react app is first loaded? In my scenario, I have a parent component named app.js that renders a home component containing a JSON component. Initially, upon loadi ...

What is the best way to incorporate a div below an image in Ubergallery?

I have integrated the jquery ubergallery into my website, and I am trying to add a new div below the main photos for additional content such as share buttons and comments. I tried using the code that worked with colorbox, but since ubergallery uses colorbo ...

Unable to adjust the margin-bottom attribute in CSS

Currently, I'm in the process of developing a website and attempting to build the client-side without using any frameworks. However, for some reason, I am facing difficulty in changing the margin-bottom property. My goal is to create space between the ...

What steps should I take to fix the `Request is not defined` ReferenceError that keeps popping up whenever I try to execute the npm start command?

I keep getting an error that says 'ReferenceError: Request is not defined' every time I try to run npm start in my Node.js environment. The issue seems to be related to the file path "node_modules/@expo/server/src/environment.ts". Here's a ...

Drift above a pair of elements

Explaining my issue is a bit complex, so I'll provide an example: I aim to place a floated element (the blue box) over two other elements (red and green), all within a fixed-width and centered layout (achieved by the black border box). Additionally, ...

Retrieving the initial data from a fetched JSON file in React

I'm struggling to extract the data from the initial object in a JSON document. const apiUrl = 'https://uniqueapi.com/data.json?limit=1'; function App() { const [items, setItems] = useState([]); useEffect(() => { async functio ...

Sliding out menu using AngularJS

I have been attempting to create a slide out menu without success. My goal is for the text (home,users) to appear when the >> button is clicked. I seem to be stuck and can't figure out what I'm missing. This project also marks my initial ve ...

Obtaining a background image within a keyframe

I have been experimenting with keyframe animations and am looking to incorporate multiple images into the animation. Specifically, I want to switch out the image every 10% increment to depict someone running. Despite my efforts to use a background-img in ...

Troubleshooting: Unable to preview Facebook Page plugin

When I visit the Facebook developer site to get the code for a Facebook page plugin, I use this link. The preview feature works perfectly with pages like "facebook.com/Nike", but when I try it with my own page, "facebook.com/BargainHideout", it doesn&apos ...

Is misbehavior expected at approximately 600 pixels in width?

What is happening with my website when the width is minimized, and how can I resolve it? Issue The website appears fine on mobile devices and full screens, but at reduced sizes, the background image disappears and the top bar behaves strangely (with the l ...

Creating a specific order for Arabic strings in HTML is simple and can be done by utilizing

I need to adjust the ordering of a datetime written in Arabic numbers. I must use an Arabic string and currently have the following C# code written in Arabic: std.InnerText = "17-02-2016"; The current output is in Arabic numerals, displayed as ٢٠١٦- ...

How to remove the horizontal scrollbar from material-ui's Drawer element

My drawer is displaying a horizontal scroll, despite my efforts to prevent it. I've tried adjusting the max-width and width settings in Menu and MenuItems, as well as using box-sizing: border-box. I also attempted setting overflow: hidden on the Drawe ...

Unable to retrieve push token for the device. Please verify the validity of your FCM configuration

Looking for a solution to the issue of obtaining a push token Error: Unable to retrieve push token for device. Ensure that your FCM configuration is correct I have integrated Expo permissions and notifications in my React Native app, but nothing seems to ...

Responsive SmoothSlider

Need help fixing the responsive breadcrumbs in my slick slider. The slick-content-slider is taking a strange height and I want to ensure that every div remains on the same line. Thank you in advance! $('.slick-content-slider').slick({ slides ...

Divide the parsed rss findings into separate parts

I am in the process of developing a project that involves aggregating job listings from various websites by parsing their RSS feeds. I am utilizing rss-parser for this purpose. Each feed has its own format for the title field, resulting in varying structu ...