MaterialUI: how to align button in x-axis end of the axis? - reactjs

I have a navbar component that looks like
<AppBar position="static" color="inherit">
<Toolbar className={classes.toolbar}>
<IconButton edge="start" color="inherit" aria-label="menu" onClick={toggleDrawer(true)}>
<MenuIcon />
</IconButton>
<Link href="#" color="primary" underline="none" variant="h5" className={classes.brand}>
{brand}
</Link>
<Button variant="contained" color="secondary" className={classes.primaryAction}>{content['primary-action']}</Button>
</Toolbar>
However the <Button variant="contained> is placed before the Link, how can I place the button all the way to the end of the x axis?
This is the styles
const useStyles = makeStyles((theme: Theme) => ({
toolbar: {
minHeight: 200
},
brand: {
lineHeight: 1,
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)'
},
primaryAction: {
justifySelf: 'end'
},
iconWrapper: {
minWidth: 40,
},
}));

Try to apply flexGrow: 1 on the link and align it right:
<AppBar position="static" color="inherit">
<Toolbar className={classes.toolbar}>
<IconButton edge="start" color="inherit" aria-label="menu" onClick={toggleDrawer(true)}>
<MenuIcon />
</IconButton>
<Link href="#" color="primary" underline="none" variant="h5" sx={{ flexGrow: 1, textAlign: 'right' }}>
{brand}
</Link>
<Button variant="contained" color="secondary" className={classes.primaryAction}>{content['primary-action']}</Button>
</Toolbar>

Try using CSS flex to place the buttons and link inside the toolbar.
Here's a working CodeSandbox. (Note that if you want the Link to come after the Button, just move the component below the Button in the code as per the comment I left).
Here's the JSX:
<AppBar position="static" color="inherit">
<Toolbar className={classes.toolbar}>
<IconButton
edge="start"
color="inherit"
aria-label="menu"
onClick={toggleDrawer(true)}
>
<MenuIcon />
</IconButton>
<div className={classes.verticalCenter}>
{/* Just move this link to come after the Button below to change the order */}
<Link
href="#"
color="primary"
underline="none"
variant="h5"
className={classes.brand}
>
{brand}
</Link>
<Button
variant="contained"
color="secondary"
className={classes.primaryAction}
>
{content["primary-action"]}
</Button>
</div>
</Toolbar>
</AppBar>
And the CSS:
const useStyles = makeStyles((theme) => ({
toolbar: {
minHeight: 200,
display: "flex",
justifyContent: "space-between"
},
brand: {
lineHeight: 1,
padding: 16
},
primaryAction: {
justifySelf: "flex-end"
},
iconWrapper: {
minWidth: 40
},
verticalCenter: {
display: "flex",
alignItems: "center"
}
}));

Related

Specifying class for properties and styles for components and tags in React

