Arranging React Grid Items with Stylish Overlapping Layout

Is there a way to create a react-grid-layout with 100 grid points width, while ensuring that the grid items do not overlap?

https://i.sstatic.net/CQiVh.png

(Reducing the number of columns can prevent overlap, but sacrifices the 100-point width resolution for grid placement)

import Button, { ButtonProps } from '@mui/material/Button';
import GridLayout from 'react-grid-layout';

const TempButton = (props: ButtonProps) => (
  <Button
    {...props}
    variant="outlined"
    sx={{
      width: '100%',
      height: '100%',
      '&:hover': {
        backgroundColor: 'primary.dark',
        opacity: [0.9, 0.8, 0.7],
      },
    }}
  />
);

export const DeckLayout = (props: any) => {
  const layout = [
    {
      i: '1',
      x: 0,
      y: 0,
      w: 2,
      h: 2,
    },
    {
      i: '2',
      x: 2,
      y: 0,
      w: 1,
      h: 1,
    },
    {
      i: '3',
      x: 3,
      y: 0,
      w: 2,
      h: 3,
    },
  ];

  return (
    <GridLayout className="layout" layout={layout} cols={100} rowHeight={10} width={500}>
      <div key="1">
        <TempButton>1</TempButton>
      </div>
      <div key="2">
        <TempButton>2</TempButton>
      </div>
      <div key="3">
        <TempButton>3</TempButton>
      </div>
    </GridLayout>
  );
};

Explore this sandbox

Edit: It seems like the issue might be related to CSS. Modifying the stock example shows the expected behavior:

https://i.sstatic.net/7gkhb.png

Check out the sandbox

Answer №1

UPDATE: In a recent post, it was suggested that in order to achieve an accurate mapping of grid units to pixels, the margin={[0,0]} setting is necessary.

Check out the sandbox


It appears that there are some challenges arising from a combination of CSS and computational issues:

  • The default minWidth for MaterialUI buttons is 64px.

  • Calculation shows that the width of columns equals (Gridlayout width) / (Gridlayout cols). Applying this formula in the example above results in a column width of 5 px/col, which conflicts with the minWidth = 64px constraint.

Solution:

width: 100%

height: 100%

<GridLayout
   className="layout"
   layout={layout}
   cols={100}
   rowHeight={10}
   width={6400}
>

https://i.sstatic.net/AdRVG.png

Complete fix:

import Button, { ButtonProps } from "@mui/material/Button";
import GridLayout from "react-grid-layout";

const TempButton = (props: ButtonProps) => (
  <Button
    {...props}
    variant="outlined"
    sx={{
      padding: 0,
      "&:hover": {
        backgroundColor: "primary.dark",
        opacity: [0.9, 0.8, 0.7]
      }
    }}
  />
);

export const DeckLayout = (props: any) => {
  const layout = [
    {
      i: "1",
      x: 0,
      y: 0,
      w: 2,
      h: 2
    },
    {
      i: "2",
      x: 2,
      y: 0,
      w: 1,
      h: 1
    },
    {
      i: "3",
      x: 3,
      y: 0,
      w: 2,
      h: 3
    }
  ];

  return (
    <GridLayout
      className="layout"
      layout={layout}
      cols={100}
      rowHeight={10}
      width={6400}
    >
      <div key="1">
        <TempButton>1</TempButton>
      </div>
      <div key="2">
        <TempButton>2</TempButton>
      </div>
      <div key="3">
        <TempButton>3</TempButton>
      </div>
    </GridLayout>
  );
};

View the updated version on sandbox

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 might be causing the component in Angular and TypeScript to have trouble reading the data returned by the service?

I've been working on this for hours without success. I created a web API get method that returns a simple array. public Hero[] getHeroes() { return new Hero[] { new Hero { Id = 1, Name = "Hunain Hafeez-1" }, ...

Poorly formatted mui table representation

I have utilized the Table component from @mui/material and referenced the documentation at https://mui.com/material-ui/react-table/. To reproduce the issue, I have created a static table with fixed values. Here is the code snippet: <TableCo ...

Disable the borders on HTML input fields

I am looking to implement a search feature on my website. It seems that in Firefox and Internet Explorer, the search function appears fine without any unexpected borders. Firefox IE However, when viewing the search function in Chrome and Safari, there ...

Display alternative text dynamically

