What is the best way to create a scrollable Material UI modal and dialog?

Having a problem with my mui modal where the top content is getting cut off and I can't scroll up. I've tried adding the {overflow:"scroll"} property to the modal but it's not working. Here's the code snippet I'm currently using:

  <Modal
    open={viewCreateSegmentModal}
    onClose={() => handleCreateSegmentModal(false)}
    sx={{ 
    overflow:'scroll',
    height:'100%'}}
  >
    <div style={{overflow:"scroll"}}>
      <CreateSegmentModal
        modalControl={(value) => handleCreateSegmentModal(value)}
      />
      </div>
  </Modal>

https://i.stack.imgur.com/S7HYG.png

https://i.stack.imgur.com/ehfPw.png

Any thoughts on how this issue can be fixed?

Answer №1

Consider placing your Modal content inside a Box element instead of a div, and applying the overflow: scroll to that specific Box - example provided below:

  <Modal
    open={viewCreateSegmentModal}
    onClose={() => handleCreateSegmentModal(false)}
  >
        <Box className="modalBox" sx={{position: "absolute", overflowY: "scroll", maxHeight: "90%"}}>

         <CreateSegmentModal
           modalControl={(value) => handleCreateSegmentModal(value)}
         />
      </Box>
  </Modal>

Answer №2

I encountered a similar issue with the Modal component in Material UI. In addition to the suggestions provided by others, I implemented the following solution. It's worth noting that I also have a styles-in-js configuration to ensure the modal appears in the center of the window. This was done using MUI 5.8.x with React. Here is my approach:

...
import { Modal, Box, ... } from '@mui/material';
...

const formStyle = {
    // Adjusting positioning for larger height viewports - centered
    position: 'absolute', 
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)',
    // Other relevant styles...
    width: '52%',
    maxWidth: '600px',
    minWidth: '270px',
    boxShadow: 24,
    py: 4,
    borderRadius: 3,
    zIndex: 100,
    // Media query at the maximum desired height (in my case, just before cutoff) - set top to '0' and adjust y-coordinate accordingly
    '@media(max-height: 890px)': {
        top: '0',
        transform: 'translate(-50%, 0%)'
    }
};

const MyCustomModal = () => {
  ...
  return (
    ...
      // Enable scroll overflow for the modal
      <Modal sx={{overflowY: 'scroll'}} disableScrollLock={false} ... >
        // Inner div (MUI Box) styled as a 'form' using the defined style
        <Box sx={formStyle} component="form" ... >
          ...
        </Box>
      </Modal>
    ...
  );
}

export default MyCustomModal;
  

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

Removing single quotes and replacing them with spaces when passing data from JavaScript to MySQL

I'm currently focused on JavaScript using Hapi.js in my role at the company. My main task involves retrieving data from MongoDB and transferring it to a MySQL database. The challenge arises when dealing with data that contains single quotes within th ...

Using Javascript and jQuery, populate a dropdown menu with options that are extracted from a different dropdown menu

These are my two <select> elements: <select class="js-select-1"> <option disabled>Starting point</option> <option>A</option> <option>B</option> <option>C</option> <option>D</op ...

I am attempting to rebuild Vuex's Getting Started example by utilizing multiple components, yet I am struggling to understand how to call root methods from within these components

The project I'm working on can be found here. It is a simple one, but for the purpose of learning, I divided it into index and four js files (parent, child, root, and store). My challenge lies in figuring out how to call the increment and decrement ro ...

Attempting to conditionally apply CSS to a component based on a prop, but unfortunately it is not functioning as expected

.storyMobile{ color:green; } .storyWeb{ color:red; } <div class="storyMobile"> hii </div> <div class="storyWeb"> hii </div> //main view <div> <story :story="stories[0]"/> </div> //here it prints ...

Unable to locate specified POST endpoint in Node.js server

I currently have a MongoDB database in Mlab with two collections. I'm working on creating a POST endpoint where users can submit comments entered in a comment box. However, I seem to be encountering an issue as testing the endpoint with Postman result ...

AJAX does not execute all inline JavaScript code

