Material UI – Global checkbox focus styling (not locally)

社会主义新天地 提交于 2020-05-23 11:05:21

问题


Trying to alter styling of checkbox when it is focused at a global level using Material-UI (react).

At the moment only default and hover styling is working:

MuiCheckbox: {
  colorSecondary: {
    color: 'green',
    '&:hover': {
      color: 'blue !important',
      border: '1px solid rgba(0,0,0,0.54)',
      outline: '2px auto rgba(19,124,189,.6)'
    }
  },
}

What i'm trying to do, but is NOT working:

MuiCheckbox: {
  colorSecondary: {
    '&$focused': {
      color: 'blue',
      border: '1px solid rgba(0,0,0,0.54)',
      outline: '2px auto rgba(19,124,189,.6)'
    },
    '&$focusVisible': {
      color: 'blue',
      border: '1px solid rgba(0,0,0,0.54)',
      outline: '2px auto rgba(19,124,189,.6)'
    },
  }
}

回答1:


The example below shows controlling the color for a bunch of possible states of the Checkbox. Material-UI doesn't manage a focused state for Checkbox, only a focusVisible state, so moving the focus to the Checkbox via the keyboard will trigger that state. I've shown a focused styling in my example below by explicitly managing a focused state using the onFocus and onBlur properties of the Checkbox.

import React from "react";
import ReactDOM from "react-dom";

import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";
import Checkbox from "@material-ui/core/Checkbox";

const theme = createMuiTheme({
  overrides: {
    MuiCheckbox: {
      colorSecondary: {
        color: "green",
        "&:hover": {
          color: "blue"
        },
        "&$checked": {
          color: "purple",
          "&:hover": {
            color: "lightblue"
          },
          "&.Mui-focusVisible": {
            color: "red"
          }
        },
        "&.Mui-focusVisible": {
          color: "orange"
        },
        "&.focused:not(.Mui-focusVisible):not($checked)": {
          color: "pink"
        }
      }
    }
  }
});
function App() {
  const [focused, setFocused] = React.useState(false);
  return (
    <ThemeProvider theme={theme}>
      <div className="App">
        <Checkbox
          className={focused ? "focused" : ""}
          onFocus={() => setFocused(true)}
          onBlur={() => setFocused(false)}
        />
        <input value="somewhere to move focus" />
      </div>
    </ThemeProvider>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);




回答2:


its either a problem with specificity or you just need to add the classes you made to your component

const styles = {
  root: {
    '&$disabled': {
      color: 'white',
    },
  },
  disabled: {},
};

compiles to:

.root-x.disable-x {
  color: white;
}

⚠️ You need to apply the two generated class names (root & disabled) to the DOM to make it work.

<Button
  disabled
  classes={{
    root: classes.root, // class name, e.g. `root-x`
    disabled: classes.disabled, // class name, e.g. `disabled-x`
  }}
>

i get this from the documentaiton

https://material-ui.com/customization/components/#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet



来源:https://stackoverflow.com/questions/59374131/material-ui-global-checkbox-focus-styling-not-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!