How to use mui Breakpoints and create a responsive ui? - reactjs

I am creating a webpage using mui and and nextjs with typescript. Here I create two separate folder for using mui.
That's why I create a Styles folder-
Single.styles.ts Here I defin my all styles-
export default {
Title: {
fontSize: "18px",
pb: "10px",
mb: "20px",
borderBottom: `1px solid ${theme.palette.primary.border_bottom}`,
position: "relative",
fontWeight: 500,
"&:after": {
content: '""',
position: "absolute",
width: "25%",
bgcolor: "primary.main",
height: "2px",
left: "0",
bottom: "0"
}
},
}
And then I import it in my component I use it-
Single.tsx (Component)
//Styles
import styles from "Styles/Home/SingleTop.styles";
const SingleTop = () => {
return (
<Box>
<Typography variant="h6" component="h6" sx={styles.Title}>
This is title
</Typography>
</Box>
);
};
export default SingleTop;
And it's working perfectly.
Now I am trying to do responsiveness in this webpage. I already search it and I found it-
https://mui.com/material-ui/customization/breakpoints/
From This documentation I am facing two problems. I changes my Single.styles.ts file according to that documentation like this-
const styles = (theme) => ({
Title: {
fontSize: "18px",
pb: "10px",
mb: "20px",
borderBottom: `1px solid ${theme.palette.primary.border_bottom}`,
position: "relative",
fontWeight: 500,
"&:after": {
content: '""',
position: "absolute",
width: "25%",
bgcolor: "primary.main",
height: "2px",
left: "0",
bottom: "0"
},
[theme.breakpoints.up('lg')]: {
}
}
})
export default styles;
And here I found two error. One is type difination for theme. Here How can I define types for this-
const styles = (theme: //Types)=> ({});
My second problem is Where I use this styles in my component by using sx-
sx={styles.Title}
I found this error here-
Property 'Title' does not exist on type '(theme: any) => {}
Please help me how can I solve that problem. How can I apply them perfectly? What is the right way?

Related

react-easy-crop overwrite custom styles

Could you tell me how to overwrite the custom styles in react-easy-crop library?
<Cropper
image={headerImage}
crop={crop}
aspect={4 / 1}
onCropChange={setCrop}
onCropComplete={onCropComplete}
style={{
containerStyle: {
borderRadius: "16px",
},
cropAreaStyle: {
border: "2px solid #FFFFFF",
borderRadius: "16px",
// '& .reactEasyCrop_CropAreaGrid::after ': {
// top: '0'
// }
},
}}
classes={{ cropAreaClassName: cssStyle.reactEasyCrop_CropAreaGrid }}
/>;
I was trying to fix it with style or classes props but not handle it.

Video loader overrides the div element

The video loader overrides the fixed bottom element, thus makes it quite unpleasant. I'am streaming the videos online and player used for it is React HLS player. What would be the best solution to prevent the overriding of loader. Following is the code reference
React HLS Player
<ReactHlsPlayer
url={video_url}
autoplay={false}
controls={true}
width="100%"
height="auto"
config={{
file: {
forceHLS: true,
}
}}
/>
Bottom Navbar Code
const useStyles = makeStyles({
root: {
width: "100%",
bottom: "0px",
position: "sticky"
},
gridList: {
flexWrap: "nowrap",
position: "fixed",
bottom: "0px",
background: "white",
border: "1px solid grey",
width: "100%"
}
});
<GridList className={classes.gridList}>
{itemList.map((tile, index) => {
return (
<GridListTile
key={tile.icon}
style={{ height: "70px", width: "25%" }}
>
<ListItem button key={tile.text}
onClick={(tile.text == "DirectLine") ? directLineFunc : ''}
>
<NavLink
exact
to={tile.link}
key={tile.key}
activeClassName="main-nav-active"
style={{ textAlign: "center" }}
isActive={(match, location) => {
match && setNewActiveLink(index)
return match;
}}
>
<ListItemText
disableTypography
primary={
<Typography
style={{
fontSize: "10px",
fontWeight: "bold",
fontFamily: "Nunito"
}}
>
{tile.text}
</Typography>
}
/>
</NavLink>
</ListItem>
</GridListTile>
);
})}
</GridList>
See the image below
And this the codesandbox link: https://codesandbox.io/s/react-material-forked-dtx6w
Finally I was able to sort out by adding 'zIndex:999' to gridList of BottomNavbar with the following changes in useStyles as:
const useStyles = makeStyles({
root: {
width: "100%",
bottom: "0px",
position: "sticky"
},
gridList: {
flexWrap: "nowrap",
position: "fixed",
bottom: "0px",
background: "white",
border: "1px solid grey",
width: "100%",
zIndex: 999 {/* <-- Here I added this and issue solved */}
}
});
I added a style={{marginBottom: "100px"}} property in the <ReactHlsPlayer /> and it seems to fix your problem

Material UI V4.11.0 - Override Autocomplete nested styles

I'm actually trying to override the input styling for the autocomplete component, I already overrided some of the styles on the theme, but I wasn't able to override the one for the input underline to remove it, on the devtools I found that I have to remove the borderBottom and the content styles but since this is kinda nested nothing that I tried worked:
Actual behavior
Desired behavior
This is what I need
This styles are working:
MuiAutocomplete: {
root: {
paddingLeft: "15px",
paddingRight: "15px",
},
groupLabel: {
fontWeight: 700,
color: "black",
fontSize: 14
},
option: {
paddingTop: "0px",
paddingBottom: "0px",
fontSize: 14,
height: "25px"
}
}
I tried something like this:
MuiAutocomplete: {
input: {
content: "",
borderBottom: "none"
},
inputFocused: {
content: "",
borderBottom: "none"
},
inputRoot: {
content: "",
borderBottom: "none"
},
root: {
paddingLeft: "15px",
paddingRight: "15px",
},
groupLabel: {
fontWeight: 700,
color: "black",
fontSize: 14
},
option: {
paddingTop: "0px",
paddingBottom: "0px",
fontSize: 14,
height: "25px"
}
}
Also tried using the makeStyles with inputRoot, input and inputFocused with no success:
const useStyles = makeStyles(theme => ({
inputRoot: {
"& .MuiInput-underline": {
content: "",
borderButton: "none"
},
"&:before .MuiInput-underline": {
content: "",
borderButton: "none"
},
"&.after ..MuiInput-underline": {
content: "",
borderButton: "none"
}
}
}));
Thanks in advice!
That underline is a pseudo element of the MuiInput-underline not the root input. Furthermore, a pseudo element cannot have a child so this &:before .MuiInput-underline type of syntax won't work
To solve this issue: just reference the generated root class, its descendant should be .MuiInput-underline, with pseudo element before
const useStyles = makeStyles(theme => ({
inputRoot: {
"& .MuiInput-underline:before": {
borderBottom: "none"
}
}
}));
<Autocomplete
renderInput={(params) => <TextField classes={{root: classes.inputRoot}} {...params} label="Combo box" />}
/>

Markdown is not being displayed correctly due to CSSReset - Chakra-ui

I'm making a NextJs Blog, and I have to render my markdown in a dynamic page. CSSReset is being used in my app, and as a consequence, markdown is not being displayed correctly. Can someone help me out on this??
my ThemeContainer
0 import React from "react";
1
2 import {
3 ThemeProvider as ChakraThemeProvider,
4 ColorModeProvider,
5 CSSReset,
6 } from "#chakra-ui/core";
7
8 import { ThemeProvider as EmotionThemeProvider } from "emotion-theming";
9 import theme from "../styles/theme";
10
11 export const ThemeContainer = ({ children }) => {
12 return (
13 <ChakraThemeProvider theme={theme}>
14 <ColorModeProvider value={"light"}>
15 <EmotionThemeProvider theme={theme}>
16 <CSSReset />
17 {children}
18 </EmotionThemeProvider>
19 </ColorModeProvider>
20 </ChakraThemeProvider>
21 );
22 };
My markdown file
10 const Post = (postData: Post) => {
+ 11 const content = hydrate(postData.source);
+ 12
+ 13 console.log(content);
_ 14
15 return (
~ 16 <Flex w="full" overflowY="hidden">
~ 17 <div>{content}</div>
~ 18 </Flex>
19 );
20 };
21
22 export default Post;
I haven't done styling because I want to know how to solve this problem I'm having, so I left just a small component rendering the content
Should I try to fork this CSSReset and remove the styles that are making it happen??
I had the same issue a while ago. I wouldn't recommend using ChakraUI for Markdown because it needs to reset all CSS styles which results in this issue, but If you have a hybrid app with both static and dynamic (non-markdown) sites and you only want to use ChakraUI with its CSSReset on your dynamic sites, then you can modify your _app, or ThemeContainer in this case, as following to prevent the CSSReset from being applied to your Markdown sites:
Let's say you have dynamic pages like x.com/[slug] and static pages like x.com/blog/[slug]
import { useRouter } from "next/router";
<ChakraThemeProvider theme={theme}>
<ColorModeProvider value={"light"}>
<EmotionThemeProvider theme={theme}>
{!useRouter().asPath.includes("/blog/") ? <CSSReset /> : null}
{children}
</EmotionThemeProvider>
</ColorModeProvider>
</ChakraThemeProvider>
This will prevent the CSSReset Component from being rendered on your static pages (if their path includes /blog/) but ChakraUI will still work fine on any other paths.
But again, ChakraUI isn't a good solution to style Markdown since it requires CSSReset to work properly.
This solution worked for me (inside the _app.js file).
I have the same problem when I try to follow the Next.js official tutorial to create a blog using Chakara UI.
My problem is that remark.js renders HTML tags from a markdown file but HTML tags have no styling because Chakra UI requires CSSReset. And setting {resetCSS={false} messed up my website
I solved the problem by adding the default CSS of HTML tags to the global styles in theme.js.
But this feels like a dumb solution and I'm looking for better solutions as well
const styles = {
global: (props) => ({
body: {
bg: mode("#f0e7db", "#111111")(props),
},
p: {
display: "block",
marginBlockStart: "1em",
marginBlockEnd: "1em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
lineHeight: "200%",
},
li: {
display: "list-item",
textAlign: "-webkit-match-parent",
},
ul: {
paddingInlineStart: "20px",
},
ol: {
display: "block",
listStyleType: "decimal",
marginBlockStart: "1em",
marginBlockEnd: "1em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
paddingInlineStart: "40px",
},
h1: {
display: "block",
fontSize: "2em",
marginBlockStart: "0.67em",
marginBlockEnd: "0.67em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
fontWeight: "bold",
},
h2: {
display: "block",
fontSize: "1.5em",
marginBlockStart: "0.83em",
marginBlockEnd: "0.83em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
fontWeight: "bold",
},
h3: {
display: "block",
fontSize: "1.17em",
marginBlockStart: "1em",
marginBlockEnd: "1em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
fontWeight: "bold",
},
h4: {
display: "block",
fontSize: "1em",
marginBlockStart: "1.33em",
marginBlockEnd: "1.33em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
fontWeight: "bold",
},
h5: {
display: "block",
fontSize: "0.83em",
marginBlockStart: "1.67em",
marginBlockEnd: "1.67em",
marginInlineStart: "0px",
marginInlineEnd: "0px",
fontWeight: "bold",
},
a: {
color: "#58a6ff",
},
pre: {
display: "block",
fontFamily: "monospace",
whiteSpace: "pre",
margin: "1em 0",
},
strong: {
fontWeight: "bold",
},
em: { fontStyle: "italic" },
blockquote: {
display: "block",
marginBlockStart: "1em",
marginBlockEnd: "1em",
marginInlineStart: "40px",
marginInlineEnd: "40px",
borderLeft: "5px solid #ccc",
margin: "1.5em 10px",
padding: "0.5em 10px 0.5em 10px",
},
code: { fontFamily: "monospace" },
}),
};

How to add background image in material-ui drawer component

I have tried these two methods, but neither work.
Method one: change Drawer component directly.
<Drawer
style={{backgroundImage: url('../../public/images/sideList.jpg')}}>
<div>
<SideList/>
</div>
</Drawer>
Method two: add background-image property for a nested div.
<Drawer>
<BackgroundImageDiv>
<SideList/>
</BackgroundImageDiv>
</Drawer>
Please add background image url for your drawer style class
import drawerImage from "../../resources/images/drawer.jpg";
const styles = theme => ({
drawerPaper: {
backgroundImage: `url(${drawerPaper})`
},
})
drawer component sample
<Drawer classesName={classes.drawerPaper} />
In MUI 5.0+, you can do it with this:
Example of the navigation drawer with background image and opacity in black, like this.
<Drawer
variant="permanent"
sx={ {
display: { xs: 'none', sm: 'block' },
'& .MuiDrawer-paper': {
boxSizing: 'border-box',
width: DRAWER_WIDTH,
backgroundImage: 'url(/img/meteorImpact.jpg)',
position: 'absolute',
backgroundSize: 'cover',
backgroundPosition: 'center center',
'&:before': {
position: 'absolute',
width: '100%',
height: '100%',
content: '""',
display: 'block',
background: '#000',
opacity: '0.6'
}
}
} }
open
>
{ brand }
{ drawerItems }
</Drawer>

Resources