在 React MUI 中创建自定义按钮并设置样式

问题描述 投票:0回答:1

我正在 React Material-UI 中处理图像和按钮。 我们如何创建这种类型的按钮以及创建这些类型的按钮时的正确方法是什么? 我附上了图片供参考,预期的输出应该是这样的。 (https://i.stack.imgur.com/IpeJu.png)

reactjs button material-ui
1个回答
0
投票

您可以查看此 MUI 文档以进行按钮自定义:

MUI按钮定制

import * as React from 'react';
import { styled } from '@mui/material/styles';
import Button, { ButtonProps } from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import { purple } from '@mui/material/colors';

const BootstrapButton = styled(Button)({
  boxShadow: 'none',
  textTransform: 'none',
  fontSize: 16,
  padding: '6px 12px',
  border: '1px solid',
  lineHeight: 1.5,
  backgroundColor: '#0063cc',
  borderColor: '#0063cc',
  fontFamily: [
    '-apple-system',
    'BlinkMacSystemFont',
    '"Segoe UI"',
    'Roboto',
    '"Helvetica Neue"',
    'Arial',
    'sans-serif',
    '"Apple Color Emoji"',
    '"Segoe UI Emoji"',
    '"Segoe UI Symbol"',
  ].join(','),
  '&:hover': {
    backgroundColor: '#0069d9',
    borderColor: '#0062cc',
    boxShadow: 'none',
  },
  '&:active': {
    boxShadow: 'none',
    backgroundColor: '#0062cc',
    borderColor: '#005cbf',
  }, 
  '&:focus': {
    boxShadow: '0 0 0 0.2rem rgba(0,123,255,.5)',
  },
});

const ColorButton = styled(Button)<ButtonProps>(({ theme }) => ({
  color: theme.palette.getContrastText(purple[500]),
  backgroundColor: purple[500],
  '&:hover': {
    backgroundColor: purple[700],
  },
}));

export default function CustomizedButtons() {
  return (
    <Stack spacing={2} direction="row">
       <ColorButton variant="contained">Custom CSS</ColorButton>
       <BootstrapButton variant="contained" disableRipple>
        Bootstrap
      </BootstrapButton>
   </Stack>
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.