I am pretty new to React. I was actually building an AppBar with a logo at center based on a suggestion in this post
<Box sx={{ display: "flex", alignItems: "center", flex: "1" }}>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={onDrawerOpen}
edge="start"
sx={{ mr: 2, ...(open && { display: "none" }) }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
TITLE
</Typography>
</Box>
But is there is any best way of specifying a single class or constant for these settings outside the definition like this
<IconButton class='settings'>
instead of all this
Tried the way #Dimitriy suggested and below my code
export default function AppBar({ open, onDrawerOpen }:any) {
const theme = useTheme();
const iconButtonOptions = {
color: "inherit",
ariaLabel: "open drawer",
onClick: onDrawerOpen,
edge: "start",
sx: {{ mr: 2, ...(open && { display: "none" }) }}
}
return (
<AppBar position="fixed" style={{ background: "#002a5e" }} open={open}>
<Toolbar>
<Box sx={{ display: "flex",flexDirection:"row", alignItems: "center", flex: "1" }}>
<IconButton {...iconButtonOptions} >
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
TITLE
</Typography>
</Box>
<Box
component="img"
sx={{
height: 32,
}}
alt="MyLogo"
src={logo}
/>
<Box sx={{ flex: "1" }}></Box>
</Toolbar>
</AppBar>
);
}
But its saying
Cannot redeclare block-scoped variable '(Missing)'.ts(2451)
Yes, there's actually is the way for that.
First, you can make a new object variable that will hold all of your props.
Example:
const iconBtnProps = {
color: 'inherit',
aria-label: 'open drawer',
onClick: onDrawerOpen,
edge: 'start',
sx: { mr: 2, ...(open && { display: "none" }) }
};
// render the <IconButton /> component and spread the object
<IconButton {...iconBtnProps} />
I would advise you to check styled-components. It would really make things easier for styling components.
You can't define jsx component props through classes. But if you want to make your jsx more understandable, or avoid repetition, you can move all the props into an object.
const iconButtonOptions = {
color: "inherit",
ariaLabel: "open drawer",
onClick: onDrawerOpen,
edge: "start",
sx: { mr: 2, ...(open && { display: "none" }) }
}
return (
<IconButton {...iconButtonOptions}>
<MenuIcon />
</IconButton>
)

How to shift logo (.png / .jpg) to center of navbar when the screen gets smaller for Material UI template?

I'm using an app bar with responsive menu template from Material UI. The template came with an original logo that is one of the MUI icons. I need to replace this MUI icon (AdbIcon in the code) with my actual logo, a .png/.jpg file.
But here's the before and after.
Before:
After:
How do I shift the logo to center of navbar when the screen gets smaller to make the site more responsive?
I have made some minor amendments and here's my code.
import { useState } from "react";
import { Fragment } from "react";
import { Outlet, Link } from "react-router-dom";
import AppBar from "#mui/material/AppBar";
import Box from "#mui/material/Box";
import Toolbar from "#mui/material/Toolbar";
import IconButton from "#mui/material/IconButton";
import Typography from "#mui/material/Typography";
import Menu from "#mui/material/Menu";
import MenuIcon from "#mui/icons-material/Menu";
import Container from "#mui/material/Container";
import Avatar from "#mui/material/Avatar";
import Button from "#mui/material/Button";
import Tooltip from "#mui/material/Tooltip";
import MenuItem from "#mui/material/MenuItem";
import AdbIcon from "#mui/icons-material/Adb";
import "./navigation-bar.styles.scss";
const pages = ["Page 1", "Page 2", "Page 3"];
const settings = ["Profile", "My purchases", "Logout"];
const NavigationBar = () => {
const [anchorElNav, setAnchorElNav] = useState(null);
const [anchorElUser, setAnchorElUser] = useState(null);
const handleOpenNavMenu = (event) => {
setAnchorElNav(event.currentTarget);
};
const handleOpenUserMenu = (event) => {
setAnchorElUser(event.currentTarget);
};
const handleCloseNavMenu = () => {
setAnchorElNav(null);
};
const handleCloseUserMenu = () => {
setAnchorElUser(null);
};
return (
<Fragment>
<AppBar position="fixed">
<Container maxWidth="xl">
<Toolbar disableGutters>
// This is the logo I want to add.
<Link>
<img
className="nav-logo"
src="https://cdn.dribbble.com/userupload/3158903/file/original-3f5abe8b99ff4ba4626ddf6660115182.jpg?compress=1&resize=752x"
alt="Porousway Logo"
/>
</Link>
// This is the original logo
<AdbIcon sx={{ display: { xs: "none", md: "flex" }, mr: 1 }} />
<Typography
variant="h6"
noWrap
component="a"
href="/"
sx={{
mr: 2,
display: { xs: "none", md: "flex" },
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
>
LOGO
</Typography>
<Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}>
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
<Menu
id="menu-appbar"
anchorEl={anchorElNav}
anchorOrigin={{
vertical: "bottom",
horizontal: "left",
}}
keepMounted
transformOrigin={{
vertical: "top",
horizontal: "left",
}}
open={Boolean(anchorElNav)}
onClose={handleCloseNavMenu}
sx={{
display: { xs: "block", md: "none" },
}}
>
{pages.map((page) => (
<MenuItem key={page} onClick={handleCloseNavMenu}>
<Typography textAlign="center">{page}</Typography>
</MenuItem>
))}
</Menu>
</Box>
<AdbIcon sx={{ display: { xs: "flex", md: "none" }, mr: 1 }} />
<Typography
variant="h5"
noWrap
component="a"
href=""
sx={{
mr: 2,
display: { xs: "flex", md: "none" },
flexGrow: 1,
fontFamily: "monospace",
fontWeight: 700,
letterSpacing: ".3rem",
color: "inherit",
textDecoration: "none",
}}
>
LOGO
</Typography>
<Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}>
{pages.map((page) => (
<Button
key={page}
onClick={handleCloseNavMenu}
sx={{ my: 2, color: "white", display: "block" }}
>
{page}
</Button>
))}
</Box>
<Box sx={{ flexGrow: 0 }}>
<Tooltip title="Open settings">
<IconButton onClick={handleOpenUserMenu} sx={{ p: 0 }}>
<Avatar alt="Remy Sharp" src="/static/images/avatar/2.jpg" />
</IconButton>
</Tooltip>
<Menu
sx={{ mt: "45px" }}
id="menu-appbar"
anchorEl={anchorElUser}
anchorOrigin={{
vertical: "top",
horizontal: "right",
}}
keepMounted
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
open={Boolean(anchorElUser)}
onClose={handleCloseUserMenu}
>
{settings.map((setting) => (
<MenuItem key={setting} onClick={handleCloseUserMenu}>
<Typography textAlign="center">{setting}</Typography>
</MenuItem>
))}
</Menu>
</Box>
</Toolbar>
</Container>
</AppBar>
<Outlet />
</Fragment>
);
};
export default NavigationBar;
The original can be found here.
Edit: To include "LOGO" in code.
Using the example in the documentation, you can see that there are actually two separate "logos" and they just hide one or the other based on the screen size. You could do the same thing:
// excerpted return:
return (
<AppBar position="static">
<Container maxWidth="xl">
<Toolbar disableGutters>
{/* YOUR LOGO HERE FOR MD+ SCREENS */}
<Link
href="#"
underline="none"
sx={{
mr: 2,
display: { xs: 'none', md: 'flex' },
}}
>
<img
className="nav-logo"
src="https://cdn.dribbble.com/userupload/3158903/file/original-3f5abe8b99ff4ba4626ddf6660115182.jpg?compress=1&resize=752x"
alt="Porousway Logo"
width="40"
height="40"
/>
</Link>
{/* END LOGO */}
<Box sx={{ flexGrow: 1, display: { xs: 'flex', md: 'none' } }}>
<IconButton
size="large"
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleOpenNavMenu}
color="inherit"
>
<MenuIcon />
</IconButton>
<Menu
id="menu-appbar"
anchorEl={anchorElNav}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'left',
}}
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
open={Boolean(anchorElNav)}
onClose={handleCloseNavMenu}
sx={{
display: { xs: 'block', md: 'none' },
}}
>
{pages.map((page) => (
<MenuItem key={page} onClick={handleCloseNavMenu}>
<Typography textAlign="center">{page}</Typography>
</MenuItem>
))}
</Menu>
</Box>
{/* YOUR LOGO HERE FOR SMALL SCREENS */}
<Link
href="#"
underline="none"
sx={{
mr: 2,
display: { xs: 'flex', md: 'none' },
width: '100%',
justifyContent: 'center',
}}
>
<img
className="nav-logo"
src="https://cdn.dribbble.com/userupload/3158903/file/original-3f5abe8b99ff4ba4626ddf6660115182.jpg?compress=1&resize=752x"
alt="Porousway Logo"
width="40"
height="40"
/>
</Link>
{/* END LOGO */}
<Box sx={{ flexGrow: 1, display: { xs: 'none', md: 'flex' } }}>
{pages.map((page) => (
<Button
key={page}
onClick={handleCloseNavMenu}
sx={{ my: 2, color: 'white', display: 'block' }}
>
{page}
</Button>
))}
</Box>
<Box sx={{ flexGrow: 0 }}>
<Tooltip title="Open settings">
<IconButton onClick={handleOpenUserMenu} sx={{ p: 0 }}>
<Avatar alt="Remy Sharp" src="/static/images/avatar/2.jpg" />
</IconButton>
</Tooltip>
<Menu
sx={{ mt: '45px' }}
id="menu-appbar"
anchorEl={anchorElUser}
anchorOrigin={{
vertical: 'top',
horizontal: 'right',
}}
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'right',
}}
open={Boolean(anchorElUser)}
onClose={handleCloseUserMenu}
>
{settings.map((setting) => (
<MenuItem key={setting} onClick={handleCloseUserMenu}>
<Typography textAlign="center">{setting}</Typography>
</MenuItem>
))}
</Menu>
</Box>
</Toolbar>
</Container>
</AppBar>

