React How to remove the animation of Material-UI Select

六月ゝ 毕业季﹏ 提交于 2020-05-16 06:36:10

问题


I'm using the React Material UI's Select Component. I wish to remove or speeden the animation that comes when the menu is opening. I tried something like:

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

But this is not working, please help


回答1:


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.

You can also change the duration according to what you like (make it faster). The default animation duration is:

transition-duration: 251ms, 167ms;



回答2:


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

Refer: API document of

  • MUI Tooltip

  • MUI Select

Related 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:

Since it's been dynamically generated outside the main component, it's not suitable for us to directly set styles for them.

However, we can optionally override the styles by those classNames like MuiPaper-root, or some other ways like a given id.

<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>;



来源:https://stackoverflow.com/questions/61226270/react-how-to-remove-the-animation-of-material-ui-select

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