简体   繁体   English

React 如何删除 Material-UI Select 的 animation

[英]React How to remove the animation of Material-UI Select

I'm using the React Material UI's Select Component.我正在使用 React Material UI 的 Select 组件。 I wish to remove or speeden the animation that comes when the menu is opening.我希望删除或加速菜单打开时出现的 animation。 I tried something like:我试过类似的东西:

 <Select
     ...
     TransitionComponent={({children}) => children}
 >
     <MenuItem value={...}>...</MenuItem>
     ...
 </Select>

But this is not working, please help但这不起作用,请帮助

add this to your stylesheet:将此添加到您的样式表中:

.MuiMenu-paper {
    transition-duration: 0s !important;
}

This basically overrides the transition duration of the select dropdown and sets it to 0 seconds.这基本上覆盖了 select 下拉列表的转换持续时间并将其设置为 0 秒。

You can also change the duration according to what you like (make it faster).您还可以根据自己的喜好更改持续时间(使其更快)。 The default animation duration is:默认的 animation 持续时间为:

transition-duration: 251ms, 167ms;

The reason why it doesn't work:它不起作用的原因:

MUI <Select /> API don't have props TransitionComponent , as well as some other components like <Tooltip /> do have MUI <Select /> API 没有道具TransitionComponent ,以及其他一些组件,如<Tooltip />确实有

Refer: API document of参考:API 文档

Related QA: React Material UI Tooltips Disable Animation相关 QA: React Material UI Tooltips Disable Animation


Solution解决方案

Override the transition style would be fine.覆盖过渡样式就可以了。

div.MuiPaper-root {
  transition: none !important;
}

在此处输入图像描述


Explanation解释

The HTML structure for options:选项的 HTML 结构:

Since it's been dynamically generated outside the main component, it's not suitable for us to directly set styles for them.由于是在主组件之外动态生成的,我们不适合直接给它们设置styles。

However, we can optionally override the styles by those classNames like MuiPaper-root , or some other ways like a given id.但是,我们可以选择通过诸如MuiPaper-root之类的类名或诸如给定 id 之类的其他方式覆盖 styles。

<div
  class="MuiPaper-root MuiMenu-paper MuiPopover-paper MuiPaper-elevation8 MuiPaper-rounded"
  tabindex="-1"
  style="opacity: 1; transform: none; min-width: 40px; transition: opacity 251ms cubic-bezier(0.4, 0, 0.2, 1) 0ms, transform 167ms cubic-bezier(0.4, 0, 0.2, 1) 0ms; top: 16px; left: 16px; transform-origin: -8px 7.7px;"
>
  <ul
    class="MuiList-root MuiMenu-list MuiList-padding"
    role="listbox"
    tabindex="-1"
  >
    ...
  </ul>
</div>;

在此处输入图像描述

To add to keikai's answer, you can also do this globally with a theme change:要添加到 keikai 的答案,您还可以通过主题更改全局执行此操作:

const theme = createMuiTheme({
  overrides: {
    MuiPaper: {
      root: {
        transition: 'none !important'
      },
    },
  }
});

For those that are using a corresponding Material UI InputLabel component with a mui Select component, I was able to pass in the following props to the InputLabel component to disable the animation and shrink altogether:对于使用相应 Material UI InputLabel组件和 mui Select组件的用户,我能够将以下道具传递给InputLabel组件以禁用 animation 并完全收缩:

<div>
   <FormControl>
     <InputLabel
       disableAnimation={true}
       shrink={false}
       ...
     >
       {`some label`}
     </InputLabel>
     <Select>
      {`...`}
     </Select>
   </FormControl>
 </div>

MUI Input Label API MUI 输入 Label API

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

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