How do I set the drop down menu position in Material UI - css

I am building a Gatsby app and I want once the Menu icon is clicked, my menu will drop down right under the and width to be 100%. Here is my code:
I tried to solve the problem by setting this inside the component, but the component doesn't change at all:
getContentAnchorEl={null}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
transformOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
import React, { useState } from 'react';
import { makeStyles } from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Toolbar from '#material-ui/core/Toolbar';
import Typography from '#material-ui/core/Typography';
import IconButton from '#material-ui/core/IconButton';
import MenuIcon from '#material-ui/icons/Menu';
import MenuItem from "#material-ui/core/MenuItem"
import { Menu } from '#material-ui/core';
import { Link } from "gatsby"
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
backgroundColor: "#e8eaf6",
color: "black"
},
menuButton: {
marginRight: theme.spacing(2),
},
title: {
flexGrow: 1
},
}));
export default function NavBar() {
const classes = useStyles();
const [open, setOpen] = useState(false)
const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = (e) =>{
setOpen(!open)
setAnchorEl(document.querySelector('header'))
}
const handleMenuClose = () => {
setOpen(!open)
setAnchorEl(null)
}
return (
<AppBar position="static" className={classes.root}>
<Toolbar>
<Typography variant="h5" className={classes.title}>
Inhouse Orders
</Typography>
<IconButton edge="false" className={classes.menuButton} color="inherit" aria-label="menu">
<MenuIcon onClick={(e)=>handleClick(e)}/>
</IconButton>
<Menu
className={classes.menu}
open={open}
onClose={handleMenuClose}
getContentAnchorEl={null}
anchorEl={anchorEl}
anchorOrigin={{
vertical: 'top',
horizontal: 'left',
}}
transformOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
>
<Link to="/1"><MenuItem>1</MenuItem></Link>
<Link to="/2"><MenuItem>2</MenuItem></Link>
<Link to="/3"><MenuItem>3</MenuItem></Link>
<Link to="/4"><MenuItem>4</MenuItem></Link>
</Menu>
</Toolbar>
</AppBar>
);
}
What should I do? Should I create my own theme? Or change a style is ok?

You can use the Anchor playground of the Material UI Popover documentation to determine which parameters to use to adjust the position of the dropdown menu.
About the width of 100%, I am not sure to understand what you want to achieve. The anchor of the dropdown menu is fixed and depending on the style you apply, the width will adapt to the size of the content. With a width of 100%, perhaps you want to use a Dialog instead of a dropdown menu.

Related

How do I set up make styles using MUI for my app?

Im having problems with MUI im trying to color my buttons and the 2 ways ive tried have not worked.
example 1
import React from 'react'
import { Typography } from '#mui/material'
import Button from '#mui/material/Button'
import Container from '#mui/material/Container'
import SendOutlinedIcon from '#mui/icons-material/SendOutlined';
import { makeStyles } from '#mui/styles';
import useStyles from './create-styles'
/* Styling Section (CSS) */
export default function Create() {
const classes = useStyles()
return (
<Container>
<Typography className={classes.title} variant="h6" component='h2' gutterBottom color="textSecondary" align='center'>
Create a New Note
</Typography>
<div>
<Button onClick={() => console.log('you clicked me')} type="submit" variant='contained' endIcon={<SendOutlinedIcon/>}>
Submit
</Button>
</div>
</Container>
)
}
import { makeStyles } from '#mui/styles';
export default makeStyles(() => ({
btn: {
fontSize: 500,
backgroundColor: 'pink',
'&:hover': {
backgroundColor: 'orange'
}
},
title: {
color: 'orange',
textDecoration: 'underline',
marginBottom: 20
}
}))
This isn't working.
and neither is this.
example 2
import React from 'react'
import { Typography } from '#mui/material'
import Button from '#mui/material/Button'
import Container from '#mui/material/Container'
import SendOutlinedIcon from '#mui/icons-material/SendOutlined';
import { makeStyles } from '#mui/styles';
/* Styling Section (CSS) */
const useStyles = makeStyles({
btn: {
fontSize: 500,
backgroundColor: 'pink',
'&:hover': {
backgroundColor: 'orange'
}
},
title: {
color: 'orange',
textDecoration: 'underline',
marginBottom: 20
}
})
export default function Create() {
const classes = useStyles()
return (
<Container>
<Typography className={classes.title} variant="h6" component='h2' gutterBottom color="textSecondary" align='center'>
Create a New Note
</Typography>
<div>
<Button sx={{fontSize: 60, backgroundColor: "red"}} onClick={() => console.log('you clicked me')} type="submit" variant='contained' endIcon={<SendOutlinedIcon/>}>
Submit
</Button>
</div>
</Container>
)
}
only when I use this does it work.
sx={{fontSize: 60, backgroundColor: "red"}}
Are there any solutions so I don't have to style inline?
Im following a video and I guess the old way of styling using MUI has depreciated
Here you go with a solution
In the button element you need to provide classes
import React from 'react'
import { Typography } from '#mui/material'
import Button from '#mui/material/Button'
import Container from '#mui/material/Container'
import SendOutlinedIcon from '#mui/icons-material/SendOutlined';
import { makeStyles } from '#mui/styles';
/* Styling Section (CSS) */
const useStyles = makeStyles({
btn: {
fontSize: 500,
backgroundColor: 'pink',
'&:hover': {
backgroundColor: 'orange'
}
},
title: {
color: 'orange',
textDecoration: 'underline',
marginBottom: 20
}
})
export default function Create() {
const classes = useStyles()
return (
<Container>
<Typography className={classes.title} variant="h6" component='h2' gutterBottom color="textSecondary" align='center'>
Create a New Note
</Typography>
<div>
<Button
classes={{root: classes.btn}} // -- added this classes
onClick={() => console.log('you clicked me')} type="submit" variant='contained' endIcon={<SendOutlinedIcon/>}>
Submit
</Button>
</div>
</Container>
)
}

