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

Applying a CSS class to multiple instances of a control in ASP.NET

Incorporating the jQuery UI framework into my ASP.NET project is a current challenge I am facing. My approach involves applying CSS to the controls in order to achieve this objective. Specifically, I have multiple GridView controls that need styling. Is t ...

React function does not provide a return value

In my function, I am calculating the current sum of elements. Within my Api, there is a method called Getcoin that works correctly when I log each element during the foreach cycle. However, I am not getting any results in the return statement or console.lo ...

The module "react" does not have a member named "SuspenseList" available for export. Error code: TS2305

Currently diving into the new features of React 18 like SuspenseList and the fresh useId hook, but strangely encountering a recurring error: Module '"react"' has no exported member 'SuspenseList'. TS2305 This is what my pack ...

The useEffect callback is not functioning when using an empty array

I have a problem with my useEffect hook - it keeps running endlessly even though it fetches the data correctly. However, when I try using an empty array as the dependencies parameter to make it run only once, it doesn't work at all. Here is the front ...

Sending user input from search component to main App.js in React

I'm currently working on an app that searches a Movies database API. I have a main fetch function in App.js, and in tutorials, people are using a search bar within this main APP component. I'm wondering if it would be better to create a separate ...

unable to transfer theme to the styled() function in "@mui/system"

Trying to apply a theme like this: declare module "@mui/styles/defaultTheme" { // eslint-disable-next-line @typescript-eslint/no-empty-interface interface DefaultTheme extends Theme {} } ReactDOM.render( <StyledEngineProvider injectFirs ...

When initializing the page, Next.js and i18Next both load all language JSON files

I am currently utilizing Nextjs along with next-i18next for multilingual support. Upon initialization of the page, it loads all language JSON files including en.json, vi.json, ja.json, and more. Is there a way to EXCLUSIVELY load the language JSON file co ...

Shine a light on the input with a captivating outer

Can we replicate the outer glow effect that Safari and other browsers automatically add to an input field without using jQuery? If you want to remove it, check out this article: Thank you! ...

Why do my tablet media queries take precedence over mobile view media queries?

I've noticed that when I view my website on mobile devices, the tablet media queries always seem to override my mobile media queries. Can anyone explain why this is happening? Here is how I have set it up: /* X-Small devices (portrait phones, less t ...

I encountered a problem with my code where the effect callbacks are synchronous to avoid race conditions. I am currently struggling to format it in the desired manner

Despite following the same format as examples, I keep receiving this error message. const response = await axios.get('http://localhost:5000/get-tasks') const dataObject = response.data const arrayOfKeys = Object.keys(dataObject ...

The autoComplete feature in my ReactJs form is not functioning properly

I have a form in React where I would like to enable auto complete so that when users input the same value again, it will be suggested as an auto complete option. Below is the code snippet: <form className={classes.container} noValidate> <Grid c ...

Pause the React rendering process to initiate a redirection

Currently, I am developing a React application using Next.js and facing a specific challenge that requires a solution: There are certain pages in my application that should only be accessible once a particular context has been established. To achieve this ...

Encountering a problem while attempting to initiate a React app with npm start, an error

I am a beginner in web development with React and I recently followed these steps - npm install -g create-react-app create-react-app my-app cd my-app npm start However, I encountered the following error message: E:\Study\React-course\React- ...

The storage system in Heroku

As a newcomer to the world of development, I've been diving into understanding how the Heroku filesystem functions. I recently completed an Express project that utilized multer for uploading images. In production, everything ran smoothly and I had no ...

Eliminate spacing between divs of varying heights

I'm facing an issue with my small gallery of images. Despite having the same width, they vary in height causing the second row to start below the tallest image from the previous row. How can I eliminate these empty spaces while still maintaining the v ...

Problem arises when the DIV elements are not expanding horizontally together

Struggling to create a round corner box using divs. I have been working on it all day and can't figure out what I'm missing. If anyone could help me spot the issue, that would be great. I want the 'header' and 'footer' to exp ...

Is it possible for style sheets to malfunction due to the presence of the TITLE attribute on <link> tags?

We are currently involved in the process of upgrading an outdated corporate intranet. The challenge is that our users primarily rely on IE8 and IE9, while most of our sites were designed to function with compatibility for browsers between IE5 - IE9. While ...

The input box refuses to accept any typed characters

I encountered a strange issue where the input box in the HTML was not allowing me to type anything. const para = document.createElement('p') const innerCard = document.getElementsByClassName('attach') for(let i = 0; i < innerCard.l ...

Capybara's attach_file function is not properly activating the React onChange handler in Firefox

Currently, I am conducting tests on the file upload feature of a React-built page. The page includes a hidden file input field with an onChange event listener attached to it. Upon selecting a file, the onChange event is triggered and the file is processed ...

Utilize JSON categories to assign groups to TextFields or Selects according to a JSON data attribute

I have retrieved multiple JSON groups from an API, each containing one or more questions objects. My goal is to display each question along with its corresponding response in a MUI TextField or Select component, based on the value of QuestionType. Current ...