Material UI v1 with React - styling buttons - reactjs

I'm trying to learn how to use Material UI with React.
I have incorporated v1 of Material UI in my project.
Nothing about coding comes intuitively to me, so I struggle to fill the gaps between the clues left in the documentation for resources.
I know I haven't got the hang of this yet, but piecing together what others have been able to do, I have set up a button in my project, as follows:
import React from 'react';
import Button from 'material-ui/Button';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import { fade } from 'material-ui/styles/colorManipulator';
const MyButton = (props) => {
return <span>
<Button {...props} />
</span>
};
export default MyButton;
I then try to use my button in a couple of places:
import React from 'react';
import MyButton from '../materialui/Button.js';
const style = {
background: '#FF5349',
color: 'white',
padding: '0 30px',
// marginBottom: '30px',
};
const Start = () => (
<span>
<MyButton style={style} size="large">
GET STARTED
</MyButton>
</span>
);
export default Start;
I am trying to change the size of the button.
The Material UI documentation indicates that I should be able to toggle the size property by inserting size="large".
The example given in the documentation is:
<Button size="large" className={classes.button}>
Large
</Button>
I've tried to insert size="large" in the const style in my Start component, in the use of MyButton in the start component and in the Button component itself. None of these attempts do anything to alter the size. The text label in the button looks miniature at the moment and I can't figure out how to manipulate the size.
Can anyone see how to increase the size of the button?

Here is how I have been using it.
You need to set the root Class of the button object (or another available class, refer to the documentation for each available class by components)
import React, { Component } from "react";
import { withStyles } from "material-ui/styles";
import Button from "material-ui/Button";
const styles = theme => ({
button: {
width: "300px",
margin: "0 auto",
textTransform: "uppercase",
padding: "20px 30px",
alignSelf: "center",
},
});
class MyCustomButton extends Component {
render() {
const { classes } = this.props;
return (
<Button color="primary" raised={true} classes={{ root: classes.button }} />
);
}
}
export default withStyles(styles)(MyCustomButton);

Related

antDesign: tabs styles

image-1:
image-2:
I have a project that contains several interfaces, and among these interfaces there is an interface that displays two tables through “Tabs”, when I click on the first tab it displays the first table and when I click on the second tab it displays the second table, but my problem is only in the style of the Tabs and not my problem In the content, I want the style of the second image to be the same as the style of the first image
file.tsx:
import _ from 'lodash';
import { FunctionComponent } from 'react';
import { Tabs } from 'antd';
import AllPatients from './components/all-patients';
import AllPresentPatient from './components/present-patients';
import { FormattedMessage } from 'react-intl';
const { TabPane } = Tabs;
interface PatientListPageProps { }
const PatientListPage: FunctionComponent<PatientListPageProps> = () => {
return (
<>
<div style={{ background: '#fff', padding: '16px', borderColor: 'transparent', borderRadius: '4px' }}>
<Tabs type="card">
<Tabs.TabPane
tab={<FormattedMessage id='all' />} key="1">
<AllPatients />
</Tabs.TabPane>
<Tabs.TabPane tab={<FormattedMessage id='attendees' />} key="2">
<AllPresentPatient />
</Tabs.TabPane>
</Tabs>
</div>
</>
);
};
export default PatientListPage;
i think you must custom the css.
in tabs add the new name of classname
.custom-classname .ant-tabs-rtl {
direction: ltr !important;
}
if it doesn't change you can try using
.custom-classname {
direction: ltr !important;
}
then if you want the contents of the tab to stay on the right, you have to also customize the css so that the direction stays on the right
i think you will use ":global" to overwrite the default style in antd
like this
:global {
.ant-select-selection-item {
// overwrite style
...
}
}

I want to style the Button Component with some properties but I see that I can't use classname in updated MUI5

import { styled } from "#mui/system";
import DeleteIcon from "#mui/icons-material/Delete";
import SendIcon from "#mui/icons-material/Send";
import { Button } from "#mui/material";
const Button = styled("button")({
color: "red",
backgroundColor: "black",
padding: "1rem",
});
function App() {
return (
<div>
<Button
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</Button>
</div>
);
}
export default App;
I think styled is now a new way in MUI to create make and use custom
styles. I don't think I can use className property in this case.
Previously I could use makeStyles and useStyles and assign classname
to the Button component to customize it, How can I use Button
Component from MUI and customize it using styled . Right now I should
define at the top what kind of component is. I want to use Button component, its variant color properties and also customize it using styled.
You can also use sx to add css to components in MUI 5
<Button
sx={{color: "red",
backgroundColor: "black",
padding: "1rem"}}
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</Button>
you need to use !important to overwrite styles and styled(Button) as it is clear in code. complete version is here in sandbox
import * as React from "react";
import { styled } from "#mui/styles";
import Button from "#mui/material/Button";
import DeleteIcon from "#mui/icons-material/Delete";
import SendIcon from "#mui/icons-material/Send";
const MyButton = styled(Button)({
backgroundColor: "#000 !important",
color: "red !important",
padding: "1rem !important"
});
export default function App() {
return (
<MyButton
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Styled Components
</MyButton>
);
}
Try to use the code below
const StyledButton = styled(Button)(() => ({
color: "red",
backgroundColor: "black",
padding: "1rem",
}));
In the first () you need to pass the material ui component as the named export and then you can use the const-name.
So in your code in stead of <Button></Button> you will use the <StyledButton></StyledButton>
Usage:
<StyledButton
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</StyledButton>
I see a couple of problems here:
you need to make sure that you import as follows:
import { styled } from '#mui/material/styles';
https://mui.com/material-ui/migration/migrating-from-jss/
since you want to customize the mui, pass the Button to styled and rename the component
const CustomButton = styled(Button)({
color: "red",
backgroundColor: "black",
padding: "1rem",
});
Then you can use it
function App() {
return (
<CustomButton
variant="contained"
size="medium"
startIcon={<DeleteIcon />}
endIcon={<SendIcon />}
>
Material UI
</CustomButton>
);
}

