Positioning a material UI dialog in the middle of the screen, taking into account variations in its height

Dealing with an MUI Dialog that has a dynamic height can be frustrating, especially when it starts to "jump around" the screen as it adjusts to fit the content filtered by the user. Take a look at this issue:

https://i.stack.imgur.com/IndlU.gif

An easy fix for this problem would be to remove the alignItems property from the dialogPaper using the classes property. Here's an example:

const useStyles = makeStyles((theme) => ({
  pivottedDialog: {
    alignItems: 'unset',
    ...
  },
}));

const MyDialog = () => (
  <Dialog
    {...otherProps}
    classes={{
      scrollPaper: classes.pivottedDialog
    }}
  >
    ...
  </Dialog>
)

However, this solution causes the dialog to lose its centered position on the screen, which is essential for the full list dialog without any filters applied. I want the dialog to start in the same position as the original one, regardless of screen size (above 600px width)

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

How can I achieve this?

I've attempted adjusting the positioning and using the top property for the pivottedDialog class, but it doesn't remain consistent across different screen sizes. It needs to stay centered and responsive on all screens wider than 600px.

I created a demo app with both dialogs, which you can experiment with here: https://codesandbox.io/s/crazy-tesla-emc8m?file=/src/App.js

Answer №1

If the height of DialogContent changes based on the filter selection, it is suggested to set specific heights and widths for both the Dialog and DialogContent.

See a Demo in Action:

https://codesandbox.io/s/dynamic-filterable-material-modal-1z7o8?fontsize=14&hidenavigation=1&theme=dark

Why Does the Content Shift? The issue arises from the use of flexbox properties such as display: flex, justify-content: center, and align-items: center, which align the content to the center of the dialog box relative to the view port.

Are There Any Solutions? Custom layouts or modals can be implemented to achieve a centered modal. Check out this demo for more details:

https://codesandbox.io/s/dynamic-filterable-material-modal-no-specified-height-e32tq?fontsize=14&hidenavigation=1&theme=dark

The demo example uses a layout with three rows, anchoring the modal at the top of the second row to allow for expansion within that row only.

 ___________________________
|                           |
|         Empty Row         |
|      · 25% of view        |
|___________________________|
|   |------ Modal ------|   |
|   |  · Top anchored   |   |
|   |  · Expandable †   |   |
|   |  · 50% of view    |   |
|   |___________________|   |
|                           |
|                           |
|                           |
|___________________________|
|                           |
|         Empty Row         |
|      · 25% of view        |
|___________________________|

† Modal can only expand to the max height of the row it resides within

This setup allows the modal to resize while remaining centered vertically and horizontally. However, uneven division of viewport space may lead to squished results in small viewports.

The benefits include:

Best Approach: Setting defined height/width for the modal is the most straightforward solution. Alternatively, consider using a filterable dropdown select list to prevent modal height interference.

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

Is there a way to use jQuery to hide rows in a data table that contain a specific code, such as "1.1" or "1.2," that includes a

When using ajax, I pass data from two inputs to another page. These values are then used in a query to display the results in a data table on the main page within the success function. Now, I'm looking to implement a condition where any data containi ...

Ways to leverage ember.js in a serverless environment

