The MUI Modal is too large for the display

I'm using a Modal component from MUI in my project and I am facing an issue with displaying a large JSON object inside the modal.

The problem is, the top of the modal extends beyond the screen height and even after making it scrollable, I am unable to scroll up to view the content at the top.

Below is the code snippet for my React component:

const modal = (task_definition: string) => {
  const [open, setOpen] = React.useState(false);

  const handleOpen = () => { 
    setOpen(true);
  };
  const handleClose = () => setOpen(false);

  const style = {
    position: 'absolute' as 'absolute',
    top: '50%',
    left: '50%',
    transform: 'translate(-50%, -50%)',
    width: '75%',
    bgcolor: '#333333',
    border: '2px solid #000',
    boxShadow: 24,
    p: 4
  };

  return (
    <div>
      <Button onClick={handleOpen}>Describe Task</Button>
      <Modal
        open={open}
        onClose={handleClose}
        aria-labelledby="modal-modal-title"
        aria-describedby="modal-modal-description"
        style={{ overflow: "auto" }}
      >
        <Box sx={{...style, overflow: "auto"}}>
          <Typography id="modal-modal-title" variant="h6" component="h2">
            Task Definition Described
          </Typography>
          <Typography id="modal-modal-description" sx={{ mt: 2 }} component={'span'}>
               {prettyJson(task_definition)}
          </Typography>
        </Box>
      </Modal>
    </div>
  );
} 

Can you suggest which CSS property I should adjust to ensure that my modal fits properly within the screen?

Answer №1

To fix this issue, you'll need to make adjustments to the modal's position so that it fits within the screen height and remains scrollable.

import React from 'react';
import { Button, Modal, Box, Typography } from '@mui/material';

const CustomModal = ({ task }) => {
  const [open, setOpen] = React.useState(false);

  const handleOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  const modalStyle = {
    position: 'absolute',
    top: '50%', // Center vertically
    left: '50%', // Center horizontally
    transform: 'translate(-50%, -50%)',
    width: '90%', // Adjust the width as needed
    height: '90vh', // Ensure it fits within the screen height
    bgcolor: '#333333',
    border: '2px solid #000',
    boxShadow: 24,
    p: 4,
    overflowY: 'auto', // Allow vertical scrolling
  };

  return (
    <div>
      <Button onClick={handleOpen}>Task Details</Button>
      <Modal
        open={open}
        onClose={handleClose}
        aria-labelledby="modal-modal-title"
        aria-describedby="modal-modal-description"
      >
        <Box sx={modalStyle}>
          <Typography variant="h6" component="h2" id="modal-modal-title">
            Task Description
          </Typography>
          <Typography sx={{ mt: 2 }} component={'span'} id="modal-modal-description">
            {JSON.stringify(task, null, 2)}
          </Typography>
        </Box>
      </Modal>
    </div>
  );
};

export default CustomModal;

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

Select the object attribute to use as a value in MUI 5 Autocomplete

Here are the available options: const choices = [ { "Type": "Option A" }, { "Type": "Option B" } ] Implementation: const [selectedOption, setSelectedOption] = useState(''); <Autocomplet ...

At times, the Kendo UI Tooltip may linger on screen longer than expected, failing to disappear as

I've been experimenting with this issue for quite some time now, but I'm stumped. Whenever I quickly move my mouse cursor over a series of links, occasionally a tooltip will linger on the screen even after the cursor has moved away from the link. ...

React-vertical-timeline component is failing to display on the webpage. The content in the output HTML remains concealed due to the presence of the "is-hidden"

I am facing an issue while trying to incorporate a vertical timeline into my project. The generated HTML code does not display the timeline as expected. Upon inspection, I noticed that the classes are set to be hidden with "is-hidden". It seems like there ...

Having trouble with the page redirection issue? Here's how you can troubleshoot and resolve

My goal is to restrict access to both the user home and admin dashboard, allowing access only when logged in. Otherwise, I want to redirect users to the login or admin login page. import { NextResponse } from 'next/server' import { NextRequest } ...

Attempting to ensure that the social media icons are perfectly centered in between the separators on the page

