Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Material UI ButtonGroup style props not reflecting

enter image description here

I have a Product image and associated buttons displayed inside a Grid container. The Buttons are conditionally rendered based on some state variables. Basically if the product quantity is 0, it displays a Button (ADD TO CART) else a Button Group (with ‘-‘ , ‘quantity’ , ‘+’ buttons).

I want the Buttons to have a positioning slightly inside the bottom image boundary. This is successfully implemented for the single Add to Cart button using the style props, however the same style props passed to the Button group doesn’t reflect the expected behaviour. And hence the button floats back and forth around the image bottom upon interaction. Any fixes appreciated !

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code below:

<Grid container item xs={2} direction="column" alignItems="center">
      <Grid item sx={{ height: '70%' }}>
        <Box
          component="img"
          src={`/assets/images/products/product_3.jpg`}
          sx={{
            width: '100%',
            height: '100%',
            objectFit: 'cover',
            borderRadius: 1.5,
          }}
        />
      </Grid>
      <Grid item>
        {!productInCart ? (
          <Button
            variant="contained"
            style={{
              top: '-10px',
            }}
            onClick={handleAddToCart}
          >
            ADD TO CART
          </Button>
        ) : (
          <ButtonGroup
            variant="contained"
            style={{
              top: '-10px',
            }}
          >
            <Button onClick={handleRemoveFromCart}>-</Button>
            <Button>{productInCart.quantity}</Button>
            <Button onClick={handleAddToCart}>+</Button>
          </ButtonGroup>
        )}
      </Grid>
    </Grid>

>Solution :

I suggest you add marginBottom to the image and remove the style attribute from both the button/button group, so you don’t have double styled, also, if there is a mismatch between the button position and button group position you will see some flickering when switching between them. (like the image you uploaded)

<Grid container item xs={2} direction="column" alignItems="center">
  <Grid item sx={{ height: '70%' }}>
    <Box
      component="img"
      src={`/assets/images/products/product_3.jpg`}
      sx={{
        width: '100%',
        height: '100%',
        objectFit: 'cover',
        borderRadius: 1.5,
        marginBottom: '10px'
      }}
    />
  </Grid>
  <Grid item>
    {!productInCart ? (
      <Button
        variant="contained"
        onClick={handleAddToCart}
      >
        ADD TO CART
      </Button>
    ) : (
      <ButtonGroup
        variant="contained"
      >
        <Button onClick={handleRemoveFromCart}>-</Button>
        <Button>{productInCart.quantity}</Button>
        <Button onClick={handleAddToCart}>+</Button>
      </ButtonGroup>
    )}
  </Grid>
</Grid>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading