My react Material UI buttons aren't rendering - reactjs

I've written the same code everywhere but it doesn't render my buttons. It does render but is not visible. The button are present between the logo and the search input.
There aren't any errors nor build fail to debug the problem.
I tried copying and pasting the code from the second too!
import React from "react";
import { Link } from "react-router-dom";
import { Box, Container, ThemeProvider, Input, useScrollTrigger, Slide, AppBar, Toolbar } from '#mui/material'
import { Button } from "#mui/material";
import customTheme from "./Theme"
function HideOnScroll(props) {
const { children, window } = props;
const trigger = useScrollTrigger({
target: window ? window() : undefined,
});
return (
<Slide appear={false} direction="down" in={!trigger}>
{children}
</Slide>
);
}
const ariaLabel = { 'aria-label': 'description' };
function Navigation(props) {
return (
<Container>
<ThemeProvider theme={ customTheme }>
<HideOnScroll>
<AppBar color={"primary"}>
<Toolbar>
<Box m={1} p={1}>
<Link to=""><img src="/static/brand-logo.svg" alt="brand-logo" /></Link>
</Box>
<Box m={1} p={1}>
<Button variant="outlined" style={{ borderRadius: 20 }} href="/">Home</Button>
<Button variant="outlined" style={{ borderRadius: 20 }} href="/blog">Blogs</Button>
<Button variant="outlined" style={{ borderRadius: 20 }} href="/create-blog">Create</Button>
</Box>
<Box m={1} p={1}>
<Input color={"secondary"} placeholder="Can't search :)" inputProps={ariaLabel} />
</Box>
</Toolbar>
</AppBar>
</HideOnScroll>
<Toolbar />
</ThemeProvider>
</Container>
);
}
export default Navigation;
Another button code which works
import React from "react";
import { Grid, Divider, Typography, Button } from "#mui/material";
import InstagramIcon from '#mui/icons-material/Instagram';
import LinkedInIcon from '#mui/icons-material/LinkedIn';
import GitHubIcon from '#mui/icons-material/GitHub';
export default function Home(props) {
return(
<Grid container spacing={4} justifyContent="center" alignItems="center" color={"primary"}>
<Grid item xs={12} sm={6} lg={4}>
<Typography variant="h1" component="div" gutterBottom>A painting of my life.</Typography>
<Button size="large" variant="outlined" style={{ borderRadius: 50, fontSize: 40 }} href="/blog">Peek My Diary</Button>
</Grid>
</Grid>
);
}

Related

How to move a React component using CSS ? No functions or classes are working

I am trying to move a button in my appbar to the right. Here is what it looks like:
I have tried 2 ways to fix this:
return (
<AppBar
position="sticky">
<Toolbar
sx = {{background:'black', float:'right'}}
variant="dense">
<CustomButton
text='Upload'
sx = {{float:'right'}}
endIcon={<UploadIcon/>}/>
</Toolbar>
</AppBar>
)
}
export default Header;
I also tried to do the same thing with a useStyles hook:
import UploadIcon from "#mui/icons-material/Upload";
import CustomButton from "./CustomButton";
import AppBar from '#mui/material/AppBar';
import Toolbar from '#mui/material/Toolbar';
import { makeStyles } from '#material-ui/core/styles';
import {createMuiTheme} from '#material-ui/core/styles';
const useStyles = makeStyles({
root: {
float:'right'
}
});
const Header = () => {
const classes = useStyles();
return (
<AppBar position="sticky">
<Toolbar
sx = {{background:'black'}}
variant="dense">
<CustomButton text='Upload'
className={classes.root}
endIcon={<UploadIcon/>}/>
</Toolbar>
</AppBar>
)
}
export default Header;
But none of them have worked and the button won't move to the right.
The code of my button component is as follows:
import {Button} from '#material-ui/core';
import UploadIcon from '#mui/icons-material/Upload';
const CustomButton = ({float,text, endIcon}) => {
return (
<div className='CustomButton'>
<Button style={{fontWeight: 'bold'}}
variant="contained"
size='small'
endIcon={endIcon}>
{text}
</Button>
</div>
)
}
export default CustomButton;
In mui, u will use flex alot.
return (
<AppBar
position="sticky">
<Toolbar
sx = {{background:'black', display: 'flex', justifyContent: 'flex-end'}}
variant="dense">
<CustomButton
text='Upload'
endIcon={<UploadIcon/>}/>
</Toolbar>
</AppBar>
)
}
export default Header;
Since mui v5, also nice to learn how to use Stack.
<Toolbar>
<Stack direction="row" justifyContent="space-between">
<MyLogo /> //extreme left
<MyButton /> //extreme right
</Stack>
</Toolbar>
Stack can also be nested
<Toolbar>
<Stack direction="row" justifyContent="space-between">
<Stack direction="row"> // content in extreme left
<MyLogo />
<MyOtherLeftComponent />
</Stack>
<MyButton /> //extreme right
</Stack>
</Toolbar>
On the toolbar use following style
display:flex;
justify-content:flex-end.