How to change label transform in TextField (MUI package)

When clicking the text area, I want to put the label on top right of text box with transparent border, so it will be like this:
But instead, I received something strange:
import TextField from "#mui/material/TextField";
import { createTheme, ThemeProvider } from "#mui/material/styles";
import { makeStyles } from "#material-ui/core/styles";
const theme = createTheme({
direction: "rtl"
});
const useStyles = makeStyles((theme) => ({
root: {
"& label": {
width: "100%",
textAlign: "right",
transformOrigin: "center",
"&.Mui-focused": {
transform: "translate(-5, -5px) scale(0)",
transformOrigin: "center",
},
},
},
}));
export default function CatalogTextBox(params) {
const classesStyles = useStyles();
return (
<ThemeProvider theme={theme}>
<div className="pt-2 col-sm" dir="rtl">
<TextField
className={classesStyles.root}
type={params.type}
id="outlined-basic"
label={params.text}
variant="outlined"
/>
</div>
</ThemeProvider>
);
}

Material-UI: Prevent Accordion summary vertical movement

Created this accordion and will use it as a menu item.
However, when I click Main title, accordion summary moves vertically downward.
How can I keep Main tile fixed while opening?
sandbox
import React from "react";
import {
Typography,
Grid,
Accordion,
AccordionSummary,
AccordionDetails,
ListItem
} from "#material-ui/core";
import { createStyles, makeStyles, Theme } from "#material-ui/core/styles";
import ExpandMoreIcon from "#material-ui/icons/ExpandMore";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
panelSummary: {
flexDirection: "row-reverse",
paddingLeft: "0px"
},
heading: {
fontSize: theme.typography.pxToRem(15),
fontWeight: theme.typography.fontWeightRegular
},
innerMenuItem: {
paddingLeft: "32px"
},
expanded: {
padding: "0px"
}
})
);
export default function App() {
const classes = useStyles();
return (
<Accordion>
<AccordionSummary
className={classes.panelSummary}
expandIcon={<ExpandMoreIcon />}
aria-controls="panel1a-content"
id="panel1a-header"
>
<Typography className={classes.heading}>Main title</Typography>
</AccordionSummary>
<AccordionDetails>
<Grid container direction="column">
<ListItem className={classes.innerMenuItem} button key={1}>
<Typography className={classes.heading}>Sub Item 1</Typography>
</ListItem>
<ListItem
className={classes.innerMenuItem}
button
key={2}>
<Typography className={classes.heading}>Sub Item 2</Typography>
</ListItem>
</Grid>
</AccordionDetails>
</Accordion>
);
}
You can just pass in disableGutters as true in version 5 of mui
<Accordion disableGutters>
// ....
</Accordion>
When expanded, the summary's vertical margin is set to some value, you need to reset it if you don't want to see the summary size changes during the expansion:
V5
<AccordionSummary
sx={{
"&.Mui-expanded": {
minHeight: 0
},
"& .MuiAccordionSummary-content.Mui-expanded": {
// margin from https://github.com/mui-org/material-ui/blob/cc0e2ab63e8be9ec4d51a49bfde17ef28fc77b9c/packages/mui-material/src/AccordionSummary/AccordionSummary.js#L64-L64
margin: "12px 0"
}
}}
>
V4
panelSummary: {
"&.Mui-expanded": {
minHeight: 0
},
"& .MuiAccordionSummary-content.Mui-expanded": {
margin: "auto"
}
},