I am currently using AJAX to load a fragment of a document. I have successfully loaded 'external' scripts, but I am encountering issues when trying to execute all the JavaScript within <script> tags. Below is an example of the HTML fragmen ...

Creating a full-screen background image in React and updating it with a button

This issue has been consuming a lot of my time, and though I know the answer must be out there somewhere. My problem is quite similar to what's being discussed here. Essentially, I am trying to dynamically change the background image on button click b ...

Is it possible to execute a function when the AJAX request is successful and returns a status code of

I am looking to implement the success function to only run a certain function if the status code is 200. I have come across this example: $.ajax ({ success: function(data,textStatus,jqXHR){ external(); } )}; However, I have not found a clear ...

javascript - developing a neutral constructor

Similar Question: Constructors in Javascript objects I am exploring the concept of creating classes in JavaScript. I am struggling to grasp it fully. Now, I am curious if it's possible to create a constructor in JavaScript, similar to what can b ...

Using flex and align properties to position two elements on the right and one on the left is a common layout challenge

I'm facing an issue with element positioning and text alignment. The navigation container consists of three elements: logo, title, and nav-list. You can find the code on CodePen. /**{ margin: 0; padding: 0; box-sizing: ...

Unable to utilize the namespace 'RouteComponentProps' as a specified type

When using TypeScript to define an interface that extends RouteComponentProp, I encountered some issues: VSCode error: [ts] Cannot use namespace "RouteComponentProps" as a type. Console error: Cannot use namespace 'RouteComponentProps' as a typ ...

The process of incorporating types into Node.js and TypeScript for handling req and res objects

Check out the repository for my project at https://github.com/Shahidkhan0786/kharidLoapp Within the project, the @types folder contains a file named (express.d.ts) where I have added some types in response. The (express.d.ts) file within the @types folde ...

What is the best way to shrink or enlarge individual sections of the accordion?

Exploring AngularJs Accordions I'm struggling to understand how to control accordions in AngularJS. I have a step-by-step module set up as an accordion and I want to collapse one part while expanding another based on completion. Here is the accordion ...

The 'xxx' type does not have an index signature, so the element is implicitly assigned an 'any' type

I'm currently facing an issue with TypeScript. The error message I'm encountering is related to the following section of code: The Interface: export default interface IUser { username: string; email?: string; isActive: boolean; group: s ...

Testing the Material UI autocomplete component with Google Maps integration using React Testing Library is an important step in

Recently, I've been working on creating a test for the Material UI autocomplete component. I found a similar implementation in this demo, and attempted to troubleshoot an issue by referring to this query. Unfortunately, I wasn't able to find a so ...

Potential dangers involved in sending HTML content through an AJAX request over a secure HTTPS connection

I am exploring the idea of dynamically loading HTML content, such as updating a Bootstrap modal dialog's contents using AJAX calls instead of refreshing the entire page. However, before I proceed, I want to understand the potential risks involved and ...

How can you ensure that text inside a tag is styled as italic using font-style: italic and the !important keyword in HTML and CSS?

I've created an HTML site using chordpro.php generator, and the specific aspect related to this query is as follows: <div class="song"> <table border=0 cellpadding=0 cellspacing=0 class="songline"> <tr class="songlyricchord"><td& ...

What is the best way to retrieve an input value and store it as a variable in AngularJS?

Is there a way to properly assign an input value to a variable in AngularJS without errors? Currently, I have a working test version on my page, but it involves using both JavaScript and jQuery alongside AngularJS. I'm looking for a cleaner solution w ...

The pagination feature in React MUI DataGrid is malfunctioning

I am currently working with a datagrid and implementing server-side pagination. I have set a limit of 5 objects to be returned from the backend, and an offset variable to indicate from which index the next set of data should come. const handlePageChange = ...

Utilizing AngularJS to make an API call with $http method and handling a

I am attempting to make a call to my webservice using "$http". When I use "$ajax", it works fine. Here is an example of jQuery $Ajax working correctly: $.ajax({ type: "Get", crossDomain: true, contentType: "application/json; chars ...