React material-ui links

I am using react material ui. In my header i have used links as well as tabs both. Please tell me how to do linking using material links as we can't use to. Can it be done by on click method and navigate to that page as well as what changes i need to do in my app.tsx file. please help me. I am beginner. Home I had made a component name home but how can i call on click of link?
import React, { Component } from 'react';
import { makeStyles, createStyles, Theme } from '#material-ui/core/styles';
import Link from '#material-ui/core/Link';
import Typography from '#material-ui/core/Typography';
import Home from '../pages/Home';
import Home2 from '../pages/Home2';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
'& > * + *': {
marginLeft: theme.spacing(3),
color:"#F08713",
paddingRight: "revert",
borderRight: "1px solid #ddd"
},
},
}),
);
function Links() {
const classes = useStyles();
const preventDefault = (event: React.SyntheticEvent) => event.preventDefault();
return (
<Typography className={classes.root}>
<Link
underline ="hover"
component="button"
href="Home">
Home
</Link>
<Link
underline ="hover"
component="button"
href="#/home2">
For home2
</Link>
</Typography>
);
}
export default Links

Can you add an additional color in Material UI?

I already have a styleguide that I'm trying to implement in Material UI. I can see the Button's color prop takes these options:
| 'default'
| 'inherit'
| 'primary'
| 'secondary'
However I need an additional one:
| 'default'
| 'inherit'
| 'primary'
| 'secondary'
| 'tertiary'
Can you create a new color in Material UI that works with the general theming system? Or is this not really how it's supposed to be used?
UPDATE - This answer was written for v4 of Material-UI. v5 supports custom colors directly and I have added a v5 example at the end.
Though Material-UI does not support this directly in v4, you can wrap Button in your own custom component to add this functionality.
The code below uses a copy of the styles for textPrimary, outlinedPrimary, and containedPrimary but replaces "primary" with "tertiary".
import * as React from "react";
import Button from "#material-ui/core/Button";
import { makeStyles } from "#material-ui/core/styles";
import clsx from "clsx";
import { fade } from "#material-ui/core/styles/colorManipulator";
const useStyles = makeStyles(theme => ({
textTertiary: {
color: theme.palette.tertiary.main,
"&:hover": {
backgroundColor: fade(
theme.palette.tertiary.main,
theme.palette.action.hoverOpacity
),
// Reset on touch devices, it doesn't add specificity
"#media (hover: none)": {
backgroundColor: "transparent"
}
}
},
outlinedTertiary: {
color: theme.palette.tertiary.main,
border: `1px solid ${fade(theme.palette.tertiary.main, 0.5)}`,
"&:hover": {
border: `1px solid ${theme.palette.tertiary.main}`,
backgroundColor: fade(
theme.palette.tertiary.main,
theme.palette.action.hoverOpacity
),
// Reset on touch devices, it doesn't add specificity
"#media (hover: none)": {
backgroundColor: "transparent"
}
}
},
containedTertiary: {
color: theme.palette.tertiary.contrastText,
backgroundColor: theme.palette.tertiary.main,
"&:hover": {
backgroundColor: theme.palette.tertiary.dark,
// Reset on touch devices, it doesn't add specificity
"#media (hover: none)": {
backgroundColor: theme.palette.tertiary.main
}
}
}
}));
const CustomButton = React.forwardRef(function CustomButton(
{ variant = "text", color, className, ...other },
ref
) {
const classes = useStyles();
return (
<Button
{...other}
variant={variant}
color={color === "tertiary" ? "primary" : color}
className={clsx(className, {
[classes[`${variant}Tertiary`]]: color === "tertiary"
})}
ref={ref}
/>
);
});
export default CustomButton;
Then this CustomButton component can be used instead of Button:
import React from "react";
import {
makeStyles,
createMuiTheme,
ThemeProvider
} from "#material-ui/core/styles";
import Button from "./CustomButton";
import lime from "#material-ui/core/colors/lime";
const useStyles = makeStyles(theme => ({
root: {
"& > *": {
margin: theme.spacing(1)
}
}
}));
const theme = createMuiTheme({
palette: {
tertiary: lime
}
});
// This is a step that Material-UI automatically does for the standard palette colors.
theme.palette.tertiary = theme.palette.augmentColor(theme.palette.tertiary);
export default function ContainedButtons() {
const classes = useStyles();
return (
<ThemeProvider theme={theme}>
<div className={classes.root}>
<Button variant="contained">Default</Button>
<Button variant="contained" color="primary">
Primary
</Button>
<Button variant="contained" color="secondary">
Secondary
</Button>
<br />
<Button variant="contained" color="tertiary">
Tertiary
</Button>
<Button color="tertiary">Tertiary text</Button>
<Button variant="outlined" color="tertiary">
Tertiary outlined
</Button>
</div>
</ThemeProvider>
);
}
In v5, the custom button is not necessary. All you need to do is create the theme appropriately:
import React from "react";
import { styled, createTheme, ThemeProvider } from "#material-ui/core/styles";
import Button from "#material-ui/core/Button";
import { lime } from "#material-ui/core/colors";
const defaultTheme = createTheme();
const theme = createTheme({
palette: {
// augmentColor is a step that Material-UI automatically does for the standard palette colors.
tertiary: defaultTheme.palette.augmentColor({
color: { main: lime[500] },
name: "tertiary"
})
}
});
const StyledDiv = styled("div")(({ theme }) => ({
"& > *.MuiButton-root": {
margin: theme.spacing(1)
}
}));
export default function ContainedButtons() {
return (
<ThemeProvider theme={theme}>
<StyledDiv>
<Button variant="contained">Default</Button>
<Button variant="contained" color="primary">
Primary
</Button>
<Button variant="contained" color="secondary">
Secondary
</Button>
<br />
<Button variant="contained" color="tertiary">
Tertiary
</Button>
<Button color="tertiary">Tertiary text</Button>
<Button variant="outlined" color="tertiary">
Tertiary outlined
</Button>
</StyledDiv>
</ThemeProvider>
);
}
Taken from material UI's color palette docs, https://material-ui.com/customization/palette/
A color intention is a mapping of a palette color to a given intention within your application. The theme exposes the following palette colors (accessible under theme.palette.):
primary - used to represent primary interface elements for a user. It's the color displayed most frequently across your app's screens and components.
secondary - used to represent secondary interface elements for a user. It provides more ways to accent and distinguish your product. Having it is optional.
error - used to represent interface elements that the user should be made aware of.
warning - used to represent potentially dangerous actions or important messages.
info - used to present information to the user that is neutral and not necessarily important.
success - used to indicate the successful completion of an action that user triggered.
If you want to learn more about color, you can check out the color section.
So you could probably look into reassigning either of the warn/success/info/error buttons. Generally i would keep the error and warn colors both as red, so the warn palette would be free to reassign for me.

