Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI grid with independent scrolling columns

I'm looking to make multiple scrolling columns using Material UI in React. I had a way of doing it in bootstrap with flex, but I can't get it to translate over. I have put together a demo of a hacky way to do it that requires knowing the size of the contents above what you're trying to scroll (in this case, the AppBar)

https://codesandbox.io/s/pmly895mm

html,
body {
  margin: 0;
  height: 100%;
}

#root {
  height: 100%;
}

.grid-container {
  height: calc(100% - 64px);
}

.grid-column {
  height: 100%;
  overflow-y: auto;
}

In this demo, I set all of the heights to 100% (html, body, #root) and then created two classes grid-container with a height of 100% - AppBar height and grid-column with a height of 100% and an overflow-y of auto.

In Bootstrap, I would apply these classes to the row and column elements respectively

.flex-section {
  flex-grow: 1;

  display: flex;
  flex-direction: column;

  min-height: 0;
}

.flex-col-scroll {
  flex-grow: 1;

  overflow: auto;

  min-height: 100%;
}

And it wouldn't matter what was above the elements because flex took care of the heights.

Specifically, I am looking to avoid having to do height: calc(100% - 64px) as this requires me knowing the element heights beforehand. I'm going to have some pages where I'd like to put some content above the scrolling area that will have dynamically tall content.

like image 203
AKrush95 Avatar asked Sep 19 '18 15:09

AKrush95


People also ask

How do you make a grid scrollable in MUI?

You can scroll to a specific cell by calling apiRef. current. scrollToIndexes() . The only argument that must be passed is an object containing the row index and the column index of the cell to scroll.

How do you make a grid scrollable in react?

By configuring the style. maxHeight property, you can set the Grid in scrollable mode only when its rendered content exceeds certain height limits. If the content does not exceed the set maximum height, the height of the Grid will be the same as the height of its content.

How do I add a scroll bar to a table in MUI?

There are two primary steps to force the horizontal scrollbar to be visible: Set a fixed width on the element that wraps the Table component. I am using a MUI TableContainer in my example. Set the width of the Table to be larger than the width of the wrapping component.


2 Answers

2020 Update

FWIW I was just able to add these two style properties to a Box component to achieve a scrollable <div> column:

<Box style={{maxHeight: '100vh', overflow: 'auto'}}>...</Box>
like image 126
Davis Jones Avatar answered Sep 29 '22 08:09

Davis Jones


In my determination to recreate how I used to do it in bootstrap (https://jsfiddle.net/aq9Laaew/226591/) I was actually able to get it working in Material UI as well. And since I wasn't able to find any sort of examples of this online for this specific use case (React / Material UI scrollable columns), hopefully this will help someone else.

Here is the example: https://codesandbox.io/s/z24wl3n58m

html,
body {
  margin: 0;
  height: 100%;
}

#root {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.flex-section {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.flex-col-scroll {
  flex-grow: 1;
  overflow: auto;
  min-height: 100%;
}

.flex-no-shrink {
  flex-shrink: 0;
}

What I was missing was setting the flex on the root. Once we do that, then we can utilize flex-section, flex-col-scroll, and flex-no-shrink (the latter of which is used to prevent elements above the scrolling from being compressed)

like image 20
AKrush95 Avatar answered Sep 29 '22 07:09

AKrush95