Using Material-UI Box Component with the Drawer Compoment - reactjs

The Material-UI Box component allows us to reference other components as follows:
import Button from "#material-ui/core/Button";
import Box from "#material-ui/core/Box";
const NewButton = ({ children }) => (
<Box compoment={Button} px={3} py={1} color="white" bgcolor="primary.dark">
{children}
</Box>
)
This works just as I want it to. However, let me now try it with the Drawer component:
import Drawer from "#material-ui/core/Drawer";
import Box from "#material-ui/core/Box";
const NewDrawer = ({ children, open }) => {
return (
<Box component={Drawer} width="300px" bgcolor="secondary.dark">
{children}
</Box>
)
}
This does not work.
Any idea why not and how I can get it to work?
Thanks.

As per Material UI doc, For the Drawer component, we have to pass the open prop as true.
And also need to pass the drawer content like below,
<Drawer open={true}>{renderContents()}</Drawer>
In Box component api, we can pass the component data as a 'function'. like the below example.
import Drawer from "#material-ui/core/Drawer";
import Box from "#material-ui/core/Box";
const NewDrawer = ({ children, open }) => {
return (
<Box component={() => {
return <Drawer open={true}>{renderContents()}</Drawer>
}} width="300px" bgcolor="secondary.dark">
{children}
</Box>
)
}
Refer to my code sandbox example.

When using a Drawer with the temporary variant (the default), the className prop gets applied to the Modal which is not the element that you are trying to style.
Instead, you want to apply the styles from the Box to the Paper element within the Drawer. You can accomplish this using the render props approach for the Box children as shown in my example below.
import React from "react";
import Drawer from "#material-ui/core/Drawer";
import Box from "#material-ui/core/Box";
import Button from "#material-ui/core/Button";
const BoxDrawer = explicitProps => {
return (
<Box width="300px" bgcolor="secondary.dark">
{({ className }) => (
<Drawer {...explicitProps} PaperProps={{ className: className }} />
)}
</Box>
);
};
export default function App() {
const [open, setOpen] = React.useState(false);
return (
<div className="App">
<Button variant="contained" onClick={() => setOpen(!open)}>
Open Drawer
</Button>
<BoxDrawer open={open} onClose={() => setOpen(false)}>
<div style={{ textAlign: "center" }}>
<h1>Hello CodeSandbox</h1>
<Button variant="contained" onClick={() => setOpen(!open)}>
Close Drawer
</Button>
</div>
</BoxDrawer>
</div>
);
}

Related

React passing down a function that modifies a third component

