I would like to center text in a Material-UI button so that the text is centered irrespective of the icon next to it. At the moment the icon is included in the centering.
The top two buttons in the demo show how it currently looks, I am looking for the text to appear as it does in the bottom two buttons. With the icons before and after these buttons respectively, without influencing the text of the buttons.
Bottom two are desired output
https://codesandbox.io/s/material-demo-forked-tj8ko?file=/demo.js
import React from "react";
import Button from "#material-ui/core/Button";
import { makeStyles } from "#material-ui/core/styles";
import KeyboardArrowRightIcon from "#material-ui/icons/KeyboardArrowRight";
import KeyboardArrowLeftIcon from "#material-ui/icons/KeyboardArrowLeft";
const useStyles = makeStyles((theme) => ({
button: {
width: "100%",
marginBottom: theme.spacing(1),
marginTop: theme.spacing(1)
}
}));
export default function IconLabelButtons() {
const classes = useStyles();
return (
<div>
<Button
variant="contained"
color="secondary"
className={classes.button}
startIcon={<KeyboardArrowLeftIcon />}
>
Back
</Button>
{/* This Button uses a Font Icon, see the installation instructions in the Icon component docs. */}
<Button
variant="contained"
color="primary"
className={classes.button}
endIcon={<KeyboardArrowRightIcon />}
>
Forward
</Button>
<Button variant="contained" color="secondary" className={classes.button}>
Back
</Button>
{/* This Button uses a Font Icon, see the installation instructions in the Icon component docs. */}
<Button variant="contained" color="primary" className={classes.button}>
Forward
</Button>
</div>
);
}
It's possible to move the icons into the inner html of the button and tweak the margins respectively. It's kind of hacky but solves your problem. I forked your demo with the updates: https://codesandbox.io/s/material-demo-forked-xmltd?file=/demo.js
<Button variant="contained" color="secondary" className={classes.button}>
<KeyboardArrowLeftIcon style={{ marginLeft: -28 }} />
Back
</Button>
<Button variant="contained" color="primary" className={classes.button}>
Forward
<KeyboardArrowRightIcon style={{ marginRight: -28 }} />
</Button>
You can get the same effect while still using startIcon and endIcon by customizing the styles for the start/end icons using withStyles and then using the resulting customized component:
const CenteredTextButton = withStyles({
startIcon: {
marginLeft: -28
},
endIcon: {
marginRight: -28
}
})(Button);
Related
I am trying to load a picture using Material UI card component. Everything seems to load except for the actual picture. I have tried all possible ways and other suggestions from the net but no luck.
Here is my code.
I've tried the imageURL, path i even copied the path exactly from the folder structure to see if i wrote it wrong.
import Card from '#mui/material/Card';
import CardMedia from '#mui/material/CardMedia';
import { Button, CardActionArea, CardActions } from '#mui/material';
function DisplayProjectCard(props) {
return (
<Card elevation={0}
sx={{ maxWidth: 645,
backgroundColor: "transparent", p:4 }}>
<CardActionArea>
<CardMedia
component="img"
title="My Next Promo App landing page"
height="240"
width="440"
image="src/static/images/MyNextPromoApp.png"
alt="Home page of My Next Promo App"
/>
</CardActionArea>
<CardActions sx={{
justifyContent:'center'
}}>
<Button size="small" color="primary">
View
</Button> |
<Button size="small" color="primary">
Github
</Button>
</CardActions>
</Card>
);
}
export default DisplayProjectCard```
I want to display two tooltips on a button. However, I can't find any way to do it without errors in the console.
My goal:
Minimal reproducible example
import * as React from 'react';
import { Tooltip, Button, Box } from '#mui/material';
export default function BasicTooltip() {
return (
<Box sx={{ margin: 10 }}>
<Tooltip title="Tooltip on top" placement="top">
<Tooltip title="Tooltip on button" placement="bottom">
<Button>Hello</Button>
</Tooltip>
</Tooltip>
</Box>
);
}
Error I'm getting in the console with this code:
MUI: You have provided a title prop to the child of Tooltip.
Remove this title prop Tooltip on button or the Tooltip component.
Link to stackblitz: https://stackblitz.com/edit/react-qgtibd?file=demo.js
I already tried to isolate the inner tooltip with button in another component like this, but it did not work:
import * as React from 'react';
import { Tooltip, Button, Box } from '#mui/material';
const MyButton = React.forwardRef(({ tooltipText }, ref) => (
<Tooltip title={tooltipText} placement="bottom">
<Button ref={ref}>Hello</Button>
</Tooltip>
));
export default function BasicTooltip() {
return (
<Box sx={{ margin: 10 }}>
<Tooltip title="Tooltip on top" placement="top">
<MyButton tooltipText="Tooltip on bottom" />
</Tooltip>
</Box>
);
}
edit: I also can't put a div nor React.fragment between those tooltips, because it completely screws up the layout in my real case scenario.
Looks like you need to isolate the tooltips from one another
<Box sx={{ margin: 10 }}>
<Tooltip title="Tooltip on top" placement="top">
<Box>
<Tooltip title="Tooltip on button" placement="bottom">
<Button>Hello</Button>
</Tooltip>
</Box>
</Tooltip>
</Box>
modified stackblitz with the aligment
I am trying to center a button group using Material UI in React, no matter what i do it floats to the left.
<Container>
<ButtonGroup style={{alignItems:"center"}} color="primary" aria-label="outlined primary button group">
<Button onClick={()=>{setDonation(1)}}>$1</Button>
<Button onClick={()=>{setDonation(5)}}>$5</Button>
<Button onClick={()=>{setDonation(10)}}>$10</Button>
<Button onClick={()=>{setDonation(25)}}>$25</Button>
</ButtonGroup>
</Container>
Here is a picture of the chrome inspector:
You can center the ButtonGroup by applying the following CSS attributes to the container.
const useStyles = makeStyles({
container: {
display: "flex",
justifyContent: "center",
},
});
const classes = useStyles();
<Container className={classes.container}>
<ButtonGroup
color="primary"
aria-label="outlined primary button group"
>
<Button>$1</Button>
<Button>$5</Button>
<Button>$10</Button>
<Button>$25</Button>
</ButtonGroup>
</Container>
There is documentation here too https://material-ui.com/styles/basics/.
Just found out that you can use Box component as a container with all the properties of a div container in css (At least the necesary ones)
<Box
display="flex"
flexDirection="column"
alignItems="center"
justifyContent="center"
>
<ButtonGroup style={{textAlign:"center"}} color="primary" aria-label="outlined primary button group">
<Button onClick={()=>{setDonation(1)}}>$1</Button>
<Button onClick={()=>{setDonation(5)}}>$5</Button>
<Button onClick={()=>{setDonation(10)}}>$10</Button>
<Button onClick={()=>{setDonation(25)}}>$25</Button>
</ButtonGroup>
</Box>
When i build the project everything looks fine but after i refresh or go to a different page material-ui breaks down and everything looks weird.
Every solution is related to styled-components. I tried adding babel-styled-components but that broke things even more leading to complete non-rendering of material-ui.
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import Button from '#material-ui/core/Button';
const useStyles = makeStyles(theme => ({
root:{
justifyContent: 'center'
},
'#global': {
body: {
backgroundColor: theme.palette.common.black,
},
},
button: {
// margin: theme.spacing(20),
margin: 20
},
input: {
display: 'none',
},
}));
function Index() {
const classes = useStyles();
return (
<div style={{justifyContent: 'center'}}>
<Button href={"/"} variant="outlined" color="secondary" className={classes.button}>
home
</Button>
<Button href={"/login"}variant="outlined" color="secondary" className={classes.button}>
Login
</Button>
<Button variant="outlined" color="primary" className={classes.button}>
Dashboard(protected)
</Button>
</div>
);
}
export default Index;
This should render 3 buttons and some spacing between.
At build, it looks how it should be. After refresh the 3 buttons are glued to each other and the spacing is not existent.
https://codesandbox.io/s/material-demo-yp7pv renders it correctly
https://imgur.com/2xTINrw how it looks in browser
I want to create a button with a built-in linear progress bar. something like this experience, but with Material components:
https://demo.tutorialzine.com/2013/10/buttons-built-in-progress-meters/
I know that there's a way to integrate <CircularProgress/> into a button, is there a way to integrate <LinearProgress/>? it didn't work for me.
Thanks in advance.
Much like the CircularProgress example, which I presume you are referring to this, it's just about getting the CSS correct.
I've forked that example and added a button that has LinearProgress integrated to give you an idea, the relevant code for that example is:
linearProgress: {
position: "absolute",
top: 0,
width: "100%",
height: "100%",
opacity: 0.4,
borderRadius: 4
}
...
<div className={classes.wrapper}>
<Button
variant="contained"
color="primary"
className={buttonClassname}
disabled={loading}
onClick={handleButtonClick}
>
Linear
</Button>
{loading && (
<LinearProgress
color="secondary"
className={classes.linearProgress}
/>
)}
</div>
Something like this:
import React from 'react'
import { makeStyles } from '#material-ui/core/styles'
import Button from '#material-ui/core/Button'
import LinearProgress from '#material-ui/core/LinearProgress'
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
button: {
margin: theme.spacing(1),
},
}))
export default function ContainedButtons() {
const classes = useStyles()
return (
<div className={classes.root}>
<Button variant="contained" className={classes.button}>
<div>
Demo
<LinearProgress variant="determinate" value={75} />
</div>
</Button>
</div>
)
}