Reputation: 182
I am creating the front-end of for shopping cart. I have divided the container into two columns (using Grid container and Grid item) and inside each column, I have inserted some components. The left side contains components for each item and the left side contains only one component that shows the Order Summary. This picture illustrates what I am saying:
My question is how to make this left side (Order Summary) sticky at the top of the page when a user scrolls. I tried a couple of things with z-index
and costume CSS-styling (position: fixed
and position: sticky
) but they break the Grid Structure that the Material UI offers. Also considering that the view is responsive it would be great that for mobile screens the Order Summary component stays at the end of the page where it is now:
I could achieve to stick the component on top on desktop but then when I added custom styling the component stuck in the top in the mobile view and looks terrible. Is there any practical way to achieve this that I may have not noticed?
Here is the Sandbox of the project: https://codesandbox.io/s/charming-cherry-eh27f
I hope that this way of dividing the page into columns would be helpful for somebody because I struggled a bit to find something similar online and thought of this way myself this morning. At the same time, I would appreciate any feedback and help for this feature I am trying to implement.
The solution provided by MaCadiz below works totally fine. I just wanted to say that feel free to create any pull requests if you want to add more features to this project. Here is the Github repo link: https://github.com/kleviss/shopping-cart-frontend-react-material
Upvotes: 7
Views: 16696
Reputation: 1807
In your sandbox I realized that you have a nested grid item inside another grid item, I don't know if this was intentional, but I removed it anyway.
<Grid item xs={12} sm={6} md={5} lg={5}>
<Grid item xs elevation={3}> <==== THIS ONE
<OrderSummaryItem />
</Grid>
</Grid>
Then I set the position of the OrderSummaryItem component to sticky and some tweaks and I think it's working fine. Check this fork I made from your sandbox: https://codesandbox.io/s/amazing-darkness-q0o5h
Upvotes: 8