在线旅游网站建设方案,手机网站开发算什么费用,seo运营经理,海报在线设计平台Material UI 5 学习01-按钮组件 一、安装Material UI二、 组件1、Button组件1、基础按钮2、variant属性3、禁用按钮4、可跳转的按钮5、disableElevation属性6、按钮的点击事件onClick 2、Button按钮的颜色和尺寸1、Button按钮的颜色2、按钮自定义颜色3、Button按钮的尺寸 3、图… Material UI 5 学习01-按钮组件 一、安装Material UI二、 组件1、Button组件1、基础按钮2、variant属性3、禁用按钮4、可跳转的按钮5、disableElevation属性6、按钮的点击事件onClick 2、Button按钮的颜色和尺寸1、Button按钮的颜色2、按钮自定义颜色3、Button按钮的尺寸 3、图标按钮1、带有图标的按钮 一、安装Material UI
首先我们创建React工程。然后安装样式和字体。下面使用yarn安装
//Material UI
yarn add mui/material emotion/react emotion/styled
//styled-components
yarn add mui/material mui/styled-engine-sc styled-components
//安装Roboto字体
yarn add fontsource/roboto
//安装MUI图标
yarn add fontsource/roboto//在public index.html引入google字体样式
link relpreconnect hrefhttps://fonts.googleapis.com /
link relpreconnect hrefhttps://fonts.gstatic.com crossorigin /
link relstylesheet hrefhttps://fonts.googleapis.com/css2?familyRoboto:wght300;400;500;700displayswap/
//在public index.html引入google MUI字体样式
link relstylesheet hrefhttps://fonts.googleapis.com/icon?familyMaterialIcons/
二、 组件
我们先学习MUI的组件
1、Button组件
1、基础按钮
最基本的button按钮定义
import {Button} from mui/material;
function App() {return (divButton这是一个按钮/Button/div);
}
export default App; 可以看出最基本的按钮没有边框字体默认是蓝色的其鼠标点击上去有背景颜色是淡蓝色。其他的就没有什么了。
2、variant属性
序号属性值含义1text文本按钮这个是按钮的默认属性。文本按钮 通常用于不太明显的动作包括那些位于对话框、卡片中的动作。 在卡片中文本按钮有助于保持卡片内容的重点。2contained包含按钮。包含按钮 是高度强调的以其使用的抬高和填充来区分。 它们包含对你的应用来说是主要的操作。3outlined轮廓按钮。轮廓按钮也是包含按钮的一种较低强调度的选择 或者更强调文本按钮的替代品。
代码演示
Button varianttext这是一个文本按钮/Button
Button variantcontained这是包含按钮/Button
Button variantoutlined这是轮廓按钮/Button3、禁用按钮
加上disabled属性可以禁用按钮。默认是true。false就不是禁用了。
Button varianttext disabled禁用文本按钮/Button4、可跳转的按钮
加尚href属性可以进行跳转
Button varianttext href#禁用文本按钮/Buttonimport {Button} from mui/material;
function App() {return (divButton varianttext这是一个文本按钮/ButtonButton varianttext disabled禁用文本按钮/ButtonButton varianttext href#禁用文本按钮/Button/div);
}
export default App;5、disableElevation属性
disableElevation是禁用阴影的意思。加上此属性那么点击按钮没有阴影。 默认的按钮点击之后都存在阴影左边的按钮是默认的按钮右边的加了disableElevation属性因此右边的点击没有阴影
6、按钮的点击事件onClick
import {Button} from mui/material;function App() {const showData () {alert(按钮被点击了);}return (divButton onClick{showData} variantcontained按钮/Button/div);
}export default App;上面的代码也可以简写
import {Button} from mui/material;
function App() {return (divButton onClick{(){alert(按钮被点击)}} variantcontained按钮/Button/div);
}
export default App;2、Button按钮的颜色和尺寸
1、Button按钮的颜色
按钮使用color属性即可设置值color“primary” 默认的按钮属性值有7个
序号属性值含义1color“inherit”按钮文字的颜色取决于父组件的颜色2color“primary”强调重要行为的按钮1color“secondary”次要行为的按钮1color“success”显示操作成功的按钮1color“error”显示操作错误的按钮1color“info”陈述按钮1color“warning”警告按钮 |
2、按钮自定义颜色
使用Theme自定义颜色 创建Theme.jsx文件编写自定义的样式
import {createTheme} from mui/material;
export const theme createTheme({palette: {primary: {main: #000000,},secondary: {main: #F5EBF7,light: #F5EBFF,contrastText: #47008F,},},
});在index中使用ThemeProvider引入theme
import React from react;
import ReactDOM from react-dom/client;
import App from ./App;
import reportWebVitals from ./reportWebVitals;
import {ThemeProvider} from mui/material;
import {theme} from ./Themeconst root ReactDOM.createRoot(document.getElementById(root));
root.render(React.StrictModeThemeProvider theme{theme}App //ThemeProvider/React.StrictMode
);reportWebVitals();最后在按钮中应用
import {Button, Stack} from mui/material;function App() {return (div Stack spacing{3}Button variantcontained colorprimary主要的按钮/Button/Stack/div);
}
export default App;由于自定义的primary为黑色所以最终按钮呈现黑色
疑惑点Button好像颜色属性只能从那7个属性值去选择。虽然可以自定义7个属性值的颜色。但是无法使用main、light、dark更为详细的定义。Box就可以定义具体的色调
export const theme createTheme({palette: {primary: {main: #000000,light: #F5EBF7,contrastText: #80ff80,/*文本颜色*/},secondary: {main: #F5EBF7,light: #F5EBFF,contrastText: #47008F,},},
});也就是说Button组件默认就是main。不知道如何使用light。
3、Button按钮的尺寸
使用size属性申明
序号属性含义1size“small”小按钮2size“medium”中按钮3size“large”大按钮
import {Button, Stack} from mui/material;function App() {return (div Stack spacing{3}Button variantcontained sizesmall按钮1/ButtonButton variantcontained sizemedium按钮2/ButtonButton variantcontained sizelarge按钮3/Button/Stack/div);
}
export default App;3、图标按钮
使用图标可以直接在Material Icons中选择前提是安装icon库
yarn add mui/icons-material mui/material emotion/styled emotion/react
1、带有图标的按钮
序号属性含义1startIcon{图标组件/}在按钮文字前面的图标2endIcon{图标组件 /}在按钮文字之后的图标
import {Button, Stack} from mui/material;
import DeleteIcon from mui/icons-material/Delete;
import SendIcon from mui/icons-material/Send;function App() {return (div Stack spacing{3}Button variantoutlined startIcon{DeleteIcon /}Delete/ButtonButton variantcontained endIcon{SendIcon /}Send/Button/Stack/div);
}
export default App;上面的代码引入了两个图标组件DeleteIcon 、SendIcon 。图标可以自定搜索 文章转载自: http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.nyqnk.cn.gov.cn.nyqnk.cn http://www.morning.qbwbs.cn.gov.cn.qbwbs.cn http://www.morning.hjssh.cn.gov.cn.hjssh.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.pdghl.cn.gov.cn.pdghl.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.ynryz.cn.gov.cn.ynryz.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.wschl.cn.gov.cn.wschl.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.jwbfj.cn.gov.cn.jwbfj.cn http://www.morning.hmnhp.cn.gov.cn.hmnhp.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.gnjkn.cn.gov.cn.gnjkn.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.blxlf.cn.gov.cn.blxlf.cn http://www.morning.rdnpg.cn.gov.cn.rdnpg.cn http://www.morning.mbbgk.com.gov.cn.mbbgk.com http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.clpkp.cn.gov.cn.clpkp.cn http://www.morning.ngcw.cn.gov.cn.ngcw.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.buyid.com.cn.gov.cn.buyid.com.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.qdcpn.cn.gov.cn.qdcpn.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.lysrt.cn.gov.cn.lysrt.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.qlsyf.cn.gov.cn.qlsyf.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.ktcrr.cn.gov.cn.ktcrr.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.skql.cn.gov.cn.skql.cn http://www.morning.thzwj.cn.gov.cn.thzwj.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.qxmpp.cn.gov.cn.qxmpp.cn