How to hide/show elements at certain breakpoints with Material UI?

I want to show/hide elements on certain break points, like what I would do with Bootstraph or Zurb Foundation.
I see in the documentation here https://material-ui.com/system/display/#api we add
display={{ xs: 'block', md: 'none' }}
to our elements. I have done this, but I don't get any results - no hiding/showing elements, no errors, no compilation problems.
Would anyone know how this is done?
My code is:
import React from 'react'
import PropTypes from 'prop-types'
import makeStyles from '#material-ui/core/styles/makeStyles'
import Button from '#material-ui/core/Button'
const useStyles = makeStyles(styles)
const PhoneActionLink = ({ children, prefix, href, value, display, isFirst, ...other }) => {
const classes = useStyles()
return (
<Button
display={{ xs: 'block', md: 'none' }}
{...other}
>
{children}
</Button>
</div>
)
}
Here you go with a solution
import React from 'react'
import PropTypes from 'prop-types'
import makeStyles from '#material-ui/core/styles/makeStyles'
import Box from '#material-ui/core/Box'
import Button from '#material-ui/core/Button'
const useStyles = makeStyles(styles)
const PhoneActionLink = ({ children, prefix, href, value, display, isFirst, ...other }) => {
const classes = useStyles()
return (
<Box component="Button" display={{ xs: 'block', md: 'none' }} m={1}>
{children}
</Box>
)
}
Wrap the Button component within Box component.
The answer strictly depends on the version of MUI you're using.
The accepted answer should work for MUI v4 and prior versions, but didn't work for me (MUI v5).
I dug into the documentation From the official MUI website and found that the following code works for me:
<Box sx={{ display: { xs: 'block', md:'none' } }}>
{children}
</Box>
I'm guessing that the sx attribute allows you to modify some of the CSS properties based off specific breakpoints.
I'm no expert so I can't fully explain the solution, but I'm hoping that this solution helps another person out there.
Along with the other solutions, you could also use the component. Although it's deprecated now.

Resources