After adding new menu separators, the alignment of my social media icons has been thrown off and I need them to be centered. I'm not sure why this happened or how to fix it. I attempted to add some CSS but had no success. My website can be found at . ...

What is the reason for the countdown number's color remaining the same even after it reaches a specific time threshold?

Creating a simple countdown for sports was my idea, but now I'm stuck on the "changeColor" part that I don't have enough knowledge about. The countdown is functioning perfectly, but customizing the colors and adding CSS animations seems challeng ...

Utilizing a switch case for typing

I am working on a React component that takes in a list and a type as props. The list is an array of objects, while the type is an optional enum string. Inside this component, there is a function that uses a switch case statement to enforce a specific type ...

Tips for resolving dependency conflicts in a JavaScript project starter template

Currently, I'm utilizing the Airframe React template and the procedure seems quite simple: Extract the files and execute npm install in the project directory. However, an issue arises when running npm install as follows: npm WARN config global `--glob ...

unitary incline division assemblage

I noticed a particular element in the design that is currently displayed with an image (png). However, I am facing limitations due to needing to make it adaptive. Can a gradient be used as a sub-element instead? background-image: linear-gradient(to left, ...

Arrange the child divs on top of the parent div

<div id="1"> <div id="2"> </div> </div> It is proving challenging to position div2 above div1 in the vertical dimension. Applying a negative top position does not have the desired effect, as it appears that the top border of the ...

Stretch element to reach the edge of the container

I need help with positioning an input and a textarea inside a container that has a height of 52.5vh. I want the textarea to start right below the input and expand all the way to the end of the container. How can I achieve this layout? Both elements should ...

Tell me about the CSS ruby-align characteristic

While browsing online, I stumbled upon a reference to the ruby-align property that piqued my interest. After searching on w3schools for some examples, I was surprised to find no mention of it anywhere. I remembered that Ruby is a newer tag in HTML5 and de ...

How can I effectively display a blank menu item for the SelectField component in Material-UI within a React application?

I am using the select-field component from the material-ui framework version 0.15.4 with React version 15.4.0. I am attempting to add a blank menu-item to the select-field in order to allow for deselecting a value in the dropdown field when clicked. Howeve ...

Creating a sleek grayscale effect when hovering with CSS is both simple and effective

My website features a grayscale logo that smoothly transitions to color upon hover. However, I am experiencing some issues with the smoothness of this transition when using CSS. Here is my current code: <img alt="TT ltd logo" src="./img/tt-logo.png" c ...

Encountering issues with dependencies while updating React results in deployment failure for the React app

Ever since upgrading React to version 18, I've been encountering deployment issues. Despite following the documentation and scouring forums for solutions, I keep running into roadblocks with no success. The errors displayed are as follows: $ npm i np ...

Is there a problem with the toolbar navigation icon color not updating?

I am experiencing an issue with my Material toolbar where the navigation icon color is not changing properly. Although I have made changes in the Layout validation, the updated color is not reflected when I install the app on my phone. For reference, here ...

Caution with React Material-ui Server-Side Rendering: The 'd' property did not align. Server side rendering displayed as "M 0 0 h 24 v 24 H 0 Z" while client side rendering showed as "M0 0h24v24H0z". Double

I've been developing a React website with server-side rendering and Material-ui, and everything was running smoothly, including the mui JSS styles. However, when I added an SVG icon from @material-ui/icons, Edge and IE11 started showing warnings: Wa ...

Animating component transitions using react-router

Currently working with React and react-router, I have page-like components that I want to implement two phase transition animations for when the main view of the application changes. When a <Link> is clicked The old (current) view fades out (co ...

Subsequent components unable to utilize the useState hook

https://i.sstatic.net/jRvl6.png When I try to click on the rating star in the last three cards, all the changes happen on the first one instead. I'm not sure what is causing this issue. I don't just want a simple showcase, I want to understand t ...

Tips for creating a dynamically responsive eye icon placement

When the screen width is less than 991px, the content is centered but the eye icon that is absolutely positioned goes off-center. This issue only occurs on devices with a small or medium screen size. The reason for using absolute positioning on the icon i ...