Populate User Interface Popover - Placement on Compact Display

I have been grappling with finding a solution to position the material ui popover beneath my anchor consistently, even on smaller screens.

Take a look at this sandbox example.

The current setup is working alright, but the issue is that the scroll is contained within the popover container div, which isn't ideal for me.

To elaborate, I understand that I can use AnchorElement with positioning, but on smaller screens, the popover ends up covering the Anchor. I actually want the popover to always be positioned below it and allow the body to scroll, displaying the full content of the popover when scrolling down.

import React from "react";
import {makeStyles,MuiThemeProvider,createMuiTheme} from "@material-ui/core/styles";
import Popover from "@material-ui/core/Popover";
import Button from "@material-ui/core/Button";

export default function SimplePopover() {
    const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = (event) => {
    setAnchorEl(event.currentTarget);
  };

  const handleClose = () => {
    setAnchorEl(null);
  };

  const theme2 = createMuiTheme({
    overrides: {
      MuiButton: {
        root: {
          top: 400
        }
      },
      MuiPopover: {
        root: {
        },
        paper: {
          
          height: 500
        }
      }
    }
  });
  return (
    <div>
      <MuiThemeProvider theme={theme2}>
      <Button
        variant="contained"
        color="primary"
        onClick={handleClick}
      >
         Open Popover with anchor
      </Button>
      <Popover
        id="popover-with-anchor"
        open={Boolean(anchorEl)}
        anchorEl={anchorEl}
        onClose={handleClose}
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'center',
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'center',
        }}
      >
       Popover content.
   </Popover>
   </MuiThemeProvider>
    </div>
  );
}

Images provided as an example. When the popover exceeds the screen size, it adjusts itself within the screen and overlaps the anchor

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

instead of being positioned underneath the anchor

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

Answer №1

It's worth noting that the Popover component lacks a specific property to address this particular issue. I encountered a similar problem on a smaller device where I had an extensive list of data in the popover. In order to resolve this, I took matters into my own hands and manually set the top attribute within the PaperProps property of the Popover component. Take a look at the example below:

<Popover PaperProps={{ style: { top: myAnchor.current ? myAnchor.current.getBoundingClientRect().bottom : 0 } }}></Popover>

Answer №2

Avoid using overflow: scroll on the root element. Instead, apply overflowY: auto to the paper element.

For more information and experimentation, check out the following codesandbox link.

Give this a try:

overrides: {
      MuiPopover: {
        root: {
          // overflow: "scroll"
        },
        paper: {
          left: 50,
          top: "500px !important",
          height: 50,
          overflowY: "auto"
        }
      }
    }

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

Prevent changing the year in MobileDatePicker for MUI 5

Is there a way to prevent users from changing the year in the mobile date picker I am currently using? import { LocalizationProvider, MobileDatePicker } from '@mui/x-date-pickers'; I am using version "@mui/x-date-pickers": "^5.0.0 ...

Encountering errors with React 17 when trying to install the react-helmet-async package due to npm peer

Working on a React 17 project initialized with create-react-app, my goal is to incorporate the react-helmet-async package into it. The project specifies certain peer dependencies in the package.json file: "peerDependencies": { "react&q ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

The layout of the table is not formatted in a single continuous line

I recently implemented the material-ui table and noticed that the header has a multiline break space. I am looking for a way to make it display in a single line instead. Is there any solution for achieving this using material UI or CSS? Feel free to chec ...

The issue with the left border color not functioning in JavaScript

Attempting to alter the border-left-color property using this script is resulting in a Uncaught SyntaxError: Unexpected token -. Is border-left-color actually supported? Javascript function logoChange() { var description = new Array (); description[0] ...

Forward checkout.session items from Stripe webhook to Supabase

Currently, I am utilizing next.js in conjunction with Stripe webhooks to insert checkout sessions into Supabase for generating a customer's order history. While I have successfully managed to store the entire order information in a table named 'o ...

Advancing in the Mozilla fashion

Having an issue with my progress bar design... When viewed in Chrome, the value of the progress bar appears in red due to CSS styling I applied... But when opened in Mozilla, it shows up as a dull grey color. progress[value] { width: 250px; height ...

Disabling file-system routing in Next.js is a crucial step for optimizing

I'm a bit confused about the documentation's explanation of setting the useFileSystemPublicRoutes configuration option to false in Next.js. It mentions that this prevents routing based on files in the /pages directory, but the details are not ver ...

What techniques does Google use to craft mobile-friendly fixed backgrounds and parallax content within iframes?

Currently, I am working on a test that involves utilizing an intersectionobserver in conjunction with iframe postMessage to adjust the background image in a translate3d manner within the iframe. However, this method is causing significant jitter and potent ...

What could be causing an issue with CORS in ExpressJS in one scenario but not in another?

I am currently in the process of setting up a database and connecting it to various routes. Interestingly, I have been successful with one route ('register') but encountering issues with another ('login'). Whenever I attempt to run the ...

Ensure that the divs are properly aligned with the paragraphs

I received assistance previously, and I am seeking additional help in maintaining the position or adjusting the size of elements to fit different window viewport sizes such as tablets, mobile devices, or varying monitor sizes while keeping the appearance c ...

Struggling to configure dynamic routing in NextJS for endpoints retrieved from Medium API

Having trouble configuring dynamic routing in my Next.js app. I'm attempting to showcase my Medium.com posts using the unofficial Medium API. Currently, I can display a list of cards on the homepage with the posts and each card should link to its corr ...

Move the object beyond the boundaries of the container that can be scrolled

I'm having an issue where the "item" is draggable, but once I try to move it outside of the scrollable container, it disappears. I've attempted adjusting the z-index, but it doesn't seem to be resolving the problem. Any suggestions on what m ...

Utilizing Strophe in conjunction with ReactJS

Struggling to log in to my XMPP server using React JS. New to React and feeling a bit lost, can someone please assist me here? import './App.css'; var BOSH_SERVICE = 'ws://chat.example.com:7070/ws'; var connection = null; const Strophe ...

Achieve full parent width with floated divs

My goal is to arrange 10 boxes horizontally on a webpage. Each box must have a minimum width of 150px and a maximum width of 299px. The challenge is to fit as many boxes as possible across the page without any gaps, ensuring that each box has an equal widt ...

What is the process for adding color to HTML elements?

Is there a way to assign unique random colors to each individual p tag in ReactJS? function App() { return ( <div> <p style={{ color: "#7fbd73" }}>Hi</p> <p style={{ color: "#3a82e4" }}>Hello</p> < ...

What could be causing the issue with the width: 100% not rendering correctly in this

I'm currently working on my footer, but I'm having trouble getting the width: 100% tag to work correctly. Here's a screenshot of what I mean: http://gyazo.com/9c23897cd7b9a74ca55ee9fb977f810c This is my CSS code: /*..Portfolio - Lars Beut ...

Horizontal Alignment of DIV Tags

I'm currently working on aligning some div tags to serve as contact links on my website. My goal is for them to be positioned on the right-hand side and aligned in a single line. Here's how they look at the moment: Here's the preferred ali ...

Looking for ways to streamline your React and Vue projects by reducing the number of

Coming from a background in emacs and xterm, I am accustomed to working with just a few files and having complete control over my work. Now I am delving into the world of React and Vue, but I was taken aback when I saw the sheer number of files generated f ...

What is the best way to eliminate wrapper spans from next/image in Next.js?

I'm currently learning Next.js and I've noticed that when using next/image, it wraps the img tag with additional spans and applies inline styles that override my custom class styles. Is there a way to remove the inline styles as well as the wrap ...