Create a grid with variable card heights using material-ui react

I am trying to create a grid similar to this website by using Material-UI. However when the height of one card changes every card height changes. I tried to use direction="row" and direction="column" but it doesn't seem to work. I was wondering if there a way to change the height of the image inside depending on the size of the image while having a grid like the website above.
All I can see at the moment is this:
Here is my code for the card:
import React from 'react';
import Card from '#material-ui/core/Card';
import CardHeader from '#material-ui/core/CardHeader';
import CardMedia from '#material-ui/core/CardMedia';
import CardContent from '#material-ui/core/CardContent';
import CardActions from '#material-ui/core/CardActions';
import Avatar from '#material-ui/core/Avatar';
import IconButton from '#material-ui/core/IconButton';
// import Typography from '#material-ui/core/Typography';
import FavoriteIcon from '#material-ui/icons/Favorite';
import ShareIcon from '#material-ui/icons/Share';
import MoreVertIcon from '#material-ui/icons/MoreVert';
import { useStyles } from '../style/card.styles';
export default function Cards({ profile, sourceImage }) {
const classes = useStyles();
return (
<Card className={classes.root} style={{ height: '100%' }}>
<CardHeader
avatar={
<Avatar aria-label="recipe" src={profile} className={classes.avatar}>
</Avatar>
}
action={
<IconButton aria-label="settings">
<MoreVertIcon />
</IconButton>
}
title="Shrimp and Chorizo Paella"
subheader="September 14, 2016"
/>
<CardContent>
<CardMedia
className={classes.media}
image={sourceImage}
title="Paella dish"
/>
{/* <img src={sourceImage} style={{ maxHeight: '20rem' }} /> */}
</CardContent>
<CardActions disableSpacing>
<IconButton aria-label="add to favorites">
<FavoriteIcon />
</IconButton>
<IconButton aria-label="share">
<ShareIcon />
</IconButton>
</CardActions>
</Card>
);
}
And the layout page:
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import Grid from '#material-ui/core/Grid';
import Cards from './Card';
import vertical from '../images/v1.jfif';
import horizontal from '../images/h1.png';
import '../style/card.css';
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
},
paper: {
height: 140,
width: 100,
},
control: {
padding: theme.spacing(2),
}
}));
export default function HomePage() {
const imagess = [vertical, horizontal, vertical, horizontal]
const classes = useStyles();
return (
<Grid container justify="center" className={classes.root} spacing={2}>
<Grid item xs={12}>
<Grid container alignItems="flex-start" justify="center" spacing={1}>
{imagess.map((image, index) => (
<Grid key={index} item>
<Cards profile={""} sourceImage={image} />
</Grid>
))}
</Grid>
<Grid container alignItems="flex-start" justify="center" spacing={1}>
{imagess.map((image, index) => (
<Grid key={index} item>
<Cards profile={""} sourceImage={image} />
</Grid>
))}
</Grid>
</Grid>
</Grid>
);
}
I think what you are looking for is a masonry grid. It's worth a google search.
What these packages do is calculate positions within a container on page load and resize. One example for react could be react-responsive-masonry
Example usage with react-responsive-masonry
import React from "react"
import Masonry, {ResponsiveMasonry} from "react-responsive-masonry"
// The number of columns change by resizing the window
class MyWrapper extends React.Component {
render() {
return (
<ResponsiveMasonry
columnsCountBreakPoints={{350: 1, 750: 2, 900: 3}}
>
<Masonry>
<ChildA />
<ChildB />
{/* Children */}
<ChildY />
<ChildZ />
</Masonry>
</ResponsiveMasonry>
)
}
}
// The number of columns don't change by resizing the window
class MyWrapper extends Component {
render() {
return (
<Masonry columnsCount={3}>
<ChildA />
<ChildB />
{/* Children */}
<ChildY />
<ChildZ />
</Masonry>
)
}
}
In case of the material ui you would basically replace the Grid and only render cards inside the Masonry.

