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

CSS with three columns: the middle column has a fixed size, while the right and left columns

I am attempting to create a 3 column layout with specific requirements: The middle column is fixed at a width of 660px The left and right columns should each take up half of the remaining space, but have a minimum width of 120px The middle div need ...

What is the process to access array elements in AngularJS?

When coding in HTML, <select class="element-margin-top" ng-model="vm.selectedRole" ng-options="(roleName,enabled) in vm.roleNames"> <option value="">All Roles</option>{{vm.roles[0]}} </select> I am tryin ...

A guide on utilizing nodejs to automatically open the default browser and direct it to a specific web address

I am currently developing a program using Node.js. One of the features I aim to implement is the ability to launch the default web browser and direct it to a particular URL. I want this functionality to be compatible across Windows, Mac, and Linux operat ...

Displaying a single photo on mobile, while simultaneously showing four photos on the web using Nuxt.js

When viewing the web browser from a mobile device, only one image should be displayed instead of all four images fetched from an API. What are some possible ways to achieve this? The data retrieved includes all the images at once. <template> <d ...

Struggling to keep my image buttons aligned in a horizontal line at the center, but they keep stacking vertically instead

I'm struggling to keep a row of image buttons centered horizontally, regardless of the window size. However, when I manage to center the buttons, they end up stacking vertically instead of aligning in a single line. I've experimented with differ ...

Django: Error - < found where unexpected

Using a combination of Django and jQuery, I have implemented a file upload feature with AJAX. Everything seems to be working correctly - the files are successfully uploaded, reflected in the database, and stored on the server. However, upon completion of t ...

Alignment of Bootstrap thumbnails in a horizontal layout

After adding the thumbnail class to my thumbnails div, I noticed that some of the thumbnails were smaller in size than others. Wanting them all to have the same height, I decided to give each thumbnail a fixed height of 210px. However, this caused the sm ...

Flexbox problem within Material UI

I'm facing an unusual issue where the max-width setting on a grid element is mysteriously being switched to 16%, and I can't seem to figure out why. This problem arises in a React application that utilizes Sass, JSS, and Material UI. Interestingl ...

Using React with Axios to trigger several requests with a single action

Let's say a user selects a team from a dropdown menu, triggering a request to an API endpoint. const selectHomeTeamStat = evt => { const { value } = evt.target; getStats(leagueId, value, 'home'); }; In this hypothetical scen ...

Issue with jQuery 'on' event not triggering following 'load' event

I am facing an issue on a page where similar events occur but when new content is loaded halfway through, most of the jQuery functionalities stop working. The scenario involves answering questions in a 'game' format using AJAX calls. Once all que ...

What is the best way to determine the position of a Draggable element in relation to a Droppable element that it is currently hovering over

VIEW IMAGE My Objective: I am working on a project where I need to determine the distance between my draggable element and a droppable target. Specifically, I want to calculate how far away my mouse is from the center, top border, or bottom border of the ...

Can someone explain why line-height can affect the width in CSS?

Can you explain how the line-height property functions in CSS? I have noticed that when I set the line-height to be equal or less than the font size, it causes layout width issues. You can view an example of this problem on a jsFiddle here. Currently, my ...

Designing a unique CSS border for custom list item bullets

I'm currently exploring the possibility of implementing a CSS border around an image that I have imported as a custom bullet for my list items: ul { list-style: none; margin-right: 0; padding-left: 0; list-style-position: inside; } ul > ...

ZK: maintaining session connectivity

When I need to redirect to a tel:**** link in ZK and then redirect the user to another page after the call, I encounter a problem. Every time I click on the link, ZK interprets it as leaving the browser, which results in my client session ending automatica ...

Minimizing Jitter in HTML5 and JavaScript Applications

I am currently working on developing a metronome as a hobby using JavaScript/HTML5 with the intention of turning it into a FirefoxOS app. One issue I've encountered is Jitter, which is unacceptable for Metronomes. As JavaScript is single-threaded and ...

Steps to include a personalized function in a Mongoose Model

One way to extend Mongoose is by adding methods to documents. Here's an example: const userSchema = new mongoose.Schema({ balance: Number }) userSchema.methods.withdrawBalance = function(amount){ const doc = this doc.balance = doc.balance - amou ...

Exploring the power of internal linking with GatsbyJS

I am currently developing a website using gatsbyjs. I have concerns about the crawlability of "onClick" for SEO purposes and I would appreciate some assistance. This is my current code: render={data => ( <div className='feed'> ...

Identify the difference between a regular function and a constructor in JavaScript

How can we determine whether a function in javascript is a simple, plain (anonymous) function, or a constructor (a function with a prototype)? I have developed the following function for this purpose: function checkFunctionType(value) { var ownPropert ...

"Learn how to implement a feature that allows for the acceptance of string input in discord

I am struggling to find the right solution for my issue. I am trying to implement a change_nick command but unsure about what type/number option to utilize. This is how I'm creating my command: commands.create ( { ...

Is opting to exclude the language file from the main bundle a viable strategy for minimizing the overall bundle size?

As I brainstorm ways to optimize my app's bundle size, I had a thought and would appreciate some feedback. Do you think it's advisable to exclude the language file altogether from the main bundle of an application? If your app is large with nume ...