简体   繁体   English

手动设置 MaterialUI Switch 组件的大小

[英]Manually set size of MaterialUI Switch component

Im trying to change the size of a Material-UI Switch because the default size is not large enough.我试图更改 Material-UI 开关的大小,因为默认大小不够大。 Ive managed to increase the size, but the behaviour is now not fantastic.我设法增加了大小,但现在的行为并不好。
It looks good when it is in its default state:它在默认 state 中时看起来不错:
在此处输入图像描述

However, when I change its state it loses the style:然而,当我改变它的 state 它失去了风格:
在此处输入图像描述

I cant work out how to change the styles so the Thumb goes all the way to the right of the Track.我不知道如何更改 styles,所以拇指一直到轨道的右侧。

 <Switch
   onChange={setPrivateSwap}
   classes={{
     root: classes.root,
     switchBase: classes.switchBase,
     checked: classes.checked,
     track: classes.track,
     thumb: classes.thumb,
   }}
 />

const useStyles = makeStyles({
  root: {
    width: '90px',
    height: '50px',
    padding: 0,
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  },
  track: {
    width: '40px',
    height: '20px',
    borderRadius: '10px',
  },
  switchBase: {
    '&$checked': {
      color: '#007D81',
    },
    '&$checked + $track': {
      backgroundColor: 'rgba(0,125,129,0.3)',
    },
  },
  checked: {},
  thumb: {
    width: '32px',
    height: '32px',
  },
});

Here I have adjusted the switch use translateX(value) property.这里我调整了 switch 使用 translateX(value) 属性。 Here is the code:这是代码:

import React from "react";
import { Switch } from "@material-ui/core";
import { makeStyles } from "@material-ui/styles";

    const useStyles = makeStyles({
      root: {
        width: "90px",
        height: "50px",
        padding: 0,
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
      },
      track: {
        width: "40px",
        height: "20px",
        borderRadius: "10px",
      },
      switchBase: {
        "&$checked": {
          color: "#007D81",
          transform: "translateX(40px)",
        },
        "& + $track": {
          backgroundColor: "rgba(0,125,129,0.3)",
        },
      },
      checked: {},
      thumb: {
        width: "32px",
        height: "32px",
        transform: "translateX(0px)",
      },
    });
    
    export default function () {
      const classes = useStyles();
    
      const setPrivateSwap = function () {
        console.log("I a ali");
      };
      return (
        <Switch
          onChange={setPrivateSwap}
          classes={{
            root: classes.root,
            switchBase: classes.switchBase,
            checked: classes.checked,
            track: classes.track,
            thumb: classes.thumb,
          }}
        />
      );
    }

You can make further adjustments and refinements using translateX in thumb and switchBase class.您可以使用 thumb 和 switchBase class 中的 translateX 进行进一步的调整和改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM