Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[styles] Arrow functions (with or without props) do not work within makeStyles for @keyframes #21011

Closed
sagarsys opened this issue May 13, 2020 · 3 comments
Labels
bug 🐛 Something doesn't work package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5.
Milestone

Comments

@sagarsys
Copy link

I want to dynamically generate an @keyframes animation using the makeStyles helper.
I want to access props within makeStyles to generate this css keyframes.
However, it does not seem to work.

Current Behavior 😯

Passing an arrow function (with or without props) to @keyframes does not generate the animation.

const useStyles = makeStyles({
  ...,
  [`@keyframes failsWithProps`]: props => ({
    from: { opacity: props.opacity },
    to: { opacity: 0 }
  }),
})

export function MyComponent() {
  const classes = useStyles({ opacity: 0.9 })
}

Expected Behavior 🤔

Being able to use props to generate @keyframes rules
There is a workaround for dynamically generating@keyframes with the es6 spread operator, but it does not work with props.

Steps to Reproduce 🕹

Live example (CodeSandox)

Steps:

  1. Use an arrow function to generate @keyframes animation

Context 🔦

I want to create dynamic animation rules, based on a number of items that is being rendered. So I want to be able to use props within the component to define the resulting animation.

Right now, within makeStyles, I have to use:

    [`@keyframes change-text`]: {
        ...generateTextLineChangeKeyFrameAnimation(4),
    },

but I want to be able to use:

    [`@keyframes change-text`]: props => generateTextLineChangeKeyFrameAnimation(props.numLines),

and within MyComponent:

export function MyComponent() {
  const items = [ 1, 2, 3, 4 ] // dynamic values
  const classes = useStyles({ numLines: items.length })
  return (...)
}

Your Environment 🌎

Tech Version
Material-UI v4.9.13
React v16.13.1
Browser Chrome v81.0.4044.138
@oliviertassinari oliviertassinari added duplicate This issue or pull request already exists package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5. and removed duplicate This issue or pull request already exists labels May 13, 2020
@oliviertassinari oliviertassinari changed the title Arrow functions (with or without props) do not work within makeStyles for @keyframes [styles] Arrow functions (with or without props) do not work within makeStyles for @keyframes Nov 11, 2020
@oliviertassinari oliviertassinari added the bug 🐛 Something doesn't work label Nov 11, 2020
@oliviertassinari
Copy link
Member

oliviertassinari commented Nov 11, 2020

An update, this issue is being resolved in v5 thanks to #22342 and the new @material-ui/styled-engine package: https://codesandbox.io/s/wizardly-meadow-r0h7w?file=/src/App.js.

const getAnimation = (props) => keyframes`
  0% {
    opacity: props.opacity;
  }
  100% {
    opacity: 0;
  }
`;

const H2 = styled("h2")((props) => ({
  animation: `${getAnimation(props)} 1s infinite`
}));

@oliviertassinari
Copy link
Member

An update, we have now made enough progress with the new @material-ui/styled-engine package in v5 to move toward a progressive removal of the @material-ui/styles package (based on JSS). The current plan:

  • In v5.0.beta.0, this package will come standalone, in complete isolation from the rest.
  • In v5.0.0, this package will be soft deprecated, not promoted in the docs, nor very actively supported.
  • During v5, work on making the migration easier to the new style API (sx prop + styled() API). We might for instance, invest in the documentation for using react-jss that has more or less the same API. We could also invest in an adapter to restore the previous API but with emotion, not JSS.
  • In v6.0.0, this package will be discontinued (withStyles/makeStyles API).

This was made possible by the awesome work of @mnajdova.

@oliviertassinari
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work package: styles Specific to @mui/styles. Legacy package, @material-ui/styled-engine is taking over in v5.
Projects
None yet
Development

No branches or pull requests

2 participants