Skip to content

Instantly share code, notes, and snippets.

@eshrinivasan
Last active January 11, 2022 06:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eshrinivasan/b72206c5aba6c8271c7085be28dfe793 to your computer and use it in GitHub Desktop.
Save eshrinivasan/b72206c5aba6c8271c7085be28dfe793 to your computer and use it in GitHub Desktop.
react component template
import React from 'react';
import {
Box,
Grid,
Typography,
makeStyles,
Button
} from '@material-ui/core';
import IconButton from '@material-ui/core/IconButton';
import CloseIcon from '@material-ui/icons/Close';
import Alert from '@material-ui/lab/Alert';
import { withStyles } from '@material-ui/core/styles';
import { getCrawlerData } from '../../services/user.service';
const useStyles = makeStyles(() => ({
root: {},
avatar: {
height: 100,
width: 100
}
}));
const styles = theme => ({
extendedIcon: {
marginRight: theme.spacing.unit,
},
drawer: {
width: "50%"
}
});
class ProfileUpdateDialog extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
componentDidMount() {}
render() {
const { classes } = this.props;
return (
<p>
<Alert
action={
<Button color="inherit" size="small">
View
</Button>
}
>
New data is available.
</Alert>
</p>
)
}
}
export default withStyles(styles)(ProfileUpdateDialog);
@eshrinivasan
Copy link
Author

eshrinivasan commented Jun 18, 2021

https://stackoverflow.com/questions/56554586/how-to-use-usestyle-to-style-class-component-in-material-ui

import React from 'react';
import {    Box,    Grid,    Typography,    makeStyles,    Button} from '@material-ui/core';
import { withStyles } from '@material-ui/core/styles';

const useStyles = theme => ({
fab: {
  position: 'fixed',
  bottom: theme.spacing(2),
  right: theme.spacing(2),
},
});

class ClassComponent extends Component {
   render() {
            const { classes } = this.props;

            {/** your UI components... */}
          return (
                <div className={classes.fab}> hello</div>
           )
      }
} 


export default withStyles(useStyles)(ClassComponent)

@eshrinivasan
Copy link
Author

How to use spacing with theme:

import React, { useState, useEffect } from 'react';
import {
    Box,
    Container,
    makeStyles,
    Card,
    CardContent,
    TextField,
    InputLabel,
    Button,
    ThemeProvider
} from '@material-ui/core';
import { createMuiTheme } from '@material-ui/core/styles';
import { withStyles } from '@material-ui/core/styles';
import { Label } from '@material-ui/icons';

const theme = createMuiTheme({
    spacing: factor => `${0.25 * factor}rem`,
    palette: {
        text: {
            color: 'red'
        },
    },
    overrides: {

    },
});

const useStyles = (theme) => ({
    root: {
        flexGrow: 1
    },
})

class ConnectionInterface extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        const { classes } = this.props;
        return (
            <ThemeProvider theme={theme}>
                <Container maxWidth={false} className={classes.root}>
                <Box m={2}>
                    <Card>
                        <CardContent style={{ padding: 8 }}>
                            <Box m={2}>
                                <TextField id="outlined-basic-systemid" label="System Id" variant="outlined" />
                            </Box>
                            <Box m={2}>
                                <TextField id="outlined-basic" label="Technical User" variant="outlined" />
                            </Box>
                            <Box m={2}>
                                <Button variant="contained" color="primary">
                                    Connect
                                </Button>
                            </Box>
                        </CardContent>
                    </Card>
                </Box>
            </Container>
            </ThemeProvider>
            
        );
    }
}

export default withStyles(useStyles, { withTheme: true })(ConnectionInterface);

@eshrinivasan
Copy link
Author

eshrinivasan commented Nov 8, 2021

functional approach - latest

import React, { useState, useEffect } from 'react';
import {
    Box,
    Container,
    Paper,
    Card,
    CardContent,
    TextField,
    InputLabel,
    Button,
    ThemeProvider
} from '@material-ui/core';
import { createTheme, withStyles, makeStyles  } from '@material-ui/core/styles';


const theme = createTheme({
    spacing: factor => `${0.25 * factor}rem`,
    palette: {
        text: {
            color: 'red'
        },
        background:{
            paper:{
                background: '#fff'
            }
        }
    },
    overrides: {
        MuiButton: {
            contained: {
              backgroundColor: '#354A5F',
              color: '#fff',
      
              '&:hover':{
                backgroundColor: '#354A5F',
                color: '#fff'
              }
            },
          }
    },
    typography: {
        body1: {
            
        }
    }
});

const useStyles = makeStyles(theme => ({
    root: {
        backgroundColor: theme.palette.background.paper,
        color:"black",
        margin: theme.spacing(2),
        width: "auto",
        height: "-webkit-fill-available"
    },
}))

function OrderSuccess() {
        const classes = useStyles();
    
        return (
            <ThemeProvider theme={theme} >
            <Container maxWidth={false} className={classes.root}>
           
                <Box m={2}>
                    Order successful!
                </Box>
            </Container>
            </ThemeProvider>
            
        );
}

export default OrderSuccess;

@eshrinivasan
Copy link
Author

Expected output from above code

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment