Is there any way I can do to let the drop-down menu appear below the bar? (not covering the Your order
label and the number)
My codes are as below: I try to modify the anchorOrigin
property and transformOrigin
property but it didn't work.
<Menu \n id="order-menu" \n anchorEl={anchorEl} \n keepMounted \n open={Boolean(anchorEl)}\n onClose={() => setAnchorEl(null)} \n elevation={20} \n getContentAnchorEl={null}\n anchorOrigin={{ vertical: "bottom", horizontal: "center", }} \n transformOrigin={{ vertical: -100, horizontal: 150, }} > \n
\nI will really appreciate your help!
\n","author":{"@type":"Person","name":"Yunxiu Qiu"},"upvoteCount":7,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Here's an example that aligns the top-center (transformOrigin
) of the menu with the bottom-center (anchorOrigin
) of the button:
import React from "react";\nimport Button from "@material-ui/core/Button";\nimport Menu from "@material-ui/core/Menu";\nimport MuiMenuItem from "@material-ui/core/MenuItem";\nimport styled from "styled-components";\n\nconst MenuItem = styled(MuiMenuItem)`\n justify-content: flex-end;\n`;\n\nexport default function SimpleMenu() {\n const [anchorEl, setAnchorEl] = React.useState(null);\n\n const handleClick = event => {\n setAnchorEl(event.currentTarget);\n };\n\n const handleClose = () => {\n setAnchorEl(null);\n };\n\n return (\n <div>\n <Button\n aria-controls="simple-menu"\n aria-haspopup="true"\n onClick={handleClick}\n >\n Open Menu\n </Button>\n <Menu\n id="simple-menu"\n anchorEl={anchorEl}\n keepMounted\n open={Boolean(anchorEl)}\n onClose={handleClose}\n getContentAnchorEl={null}\n anchorOrigin={{ vertical: "bottom", horizontal: "center" }}\n transformOrigin={{ horizontal: "center" }}\n >\n <MenuItem onClick={handleClose}>1</MenuItem>\n <MenuItem onClick={handleClose}>2</MenuItem>\n <MenuItem onClick={handleClose}>3</MenuItem>\n <MenuItem onClick={handleClose}>10</MenuItem>\n <MenuItem onClick={handleClose}>20</MenuItem>\n <MenuItem onClick={handleClose}>300</MenuItem>\n </Menu>\n </div>\n );\n}\n
\n\nRelated documentation: https://material-ui.com/api/popover/#props
\n","author":{"@type":"Person","name":"Ryan Cogswell"},"upvoteCount":11}}}Reputation: 169
I created a drop-down menu by using Material-UI, and I found one thing annoying: I want to let my drop-down menu appear under the bar when I click it, but every time it just covers the bar (as the image below)
Is there any way I can do to let the drop-down menu appear below the bar? (not covering the Your order
label and the number)
My codes are as below: I try to modify the anchorOrigin
property and transformOrigin
property but it didn't work.
<Menu
id="order-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={() => setAnchorEl(null)}
elevation={20}
getContentAnchorEl={null}
anchorOrigin={{ vertical: "bottom", horizontal: "center", }}
transformOrigin={{ vertical: -100, horizontal: 150, }} >
I will really appreciate your help!
Upvotes: 7
Views: 7998
Reputation: 81136
Here's an example that aligns the top-center (transformOrigin
) of the menu with the bottom-center (anchorOrigin
) of the button:
import React from "react";
import Button from "@material-ui/core/Button";
import Menu from "@material-ui/core/Menu";
import MuiMenuItem from "@material-ui/core/MenuItem";
import styled from "styled-components";
const MenuItem = styled(MuiMenuItem)`
justify-content: flex-end;
`;
export default function SimpleMenu() {
const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = event => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
return (
<div>
<Button
aria-controls="simple-menu"
aria-haspopup="true"
onClick={handleClick}
>
Open Menu
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
getContentAnchorEl={null}
anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
transformOrigin={{ horizontal: "center" }}
>
<MenuItem onClick={handleClose}>1</MenuItem>
<MenuItem onClick={handleClose}>2</MenuItem>
<MenuItem onClick={handleClose}>3</MenuItem>
<MenuItem onClick={handleClose}>10</MenuItem>
<MenuItem onClick={handleClose}>20</MenuItem>
<MenuItem onClick={handleClose}>300</MenuItem>
</Menu>
</div>
);
}
Related documentation: https://material-ui.com/api/popover/#props
Upvotes: 11