7

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:

current view of the shopping cart on the desktop

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:

current view of the shopping cart on the mobile

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.

UPDATE

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

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
klevisx
  • 175
  • 1
  • 2
  • 12

2 Answers2

8

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

MaCadiz
  • 1,699
  • 9
  • 11
  • 1
    Awesome. Yea actually that was intentional but that doesn't make sense to have another nest grid when there will be only one element on the right side. Surely that could have broken the styling I was trying to add. Your solution is exactly what I was looking for. Next time I need to be more careful when nesting those Grids – klevisx Dec 05 '20 at 07:38
  • Great answer. How would you handle it if there is also Appbar ? [https://codesandbox.io/s/trusting-hellman-qg3ml](https://codesandbox.io/s/trusting-hellman-qg3ml) Because now the right component is moving as we scroll – filo Feb 24 '21 at 11:23
  • 1
    You can use the 'calc()' in the css of the doc. Check this out https://codesandbox.io/s/strange-browser-440y1 – MaCadiz Feb 24 '21 at 15:54
-5

Try position:"fixed" if position:"sticky" is not working.

Elchin Guseynov
  • 409
  • 4
  • 8