I have been trying to create a component per function in my app, but I am facing the following issue.
I have the component DisplayAllData that sends the data and an actionable button to DisplayDataWithButton, the issue is that when someone clicks on the Button send in the props, the function modifies the state of the parent component, which is also sent as a parameter to FullScreenDialog, and that throws a Warning: Cannot update a component while rendering a different component.
I designed the components in this particular way because:
DisplayAllData is the only function that has the data to render and the actionable button. (Model)
DisplayDataWithButton only renders the data and displays the actionable components for that particular data, in this case a button that opens a Dialog in screen. (Viewer)
You can find a running example here: https://codesandbox.io/s/material-demo-forked-8oyef
import React from "react";
import Button from "#material-ui/core/Button";
import DisplayDataWithButton from "./DisplayDataWithButton";
import FullScreenDialog from "./fullscreendialog";
export default function App(props) {
const [openFullScreen, setopenFullScreen] = React.useState(false);
var items = ["John", "Melinda"];
var dataDisplayFunction = (data) => {
return data.map((item) => {
return [
item,
<Button
color="success"
size="small"
className="px-2"
variant="contained"
onClick={setopenFullScreen()}
>
Show Dialog
</Button>
];
});
};
return (
<>
<DisplayDataWithButton
shapeDataFunction={dataDisplayFunction}
data={items}
/>
<FullScreenDialog open={openFullScreen} />
</>
);
}
DisplayDataWithButton.js
export default function DisplayDataWithButton(props) {
return props.shapeDataFunction(props.data);
}
I suspect that there is another way to implement this model, any suggestion, or ideas on how to fix this one.
Thanks
A couple of things: "I have been trying to create a component per function in my app". Forget that - the pattern you have opted for here is called render props but I don't see how it is necessary. Keep it simple. If a big component is simpler to understand than a small component I always opt for the bigger component. Splitting your components will not magically make them easier to understand.
All of the warnings have been dealt with. Most of them were simple mistakes, for example: onClick={setopenFullScreen()} should be onClick={setopenFullScreen}. You can compare your sandbox with my sandbox for all of the changes.
import React from "react";
import ReactDOM from "react-dom";
import Button from "#material-ui/core/Button";
import FullScreenDialog from "./fullscreendialog";
export default function App() {
const [openFullScreen, setopenFullScreen] = React.useState(false);
const items = ["John", "Melinda"];
return (
<>
{items.map((item) => [
item,
<Button
key={item}
color="primary"
size="small"
className="px-2"
variant="contained"
onClick={() => setopenFullScreen((prev) => !prev)}
>
Show Dialog
</Button>
])}
<FullScreenDialog open={openFullScreen} />
</>
);
}
ReactDOM.render(<App />, document.querySelector("#root"));
import React from "react";
import { makeStyles } from "#material-ui/core";
import Button from "#material-ui/core/Button";
import Dialog from "#material-ui/core/Dialog";
import ListItemText from "#material-ui/core/ListItemText";
import ListItem from "#material-ui/core/ListItem";
import List from "#material-ui/core/List";
import Divider from "#material-ui/core/Divider";
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 CloseIcon from "#material-ui/icons/Close";
import Slide from "#material-ui/core/Slide";
const useStyles = makeStyles((theme) => ({
appBar: {
position: "relative"
},
title: {
marginLeft: theme.spacing(2),
flex: 1
}
}));
const Transition = React.forwardRef(function Transition(props, ref) {
return <Slide direction="up" ref={ref} {...props} />;
});
export default function FullScreenDialog(props) {
const classes = useStyles();
const [open, setOpen] = React.useState(false);
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<div>
<Dialog
fullScreen
open={props.open}
onClose={handleClose}
TransitionComponent={Transition}
>
<AppBar className={classes.appBar}>
<Toolbar>
<IconButton
edge="start"
color="inherit"
onClick={handleClose}
aria-label="close"
>
<CloseIcon />
</IconButton>
<Typography variant="h6" className={classes.title}>
Sound
</Typography>
<Button autoFocus color="inherit" onClick={handleClose}>
save
</Button>
</Toolbar>
</AppBar>
<List>
<ListItem button>
<ListItemText primary="Phone ringtone" secondary="Titania" />
</ListItem>
<Divider />
<ListItem button>
<ListItemText
primary="Default notification ringtone"
secondary="Tethys"
/>
</ListItem>
</List>
</Dialog>
</div>
);
}

makeStyle does not work with custom component

I am trying to make a simple navbar using material ui with a few buttons and a custom drop down menu component. When I try to style it using the makeStyle hook, the styling only applies to the material ui's buttons and heading but not the custom drop down component.
import React, { useContext } from "react";
import { makeStyles } from "#material-ui/core/styles";
import { AppBar, Toolbar, Typography } from "#material-ui/core";
import DropDown from "./DropDown";
import { Button } from "#material-ui/core";
import { AlgoContext } from "../AlgoContext";
const useStyles = makeStyles((theme) => ({
item: {
marginRight: theme.spacing(5),
},
}));
const MainHeader = () => {
const classes = useStyles();
const [algo, setAlgo] = useContext(AlgoContext);
return (
<div>
<AppBar elevation={0} position='static'>
<Toolbar>
<Typography variant='h6' className={classes.item}>
Pathfinding Visualiser
</Typography>
<Button variant='contained' className={classes.item}>
Visualise {algo.type}
</Button>
<DropDown className={classes.item}></DropDown>
<Button variant='contained' className={classes.item}>
Clear walls
</Button>
<Button variant='contained' className={classes.item}>
Clear path
</Button>
</Toolbar>
</AppBar>
</div>
);
};
export default MainHeader;
className is a default attribute of React element. You can not style your custom component by passing style object via className. Instead of that, you should pass it as a prop to DropDown component. Try this:
const MainHeader = () => {
const classes = useStyles();
const [algo, setAlgo] = useContext(AlgoContext);
return (
<DropDown itemStyle={classes.item}></DropDown>
);
};
export default MainHeader;
const DropDown = (props) => {
...
return (
<div className={props.itemStyle}>
...
</div>
)
}

