question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with Modal scrolling/overflow sizing.

See original GitHub issue

Hi guys,

I’m having an issue with a simple modal that I am trying to implement in a react/flux project. Here is the code for the Modal component (for testing purposes).

'use strict';

//Library Dependencies
const React = require('react');
const Modal = require('react-modal');

const AmazonModal = React.createClass({

  render() {

    //Modal Config
    const customStyles = {
      content : {
        top: '50%',
        left: '50%',
        right: 'auto',
        bottom: 'auto',
        marginRight: '-50%',
        transform: 'translate(-50%, -50%)'
      },
      overlay: {
        zIndex: 10
      }
    };

    return (

      <Modal
        isOpen = { this.props.modalIsOpen }
        onRequestClose = { TBD }
        style = { customStyles } >

        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>

     </Modal>

    )
  },

  _closeModal() {
    this.props.closeModal();
  }

});

module.exports = AmazonModal;

Currently I have two Issues that I cannot solve:

The Modal is rendering but I am unable to see the full content of the modal. I am unable to scroll through the content. When I scroll it simply scrolls through the greyed out content behind it. I tried playing with all overflow settings for the content/overlay…but had no luck getting the desired outcome.

  1. I would like to be able to scroll through the content of the modal itself so that I can see all of the elements within…
  2. With this much content within the modal the top and the bottom borders of the modal are not even visible within the window… they appear to be cut off from the browser viewport, and no matter how far you scroll up/down they do not become visible.

Any help would be greatly appreciated. Please let me know if I can provide anymore info for troubleshooting! Thanks very much (i’m a newbie)

Andrew

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

41reactions
ghoshcommented, Nov 15, 2015

@andrewheppner Hi Andrew, actually that is not an issue with react-modal or react for that matter. It’s CSS positioning.

Since the modal is absolutely positioned, it is out of the document bounds, in other words the document, does not know how big the modal is and has no idea that it is bigger than the document itself and therefore doesn’t scroll.

This can be fixed by giving the modal a fixed height and setting the overflow property to scroll. Try the code below, I just tested it and it works perfectly.

    var customStyles = {
      content : {
        top: '50%',
        left: '50%',
        right: 'auto',
        bottom: 'auto',
        marginRight: '-50%',
        transform: 'translate(-50%, -50%)',
        height: '500px', // <-- This sets the height
        overlfow: 'scroll' // <-- This tells the modal to scrol
      }
    };
0reactions
corysimmonscommented, Jan 24, 2019

This is more complicated than the above solutions.

  • You have to have a fixed overlay that doesn’t scroll (body doesn’t scroll).
  • Content needs to be scrollable.
  • Content needs to be vertically and horizontally centered regardless of amount of content (1 word or 10,000 words).

image

/* _app.js -- I'm using Next.js and these class styles need to be global, alternatively you could injectGlobal() with Emotion/styled-components */

.ReactModalPortal {
  position: relative;
  z-index: 9999;
}

.ReactModal__Body--open {
  overflow: hidden;
}

.ReactModal__Overlay {
  display: flex;
  width: 100%;
  height: 100%;
  background: ${transparentize(.05, darken(.35, c.c))} !important;
  align-items: center;
  justify-content: center;
}

.ReactModal__Content {
  outline: none;

  > div {
    max-width: 70vw;
    max-height: 50vh;
    padding: 2rem;
    overflow: auto;
    border-radius: 1rem;
    background: white;
  }
}
// LoginModal.js

import ReactModal from 'react-modal'
import styled from '@emotion/styled'

ReactModal.setAppElement('#root');

const S = styled(ReactModal)`
  max-width: 200px; // Feel free to override this modal's specific styles

  .heading {
    color: red;
  }
`

const LoginModal = () => (
  <S isOpen={true}>
    <div>
      <h1 className="heading">Yeee</h1>
      <p>
        Lorem ipsum...
      </p>
    </div>
  </S>
)

export default LoginModal

It’d be nice if all this were built into react-modal’s default CSS with some centerAndScrollable prop defaulted to true.

🤷‍♀️

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to scroll the page when a modal dialog is longer than the ...
@Adyyda You can stop scrolling by wrapping the page in a DIV with overflow:hidden and settings its size to the same as the...
Read more >
How to prevent overflow scrolling in CSS - LogRocket Blog
To fix this problem, avoid using vw for your max-width and use a max-width of 100% instead. This will transfer the width of...
Read more >
Prevent Page Scrolling When a Modal is Open | CSS-Tricks
Unfortunately overscroll-behavior: contain does not prevent the scrolling when the content is less or equal to the parent(as in, no overflow).
Read more >
Modal Not Scrollable - Material Design for Bootstrap
Hi there, All the modals when open up are not scrollable on any screen size if the modal height is big. Please help...
Read more >
Modal · Bootstrap v5.0
You'll likely run into issues when nesting a .modal within another fixed ... You can also create a scrollable modal that allows scroll...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found