How to Add a Scroll Bar to a Component in React

How to add a scroll bar to a component in React?

Try adding overflow-y: scroll; on the middle component

const items = [...Array(100)].map((val, i) => `Item ${i}`);
const App = () => ( <div className="container"> <div className="left-col"> Left col </div> <div className="center-col"> <span>List</span> <ul> {items.map((item, i) => (<li key={`item_${i}`}>{ item }</li>))} </ul> </div> <div className="right-col"> Right col </div> </div>);
ReactDOM.render(<App />, document.getElementById('react'));
.container {  display: flex;  flex-direction: row;  height: 100vh;}
.left-col { flex: 1; display: flex; justify-content: center; align-items: center; background: #ddd;}
.center-col { flex: 1; background: #aaa; overflow-y: scroll;}
.right-col { flex: 1; display: flex; justify-content: center; align-items: center; background: #e7e7e7;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>

How to add scroll bar to a react component?

Add scroll-like

<div style={{overflowY:"scroll"}}>
</div>

Adding a component in React results in horizontal scroll bar

Since you are using w-screen, the navbar and welcome components will have a with of 100vw. Which is exactly the with of the viewport. You have set h-screen on the welcome component, so that one will fill the viewport completely. Now if you add the navbar, the height of the total page will exceed the hight of the viewprot, so it will add a sidebar on the right. This makes the actual area to fit the components in smaller, so your components are to wide for the window, that’s why the vertical scroll bar appears.

You can fix this by replacing w-screen with w-full, which makes components take 100% of the available space, rather than fixed width based on the width of the viewport.

See the different descriptions of the classes in the tailwind docs.

Hope this helps.

How to add a scroll bar when the content of the div is overextended? React Native

There are no div tags in ReactNative.
What you need is just to use a ScrollView component with appropriate height.

Show/hide scrollbar in react component

I'm not sure if this helps but you can use overflow: 'overlay' in your CSS Properties for the scrolling component. This makes the scroll bars not affect your content width and you can adjust your margins accordingly.

You can also use the the property overflow: 'auto' which will show your scrollbars only when the content inside the container is overflowing.

How to add a fancy scroll-bar on react material-table?

Without using CustomScroll, you can do this by setting minimum(minBodyHeight) and maximum(maxBodyHeight) viewport height in the options like this:

import React from 'react';

import MaterialTable, { MTableToolbar } from 'material-table';

import { Grid } from '@material-ui/core';

export default function App() {

return (
<MaterialTable
options={{
search: true,
paging: false,
toolbarButtonAlignment: "left",
searchAutoFocus: true,
minBodyHeight: "85vh",
maxBodyHeight: "85vh"
}}
title="Toolbar Overriding Preview"
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{ title: 'Birth City', field: 'birthCity', type: 'numeric' }
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
]}
components={{
Toolbar: props => {
// This will let you use your own Title while keeping the search
const propsCopy = { ...props };
// Hide default title
propsCopy.showTitle = false;
return (
<Grid container direction="row">
<Grid item xs={6}>
<h2>Your Own Title</h2>
</Grid>
<Grid item xs={6}>
<MTableToolbar {...propsCopy} />
</Grid>
</Grid>
);
}
}}
/>
)
}

Update:

Install react-custom-scrollbars.

Save this code into the same directory as test.css file:

.App {
height: 800px;
}

Then follow the code below:

import React from 'react';

import MaterialTable, { MTableToolbar } from 'material-table';

import { Grid } from '@material-ui/core';

import { Scrollbars } from 'react-custom-scrollbars';

import './test.css';

const renderThumb = ({ style, ...props }) => {
const thumbStyle = {
borderRadius: 6,
backgroundColor: 'rgba(35, 49, 86, 0.8)'
};
return <div style={{ ...style, ...thumbStyle }} {...props} />;
};

const CustomScrollbars = props => (
<Scrollbars
renderThumbHorizontal={renderThumb}
renderThumbVertical={renderThumb}
{...props}
/>
);

export default function App() {

return (
<div className="App">

<CustomScrollbars>

<MaterialTable
options={{
search: true,
paging: false,
toolbarButtonAlignment: "left",
searchAutoFocus: true,
}}
title="Toolbar Overriding Preview"
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{ title: 'Birth City', field: 'birthCity', type: 'numeric' }
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
]}
components={{
Toolbar: props => {
// This will let you use your own Title while keeping the search
const propsCopy = { ...props };
// Hide default title
propsCopy.showTitle = false;
return (
<Grid container direction="row">
<Grid item xs={6}>
<h2>Your Own Title</h2>
</Grid>
<Grid item xs={6}>
<MTableToolbar {...propsCopy} />
</Grid>
</Grid>
);
}
}}
/>
</CustomScrollbars>
</div>
)
}

Update 2

This is another way you can add a scrollbar on the table body, for that you've to override the table body component, if you do that body will be messed up with the header. That's why you've to override the table header as well and do some styling. Here is the code:

import React from 'react';

import MaterialTable, { MTableToolbar, MTableBody, MTableHeader } from 'material-table';

import { Grid } from '@material-ui/core';

import { Scrollbars } from 'react-custom-scrollbars';

const renderThumb = ({ style, ...props }) => {
const thumbStyle = {
borderRadius: 6,
backgroundColor: 'rgba(35, 49, 86, 0.8)'
};
return <div style={{ ...style, ...thumbStyle }} {...props} />;
};

const CustomScrollbars = props => (
<Scrollbars
renderThumbHorizontal={renderThumb}
renderThumbVertical={renderThumb}
{...props}
/>
);

export default function App() {

return (

<MaterialTable

options={{
search: true,
paging: false,
toolbarButtonAlignment: "left",
searchAutoFocus: true,
cellStyle: { minWidth: '100px', maxWidth: '100px' },
}}
title="Toolbar Overriding Preview"
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{ title: 'Birth City', field: 'birthCity', type: 'numeric' }
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
]}
components={{
Toolbar: props => {
// This will let you use your own Title while keeping the search
const propsCopy = { ...props };
// Hide default title
propsCopy.showTitle = false;
return (
<Grid container direction="row">
<Grid item xs={6}>
<h2>Your Own Title</h2>
</Grid>
<Grid item xs={6}>
<MTableToolbar {...propsCopy} />
</Grid>
</Grid>
);
},
Body: props => {

return (
<CustomScrollbars style={{ height: '650px' }}>
<MTableBody {...props} />
</CustomScrollbars>

)
},
Header: props => (
<div>
<MTableHeader {...props} />
</div>
)
}}
/>
)
}


Related Topics



Leave a reply



Submit