Material UI ButtonGroup breaks when adding href to Button - reactjs

Everything is in the title. Here is the CodeSandbox with reproduction !
When using MUI's ButtonGroup, the layout is fine with three "regular" buttons:
<ButtonGroup variant="contained" aria-label="outlined primary button group">
<Button>One</Button>
<Button>Two</Button>
<Button>Three</Button>
</ButtonGroup>
Result:
But as soon as we add an href to one of the buttons, it breaks the layout, like so:
<ButtonGroup variant="contained" aria-label="outlined primary button group">
<Button href="test.com">One</Button>
<Button>Two</Button>
<Button>Three</Button>
</ButtonGroup>
Result:
Is there any way to fix this ? I don't want to have to use the trick of onClick and location.replace or location.href...
Also, maybe I should post this as an issue on MUI's GitHub? I'm guessing only if it is relevant and not fixable, but there are so many I doubt they would see it.

Use the prop LinkComponent="button":
import * as React from "react";
import Button from "#mui/material/Button";
import ButtonGroup from "#mui/material/ButtonGroup";
export default function BasicButtonGroup() {
return (
<ButtonGroup variant="contained" aria-label="outlined primary button group">
<Button LinkComponent="button" href="test.com">
One
</Button>
<Button>Two</Button>
<Button>Three</Button>
</ButtonGroup>
);
}
Demo: https://codesandbox.io/s/basicbuttongroup-material-demo-forked-43pmhn?file=/demo.js:0-430

Related

MUI Badge is not showing in the DOM, in inspector the span is in tact

I've created a React RTK application with mainly MUI Components.
Suddenly (and I can' reconstruct where when or why) the Mui <Badge> is not showing in my app, although it is in plain sight in the DevTools, elements.
The code that leads to this example page is:
import { Badge, Button } from "#mui/material";
import React from "react";
export default () => (
<div>
<Badge variant="dot" badgevalue={10}>
<Button variant="outlined">test</Button>
</Badge>
</div>
);
I've tried a lot, but with no succes. Can anyone help me out what the problem is here?
you have not added color props to badge. thats why its not displaying. also I checked the badge API. there is no badgeValue props. but there is badgeContent props. https://mui.com/api/badge/
<div>
<Badge variant="dot" badgevalue={10} color="primary">
<Button variant="outlined">test</Button>
</Badge>
<Badge variant="dot" badgeContent={10} color="primary">
<Button variant="outlined">test</Button>
</Badge>
</div>

typography variant in the Material UI

I am a new material UI, I am trying to use Typography, if I don't use variant in the typography tag, it is fine
<div className="login-details">
<Typography>{details ? details.email : "N/A"} |</Typography>
<Button type="link" onClick={()=> document.dispatchEvent(new CustomEvent("user.start_logout"))}>
logout
</Button>
</div>
if I use a variant like below, the UI is falling apart
<div className="login-details">
<Typography variant="h2">{details ? details.email : "N/A"} |</Typography>
<Button type="link" onClick={() => document.dispatchEvent(new CustomEvent("user.start_logout"))}>
logout
</Button>
</div>
Please help me.
Thanks in Advance.
Not using a variant is equivalent to variant="body1". Check your h2 style, particularly the line height and margins. Refer to the documentation on how to customise the default typography.

Unable to load Material-UI icons

I have an issue with my icons displaying:
Here is my code:
import SearchIcon from '#material-ui/icons/Search';
<Button variant='contained' className={classes.sendButton}>
Search
<img src={SearchIcon} alt='search'/>
</Button>
However, in the UI it gets rendered with the default 'broken-page' symbol. There is no error in console.
I have npm installed the latest #material-ui/icons=^4.9.1
I am unable to ascertain what is causing this issue.
Thanks
Ciao, to show Material UI icon is not necessary to use img tag. Just write:
import SearchIcon from '#material-ui/icons/Search';
<Button variant='contained' className={classes.sendButton}>
Search
<SearchIcon />
</Button>
And you should see your icon in button.
Anyway, I suggest you to use startIcon Button prop like:
<Button
variant='contained'
className={classes.sendButton}
startIcon={<SearchIcon />}
>
Search
</Button>

Material UI Button contained style not working with Transition

Current Behavior 😯
When having a Button with a "contained" variant in a Transition (Slide, Grow or Fade) the Button's variant is no longer recognized.
Expected Behavior 🤔
The Button should have a background color.
Steps to Reproduce 🕹
`import { ButtonGroup, Button, Slide } from '#material-ui/core';`
<ButtonGroup variant="text" color="primary" aria-label="menu" id="buttongroup">
{sections.map(x => <Button onClick={() => window.location.href = `#${x.name.toLowerCase()}`} key={x.name}>{x.name}</Button>)}
<Button onClick={() => window.open('tel:00918779839201')}>(+91) 8779839201</Button>
<Slide
direction="left"
in={useScrollTrigger({threshold: document.documentElement.clientHeight/1.5})}
mountOnEnter
unmountOnExit
>
<Button variant="contained">Schedule Site Visit</Button>
</Slide>
</ButtonGroup>
You can see an example here.
Just scroll down and you'll see the Button sliding in on the top right of the Appbar.
ButtonGroup assumes that its direct children are Button elements. Unless the child has the variant prop specified, ButtonGroup will give the child its own variant. In your case, you have a child that is a Slide element and that Slide element does not have a variant specified, so ButtonGroup gives it one (text in your case). Slide passes this property on to the element it wraps and thus overrides the variant of your contained Button.
You can overcome this by specifying the variant on the Slide element:
import React from "react";
import Button from "#material-ui/core/Button";
import ButtonGroup from "#material-ui/core/ButtonGroup";
import Slide from "#material-ui/core/Slide";
export default function App() {
const [slideIn, setSlideIn] = React.useState(false);
return (
<ButtonGroup variant="text">
<Button onClick={e => setSlideIn(true)}>Slide In</Button>
<Button onClick={e => setSlideIn(false)}>Slide Out</Button>
<Slide
variant="contained"
direction="left"
in={slideIn}
mountOnEnter
unmountOnExit
>
<Button variant="contained">Schedule Site Visit</Button>
</Slide>
</ButtonGroup>
);
}

Customize Dialog Material UI

I'm using material ui dialog, and i want to insert a table inside dialog.
Can I do that?
<Button
variant="contained"
color="secondary"
onClick={this.handleOpenDialog}
>
Cancel
</Button>
<Dialog
agree={() =>
this.handleCloseDialog
}
open={this.state.openDialog}
title="Are you sure?"
dialog="Your data will be deleted!"
onClose={this.handleCloseDialog}
></Dialog>
Place the table inside a DialogContent component within your dialog: https://material-ui.com/components/dialogs/#AlertDialog.js

Resources