gpt4 book ai didi

reactjs - Material-UI 响应式卡片

转载 作者:行者123 更新时间:2023-12-02 19:37:45 24 4
gpt4 key购买 nike

我正在测试 Material-UI。我已经使用 Bootstrap 很长时间了,但有兴趣将一些 React 项目适应 Material-UI。我一直试图弄清楚如何在 Material-UI 中创建响应式卡片。过去我非常依赖 Bootstraps 响应式容器,所以我不明白为什么我的卡片随着页面扩展而不会随着窗口压缩而缩小......如果我们打算为以下内容编写自定义 css我对此很满意,只是需要指出正确的方向。

问题:

  1. Material-UI 中的响应式卡片是一开始就存在的吗?

  2. 或者我们是要编写 css 来使卡片响应式吗?

  3. 如果是这样,我可以在哪里学习这样做? (过去非常依赖 Bootstrap)

预先感谢您的帮助!

...
useStyles = () => makeStyles(theme => ({
root: {
flexGrow: 1,
},
paper: {
padding: theme.spacing.unit,
textAlign: "center",
color: theme.palette.text.secondary,
marginBottom: theme.spacing.unit
},
}));

render() {
const {baseData} = this.state;
const {hfcMetrics} = this.state;
const {stateMember} = this.state;
const {stateName} = this.state;
const {states} = this.state;
const {lineVizData} = this.state;
const classes = this.useStyles();

return (this.state.doneLoading === false ? (
<div className={classes.root}>
<Grid container>
<Grid item xs={12}>
<ReactLoading type={"spinningBubbles"} color={"black"} height={'10%'}
width={'10%'} id='spinner'/>
</Grid>
</Grid>
</div>
)
:
(stateMember === true ?
<div className={classes.root}>
<Grid container spacing={3}>
<Grid item xs={12}>
<Card>
<CardHeader
title="Options"
/>
<CardContent style={{ width: '100%', height: 300 }}>
<LineViz
data={lineVizData}
state={stateName}
source='compareTool'
states={states}
vizKey={this.state.vizKey}
/>
</CardContent>
<CardActions>
<Button size="small" color="primary">
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
</Grid>
<Grid item xs={6}>
<Card ref={this.elRef}>
<CardHeader
title="Comparison Analysis"
action={
<ButtonGroup variant="text" color="primary" aria-label="text primary button group">
<YearDropDown2
year={this.state.year}
handleChange={this.toggleYear}
/>
<IconButton color='default' component="span"
onClick={() => this.resetStateToggle()}><AutorenewRoundedIcon/></IconButton>
</ButtonGroup>
}
/>
<CardContent style={{padding: 0}}>
<DataCompareTable
data={this.state.compareData}
metric={this.state.metric}
stateName={this.state.stateName}
compareCount={this.state.compareCount}
handleChange={this.toggleStateName}
toggleOne={this.state.toggleOne}
toggleTwo={this.state.toggleTwo}
toggleThree={this.state.toggleThree}
/>
</CardContent>
</Card>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
</Grid>
</div>
: ''
)
)
}
}

export default CompareTool

最佳答案

感谢您的提问

回答您的问题:1)据我所知没有2)不,你不必编写很多CSS,但是可以在usestyles中编写一些CSS3)下面我详细解释了你必须在代码中编写的css。您同时使用内联样式和 useStyles,请尝试将所有样式放入 usestyle hook API 中并使其响应。希望这有助于让我知道是否需要更清楚地说明。谢谢

据我所知,您可以使用“useMediaQuery” Hook 来使您的设计具有响应能力。

这是来自Material UI Card组件页面的组件,我只添加了useTheme和useMediaQuery导入,并在classes.root下的useStyle内添加了一个中等断点这是一个有用的链接 关于“useMediaQuery” https://material-ui.com/components/use-media-query/#usemediaquery

import { useTheme } from "@material-ui/styles";
import useMediaQuery from "@material-ui/core/useMediaQuery";

const useStyles = makeStyles(theme => ({
root: {
maxWidth: 345,
[theme.breakpoints.down("md")] : {
maxWidth: 200
}
},
media: {
height: 140
}
}));
const Card = () => {
const classes = useStyles();
const theme = useTheme();

const matches = useMediaQuery(theme.breakpoints.up("sm"));
return (

<Card className={classes.root}>
<CardActionArea>
<CardMedia
className={classes.media}

title="Contemplative Reptile"
/>
<CardContent>
<Typography gutterBottom variant="h5" component="h2">
Lizard
</Typography>
<Typography variant="body2" color="textSecondary" component="p">
Lizards are a widespread group of squamate reptiles, with over 6,000
species, ranging across all continents except Antarctica
</Typography>
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary">
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
);
}

希望这有帮助

关于reactjs - Material-UI 响应式卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com