Stopping Accordion Title from Moving Vertically in Material-UI

Just finished creating this accordion which will be used as a menu item.

However, I've encountered an issue where clicking on the Main title causes the accordion summary to move downward vertically.

Any suggestions on how to keep the Main tile fixed while opening?

Check out the sandbox

import React from "react";
import {
  Typography,
  Grid,
  Accordion,
  AccordionSummary,
  AccordionDetails,
  ListItem
} from "@material-ui/core";
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    panelSummary: {
      flexDirection: "row-reverse",
      paddingLeft: "0px"
    },
    heading: {
      fontSize: theme.typography.pxToRem(15),
      fontWeight: theme.typography.fontWeightRegular
    },
    innerMenuItem: {
      paddingLeft: "32px"
    },
    expanded: {
      padding: "0px"
    }
  })
);

export default function App() {
  const classes = useStyles();
  return (
    <Accordion>
      <AccordionSummary
        className={classes.panelSummary}
        expandIcon={<ExpandMoreIcon />}
        aria-controls="panel1a-content"
        id="panel1a-header"
      >
        <Typography className={classes.heading}>Main title</Typography>
      </AccordionSummary>
      <AccordionDetails>
        <Grid container direction="column">
          <ListItem className={classes.innerMenuItem} button key={1}>
            <Typography className={classes.heading}>Sub Item 1</Typography>
          </ListItem>
          <ListItem
            className={classes.innerMenuItem}
            button
            key={2}>
            <Typography className={classes.heading}>Sub Item 2</Typography>
          </ListItem>
        </Grid>
      </AccordionDetails>
    </Accordion>
  );
}

Answer №1

To avoid the summary size changes during expansion, reset the vertical margin when expanded:

V5

<AccordionSummary
  sx={{
    "&.Mui-expanded": {
      minHeight: 0
    },
    "& .MuiAccordionSummary-content.Mui-expanded": {
      // Source: https://github.com/mui-org/material-ui/blob/cc0e2ab63e8be9ec4d51a49bfde17ef28fc77b9c/packages/mui-material/src/AccordionSummary/AccordionSummary.js#L64-L64
      margin: "12px 0"
    }
  }}
>

Link to Example Code Sandbox V5

V4

panelSummary: {
  "&.Mui-expanded": {
    minHeight: 0
  },
  "& .MuiAccordionSummary-content.Mui-expanded": {
    margin: "auto"
  }
},

Link to Example Code Sandbox V4

Answer №2

To implement disabling gutters in version 5 of MUI, simply set the attribute disableGutters to true.

<Accordion disableGutters>
 // Content goes here
</Accordion>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing getStaticProps to receive an incorrect slug value?

Currently, I am encountering an issue with obtaining the correct slug in getStaticProps(). My goal is to retrieve the translated slug for a specific page (for example: the about page). The getStaticPaths() function is able to generate the correct object. ...

Frontend and backend development in IntelliJ: Should you work on one or multiple projects?

My project is divided into two main parts: JVM/kotlin backend (developed with gradle) React/typescript frontend (created with vite) Here is the directory structure of my project: my-project/ frontend/ package.json src/ node_modules/ ba ...

Achieving a 100% width input with Flexbox: Tips and tricks for success

Check out this flexbox form I created using bootstrap 4: https://jsfiddle.net/kkt0k2bs/1/ I have implemented a media query to adjust the form elements for smaller screens. For larger displays, I want the form items to be displayed inline. However, on sm ...

Troubleshooting Deployment Issues on Google Cloud Platform's App Engine - Chunk Loading Error Detected

Our web application frontend is built using React and deployed on Google Cloud Platform App Engine Standard. We have implemented code splitting, where each page is loaded upon user navigation. While the application is performing well, we have encountered ...

Updating individual elements in a ReactJS state array with unique values