Can't Make MouseEnter and MouseLeave Animation on React

I'm new at react. I'm using media cards to present some picture and small text and have been trying to give small sliding animation to my text and background layer. However I failed. Codes are below:
import React, { useState, Component } from 'react';
import { makeStyles } from '#material-ui/core/styles';
import CardActionArea from '#material-ui/core/CardActionArea';
import CardContent from '#material-ui/core/CardContent';
import CardMedia from '#material-ui/core/CardMedia';
import Typography from '#material-ui/core/Typography';
import Grid from '#material-ui/core/Grid';
import Paper from '#material-ui/core/Paper';
export default function SectionCard(props) {
const [areaHeight, setAreaHeight] = useState(100);
const [imgSize, setImgSize] = useState(1);
const [lineColor, setLineColor] = useState('dodgerblue');
const [textPosition, setTextPosition] = useState(0);
const useStyles = makeStyles((theme) => ({
root: {
maxWidth: 345,
},
media: {
height: 400,
scale: imgSize,
},
greyCover: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
height: areaHeight,
width: '100%',
bottom: 0,
position: 'absolute',
},
line: {
backgroundColor: lineColor,
height: 5,
width: '100%',
borderRadius: 0,
},
textPosition: {
paddingTop: textPosition,
fontFamily: 'Quicksand !important',
color: 'white !important',
},
body: {
fontFamily: 'Quicksand !important',
},
}));
const classes = useStyles();
return (
<Grid item xs={12} sm={4} onMouseEnter={() => (setAreaHeight('100%'), setLineColor('red'), setImgSize(1.2), setTextPosition('150px!important'))} onMouseLeave={() => (setAreaHeight(100), setLineColor('dodgerblue'), setImgSize(1), setTextPosition('0px !important'))}>
<CardActionArea>
<CardMedia
className={classes.media}
image={props.img}
/>
<CardContent className={classes.greyCover} style={{ fontFamily: 'Quicksand', }}>
<div className={classes.textPosition}>
<Typography gutterBottom variant="h5" component="h2" align="center">
{props.title}
</Typography>
<Typography component="p" align="center">
{props.text}
</Typography>
</div>
</CardContent>
</CardActionArea>
<Paper className={classes.line} />
</Grid>
);
}
Codes are probably messy. Sorry for that. onMouseEnter and onMouseLeave are working fine. I just want to see that greyCover class and text moves slowly to height 100% and text to mid of the frame.
I didn't want to use another packages yet I don't know how to use too to be honest.
What I'm doing wrong?

Material UI using tabs panel and CSS

I can not change the color and CSS from the TAB PANEL using material-ui Some ideas? :(
Looks like useStyle and theme are not working. I could change some other properties like scrollable but not the colors. I wonder if there is some conflict con other CSS, but I don't think so because the colors I see from the TABs are blue, I'm not using blue in my Web-App.
import PropTypes from 'prop-types';
import { makeStyles } from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Tabs from '#material-ui/core/Tabs';
import Tab from '#material-ui/core/Tab';
import Typography from '#material-ui/core/Typography';
import Box from '#material-ui/core/Box';
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`scrollable-auto-tabpanel-${index}`}
aria-labelledby={`scrollable-auto-tabpanel-${index}`}
{...other}
>
{value === index && (
<Box p={3}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired,
};
function a11yProps(index) {
return {
id: `scrollable-auto-tabpanel-${index}`,
'aria-controls': `scrollable-auto-tabpanel-${index}`,
};
}
function LinkTab(props) {
return (
<Tab
component="a"
onClick={(event) => {
event.preventDefault();
}}
{...props}
/>
);
}
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
margin: 0,
background: 'white',
},
}));
export default function NavTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<AppBar position="static">
<Tabs
variant="container-fluid"
value={value}
onChange={handleChange}
variant="scrollable"
scrollButtons="auto"
aria-label="scrollable auto tabs example"
centered
>
It's actually the AppBar that has that blue color. Upon reviewing the stylesheet, the individual tab items actually have transparent as a default value for background-color. So to solve this, just override the background of the root element of AppBar
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
margin: 0,
background: "white"
}
}));
export default function NavTabs() {
const classes = useStyles();
<AppBar position="static" classes={{ root: classes.root }}>
...

Resources