I'm trying to replicate the "Mini drawer" example found on the MUI Drawer page here:
https://mui.com/components/drawers/#mini-variant-drawer
But as soon as I wrap it in a ThemeProvider, even if the referenced theme is empty the layout breaks. See below:
<ThemeProvider theme={baseTheme}>
<Box sx={{ display: "flex" }}>
<CssBaseline />
<AppBar position="fixed" open={open}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={handleDrawerOpen}
edge="start"
sx={{
marginRight: "36px",
...(open && { display: "none" })
}}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap component="div">
Mini variant drawer
</Typography>
</Toolbar>
</AppBar>
<Drawer variant="permanent" open={open}>
<DrawerHeader>
<IconButton onClick={handleDrawerClose}>
{theme.direction === "rtl" ? (
<ChevronRightIcon />
) : (
<ChevronLeftIcon />
)}
</IconButton>
</DrawerHeader>
<Divider />
<List>
{["Inbox", "Starred", "Send email", "Drafts"].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{["All mail", "Trash", "Spam"].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</Drawer>
<Box component="main" sx={{ flexGrow: 1, p: 3 }}>
<DrawerHeader />
<Typography paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Rhoncus
dolor purus non enim praesent elementum facilisis leo vel. Risus at
ultrices mi tempus imperdiet. Semper risus in hendrerit gravida
rutrum quisque non tellus. Convallis convallis tellus id interdum
velit laoreet id donec ultrices. Odio morbi quis commodo odio aenean
sed adipiscing. Amet nisl suscipit adipiscing bibendum est ultricies
integer quis. Cursus euismod quis viverra nibh cras. Metus vulputate
eu scelerisque felis imperdiet proin fermentum leo. Mauris commodo
quis imperdiet massa tincidunt. Cras tincidunt lobortis feugiat
vivamus at augue. At augue eget arcu dictum varius duis at
consectetur lorem. Velit sed ullamcorper morbi tincidunt. Lorem
donec massa sapien faucibus et molestie ac.
</Typography>
<Typography paragraph>
Consequat mauris nunc congue nisi vitae suscipit. Fringilla est
ullamcorper eget nulla facilisi etiam dignissim diam. Pulvinar
elementum integer enim neque volutpat ac tincidunt. Ornare
suspendisse sed nisi lacus sed viverra tellus. Purus sit amet
volutpat consequat mauris. Elementum eu facilisis sed odio morbi.
Euismod lacinia at quis risus sed vulputate odio. Morbi tincidunt
ornare massa eget egestas purus viverra accumsan in. In hendrerit
gravida rutrum quisque non tellus orci ac. Pellentesque nec nam
aliquam sem et tortor. Habitant morbi tristique senectus et.
Adipiscing elit duis tristique sollicitudin nibh sit. Ornare aenean
euismod elementum nisi quis eleifend. Commodo viverra maecenas
accumsan lacus vel facilisis. Nulla posuere sollicitudin aliquam
ultrices sagittis orci a.
</Typography>
</Box>
</Box>
</ThemeProvider>
(Note: This code is an exact copy of the Mini drawer example on the website)
It looks like the ThemeProvider makes it so that the Drawer and the main body are overlapping instead of "pushing" each other. Removing the ThemeProvider (and going back to how the example on the MUI website was set up) instantly fixes it.
Of course, in my actual use case, I want to have a light and a dark theme, but it seems that even a single theme without any setup breaks the code.
Here's a CodeSandbox that shows how App.js and theme.js are set up:
https://codesandbox.io/s/react-mui-theme-breaks-drawer-u5sg0
Is this a bug in MUI or am I doing theming wrong?
Thank you in advance for your help.
To anyone that finds this issue later down the line:
I was importing { createTheme } from "#material-ui/core/styles".
Importing it from "#mui/material/styles" fixed the issue.
Related
My titles are very long I want them to look like this:
This is a title of...
This is my code
<CardContent>
<Typography gutterBottom variant="h5" component="div">
<Link href={`/${encodeURIComponent(data.slug)}`}>
<a>{data.title}</a> // This is a string of title text
</Link>
</Typography>
</CardContent>
How to limit them to 18 characters only and add ... at the end?
Usually you solve this with CSS.
Set a max-width together with text-overflow: ellipsis;.
The advantage is better SEO and the browser is able to use the available space more efficiently than just counting characters.
For example a M character is larger than a l character.
But you can also limit it with JS:
<CardContent>
<Typography gutterBottom variant="h5" component="div">
<Link href={`/${encodeURIComponent(data.slug)}`}>
<a>{data.title.length <= 18 ? data.title: (data.title.substr(0, 18) + "...")}</a> // This is a string of title text
</Link>
</Typography>
</CardContent>
You can use simple custom css to limit this
span {
display: inline-block;
width: 200px;
white-space: nowrap;
overflow: hidden !important;
text-overflow: ellipsis;
}
<span>
rem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam lectus justo, vulputate eget mollis sed, tempor sed magna. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo. Donec vitae arcu. Nullam eget nisl. Etiam commodo dui eget wisi. Praesent vitae arcu tempor neque lacinia pretium. Aenean fermentum risus id tortor. Proin mattis lacinia justo. Integer vulputate sem a nibh rutrum consequat. Nullam justo enim, consectetuer nec, ullamcorper ac, vestibulum in, elit.
</span>
For better web app performance, I am showing different images on mobile and desktop due to resolution. The data is getting loaded from JSON file
[{
"_id": 9,
"title":"Text 9",
"subheader":"Dummy header 9",
"images": "images/four-1.png",
"imagesmob" : "images/four-1.webp",
"content":"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent nec velit consectetur, pretium risus vel, eleifend tortor. Integer enim nulla, iaculis sit amet leo vel, sagittis maximus leo. Aliquam semper sapien bibendum nulla consectetur, ut eleifend arcu accumsan. Duis consequat diam sed dolor convallis semper. Ut justo mauris, accumsan non tempus ut, ultrices at tellus. Quisque a sapien urna. Maecenas tempor consequat sollicitudin. Nullam volutpat a eros sit amet vestibulum. Cras sollicitudin vitae urna nec varius. Etiam nec facilisis urna.</p> <p>Donec ut dui condimentum, tincidunt tellus vitae, interdum lorem. Phasellus eget pretium enim. Morbi accumsan turpis vitae turpis venenatis ullamcorper. Proin pretium nisi nec nulla faucibus, a hendrerit turpis malesuada. Nullam id faucibus metus, id faucibus metus. Donec et massa suscipit, congue tortor non, ultricies turpis. Aliquam venenatis massa velit, id ornare ante efficitur vitae. Vivamus id dui tortor. Pellentesque sem mi, bibendum a orci hendrerit, mollis eleifend eros. Nunc sodales orci purus. Vivamus molestie porta egestas. <p>Suspendisse convallis mi ullamcorper, dapibus nisi a, semper felis. Praesent ut lacus eu neque vestibulum efficitur.<\/p> Sed dolor magna, mattis a ultricies vel, <b>tincidunt<\/b> eget enim.<\/p>"
}
]
The code file is :
<Card variant="outlined" sx={{ maxWidth:345, borderRadius: '11px' }} elevation={3}>
<CardMedia
component="img"
height="194"
image={welcom.images}
alt="Home Page"
sx={{display:{xs:'none', sm:'none',md:'block'}}}
/>
<CardMedia
component="img"
height="194"
image={welcom.imagesmob}
alt="Home Page"
sx={{display:{xs:'block', sm:'block',md:'none'}}}
/>
<CardHeader
titleTypographyProps={{fontSize: 18, fontWeight: 'bold', color: 'purple' }}
title = {welcom.title}
subheader = {welcom.subheader}
/>
<CardContent>
<div>{welcom.content}</div>
</CardContent>
</Card>
In tthis I want to load images for mobile in CardMedia, when I used display none and made other cardmedia for mobile, the image used for desktop is also getting loaded. I dont want the image for desktop to be loaded when on mobile. As this affect the website performance.
Any help will be appreciated. Thanks
[pic here][1]
My goal is after clicking on of the blue boxes, the red box content will be updated.
I throught just simply change the state vaule in the Typography would work.
<Typography variant="h6" noWrap>
{sys}
</Typography>
However, I'm getting the following error msg:
Warning: Failed prop type: Invalid prop children supplied to ForwardRef(Typography), expected a ReactNode.
Based on it, the children prop expected a ReactNode instead of ForwardRef(Typography) but IDK how to fix it.
Please help here.
The follosing is my code:
export default function PersistentDrawerLeft() {
const classes = useStyles();
const theme = useTheme();
const [open, setOpen] = React.useState(false);
const [sys, setSys] = React.useState("LINX");
const switchSys =(text)=>{
console.log(text)
setSys(text)
}
const handleDrawerOpen = () => {
setOpen(true);
};
const handleDrawerClose = () => {
setOpen(false);
};
return (
<div className={classes.root}>
<CssBaseline />
<AppBar
position="fixed"
className={clsx(classes.appBar, {
[classes.appBarShift]: open,
})}
>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={handleDrawerOpen}
edge="start"
className={clsx(classes.menuButton, open && classes.hide)}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
{sys}
</Typography>
</Toolbar>
</AppBar>
<Drawer
className={classes.drawer}
variant="persistent"
anchor="left"
open={open}
classes={{
paper: classes.drawerPaper,
}}
>
<div className={classes.drawerHeader}>
<IconButton onClick={handleDrawerClose}>
{theme.direction === 'ltr' ? <ChevronLeftIcon /> : <ChevronRightIcon />}
</IconButton>
</div>
<Divider />
<List>
{['LINX', 'CASPER'].map((text, index) => (
<ListItem button key={text} onClick={()=>switchSys({text})}>
{/* <ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon> */}
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
{/* <List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List> */}
</Drawer>
<main
className={clsx(classes.content, {
[classes.contentShift]: open,
})}
>
<div className={classes.drawerHeader} />
<Typography paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Rhoncus dolor purus non enim praesent elementum
facilisis leo vel. Risus at ultrices mi tempus imperdiet. Semper risus in hendrerit
gravida rutrum quisque non tellus. Convallis convallis tellus id interdum velit laoreet id
donec ultrices. Odio morbi quis commodo odio aenean sed adipiscing. Amet nisl suscipit
adipiscing bibendum est ultricies integer quis. Cursus euismod quis viverra nibh cras.
Metus vulputate eu scelerisque felis imperdiet proin fermentum leo. Mauris commodo quis
imperdiet massa tincidunt. Cras tincidunt lobortis feugiat vivamus at augue. At augue eget
arcu dictum varius duis at consectetur lorem. Velit sed ullamcorper morbi tincidunt. Lorem
donec massa sapien faucibus et molestie ac.
</Typography>
<Typography paragraph>
Consequat mauris nunc congue nisi vitae suscipit. Fringilla est ullamcorper eget nulla
facilisi etiam dignissim diam. Pulvinar elementum integer enim neque volutpat ac
tincidunt. Ornare suspendisse sed nisi lacus sed viverra tellus. Purus sit amet volutpat
consequat mauris. Elementum eu facilisis sed odio morbi. Euismod lacinia at quis risus sed
vulputate odio. Morbi tincidunt ornare massa eget egestas purus viverra accumsan in. In
hendrerit gravida rutrum quisque non tellus orci ac. Pellentesque nec nam aliquam sem et
tortor. Habitant morbi tristique senectus et. Adipiscing elit duis tristique sollicitudin
nibh sit. Ornare aenean euismod elementum nisi quis eleifend. Commodo viverra maecenas
accumsan lacus vel facilisis. Nulla posuere sollicitudin aliquam ultrices sagittis orci a.
</Typography>
</main>
</div>
);
}```
[1]: https://i.stack.imgur.com/5HL1N.png
I am composing the following using Semantic UI React: column 1: text, column 2 and 3: images. While the images are middle aligned to each other, the text next to them is aligned to the top.
Here is the code. I have to put marginTop: "-15em" to move the title up. Is there a better way to do this?
import React from "react";
import {Grid,Header} from "semantic-ui-react";
<Grid>
<Grid.Row verticalAlign="middle">
<Grid.Column width={4}>
<Header as="h4" style={{ marginTop: "-15em" }}>
Title
</Header>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse finibus hendrerit nulla, quis mattis felis eleifend
sed. Vivamus a blandit tellus. Donec non erat enim. Nullam
vulputate lectus a finibus tempor. Fusce vel purus est. Praesent
ac ex ac ex vulputate vulputate. Donec dapibus pharetra ultrices.
In maximus enim sem, id dignissim odio volutpat vitae.
</p>
</Grid.Column>
<Grid.Column width={6}>
<img src={img1} alt="img1" />
</Grid.Column>
<Grid.Column width={6}>
<img src={img2} alt="img2" />
</Grid.Column>
</Grid.Row>
</Grid>
You can put verticalAlign property on each Grid.Column and remove this property from the Grid.Row. Hopefully it should work
import React from "react";
import {Grid,Header} from "semantic-ui-react";
<Grid>
<Grid.Row>
<Grid.Column width={4}>
<Header as="h4">
Title
</Header>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse finibus hendrerit nulla, quis mattis felis eleifend
sed. Vivamus a blandit tellus. Donec non erat enim. Nullam
vulputate lectus a finibus tempor. Fusce vel purus est. Praesent
ac ex ac ex vulputate vulputate. Donec dapibus pharetra ultrices.
In maximus enim sem, id dignissim odio volutpat vitae.
</p>
</Grid.Column>
<Grid.Column verticalAlign="middle" width={6}>
<img src={img1} alt="img1" />
</Grid.Column>
<Grid.Column verticalAlign="middle" width={6}>
<img src={img2} alt="img2" />
</Grid.Column>
</Grid.Row>
</Grid>
actually, I am trying to create a responsive app bar using the material UI/app drawer. when I clicked the but it starts from the top of the page and makes the background dark but I want to make it start from the bottom of the app bar without making the background dark. please help me and give example using codesandbox https://codesandbox.io/s/64ulq
I trying to do this
but it shows like that
you can make a mix of multiple exemple of material ui drawer (Persistent and clipped one) and it give you something like this :
import React from 'react';
import { createStyles, Theme, makeStyles } from '#material-ui/core/styles';
import Drawer from '#material-ui/core/Drawer';
import AppBar from '#material-ui/core/AppBar';
import CssBaseline from '#material-ui/core/CssBaseline';
import Toolbar from '#material-ui/core/Toolbar';
import List from '#material-ui/core/List';
import Typography from '#material-ui/core/Typography';
import Divider from '#material-ui/core/Divider';
import ListItem from '#material-ui/core/ListItem';
import ListItemIcon from '#material-ui/core/ListItemIcon';
import ListItemText from '#material-ui/core/ListItemText';
import InboxIcon from '#material-ui/icons/MoveToInbox';
import MailIcon from '#material-ui/icons/Mail';
import IconButton from '#material-ui/core/IconButton';
import MenuIcon from '#material-ui/icons/Menu';
const drawerWidth = "100vw";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
},
appBar: {
zIndex: theme.zIndex.drawer + 1,
},
drawer: {
width:"100vw",
},
drawerPaper: {
width: drawerWidth,
},
drawerContainer: {
overflow: 'auto',
},
content: {
flexGrow: 1,
padding: theme.spacing(3),
},
}),
);
export default function ClippedDrawer() {
const classes = useStyles();
const [open, setOpen] = React.useState(false);
const handleDrawerOpen = () => {
setOpen(!open);
};
return (
<div className={classes.root}>
<CssBaseline />
<AppBar position="fixed" className={classes.appBar}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={handleDrawerOpen}
edge="start"
>
<MenuIcon />
</IconButton>
<Typography variant="h6" noWrap>
Clipped drawer
</Typography>
</Toolbar>
</AppBar>
<Drawer
anchor="top"
className={classes.drawer}
variant="persistent"
open={open}
classes={{
paper: classes.drawerPaper,
}}
>
<Toolbar />
<div className={classes.drawerContainer}>
<List>
{['Inbox', 'Starred', 'Send email', 'Drafts'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
<List>
{['All mail', 'Trash', 'Spam'].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>{index % 2 === 0 ? <InboxIcon /> : <MailIcon />}</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
</div>
</Drawer>
<main className={classes.content}>
<Toolbar />
<Typography paragraph>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Rhoncus dolor purus non enim praesent elementum
facilisis leo vel. Risus at ultrices mi tempus imperdiet. Semper risus in hendrerit
gravida rutrum quisque non tellus. Convallis convallis tellus id interdum velit laoreet id
donec ultrices. Odio morbi quis commodo odio aenean sed adipiscing. Amet nisl suscipit
adipiscing bibendum est ultricies integer quis. Cursus euismod quis viverra nibh cras.
Metus vulputate eu scelerisque felis imperdiet proin fermentum leo. Mauris commodo quis
imperdiet massa tincidunt. Cras tincidunt lobortis feugiat vivamus at augue. At augue eget
arcu dictum varius duis at consectetur lorem. Velit sed ullamcorper morbi tincidunt. Lorem
donec massa sapien faucibus et molestie ac.
</Typography>
<Typography paragraph>
Consequat mauris nunc congue nisi vitae suscipit. Fringilla est ullamcorper eget nulla
facilisi etiam dignissim diam. Pulvinar elementum integer enim neque volutpat ac
tincidunt. Ornare suspendisse sed nisi lacus sed viverra tellus. Purus sit amet volutpat
consequat mauris. Elementum eu facilisis sed odio morbi. Euismod lacinia at quis risus sed
vulputate odio. Morbi tincidunt ornare massa eget egestas purus viverra accumsan in. In
hendrerit gravida rutrum quisque non tellus orci ac. Pellentesque nec nam aliquam sem et
tortor. Habitant morbi tristique senectus et. Adipiscing elit duis tristique sollicitudin
nibh sit. Ornare aenean euismod elementum nisi quis eleifend. Commodo viverra maecenas
accumsan lacus vel facilisis. Nulla posuere sollicitudin aliquam ultrices sagittis orci a.
</Typography>
</main>
</div>
);
}
code sandbox