How can I reuse code wrapping Material-UI's Grid while varying the number of columns?

I would like the second grid block under the title "Other Projects of Note" to be mapped through as a 3 column grid. How can I do this without creating a new component? Material-UI controls it's columns with the grid item xs={12} sm={6} and on the 3 column grid I'd need it to read as grid item xs={12} sm={6} lg={4}.
It seems like I'd be copying and pasting the <Card /> component and renaming it to achieve this. I'd like to avoid that. Demo below:
codesandbox
Here's the code for my current Card component:
import React from 'react';
import Grid from '#material-ui/core/Grid';
import Card from '#material-ui/core/Card';
import CardActionArea from '#material-ui/core/CardActionArea';
import CardActions from '#material-ui/core/CardActions';
import { makeStyles } from '#material-ui/core/styles';
import { loadCSS } from 'fg-loadcss';
import CardContent from '#material-ui/core/CardContent';
import CardMedia from '#material-ui/core/CardMedia';
import Button from '#material-ui/core/Button';
import Typography from '#material-ui/core/Typography';
import Box from '#material-ui/core/Box';
import Icon from '#material-ui/core/Icon';
import GitHubIcon from '#material-ui/icons/GitHub';
import Tooltip from '#material-ui/core/Tooltip';
import Zoom from '#material-ui/core/Zoom';
import Link from '#material-ui/core/Link';
const useStyles = makeStyles((theme) => ({
root: {
maxWidth: '100%',
flexGrow: 1,
},
cardGrid: {
paddingTop: theme.spacing(2),
},
media: {
height: 340,
},
button: {
margin: theme.spacing(1),
},
arrow: {
color: theme.palette.common.black,
},
tooltip: {
backgroundColor: theme.palette.common.black,
},
icons: {
'& > .fab': {
marginRight: theme.spacing(4),
},
margin: '1rem 0',
},
}));
function TwoCard(props) {
const classes = useStyles();
React.useEffect(() => {
const node = loadCSS(
'https://use.fontawesome.com/releases/v5.12.0/css/all.css',
document.querySelector('#font-awesome-css')
);
return () => {
node.parentNode.removeChild(node);
};
}, []);
return (
<>
<Grid item xs={12} sm={6}>
<Card>
<CardActionArea>
<CardMedia
className={classes.media}
image={props.card.image}
title='Contemplative Reptile'
/>
<CardContent className={classes.content}>
<Typography gutterBottom variant='h5' component='h2'>
{props.card.project}
</Typography>
<Typography variant='subtitle1' gutterBottom>
{props.card.framework}
</Typography>
<Typography gutterBottom variant='body2' component='p'>
{props.card.description}
</Typography>
<Box className={classes.icons}>
<Typography gutterBottom variant='subtitle2'>
TOOLS USED
</Typography>
<Tooltip
TransitionComponent={Zoom}
arrow
title='REACT'
aria-label='react'
>
<Icon className='fab fa-react fa-3x' color='primary' />
</Tooltip>
<Tooltip
TransitionComponent={Zoom}
arrow
title='HTML5'
aria-label='add'
>
<Icon className='fab fa-html5 fa-3x' color='primary' />
</Tooltip>
<Tooltip
TransitionComponent={Zoom}
arrow
title='CSS3'
aria-label='add'
>
<Icon className='fab fa-css3 fa-3x' color='primary' />
</Tooltip>
</Box>
</CardContent>
</CardActionArea>
<CardActions>
<Button variant='outlined' size='small' color='primary'>
<Link
href={props.card.projectUrl}
target='_blank'
rel='noopener noreferrer'
className={classes.links}
underline='none'
color='inherit'
>
View Project
</Link>
</Button>
<Button
variant='outlined'
size='small'
color='primary'
className={classes.button}
endIcon={<GitHubIcon />}
>
<Link
href={props.card.githubUrl}
target='_blank'
rel='noopener noreferrer'
underline='none'
color='inherit'
>
Code
</Link>
</Button>
</CardActions>
</Card>
</Grid>
</>
);
}
export default TwoCard;
and here's the code that uses that Card component:
import React from "react";
import Grid from "#material-ui/core/Grid";
import Card from "./Card.js";
import { Typography } from "#material-ui/core";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
headings: {
padding: "20px 0"
}
}));
function Projects(props) {
const classes = useStyles();
let cards = props.data.map((card, i) => {
return <Card card={card} key={i} />;
});
return (
<>
<Typography variant="h4" gutterBottom>
Featured Projects
</Typography>
<Grid container spacing={2}>
{cards}
</Grid>
<Typography variant="h4" className={classes.headings}>
Other Projects of note
</Typography>
<Grid container spacing={2}>
{cards}
</Grid>
</>
);
}
export default Projects;
You can pass a prop to your Card component to control whether it is two-column or three-column. For instance, you can pass a maxColumns prop and use it in the following manner:
<Grid item xs={12} sm={6} lg={maxColumns > 2 ? 4 : undefined}>
Then your Projects component can pass the prop (in my example, I'm defaulting maxColumns to 2, so it doesn't need to be specified in that case):
import React from "react";
import Grid from "#material-ui/core/Grid";
import Card from "./Card.js";
import { Typography } from "#material-ui/core";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
headings: {
padding: "20px 0"
}
}));
function Projects(props) {
const classes = useStyles();
let twoColumnCards = props.data.map((card, i) => {
return <Card card={card} key={i} />;
});
let threeColumnCards = props.data.map((card, i) => {
return <Card maxColumns={3} card={card} key={i} />;
});
return (
<>
<Typography variant="h4" gutterBottom>
Featured Projects
</Typography>
<Grid container spacing={2}>
{twoColumnCards}
</Grid>
<Typography variant="h4" className={classes.headings}>
Other Projects of note
</Typography>
<Grid container spacing={2}>
{threeColumnCards}
</Grid>
</>
);
}
export default Projects;

