Overriding default breakpoints in MUI System with typescript - material-ui

I am trying to set new breakpoints for my container width in MUI System. It works without typescript, but in my typescript project, it does not recognize my custom breakpoints and only recognizes the default breakpoints (xs, sm, md, lg, and xl). I followed the suggested BreakpointOverrides suggested here
```
import React from 'react';
import Container from '#mui/system/Container';
import { createTheme, ThemeProvider } from '#mui/system';
declare module '#mui/system' {
interface BreakpointOverrides {
// Your custom breakpoints
laptop: true;
tablet: true;
mobile: true;
desktop: true;
xldesktop: true;
// Remove default breakpoints
xs: false;
sm: false;
md: false;
lg: false;
xl: false;
}
}
const theme = createTheme({
breakpoints: {
// eslint-disable-next-line #typescript-eslint/ban-ts-comment
//#ts-ignore
values: {
mobile: 0,
tablet: 640,
laptop: 1024,
desktop: 1280,
},
},
});
const HeroHomepage = ({}) => {
return (
<ThemeProvider theme={theme}>
<Container
sx={{
bgcolor: {
mobile: 'green',
tablet: 'blue',
laptop: 'orange',
desktop: 'yellow',
},
width: 200,
height: 500,
}}
>
<div>hello world</div>
</Container>
</ThemeProvider>
);
};
```
When I do this, no color at all is applied to the container, as it does not recognize the custom breakpoints.

Try to use latest version of MUI. It seems to be recently fixed, according to this discussion https://github.com/mui/material-ui/issues/26369.
In my case it helped: no TS errors now and breakpoints works.

Related

Cannot override default Material UI Icon styling

I have an AccountCircleIcon Material UI icon and I want to increase the size, I've tried several things:
import AccountCircleIcon from '#material-ui/icons/AccountCircle';
import {withStyles} from '#material-ui/styles';
const StyledIcon = withStyles({
root: {
fontSize: '50rem',
},
})(AccountCircleIcon);
const login = () => {
return (
<div><StyledIcon /></div>
);
};
import AccountCircleIcon from '#material-ui/icons/AccountCircle';
import {makeStyles} from '#material-ui/styles';
const useStyles = makeStyles({
root: {
fontSize: '50rem',
},
});
const login = () => {
const classes = useStyles();
return (
<div><AccountCircleIcon classes={{root: classes.root}} /></div>
);
};
import AccountCircleIcon from '#material-ui/icons/AccountCircle';
import {makeStyles} from '#material-ui/styles';
const useStyles = makeStyles({
avatarIcon: {
fontSize: '50rem',
},
});
const login = () => {
const classes = useStyles();
return (
<div><AccountCircleIcon className={classes.avatarIcon} /></div>
);
};
But each time the default icon styling overrides the added styling:
I missed the StyledEngineProvider https://mui.com/guides/interoperability/ wrapping that around your app assures the custom css is injected first:
import * as React from 'react';
import { StyledEngineProvider } from '#mui/material/styles';
export default function GlobalCssPriority() {
return (
<StyledEngineProvider injectFirst>
{/* Your component tree. Now you can override MUI's styles. */}
</StyledEngineProvider>
);
}
https://codesandbox.io/s/laughing-alex-nyszr?file=/src/Demo.tsx

Change checkbox icon in theme

Is it possible to change the icon of the checkbox from the theme in Material UI?
props: {
MuiButtonBase: {
disableRipple: buttonRippleDisable
},
MuiButton: {
disableElevation: buttonElevationDisable
},
MuiCheckbox: {
icon: CheckCircleOutline,
}
}
yes. pass the icon in props object in theme as you've done above.
const theme = createMuiTheme({
props:{
MuiCheckbox:{
icon: <FavoriteBorder />,
checkedIcon:<Favorite />
}
}
});
Now wherever you use the checkbox in the scope of this theme, you'll get this icon by default.
Working demo:
In MUI v5
const theme = createMuiTheme({
components: {
MuiCheckbox: {
defaultProps: {
icon: {YOUR ICON},
checkedIcon: {YOUR ICON}
}
}
}
});

Use the Facebook Login function in a React Native Component