Change the shape of the card when the screen is minimized and when the workspace consists of two words

I have a project in order to organize tasks within companies, and I have a group of workspace that I must display, but when viewing the card changes the shape of the card when the screen is minimized and only when the name of the workspace consists of two words.
As it is clear in the picture, I have a workspace whose name is Heba Youssef, and we notice the change in the shape of the card style when the screen is minimized.
How can I solve this problem?
code.tsx:
interface WorkspaceCardProps {
workspace: Workspace;
}
let theme = createMuiTheme();
theme = responsiveFontSizes(theme);
const WorkspaceCard: FC<WorkspaceCardProps> = (props) => {
const { workspace } = props;
const user = useAuthModule((state) => state.user.user);
console.log('user workspace: ', workspace)
console.log('user' , user.name)
const fileSelectedHandler = event => {
console.log(event)
}
const navigation = useNavigate();
const fileUploadHandler = ()=>{
}
return (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
p: 3
}}
>
<Card
style={{maxWidth: "24rem"}}
sx={{py: '20px'}}>
<Box
sx={{
pl: 3,
pr:3,
pb:3,
pt:2
}}
>
<Box
sx={{
alignItems: 'center',
display: 'flex',
}}
>
<Avatar
onClick={fileUploadHandler}
onChange={fileSelectedHandler}
style={{height: "5.6rem", width: "5.6rem"}}
alt="Author"
src="https://t4.ftcdnA25Jjm2q.jpg"
// src={workspace.author.avatar}
>
</Avatar>
<Box sx={{ml: 2}}>
<Link
color="textPrimary"
component={RouterLink}
to="#"
variant="h6"
style={{textAlign: "center", fontSize: "1.9rem",
paddingLeft: "0.8rem"}}
>
{workspace.name}
{/*Slark*/}
</Link>
<Typography
color="textSecondary"
variant="body2"
style={{textAlign: "center", paddingLeft: "0.8rem"}}
>
by
{' '}
<Link
color="textPrimary"
component={RouterLink}
to="#"
variant="subtitle2"
>
{user.name}
</Link>
</Typography>
</Box>
</Box>
</Box>
<Divider />
<Box
sx={{
alignItems: 'center',
display: 'flex',
pl: 2,
pr: 3,
pt:2
}}
>
<Box
sx={{
alignItems: 'center',
display: 'flex',
ml: 2
}}
>
<UsersIcon fontSize="small"/>
<Typography
color="textSecondary"
sx={{ml: 1}}
variant="subtitle2"
>
{/*{workspace.membersCount}*/}
4
</Typography>
</Box>
<Box sx={{
ml: 2
}}
>
<Button>
<a href={`/workspace-settings/settings/${workspace._id}`} >
<ViewComfyRoundedIcon style={{fontSize: 30}}/>
</a>
</Button>
</Box>
</Box>
</Card>
</Box>
);
};
WorkspaceCard.propTypes = {
// #ts-ignore
workspace: PropTypes.object.isRequired
};
export default WorkspaceCard;
I believe you are using the Grid component. You should specify a higher value for either xs, sm, md, ... You have to guess how long a workspace name typically is and set the value(s) accordingly.
The other way to you may consider is to add noWrap the following:
<Link
color="textPrimary"
component={RouterLink}
to="#"
variant="h6"
style={{
textAlign: "center", fontSize: "1.9rem",
paddingLeft: "0.8rem"
}}
noWrap
>
{workspace.name}
</Link>
Although I'm not sure whether it is a good UI design, given you have left little space to display text that is potentially very long.