Place Material-ui icon inside circular progress indicator

I would like to use a circular progress indicator from Material-UI in the header of my app.
But I just dont know how to fit nicely a download icon from Material Icons inside so that progress bar will move around the icon. Here is what I have now:
and I want to achieve this:
I tried to place icon with absolute positioning, but probably there is a better idea
import React from "react";
import ReactDOM from "react-dom";
import CircularProgress from "#material-ui/core/CircularProgress";
import Button from "#material-ui/core/Button";
import AppBar from '#material-ui/core/AppBar';
import Toolbar from '#material-ui/core/Toolbar';
import IconButton from '#material-ui/core/IconButton';
import VerticalAlignBottomIcon from '#material-ui/icons/VerticalAlignBottom';
function CircularStatic() {
const [completed, setCompleted] = React.useState(0);
React.useEffect(() => {
function progress() {
setCompleted((prevCompleted) =>
prevCompleted >= 100 ? 0 : prevCompleted + 10
);
}
const timer = setInterval(progress, 1000);
return () => {
clearInterval(timer);
};
}, []);
return (
<div>
<Button variant="contained" color="primary">
<CircularProgress variant="static" value={completed} color="inherit">
</CircularProgress>
</Button>
</div>
);
}
function ButtonAppBar() {
return (
<AppBar position="static" style={{ backgroundColor: 'teal' }}>
<Toolbar>
<IconButton edge="start" color="inherit">
<VerticalAlignBottomIcon />
<CircularStatic/>
</IconButton>
</Toolbar>
</AppBar>
);
}
ReactDOM.render(<ButtonAppBar />, document.getElementById("root"));
I customized a little, the Material-UI documentation example here
function CircularProgressWithContent(props) {
return (
<Box position="relative" display="inline-flex">
<CircularProgress />
<Box
top={0}
left={0}
bottom={0}
right={0}
position="absolute"
display="flex"
alignItems="center"
justifyContent="center"
>
<Typography variant="caption" component="div" color="textSecondary">
{props.content}
</Typography>
</Box>
</Box>
);
}
And to use it :
<CircularProgressWithLabel content={<LockOutlinedIcon />} />
It's perfectible but it works well for your use case.

