The dropdown in the Material UI GridList appears excessively wide

Currently, I'm attempting to design a grid-shaped drop-down menu in Material UI. Most of the dropdowns available in the documentation are either a single column or a single row. I tried utilizing a menu component that includes a GridList, which almost meets my requirements. However, the grid appears too wide with excessive space between columns. Despite exploring alternatives like GridListItem and Grid container along with GridItem, the issue persists.

   <Menu
        anchorEl={anchorEl}
        keepMounted
        open={open}
        onClose={handleClose}
        elevation={0}
        getContentAnchorEl={null}
        anchorOrigin={{ vertical: "bottom", horizontal: "left" }}
        transformOrigin={{ vertical: "top", horizontal: "left" }}
        TransitionComponent={Fade}
      >
        <GridList cellHeight={48} cols={2}>
          <Button onClick={(e) => handleClose(LayoutIcons.L_1x1)}>
            {GetGroupIcon(LayoutIcons.L_1x1)}
          </Button>
          ...
        </GridList>
      </Menu>

Explore CodeSandbox for a sample grid dropdown

Upon further investigation, I discovered that the width of the grid is determined by the sum of all element widths due to the use of flex display (in this case it's 6 elements). My objective is to create a flexible grid layout with any number of elements and columns while maintaining adequate spacing between items. In my current example, the two columns allocate 50% width to each element, resulting in overly wide elements.

I am seeking a tailored solution within Material UI that allows me to adjust the width of grid elements to achieve optimal spacing similar to when I set the number of columns to 6. I welcome modifications using GridList or Grid, or any other approach involving Material UI components.

Answer №1

To create a structured layout, use block elements like div. Enclose each row within its own block element and add margins for spacing if required.

<div>
  <div>
    <Button onClick={(e) => handleClose(LayoutIcons.L_1x1)}>
      {GetGroupIcon(LayoutIcons.L_1x1)}
    </Button>
    <Button onClick={(e) => handleClose(LayoutIcons.L_2x2)}>
      {GetGroupIcon(LayoutIcons.L_2x2)}
    </Button>
  </div>
  <div>
    <Button onClick={(e) => handleClose(LayoutIcons.L_5x5)}>
      {GetGroupIcon(LayoutIcons.L_5x5)}
    </Button>
    <Button onClick={(e) => handleClose(LayoutIcons.L_5x5)}>
      {GetGroupIcon(LayoutIcons.L_5x5)}
    </Button>
  </div>
  <div>
    <Button onClick={(e) => handleClose(LayoutIcons.L_5x5)}>
      {GetGroupIcon(LayoutIcons.L_5x5)}
    </Button>
    <Button onClick={(e) => handleClose(LayoutIcons.L_5x5)}>
      {GetGroupIcon(LayoutIcons.L_5x5)}
    </Button>
  </div>
</div>

https://codesandbox.io/s/square-menu-grid-example-forked-undfo?file=/demo.tsx

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

Prevent the layout from shifting with CSS

I am faced with a challenge regarding a grid of images that are dynamically generated on the screen. The number of images in each row of the grid is dependent on the size of the page. While everything functions properly when the page is at a certain size, ...

Trigger a series of functions upon clicking with ReactJS

Need some assistance with an alert message functionality. I have a button labeled Checkout, and when clicked, it should clear the cart and display an alert. At present, the individual functions work as expected - calling emptyCart() works fine, and calling ...

Bootstrap along with ROR 3.2.21 has a malfunctioning navbar

I am facing an issue with the Home section in the navbar. It used to work perfectly, but suddenly it stopped displaying as intended. I have checked and confirmed that both boostrap.min.js and boostrap.min.css are loaded properly. I have included boo ...

Adding an image within the body of text in a Django model, where both the text and image coexist

I am currently seeking a method to seamlessly insert an image within the text of my Django-powered blog. My goal is to achieve a layout similar to the one showcased in this example: https://i.stack.imgur.com/cFKgG.png The desired layout consists of two c ...

What is the correct way to accurately determine the width of a block being displayed with the flex-direction:column property?

For instance: HTML console.log($('.container').width()); // 600px as we defined in css. console.log($('.list').width()); // 600px console.log($('.box').eq('-1').position().left + $('.box').outerWidth( ...

What is causing this faint red line to persist within the parent container?

https://i.sstatic.net/U3dyK.png How can I get rid of the red line on the left side that is not disappearing, even though I've set the child div to white with a width of 50%? body{ height: 100vh; position: relative; } .main-container{ width: ...

Both of the radio buttons in Material-UI have been selected

I have a unique document that showcases an implementation of RadioGroup, FormControlLabel, and FormControl. Take a look at the code snippet below for reference. import React from 'react'; import PropTypes from 'prop-types'; import Radio ...

Leveraging npm packages within a Meteor project through cosmos:browserify

Trying to implement Radium, a JavaScript library for inline CSS, by following the instructions located here. In my app.browserify.js file: Radium = require("radium"); Within package.json: "radium": "0.13.4" Upon attempting to utilize Radium in the app&a ...

Retrieving User's Theme Preference from Local Storage in Next.js Instantly

As mentioned in various other responses, such as this one, Next.js operates on both the client and server side, requiring a guard to properly fetch from localStorage: if (typeof localStorage !== "undefined") { return localStorage.getItem("theme") } else ...

jQuery SlideDown Navigation

Is there a way to implement jQuery code that will display dropdown menu options when the Trials link is clicked? Update: jQuery(document).ready(function () { $("a[href='http://sheep.local/cms/trials']").click(function(e){ e.prevent ...

The collapse component from bootstrap is not visible due to an overlapping button

I am currently working on a responsive calendar featuring full-width buttons for events, accompanied by additional information displayed using the collapse component below. However, I am facing an issue where the button overlaps with other elements, preven ...

Every time I refresh the app, I am consistently redirected back to the home route "//"

I'm facing an issue where, after logging in successfully, I get redirected to the homepage. However, when I refresh the page from any route other than the homepage, such as "/products", I always end up getting redirected back to "/". This is what my ...

React - modeling technique

As I explore some React code in my learning journey, I've encountered the shape method within PropTypes. From what I gather, this method is used to verify if a given value aligns with a specific structure that we define as an argument. However, I&apos ...

Is it possible to activate a block display for an article based on the class value when a radio

Here is the HTML code snippet I'm working with: <div id="contentapp"> <input type="radio" name="menu" id="interest" checked> <input type="radio" name="menu" id="about"> <div id="tab"> <label for="intere ...

Error: Unable to execute this.context.router.push due to it not being a function

I recently encountered an issue where the browser console displayed the same message as the post title. The problem occurred when trying to navigate to a profile page after clicking a dropdown button. contextTypes: { router: React.PropTypes.func }, _h ...

What is the most effective way to transform values into different values using TypeScript?

If I have a list of country codes and I want to display the corresponding country names in my component, how can I achieve this using props? interface MyComponentProps { countryCode: 'en' | 'de' | 'fr'; } const MyComponent: ...

Create a DIV element that completely fills the vertical space available

My webpage is currently looking quite empty, with only one DIV element present: <div style="height: 20%; min-height: 10px; max-height: 100px; width: 100%; background-color: blue;"></div> I' ...

Reviewing and Implementing React and Material-UI Autocomplete for Selecting Multiple

Having trouble using Formik and MUI Autocomplete with multiple items. Specifically, when searching for "Pencil", it doesn't highlight the item. Also, you can select it twice by clicking on it. Desired outcome: Being able to select one or more items. ...

Having numerous calls to isNaN within a JavaScript if-else statement

I've created a JavaScript function that generates div elements and styles them based on user input values. However, I'm facing an issue when trying to display a warning message if the input is not a number. Currently, I'm unable to create th ...

Limit the ability to zoom in a webview application

I am facing an issue with my Android WebView app that is designed to load a specific URL and display it along with its links within the app. Despite setting the webpage size to 720px x 1280px using CSS to fit the screen of a Galaxy S3, I am encountering di ...