Add badge to mobileMenu in material-ui

I'm trying to add a badge to the mobileMenu (3 dots) but I can't.
On the right is a mockup of what I'm trying to do, done in Photoshop.
const renderMobileMenu = (
<Menu
anchorEl={mobileMoreAnchorEl}
anchorOrigin={{ vertical: "top", horizontal: "right" }}
id={mobileMenuId}
keepMounted
transformOrigin={{ vertical: "top", horizontal: "right" }}
open={isMobileMenuOpen}
onClose={handleMobileMenuClose}
>
<MenuItem>
<IconButton aria-label="show 11 new notifications" color="inherit">
<Badge badgeContent={cartBadge} color="secondary">
<ShoppingCartIcon />
</Badge>
</IconButton>
<p>Cart</p>
</MenuItem>
<MenuItem onClick={handleProfileMenuOpen}>
<IconButton
aria-label="account of current user"
aria-controls="primary-search-account-menu"
aria-haspopup="true"
color="inherit"
>
<AccountCircle />
</IconButton>
<p>Profile</p>
</MenuItem>
</Menu>
);
Just use
import React from "react";
import { fade, makeStyles } from "#material-ui/core/styles";
import AppBar from "#material-ui/core/AppBar";
import Toolbar from "#material-ui/core/Toolbar";
import IconButton from "#material-ui/core/IconButton";
import Typography from "#material-ui/core/Typography";
import InputBase from "#material-ui/core/InputBase";
import Badge from "#material-ui/core/Badge";
import MenuItem from "#material-ui/core/MenuItem";
import Menu from "#material-ui/core/Menu";
import MenuIcon from "#material-ui/icons/Menu";
import SearchIcon from "#material-ui/icons/Search";
import AccountCircle from "#material-ui/icons/AccountCircle";
import MailIcon from "#material-ui/icons/Mail";
import NotificationsIcon from "#material-ui/icons/Notifications";
import MoreIcon from "#material-ui/icons/MoreVert";
const useStyles = makeStyles((theme) => ({
grow: {
flexGrow: 1
},
menuButton: {
marginRight: theme.spacing(2)
},
title: {
display: "none",
[theme.breakpoints.up("sm")]: {
display: "block"
}
},
search: {
position: "relative",
borderRadius: theme.shape.borderRadius,
backgroundColor: fade(theme.palette.common.white, 0.15),
"&:hover": {
backgroundColor: fade(theme.palette.common.white, 0.25)
},
marginRight: theme.spacing(2),
marginLeft: 0,
width: "100%",
[theme.breakpoints.up("sm")]: {
marginLeft: theme.spacing(3),
width: "auto"
}
},
searchIcon: {
padding: theme.spacing(0, 2),
height: "100%",
position: "absolute",
pointerEvents: "none",
display: "flex",
alignItems: "center",
justifyContent: "center"
},
inputRoot: {
color: "inherit"
},
inputInput: {
padding: theme.spacing(1, 1, 1, 0),
// vertical padding + font size from searchIcon
paddingLeft: `calc(1em + ${theme.spacing(4)}px)`,
transition: theme.transitions.create("width"),
width: "100%",
[theme.breakpoints.up("md")]: {
width: "20ch"
}
},
sectionDesktop: {
display: "none",
[theme.breakpoints.up("md")]: {
display: "flex"
}
},
sectionMobile: {
display: "flex",
[theme.breakpoints.up("md")]: {
display: "none"
}
}
}));
export default function PrimarySearchAppBar() {
const classes = useStyles();
const [anchorEl, setAnchorEl] = React.useState(null);
const [mobileMoreAnchorEl, setMobileMoreAnchorEl] = React.useState(null);
const [messageCount, setMessageCount] = React.useState(4);
const [nottificationCount, setNottificationCount] = React.useState(11);
const [totalNotificationsCount, setTotalNotificationsCount] = React.useState(11);
React.useEffect(() => {
setTotalNotificationsCount(messageCount + nottificationCount);
}, [messageCount, nottificationCount])
const isMenuOpen = Boolean(anchorEl);
const isMobileMenuOpen = Boolean(mobileMoreAnchorEl);
const handleProfileMenuOpen = (event) => {
setAnchorEl(event.currentTarget);
};
const handleMobileMenuClose = () => {
setMobileMoreAnchorEl(null);
};
const handleMenuClose = () => {
setAnchorEl(null);
handleMobileMenuClose();
};
const handleMobileMenuOpen = (event) => {
setMobileMoreAnchorEl(event.currentTarget);
};
const menuId = "primary-search-account-menu";
const renderMenu = (
<Menu
anchorEl={anchorEl}
anchorOrigin={{ vertical: "top", horizontal: "right" }}
id={menuId}
keepMounted
transformOrigin={{ vertical: "top", horizontal: "right" }}
open={isMenuOpen}
onClose={handleMenuClose}
>
<MenuItem onClick={handleMenuClose}>Profile</MenuItem>
<MenuItem onClick={handleMenuClose}>My account</MenuItem>
</Menu>
);
const mobileMenuId = "primary-search-account-menu-mobile";
const renderMobileMenu = (
<Menu
anchorEl={mobileMoreAnchorEl}
anchorOrigin={{ vertical: "top", horizontal: "right" }}
id={mobileMenuId}
keepMounted
transformOrigin={{ vertical: "top", horizontal: "right" }}
open={isMobileMenuOpen}
onClose={handleMobileMenuClose}
>
<MenuItem>
<IconButton aria-label="show 4 new mails" color="inherit">
<Badge badgeContent={messageCount} color="secondary">
<MailIcon />
</Badge>
</IconButton>
<p>Messages</p>
</MenuItem>
<MenuItem>
<IconButton aria-label="show 11 new notifications" color="inherit">
<Badge badgeContent={nottificationCount} color="secondary">
<NotificationsIcon />
</Badge>
</IconButton>
<p>Notifications</p>
</MenuItem>
<MenuItem onClick={handleProfileMenuOpen}>
<IconButton
aria-label="account of current user"
aria-controls="primary-search-account-menu"
aria-haspopup="true"
color="inherit"
>
<AccountCircle />
</IconButton>
<p>Profile</p>
</MenuItem>
</Menu>
);
return (
<div className={classes.grow}>
<AppBar position="static">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="open drawer"
>
<MenuIcon />
</IconButton>
<Typography className={classes.title} variant="h6" noWrap>
Material-UI
</Typography>
<div className={classes.search}>
<div className={classes.searchIcon}>
<SearchIcon />
</div>
<InputBase
placeholder="Search…"
classes={{
root: classes.inputRoot,
input: classes.inputInput
}}
inputProps={{ "aria-label": "search" }}
/>
</div>
<div className={classes.grow} />
<div className={classes.sectionDesktop}>
<IconButton aria-label="show 4 new mails" color="inherit">
<Badge badgeContent={4} color="secondary">
<MailIcon />
</Badge>
</IconButton>
<IconButton aria-label="show 17 new notifications" color="inherit">
<Badge badgeContent={17} color="secondary">
<NotificationsIcon />
</Badge>
</IconButton>
<IconButton
edge="end"
aria-label="account of current user"
aria-controls={menuId}
aria-haspopup="true"
onClick={handleProfileMenuOpen}
color="inherit"
>
<AccountCircle />
</IconButton>
</div>
<div className={classes.sectionMobile}>
<IconButton
aria-label="show more"
aria-controls={mobileMenuId}
aria-haspopup="true"
onClick={handleMobileMenuOpen}
color="inherit"
>
<Badge badgeContent={totalNotificationsCount} color="secondary">
<MoreIcon />
</Badge>
</IconButton>
</div>
</Toolbar>
</AppBar>
{renderMobileMenu}
{renderMenu}
</div>
);
}

