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

Upon successful authorization, the Node Express server will pass the access token to the React client app via OAuth in the callback

I am currently working on a node server that authenticates with a third party using oauth, similar to how Stack Overflow does. After authorizing the request and obtaining the access token and other essential information from the third party, my goal is to ...

Moving through content on a single page

import React, { Component, useState } from "react"; import { Content, List, ListItem, Left, Right, Icon, Container, Header, Body, Button, Title, } from "native-base"; //Chapter One expor ...

Change the color of a row in material-table when the mouse is hovering over it

Is there a way to highlight a row in the material-table component when the mouse is over it? I haven't been able to find any properties or components to achieve this. Thank you in advance for your help. You can view the code here. ...

Make sure accordion items stay open even when another one is clicked

I have implemented an accordion component that currently opens and closes on click. However, I am facing an issue where clicking on one item closes another item that was previously open, which is not the behavior I desire. I'm unsure of the best appro ...

Is there a way to ensure the collapsible item stays in its position?

I'm encountering an issue with the display of items within collapsible cards. Here is what it currently looks like: And this is how I want it to appear: Is there a way to achieve the desired layout using Bootstrap's Grid or Flex Layout? Here i ...

What is the best way to manage the retrieved data in a situation where I am utilizing async-await along with the useEffect hook?

I need to retrieve a player's statistics based on the input provided by the user, and then show it. I am facing challenges in storing the retrieved data using the useEffect hook. Any suggestions for a more efficient approach? import React, { useEf ...

React error message #321 found. View the details at https://reactjs.org/docs/error-decoder.html?invariant=321

I have been working on creating custom React components to be packaged and published on npm for other projects to use by importing the package. The package I created is named "sample-testing" and can be found at (https://www.npmjs.com/package/sample-testi ...

Tips for eliminating a single occurrence with Array.prototype.filter()

I am facing an issue in my React app with the deleteNumberFromList method. This function is designed to remove a specific number from the array by utilizing a setter hook: const [valuesList, setValuesList] = useState<number[]>([]); const deleteNumbe ...

Eliminate the display of Last Modified date and file Size in mod_autoindex

Recently, while developing a file storage webpage for my website, I successfully implemented mod_autoindex for my Apache server. However, I now face the challenge of removing the Last Modified and Size fields from the table. I am hopeful that there is a w ...

How to eliminate column padding in Bootstrap 4

Can you help me with a layout challenge I'm facing? I have two columns set up like this: <div class="row"> <div class="col-6"> hello </div> <div class="col-6"> hello ...

Is there a way for me to prioritize the sidebar over the main content on the page

My theme is efficient and visually appealing, but there's one issue with the mobile layout - it displays the sidebar after the content. How can I rearrange this in my theme so that the sidebar appears before the content? Thanks for your help! ...

Explain the concept of render hijacking in react technology

After learning about High Order Components (HOC), I came across the concept of render hijacking. Can someone please explain what exactly render hijacking entails? ...

What is the best way to share specific links on LinkedIn articles?

When the LinkedIn button is clicked, the entire link does not get passed when the image is clicked. However, the Facebook link works perfectly fine. The LinkedIn button used to work before, has anything changed since then? <div align="center"> < ...

Testing React JSX components using ES6 unit tests

Currently, I am utilizing React, JSX, ES6, and Karma. I am facing an issue with my code. Can anyone pinpoint what might be wrong? I am attempting to execute a test using Karma-Runner but encountering some obstacles: let React = require("react") ...

Guide to retrieving objects in React.js

Struggling to extract a country from an online JSON file that I am currently fetching. I am attempting to streamline the process by creating a function to retrieve the country from the dataset and avoid repeating code. However, I am encountering difficulti ...

Switch the designation to Hover Class

I am working with nested divs and have assigned a CSS class to one of the inner divs. How can I trigger the hover effect of the class (class.hover) when the user hovers over the outer div, even if they are not directly over the inner div? I believe this m ...

What is the most effective method of testing with jest to verify that a TypeScript Enum list contains all the expected string values?

Recently, I created a list of enums: export enum Hobbies { Paint = 'PAINT', Run = 'RUN', Bike = 'BIKE', Dance = 'DANCE' } My goal is to iterate through this list using Jest and verify that all the string ...

Optimal method for utilizing @font-face syntax

Recently, I've been utilizing this specific model @font-face { font-weight: WEIGHT; font-style: STYLE; font-family: 'NAME'; src: url('font.eot'); src: url('https://dl.dropboxusercontent.com/s/dey3lzjdpgszxzt/myriad-webfont.eo ...

Create a div that pops up when clicked, just like the Fancybox jQuery feature

I'm looking to create a popup div with an elastic effect similar to jQuery's fancybox. I've tried using the following code, but it doesn't seem to work... Here is the HTML: <a href="#" id="LoginAnchorLink">ClickME</a> < ...

conceal the bootstrap navigation bar while the page is being

Struggling to toggle a Bootstrap navbar on scroll? Need some guidance from the pros out there. I admit, my Bootstrap and jQuery skills are pretty basic. The browser console doesn't throw any errors, and I've experimented with fadeIn, fadeOut, add ...