I'm trying to implement the Facebook login in my react native application.
I'm following this tutorial : https://dev.to/rishikeshvedpathak/react-native-login-with-facebook-4mmi
and that's working great, I have the Facebook login button and everything works.
But he did it in a function and I want to implement it into a component.
I have a component with a Facebook login button and I want this button to call the FacebookLogin function from my Facebook.js file which contains the code from the tutorial above.
I tried to import it like this:
import App from './Facebook.js'
and then call the function like this using the 'onPress' in my TouchableOpacity:
App.FacebookLogin, but nothing worked.
Here is the code from the tutorial:
import React, { useState } from 'react';
import { StyleSheet, Text, View, Image, TouchableOpacity, ActivityIndicator } from 'react-native';
import * as Facebook from 'expo-facebook';
console.disableYellowBox = true;
export default function App() {
const [isLoggedin, setLoggedinStatus] = useState(false);
const [userData, setUserData] = useState(null);
const [isImageLoading, setImageLoadStatus] = useState(false);
facebookLogIn = async () => {
Facebook.initializeAsync('My_App_id', 'My_App_name');
try {
const {
type,
token,
expires,
permissions,
declinedPermissions,
} = await Facebook.logInWithReadPermissionsAsync('My_App_id', 'My_App_name', {
permissions: ['public_profile'],
});
if (type === 'success') {
// Get the user's name using Facebook's Graph API
fetch(`https://graph.facebook.com/me?access_token=${token}&fields=id,name,email,picture.height(500)`)
.then(response => response.json())
.then(data => {
setLoggedinStatus(true);
setUserData(data);
})
.catch(e => console.log(e))
} else {
// type === 'cancel'
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}
logout = () => {
setLoggedinStatus(false);
setUserData(null);
setImageLoadStatus(false);
}
return (
isLoggedin ?
userData ?
<View style={styles.container}>
<Image
style={{ width: 200, height: 200, borderRadius: 50 }}
source={{ uri: userData.picture.data.url }}
onLoadEnd={() => setImageLoadStatus(true)} />
<ActivityIndicator size="large" color="#0000ff" animating={!isImageLoading} style={{ position: "absolute" }} />
<Text style={{ fontSize: 22, marginVertical: 10 }}>Hi {userData.name}!</Text>
<TouchableOpacity style={styles.logoutBtn} onPress={this.logout}>
<Text style={{ color: "#fff" }}>Logout</Text>
</TouchableOpacity>
</View> :
null
:
<View style={styles.container}>
<Image
style={{ width: 200, height: 200, borderRadius: 50, marginVertical: 20 }}
source={require("../assets/logo_yorder.png")} />
<TouchableOpacity style={styles.loginBtn} onPress={this.facebookLogIn}>
<Text style={{ color: "#fff" }}>Login with Facebook</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#e9ebee',
alignItems: 'center',
justifyContent: 'center',
},
loginBtn: {
backgroundColor: '#4267b2',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 20
},
logoutBtn: {
backgroundColor: 'grey',
paddingVertical: 10,
paddingHorizontal: 20,
borderRadius: 20,
position: "absolute",
bottom: 0
},
});
And here is the TouchableOpacity from my component :
<View style={styles.connexionServices}>
<TouchableOpacity style={[styles.connexionFacebook, styles.connexionCommon]}>
<Text style={styles.textButton}>Facebook</Text>
</TouchableOpacity>
<TouchableOpacity style={[styles.connexionGoogle, styles.connexionCommon]}>
<Text style={styles.textButton}>Google</Text>
</TouchableOpacity>
</View>
Here it is, I would like to implement Google login too so if you have some tips, it would be nice.
PS : I'm using react-native-cli: 2.0.1 and react-native: 0.61.4 with expo 3.18.6
OK I did it, i juste declared the function before the component declaration so i can call it in my component. It was a little tricky to call some of the functions in my component in the function but everything work now and i can log to facebook throught my Login component !

How do you change the height of a material-ui fab?

I wanna edit the width and height of my material-ui fab button. I have tried to change it by using classname and applying styles. The width changes but my problem is that the height doesn't.
This is the css code:
const dashboardStyles = makeStyles(theme => ({
infoButton: {
height: '20px',
width: '20px'
}
}));
It changes the width but doesn't change the height. Thanks for any help.
You can use material-ui makeStyles:
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import Fab from "#material-ui/core/Fab";
import AddIcon from "#material-ui/icons/Add";
const useStyles = makeStyles(theme => ({
fab: {
width: "80px",
height: "80px"
}
}));
export default function FloatingActionButton() {
const classes = useStyles();
return (
<Fab className={classes.fab}>
<AddIcon />
</Fab>
);
}

Clipped drawer in Material ui

According to docs, material-ui supports persistant drawer.
But my expected behaviour is a clipped persistant drawer like the photo.
My Sidebar component:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Drawer from 'material-ui/Drawer';
import List, { ListItem, ListItemIcon, ListItemText } from 'material-ui/List';
import Face from 'material-ui-icons/Face';
import Person from 'material-ui-icons/Person';
import Assignment from 'material-ui-icons/Assignment';
import NavLink from 'react-router-dom/NavLink';
import { FormattedMessage } from 'react-intl';
import styles from '../../../../style/components/global/Sidebar.scss';
const cx = require('classnames/bind').bind(styles);
const rootStyles = theme => ({
list: {
width: 250,
flex: 'initial',
},
drawer: {
top: 30,
},
});
class UndockedDrawer extends Component {
render() {
const { classes } = this.props;
const sidebarListItems = (
<div>
<NavLink
to="/users"
className={cx('noStyle')}
>
<ListItem button>
<ListItemIcon>
<Person />
</ListItemIcon>
<ListItemText primary={<FormattedMessage id="user" />} />
</ListItem>
</NavLink>
</div>
);
const sidebarList = (
<div>
<List className={classes.list}>
{sidebarListItems}
</List>
</div>
);
return (
<div>
<Drawer
open={this.props.open}
onRequestClose={this.props.onRequestClose}
onClick={this.props.onRequestClose()}
type="permanent">
{sidebarList}
</Drawer>
</div>
);
}
}
export default withStyles(rootStyles)(UndockedDrawer);
So far, I've tried to make top property as much as AppBar's height but this behaviour wasn't what I needed.
Is there any way to achieve this?
You need to provide the right styles for the AppBar. Taking the example from the docs you provided:
Instead of:
const styles = theme => ({
...
appBar: {
position: 'absolute',
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth,
},
...
});
Use:
const styles = theme => ({
...
appBar: {
position: 'absolute',
width: '100%',
zIndex: '1400',
},
...
});
Why zIndex is 1400? It's just an arbitrary number that is higher than the zIndex of the Drawer, which is 1300.