Can anyone lend me a hand with this React JS situation I'm facing? Here's the scenario: const [data, setData] = useState([ {name:'a'}, {name:'b'}, {name:'c'}]) In addition to that, I have another array containing 3 ...

Ensure that the divs are properly aligned and construct columns within a single column

I've been working on creating a step bar that needs to adapt to any number of steps provided by the backend. Here's what I have so far: https://i.sstatic.net/cj4qZ.png It looks fine, but connecting the circles with bars would be beneficial. How ...

MUI Group Selector - Array of Arrays

Struggling to create a customized MUI select group using API data. const data = [ { id: '1', name: 'Category 1', services: [ { id: '1', name: 'Service 1', }, ...

I am unable to retrieve the state values from the Redux store

I'm facing an issue with rendering my Store using react and redux. Here is the code snippet from my App.js file where I have dispatched two expenses: import React, { Component } from 'react'; import Header from './Header' import R ...

Ensuring that the empty bubble remains undisturbed, here's a guide on effectively implementing the if

I need assistance with adding an "if condition" to my text field. The condition should prevent the empty bubble from appearing when the send button is clicked. function verifyInput(){ var currentText = document.getElementById("demo").innerHTML; var x ...

Arranging the placement of an arrow within an HTML dropdown menu

I'm struggling with positioning my custom arrow in select elements. Each select has a different width, and I need to ensure that the distance from the right edge of the select to the arrow is consistent across all of them. Currently, I am using backg ...

execute a different function once the animation has finished running with the .bind

When attempting to detect the completion of a CSS animation in order to remove a class, I encountered an issue where using .bind causes the animation end event to trigger even for the other function. You can view the complete code here. How can I ensure ...

How can I create an input field in MUI that restricts input to letters, numbers, and dashes

Is there a way to configure an MUI input field so that it only accepts letters, numbers, and dashes while excluding spaces and symbols such as (*&^%$#@!,.<>{}[])? Specifically, I want to allow characters that wouldn't disrupt a URL, like . Tha ...

What is the best way to develop a parent component tailored for individual pages in order to integrate react context within it?

I need to use a specific react context for certain pages in my project. Instead of creating the context in the root file app.js, I want to create it only for these specific pages. Is there a way to create a parent component (wrapper) that can be placed ar ...

What is the best way to assign multiple classes to components in React 5?

Previously, I was able to assign multiple classes to a component like this: import React from "react"; import { makeStyles } from '@mui/styles'; const useStyles = makeStyles({ image: { borderRadius: "50%", width: 2 ...

Displaying genuine HTML content in a React application using Algolia Instantsearch

After setting up a demo app using React with an Algolia search feature, I uploaded some indices into Algolia. The content consists of raw HTML. Is there a way to display this content as real HTML using Algolia? ...

The one-click button is functional on larger screens, while on mobile devices, only a double click will register

I am facing an issue in angular where a button works perfectly fine with one click on larger screens, such as Macbook. However, on iPhone, it requires a double click to function properly (it works fine on Android too). The alert triggers with a single cl ...

Obtain the text that is shown for an input field

My website is currently utilizing Angular Material, which is causing the text format in my type='time' input field to change. I am looking for a way to verify this text, but none of the methods I have tried give me the actual displayed text. I a ...

Update state value with new data from Redux dispatch

My axios request is handled within the redux actions to ensure re-usability. However, I am facing an issue where I need to fetch data using redux and then update the state with the fetched data. Unfortunately, the data is not being populated in the state a ...

Tips for retrieving data from multiple text boxes in a loop in React js and transmitting it to an API

I am in the process of developing a quiz application where I aim to create multiple question input fields based on the administrator's inputs. For example, if the admin provides 10 questions for the quiz, I will then generate a form within a loop fo ...

Enhance your website with Bootstrap 4 by separating text and images in your carousel

I've been struggling to create a slider similar to the one shown in the image. I attempted using the Bootstrap carousel, but ran into issues with inconsistent image sizes and the text on the left side jumping around before settling in its correct posi ...