React Dialog box closed issue

I want to use a dialog box of material UI. I am navigating that dialog box from the right side menu(sidebars -> user registration), using another component. I just want to open that dialog box on the current page home or about us. It is a user registration dialog. Can you help on that?
When I tried to open the user registration dialog, I am unable to open the dialog box in the current page, that's why I have created a separate component for the dialog box component.
I want to open that dialog when I select the side menu option. The dialog box should be open in the current page.
This is the code sandbox link. https://codesandbox.io/s/immutable-sound-ggj4w
App js
import React from "react";
import "./styles.css";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Home from "./home";
import About from "./about";
import Dialog from "./dialog";
import SideMenu from "./sidemu";
export default function App() {
return (
<div className="App">
<BrowserRouter>
<SideMenu />
{/* <Switch> */}
<Route exact path="/" component={Home} />
<Route exact path="/about" component={About} />
<Route exact path="/sidemenu" component={SideMenu} />
<Route exact path="/dialog" component={Dialog} />
{/* </Switch> */}
</BrowserRouter>
{/* <h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2> */}
</div>
);
}
home js
import React, { Component } from "react";
class Home extends Component {
render() {
return (
<div>
<div>Home</div>
</div>
);
}
}
export default Home;
about us
import React, { Component } from "react";
class Home extends Component {
render() {
return (
<div>
<div>about us</div>
</div>
);
}
}
export default Home;
dialog js
import React from 'react';
import Button from '#material-ui/core/Button';
import Dialog from '#material-ui/core/Dialog';
import DialogActions from '#material-ui/core/DialogActions';
import DialogContent from '#material-ui/core/DialogContent';
import DialogContentText from '#material-ui/core/DialogContentText';
import DialogTitle from '#material-ui/core/DialogTitle';
export default function AlertDialog() {
const [open, setOpen] = React.useState(false);
const handleClickOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
return (
<div>
<Button variant="outlined" color="primary" onClick={handleClickOpen}>
Open alert dialog
</Button>
<Dialog
open={true}
onClose={handleClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{"Use Google's location service?"}</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Let Google help apps determine location. This means sending anonymous location data to
Google, even when no apps are running.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={handleClose} color="primary">
Disagree
</Button>
<Button onClick={handleClose} color="primary" autoFocus>
Agree
</Button>
</DialogActions>
</Dialog>
</div>
);
}
sidemenu js
import React from 'react';
import './styles.css';
import { Link } from 'react-router-dom';
import { makeStyles } from '#material-ui/core/styles';
import Drawer from '#material-ui/core/Drawer';
import { List, ListItemIcon, ListItem, ListItemText } from '#material-ui/core';
import MenuRoundedIcon from '#material-ui/icons/MenuRounded';
import HomeRoundedIcon from '#material-ui/icons/HomeRounded';
import MenuBookRoundedIcon from '#material-ui/icons/MenuBookRounded';
const useStyles = makeStyles({
list: {
width: 250,
},
fullList: {
width: 'auto',
},
});
export default function TemporaryDrawer() {
const classes = useStyles();
const [state, setState] = React.useState({
top: false,
left: false,
bottom: false,
right: false,
});
const toggleDrawer = (side, open) => event => {
if (event.type === 'keydown' && (event.key === 'Tab' || event.key === 'Shift')) {
return;
}
setState({ ...state, [side]: open });
};
const sideList = side => (
<div
className={classes.list}
role="presentation"
onClick={toggleDrawer(side, false)}
onKeyDown={toggleDrawer(side, false)}
>
<List>
<Link className="right-menu-data" to="/">
<ListItem button>
<ListItemIcon>
<HomeRoundedIcon />
</ListItemIcon>
<ListItemText>Home</ListItemText>
</ListItem>
</Link>
<Link className="right-menu-data" to="/about"><ListItem button>
<ListItemIcon>
<MenuBookRoundedIcon />
</ListItemIcon>
<ListItemText>About us</ListItemText>
</ListItem>
</Link>
<Link className="right-menu-data" to="/dialog"><ListItem button>
<ListItemIcon>
<MenuBookRoundedIcon />
</ListItemIcon>
<ListItemText>User Registration</ListItemText>
</ListItem>
</Link>
</List>
</div>
);
return (
<div>
<MenuRoundedIcon className="careerpedia-menu-bars" onClick={toggleDrawer('right', true)} />
<Drawer anchor="right" open={state.right} onClose={toggleDrawer('right', false)}>
{sideList('right')}
</Drawer>
</div>
);
}
One way to achieving this is to factor the logic to open/close the modal out of the dialog component to a "central" location, preferably in the app, but outside the router. This allows the dialog to be opened/closed from a single location by callbacks that can be passed around the app, and not be coupled to any specific route your app is on.
A quick refactoring of your code:
dialog.js
const AlertDialog = ({ open, onAgree, onClose, onDisagree }) => (
<Dialog
open={open}
onClose={onClose}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
{"Use Google's location service?"}
</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
Let Google help apps determine location. This means sending anonymous
location data to Google, even when no apps are running.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={onDisagree} color="primary">
Disagree
</Button>
<Button onClick={onAgree} color="primary" autoFocus>
Agree
</Button>
</DialogActions>
</Dialog>
);
App.js
export default function App() {
const [open, setOpen] = useState(false);
const openDialog = () => setOpen(true);
const closeDialog = () => setOpen(false);
const onAgreeHandler = () => {
closeDialog();
alert("AGREED");
};
const onDisgreeHandler = () => {
closeDialog();
alert("DISAGREED");
};
const onCloseHandler = (event, reason) => {
closeDialog();
alert(`Dialog closed. Reason: ${reason}`);
};
return (
<div className="App">
<BrowserRouter>
<SideMenu openDialog={openDialog} /> // Pass dialog open callback to sidemenu
<Route exact path="/" component={Home} />
<Route exact path="/about" component={About} />
<Route exact path="/dialog" component={Dialog} />
</BrowserRouter>
<Button variant="outlined" color="primary" onClick={openDialog}>
Open alert dialog test
</Button>
<Dialog
open={open}
onAgree={onAgreeHandler}
onClose={onCloseHandler}
onDisagree={onDisgreeHandler}
/>
</div>
);
}
sidemu.js
export default function TemporaryDrawer({ openDialog }) { // Accept openDialog callback
...
{ /* Add new menu list item (not in a Link!) */ }
<ListItem button onClick={openDialog}> // Set onClick handler to openDialog callback
<ListItemIcon>
<MenuBookRoundedIcon />
</ListItemIcon>
<ListItemText>Trigger dialog?</ListItemText>
</ListItem>
NOTE: As your app grows in size/complexity this pattern may become a little unwieldy (if you need to open a dialog by any further descendants) and you'll likely want to switch to using a react context provider/consumer specifically for the dialog, or use a state management system like Redux so you can dispatch simple actions to open/close it.

onClick Handler in React Picking Up Child Nodes

I'm trying to add interactivity to a four quadrant chart whereby the user can click a box to highlight it, and the others will deactivate, similar to how radio boxes work in a form.
The idea was to add an onClick event to each card and have a handler function that will check which box was clicked on, activate it, and then deactivate the rest.
The problem I'm having is that e.target seems to be picking up the child nodes of each card instead of the card itself, so I'm having trouble figuring out which card was clicked.
e.g. console log = '>> event.target <li>A</li>'
I was hoping to determine which card was picked by doing something like event.target.id
I've tried a bunch of things and nothing has worked... How do people normally set this type of interaction up?
import React from "react";
import Card from "#material-ui/core/Card";
import CardContent from "#material-ui/core/CardContent";
import CardActionArea from '#material-ui/core/CardActionArea';
import Typography from "#material-ui/core/Typography";
import Paper from "#material-ui/core/Paper";
function MyCard({ title }) {
return (
<CardActionArea onClick={quadrantClickHandler}>
<Card>
<CardContent>
<Typography>{title}</Typography>
</CardContent>
</Card>
</CardActionArea>
);
}
function quadrantClickHandler(e) {
e.stopPropagation();
console.log('>> event.target ',e.target);
//the idea here is that I will click a "card"
//and then determine which card was clicked so that
//I can highlight it similar to a radio box set in a form.
}
function Quadrants() {
return (
<React.Fragment>
<MyCard
title={
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
</ul>
} />
<MyCard title="Fast but expensive" />
<MyCard title="Slow but Cheap" />
<MyCard title="Slow but Fast" />
</React.Fragment>
);
}
function FourQuadrants() {
const classes = useStyles();
return (
<div>
<h2>Make a choice:</h2>
<Paper className={classes.paper}>
<Typography className={classes.top}>Big</Typography>
<Typography className={classes.bottom}>Small</Typography>
<Typography align="center" className={classes.left}>Expensive</Typography>
<Typography align="center" className={classes.right}>Cheap</Typography>
<Quadrants />
</Paper>
</div>
);
}
export default FourQuadrants;
Rather than trying to pull information out of the event target, you can have the click handler know everything of importance.
The key aspects in my example below are:
State at the top level (selectedId) to track which card is selected
A click-handler that knows the id of its card and sets the selectedId accordingly
I'm providing each card with its own id and the selectedId, so that it can style itself differently based on whether or not it is selected
import ReactDOM from "react-dom";
import React from "react";
import Card from "#material-ui/core/Card";
import CardContent from "#material-ui/core/CardContent";
import CardActionArea from "#material-ui/core/CardActionArea";
import Typography from "#material-ui/core/Typography";
import { makeStyles } from "#material-ui/core";
const useStyles = makeStyles({
selected: {
border: "1px solid green"
}
});
function MyCard({ title, id, selectedId, handleClick }) {
const classes = useStyles();
return (
<Card className={id === selectedId ? classes.selected : null}>
<CardActionArea onClick={handleClick(id)}>
<CardContent>
<Typography>{title}</Typography>
</CardContent>
</CardActionArea>
</Card>
);
}
function Quadrants() {
const [selectedId, setSelectedId] = React.useState();
const handleClick = id => e => {
setSelectedId(id);
};
const cardProps = { selectedId, handleClick };
return (
<React.Fragment>
<MyCard
{...cardProps}
id={1}
title={
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
<li>G</li>
</ul>
}
/>
<MyCard {...cardProps} id={2} title="Fast but expensive" />
<MyCard {...cardProps} id={3} title="Slow but Cheap" />
<MyCard {...cardProps} id={4} title="Slow but Fast" />
</React.Fragment>
);
}
function FourQuadrants() {
return (
<div>
<h2>Make a choice:</h2>
<Quadrants />
</div>
);
}
function App() {
return <FourQuadrants />;
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

How do I use Menu with Collapse as my TransitionComponent on Material-ui?

I'm having issues using the Collapse component as my TransitionComponent on Material-UI.
Not only does it not work, it also breaks the anchor.
Fade works fine, not sure where to look for answers.
Link to codesandbox
import React, { useState, useRef } from "react";
import ReactDOM from "react-dom";
import { Menu, MenuItem, Collapse, Fade, Typography } from "#material-ui/core";
function App() {
const [open, toggle] = useState(false);
const ref = useRef(null);
return (
<>
<div style={{ position: "absolute", top: "50%" }} ref={ref}>
<Typography variant="h4" className="App" onClick={() => toggle(true)}>
Click me!
</Typography>
</div>
<Menu
TransitionComponent={Collapse}
anchorEl={ref.current}
open={open}
onClose={() => toggle(false)}
>
<MenuItem>Item do menu</MenuItem>
</Menu>
<Menu
TransitionComponent={Fade}
transitionDuration={800}
anchorEl={ref.current}
open={open}
onClose={() => toggle(false)}
>
<MenuItem>Item do menu</MenuItem>
</Menu>
</>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
This is a known bug with the Collapse transition in combination with the Popover component (which is used by Menu): https://github.com/mui-org/material-ui/issues/11337

Resources