material-ui button active style

I am trying to workout how I would have appbar buttons that link to components show a different style when active.
I should also note: this is a my first material-ui app and the docs are not so clear.
Basically I want an underline on the button that is active.
<Button color="inherit" component={Link} to={"/"}>Home</Button>
<Button color="inherit" component={Link} to={"/About"}>About</Button>
My full code.
import React, {useState } from 'react';
import { Link as Links } from 'react-router-dom';
import ReactDOM from 'react-dom';
import { makeStyles } from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Fab from '#material-ui/core/Fab';
//import Icon from '#material-ui/core/Icon';
import Toolbar from '#material-ui/core/Toolbar';
import Link from '#material-ui/core/Link';
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';
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
menuButton: {
marginRight: theme.spacing(2),
},
fab: {
position: 'absolute',
background:'red',
bottom: theme.spacing(2),
right: theme.spacing(2),
"&:hover, &:focus":{
background:'black',
}
},
title: {
flexGrow: 1,
align:'center',
},
}));
function Nav() {
const classes = useStyles();
const [icon,setIcon] = useState(false)
const fabIcon = {
color: 'white',
fontSize: 40,
};
const handleClick = e => {
setIcon(!icon)
{ icon ? document.getElementById("player").play() : document.getElementById("player").pause() }
}
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" align="center" className={classes.title}>
News
</Typography>
<Button color="inherit">Login</Button>
<Button color="inherit" component={Links} to={"/"} linkButton={true}>Home</Button>
<Button color="inherit" component={Links} to={"/about"}>About</Button>
</Toolbar>
</AppBar>
<AppBar position="static">
</AppBar>
<audio id="player">
<source
src="https://samcloud.spacial.com/api/listen?sid=106487&m=sc&rid=184639"
type="audio/mpeg"
/>
</audio>
<Fab aria-label='test' className={classes.fab}>
<i className='large material-icons' style={fabIcon} onClick={handleClick}>
{ icon ? 'play_circle_outline' : 'pause_circle_outline'}</i>
</Fab>
</div>
);
}
export default class GrandChild extends React.Component {
componentDidMount() {
setTimeout(function(){ document.getElementById("player").play() }, 3000);
}
value() {
return ReactDOM.findDOMNode(this.refs.input).value;
}
render() {
return (
<div>
<Nav />
</div>
);
}
}
I recently had the same issue, and I solved it by using NavLink instead of Link from react-router-dom!
Example:
<Button
className={classes.button}
component={NavLink}
to="/page-link"
>
This will add the class ".active" to active buttons.
Using material's makeStyles hook, you can then style the buttons with this class:
const useStyles = makeStyles({
button: {
"&.active": {
background:'black',
},
},
});
If you're not using any third-party library like Gatsby, React-router then you should consider about this Tabs
Code:
import React from 'react';
import { makeStyles, Theme } 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';
interface TabPanelProps {
children?: React.ReactNode;
index: any;
value: any;
}
function TabPanel(props: TabPanelProps) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
{value === index && (
<Box p={3}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}
function a11yProps(index: any) {
return {
id: `simple-tab-${index}`,
'aria-controls': `simple-tabpanel-${index}`,
};
}
const useStyles = makeStyles((theme: Theme) => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
},
}));
export default function SimpleTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event: React.ChangeEvent<{}>, newValue: number) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={handleChange} aria-label="simple tabs example">
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
<Tab label="Item Three" {...a11yProps(2)} />
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</div>
);
}
To master in Material UI Tabs

Resources