How can I center Logo and text in material-ui Appbar?

I use Material-Ui Appbar and want to simply center my logo and title in the middle (See the picture).
But I tried different methods, still nothing works. Following is my code, do you know what's wrong with my code?
You can also try the code in codesandbox:
https://codesandbox.io/s/material-ui-appbar-logo-7q80d?file=/src/MyAppBar.tsx:797-1455
Here is my clasess:
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
flexGrow: 1
},
menuButton: {
marginRight: theme.spacing(2)
},
title: {
flexGrow: 1,
textAlign: "center"
},
logo: {
maxWidth: 40,
marginRight: '10px'
}
})
);
Here is my component code:
export function ButtonAppBar() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="menu"
>
<MenuIcon />
</IconButton>
<img src={logo} alt="Kitty Katty!" className={classes.logo} />
<Typography variant="h6" className={classes.title}>
Kitty Katty!
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
Thanks very much for the help!
Try this:
<Typography variant="h6" className={classes.title}>
<img src={logo} alt="Kitty Katty!" className={classes.logo} />
Kitty Katty!
</Typography>
import React from "react";
import { createStyles, makeStyles, Theme } 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 Button from "#material-ui/core/Button";
import IconButton from "#material-ui/core/IconButton";
import MenuIcon from "#material-ui/icons/Menu";
import logo from "./images/logo.png";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
flexGrow: 1
},
menuButton: {
marginRight: theme.spacing(2)
},
toolbar:{
justifyContent:'space-between'
},
container:{
display:'flex',
alignItems:'center',
},
title: {
flexGrow: 1,
textAlign: "center"
},
logo: {
maxWidth: 40,
marginRight: '10px'
}
})
);
export function ButtonAppBar() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar classes={{root:classes.toolbar}}>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="menu"
>
<MenuIcon />
</IconButton>
<div className={classes.container}>
<img src={logo} alt="Kitty Katty!" className={classes.logo} />
<Typography variant="h6" className={classes.title}>
Kitty Katty!
</Typography>
</div>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton
edge="start"
className={classes.menuButton}
color="inherit"
aria-label="menu"
>
<MenuIcon />
</IconButton>
<Typography variant="h6" className={classes.title} id="appBar">
<img src={logo} alt="Kitty Katty!" className={classes.logo} />
Kitty Katty!
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
Then in your style.css add this
#appBar {
display: flex;
justify-content: center;
align-items: center;
}

Resources