In the following example, I am trying to change the size the UI chip in dynamically in order to respond to the font size of its parents using the em
css
unit.
My goal: I want to do something like this
style={{size:'1em'}}
My problem: the chip element in material-ui is not resizable like most of the material-UI ponents.
I tried:
style={{transform:'scale(1em)'}}
but it did not work at all. I don't know how to change the anchor point of transform.- I also tried to create my own chip with
<img style={{float: 'left', width: '1em', borderRadius: '50%',}}/>
but it does not look original as the material UI chip.
import Avatar from '@material-ui/core/Avatar'
import Chip from '@material-ui/core/Chip'
function Chips() {
const classes = useStyles()
const handleDelete = () => {
console.info('You clicked the delete icon.')
}
const handleClick = () => {
console.info('You clicked the Chip.')
}
return (
<div className={classes.root}>
<h1>
<Chip
//style={{size:'1em'}}
avatar={<Avatar alt="Natacha" src="/static/images/avatar/1.jpg" />}
label="Deletable"
onDelete={handleDelete}
/>
</h1>
<h4>
<Chip
//style={{size:'1em'}}
avatar={<Avatar alt="Natacha" src="/static/images/avatar/1.jpg" />}
label="Deletable"
onDelete={handleDelete}
/>
</h4>
</div>
)
}
In the following example, I am trying to change the size the UI chip in dynamically in order to respond to the font size of its parents using the em
css
unit.
My goal: I want to do something like this
style={{size:'1em'}}
My problem: the chip element in material-ui is not resizable like most of the material-UI ponents.
I tried:
style={{transform:'scale(1em)'}}
but it did not work at all. I don't know how to change the anchor point of transform.- I also tried to create my own chip with
<img style={{float: 'left', width: '1em', borderRadius: '50%',}}/>
but it does not look original as the material UI chip.
import Avatar from '@material-ui/core/Avatar'
import Chip from '@material-ui/core/Chip'
function Chips() {
const classes = useStyles()
const handleDelete = () => {
console.info('You clicked the delete icon.')
}
const handleClick = () => {
console.info('You clicked the Chip.')
}
return (
<div className={classes.root}>
<h1>
<Chip
//style={{size:'1em'}}
avatar={<Avatar alt="Natacha" src="/static/images/avatar/1.jpg" />}
label="Deletable"
onDelete={handleDelete}
/>
</h1>
<h4>
<Chip
//style={{size:'1em'}}
avatar={<Avatar alt="Natacha" src="/static/images/avatar/1.jpg" />}
label="Deletable"
onDelete={handleDelete}
/>
</h4>
</div>
)
}
Share
Improve this question
edited Oct 27, 2023 at 3:23
Rajiv
3,7722 gold badges17 silver badges32 bronze badges
asked Apr 3, 2021 at 8:56
Ali HushamAli Husham
93615 silver badges37 bronze badges
1
- See Chip API - CSS Material UI – Shivam Jha Commented Apr 3, 2021 at 10:04
2 Answers
Reset to default 3Due to makeStyles()
deprecation in the v5, it is remended to use a newer styling solution sx
or styled
.
Example with custom ponent and sx
:
import React from 'react';
import { ChipProps } from '@mui/material/Chip/Chip';
import { Chip } from '@mui/material';
type ChipAtomProps = ChipProps & {
scale?: number;
};
const ChipAtom: React.FC<ChipAtomProps> = ({
id,
className,
variant,
scale = 1,
avatar,
label,
}) => {
const scaledChipSx = {
height: `${scale * 32}px`,
borderRadius: `${scale * 16}px`,
'& .MuiChip-label': {
paddingRight: `${scale * 12}px`,
paddingLeft: `${scale * 12}px`,
fontSize: `${scale * 0.8125}rem`,
},
'& .MuiChip-avatar': {
width: `${scale * 24}px`,
height: `${scale * 24}px`,
fontSize: `${scale * 0.75}rem`,
},
};
return (
<Chip
id={id}
className={className}
variant={variant}
avatar={avatar}
label={label}
sx={scaledChipSx}
/>
);
};
export default ChipAtom;
This answer is valid for MUI v4
currently,
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742288677a4415787.htmlChip
doesn't support the prop for size (Hope they support it in the future
评论列表(0条)