Trying to gain a deeper understanding of state in react, this is a perfect example to me. When I say dynamic, I mean displaying the output below instantly as it is entered in the form. class Demo extends Component { constructor(props) { super ...

Having trouble with socket.io functionality after deploying my application on Heroku platform

I encountered an issue with my app when deploying it to Heroku after working fine locally with socket.io. The problem lies in the socket setup on the client side, which is configured for localhost:5000. Unfortunately, I am unsure of how to resolve this. B ...

Issue with Google login functionality post-deployment

I'm currently using the MERN stack and I'm looking to integrate Google login into my web application. Everything functions perfectly fine in development mode, however, once deployed, I encounter an error. The verifyIdToken method requires an ID T ...

Material Design - The element provided is not valid: it should be a string for built-in components or a class/function for composite components, but instead it is an object

How are you today? I am currently working on a React project using Webpack and Babel. I encountered an issue when trying to incorporate Material UI components from https://mui.com/. Whenever I import a MUI component into my project, I receive the followin ...

Maintain the state of the toggle div after page refresh or modification

In this scenario, I am looking to ensure that my div remains open even if the page is changed or refreshed. Below is the HTML and JavaScript code provided: You can view the working code here: http://jsfiddle.net/wasimkazi/fauNg/1/ $(".widget2").hide( ...

The error in Angular states that the property 'length' cannot be found on the type 'void'

In one of my components, I have a child component named slide1.component.ts import { Component, Input, OnInit, EventEmitter, Output } from '@angular/core'; @Component({ selector: 'app-slide1', templateUrl: './slide1.component. ...

Craft a unique parallax effect using Framer Motion's clipping feature

To help visualize my concept, I have sketched it out using Figma. This idea involves a slide transitioning between pages. The goal is to cover the entire page with a sliding effect, and then place a sticker (dubbed the Parallax Box) on top of it. However ...

What is the best way to pass props to a styled component (e.g., Button) in Material-UI

One of my tasks involves creating a customized button by utilizing the Button component with styled components. export const CustomButton = styled(Button)({ borderRadius: "17px", fontWeight: 300, fontSize: ".8125rem", height: &q ...

Remove the ability to select from the dropped list item

Here is the HTML and Javascript code I used to enable drag and drop functionality for list items from one div to another: HTML: <div class="listArea"> <h4> Drag and Drop list in Green Area: </h4> <ul class="unstyle"> & ...

React is not displaying the most recent value

During the initial rendering, I start with an empty array for the object date. After trying to retrieve data from an influxDB, React does not re-render to reflect the obtained results. The get function is being called within the useEffect hook (as shown in ...

Error: Undefined object while trying to access 'injection' property

After updating to React 16.x, I encountered the following error: Uncaught TypeError: Cannot read property 'injection' of undefined at injectTapEventPlugin (injectTapEventPlugin.js:23) at eval (index.js:53) at Object.<anonymous> ...

Error: Property cannot be read after page refresh or modification

Upon refreshing or running the project for the first time, I encounter the error: TypeError: Cannot read property 'statements' of undefined This issue is perplexing as the data renders correctly but it appears that the connection is failing. ...

What is the best choice for the navigation menu: using `<ul>` or `<div>`?

Is it considered unprofessional to construct a navigation menu without utilizing unordered list tags? For instance, opting for <div> elements instead. <div class="navbar"> <a href="#"> Home </a> </div> ...

Styling the pseudo element ::part() on an ion-modal can be customized based on certain conditions

Looking for a solution regarding an ion-modal with specific CSS settings? I previously had the following CSS: ion-modal::part(content) { width: 300px; height: 480px; } Now, I need to adjust the height based on conditions: if A, the height should be lo ...

Having trouble retrieving the necessary data to generate a menu, the getStaticProps function is coming back as undefined

I'm currently working with Next.js 13 & Strapi, and my goal is to create a Menu component utilizing the getStaticProps function. To achieve this, I've implemented a Layout component within the _app.js file, and nested a Menu component inside the ...

What is the best way to display both an employee's name and ID on a single line using CSS and HTML?

I am currently working on an asp.net MVC app and facing an issue with adding employee ID and employee name on the same line, similar to the web design provided. I need to make modifications using HTML and CSS to achieve this. The problem is that I am unab ...

The UseEffect implementation in my Next.js project is causing a delay in my DOM rendering

I'm having trouble understanding something. I want to add a component at the end of my user page that will display travel options for three other users, similar to "friend suggestions". I planned on loading and rendering this component only after the ...