After checking out the example of ember.js on this website (http://todomvc.com/), I decided to clone the project onto my computer. Upon double-clicking the index.html file, the project ran smoothly, just as I had anticipated. However, following the instru ...

Tips for modifying a user's tags with Quickblox's JavaScript SDK

Is there a way to update a user's tags using the Quickblox JavaScript SDK? I have attempted using the parameter names listed below: user_tags tags with var params = { user_tags: ["testing"] }; Kindly avoid suggesting alternatives like "custom ...

babel-minify or terser over uglify-js

Exploring ES6+ (modern JavaScript) is a new adventure for me, and I've discovered that in order to use it in browsers, tools like babel-minify or terser are necessary. It's interesting to note that Babili was initially thought to be a separate to ...

PHP encountering a bad escaped character while parsing JSON using JSON.parse

I'm encountering an issue with JSON parsing. In my PHP code, I have the following: json_encode(getTeams(),JSON_HEX_APOS); This returns a large amount of data. Sample data: To provide more clarity, let's assume I have this: my_encoded_data ...

Struggling to configure a React-Redux boilerplate

I encountered an issue with my previous boilerplate while working on a project, so I decided to switch to a different one. After exploring Github, I came across this repository that seemed simple and similar to what I needed. Upon cloning it and running np ...

TypeScript - ESBuild - Encountered an unexpected '<' token

When compiling TypeScript files for a React app with esbuild, everything goes smoothly. However, upon checking the browser console, an error pops up: An unexpected token '<' is causing errors after the return statement // components/editor/ ...

What is the best way to ensure an AJAX get-request waits for the page to finish rendering before providing a response?

I've been working on a Greasemonkey Script for a specific section of this website (Site1). Site1 offers various deals and discounts, and my script is designed to perform the following task: When a user visits an offer on Site1, the script checks with ...

Encountering an Issue with NextJS on GAE: EROFS Error indicating a read-only file system

Trying to deploy a customized Next.js application into Google App Engine has hit a snag. The project runs smoothly locally and on Google Cloud Platform CLI, but upon successful deployment using gcloud app deploy, an error arises when opening the app. 2020 ...

Adding list items to an unordered list without making them draggable using jQuery

How can I allow users to build a list of sports and drag them into a "cart" using jQuery? The issue I'm facing is that the appended list items are not draggable, and I'm unsure of how to solve this problem. /* JS code for sports.html */ $(fu ...

What is the best way to incorporate the parallax effect into a v-carousel?

Currently, I have a "v-carousel" containing multiple images and now I am looking to incorporate a parallax effect into it, similar to "v-parallax". <v-carousel cycle height="600" hide-delimiter-background show-arrows-on-hover> <v-carousel-i ...

CSS. Absolute positioning in relation to a div element

I discovered a way to adjust the positioning of a div using jQuery. Check out this example. Is it possible to achieve the same result using CSS? UPDATE: I came across this solution. I also want the horizontal window scrollbar to appear if the fixed elem ...

How can you determine if a Material-UI component utilizing withStyles and essential PropTypes is successfully displayed?

I'm still learning about unit testing and facing a challenge in verifying if a Material-UI withStyles component containing required propTypes actually displays children Material-UI elements. profile.js const StaticProfile = (props) => { const { ...

Do we need to handle promise resolution after sending a response to the client?

After sending the response to the client, I have a requirement to execute an asynchronous function. res.json({}); someAsyncFunction(); //this function does not require await Is it considered problematic to call this function without awaiting its promise? ...

Using Jquery Functions within Codeigniter

I am facing an issue with calling a function containing jQuery script within an if-else statement. Below is the codeignitor view code: Code if($osoption == "windows") { ?> <script> windows(); </script> <? ...

Unusual Box-shadow glitch experienced exclusively when scrolling in Chrome 51

While working on my website, I encountered a peculiar issue with the box-shadow in Chrome 51. My site has a fixed header with a box-shadow, but when I scroll up or down, the box-shadow leaves behind some marks (horizontal gray lines): https://i.stack.imgu ...

The AJAX validation process fails to run prior to the execution of the login PHP script

My attempt to implement AJAX for form validation is not successful and I'm unsure why. Despite my efforts, the form still redirects to login_action.php instead of performing the AJAX validation as intended. I have designed a modal login form and wish ...

Toggle visibility with JQuery's onClick event

I am looking for a way to toggle the visibility of text that has display: none; in CSS when a button is clicked, as well as clear the input field on the onClick event. Currently, the text remains visible even after it has been displayed. Any assistance wou ...

What is the best way to ensure the image and card maintain the same size ratio in React Bootstrap?

One of the challenges I encountered involved a vertically aligned card group containing a card with an image: https://i.stack.imgur.com/w9Jkf.jpg This is the React code snippet that was causing the issue: import React from 'react'; import { ...

Fetching JSON data from a Node.js server and displaying it in an Angular 6 application

Here is the code snippet from my app.js: app.get('/post', (req,res) =>{ let data = [{ userId: 10, id: 98, title: 'laboriosam dolor voluptates', body: 'doloremque ex facilis sit sint culpa{ userId: 10' ...