我曾在WordPress管理区域遇到Checkbox
和Switch
相同的问题。
结果发现,存在一个全局CSS规则:
input[type="checkbox"] {
height: 1rem;
width: 1rem;
}
点击元素左上角也可以。不过,我的解决方案是在应用的根部重置了一些样式。
编辑:算了,我把整个应用程序放到了 Shadow DOM 中。这里有几个需要注意的地方:
- 必须为 Shadow DOM 中的 Material UI 样式元素提供自定义插入点。通常情况下,不能让任何东西放在你的 Shadow DOM 之外。
- 必须在 Shadow DOM 和 Light DOM 中都加载或链接字体。
- 使用
ScopedCssBaseline
来代替全局重置。
- 对话框必须指定它们的
container
属性。
以下是我如何使用 Material-UI 设置的内容:
import { create } from 'jss';
import { jssPreset } from '@material-ui/core/styles';
const shadowHostId = 'my-app-root-id'
export const appRoot = document.createElement('div')
appRoot.setAttribute('id', 'app-root')
const styleInsertionPoint = document.createComment('jss-insertion-point')
export const jss = create({
...jssPreset(),
insertionPoint: styleInsertionPoint,
})
const robotoFontLink = document.createElement('link')
robotoFontLink.setAttribute('rel', 'stylesheet')
robotoFontLink.setAttribute('href', 'https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap')
const shadowHost = document.getElementById(shadowHostId)
shadowHost.attachShadow({ mode: 'open' })
const shadowRoot = shadowHost.shadowRoot
shadowRoot.appendChild(robotoFontLink)
shadowRoot.appendChild(styleInsertionPoint)
shadowRoot.appendChild(appRoot)
import React from 'react';
import ReactDOM from 'react-dom';
import ScopedCssBaseline from '@material-ui/core/ScopedCssBaseline';
import { StylesProvider } from '@material-ui/core/styles';
import App from './App';
import { jss, appRoot } from './configure-shadow-dom';
ReactDOM.render(
<React.StrictMode>
<StylesProvider jss={jss}>
<ScopedCssBaseline>
<App />
</ScopedCssBaseline>
</StylesProvider>
</React.StrictMode>,
appRoot
);
Switch
父组件上有任何点击事件处理程序吗? - Hai Phamconsole.log('event:', e, c)
? - Hai Phamclick
事件阻止传播或防止默认行为。事实上,我用普通的<input type="checkbox">元素替换了Switches,效果很好。material-ui的Switch出了点问题。 - trusktr