Search code examples
reactjsmaterial-uiemotion

How to use theme.transitions.create with emotion?


I had this code in Material-UI v4 that worked perfectly:

<Button
  aria-expanded={optionsExpanded}
  onClick={() => dispatch(toggleOptionsExpanded())}
    endIcon={
      <ExpandMoreIcon
        className={clsx(classes.expand, {
          [classes.expandOpen]: optionsExpanded,
        })}
      />
  }
>
  Options
</Button>



const useStyles = makeStyles((theme) => ({
  expand: {
    transform: "rotate(0deg)",
      transition: theme.transitions.create("transform", {
      duration: theme.transitions.duration.shortest,
    }),
  },
  expandOpen: {
    transform: "rotate(180deg)",
  },
}));

The arrow will rotate like this:

enter image description here

However I can't seem to be able to replicate this using the v5 of Material-UI. I've tried using the sx prop, with conditional rendering and it turns but doesn't animate.


Solution

  • This should be what you're looking for:

    <Button
      aria-expanded={optionsExpanded}
      onClick={() => dispatch(toggleOptionsExpanded())}
      endIcon={
        <ExpandMoreIcon
          sx={[
            {
              transform: 'rotate(0deg)',
              transition: (theme) => theme.transitions.create('all', {
                duration: theme.transitions.duration.shortest,
              })
            },
            optionsExpanded && {
              transform: 'rotate(180deg)'
            }
          ]}
        />
      }
    >
      Options
    </Button>