Skip to content

CSS Modules + ReactCSSTransitionGroup in React 0.14 #84

Closed

Description

@ArnoBuschmann

Hello, following this page: https://gist.github.com/markdalgleish/fab73ca3ace3b9ab6077

The animation/transition in my code is still not taking place and the element renders right away without transitioning.

What am I missing or doing wrong?

The code:

import styles from '../assets/css/app.css';
import CSSTransitionGroup from 'react-addons-css-transition-group';

class App extends React.Component {
  renderMyTransition() {
    switch (this.props.currentPage) {
    case 'testpage':
      return <div key="one"><p>Test-Page-One</p></div>
    case 'secondtestpage':
      return <div key="two"><p>Test-Page-Two</p></div>
    default:
      return <div key="default"><p>Not found.</p></div>
    }
  }
  render() {
    return (
      <div className={styles.content}>
        <CSSTransitionGroup transitionAppear={true} transitionAppearTimeout={500} transitionEnterTimeout={500} transitionLeaveTimeout={300} transitionName={styles}>
          {this.renderMyTransition()}
        </CSSTransitionGroup>
      </div>
    );
  }
}

export default App;

The CSS:

.enter {
  opacity: 0.01;
  transition: opacity 1.5s ease-in;
}
.enter.enter-active {
  opacity: 1;
}
.leave {
  opacity: 1;
  transition: opacity 1.5s ease-in;
}
.leave.leave-active {
  opacity: 0.01;
}
.appear {
  opacity: 0.01;
}
.appear.appear-active {
  opacity: 1;
  transition: opacity 1.5s ease-in;
}

Activity

  1. markdalgleish commented on Nov 12, 2015

    @markdalgleish
    Member

    The CSS should use camel case instead:

    .enter {
      opacity: 0.01;
      transition: opacity 1.5s ease-in;
    }
    .enter.enterActive {
      opacity: 1;
    }
    .leave {
      opacity: 1;
      transition: opacity 1.5s ease-in;
    }
    .leave.leaveActive {
      opacity: 0.01;
    }
    .appear {
      opacity: 0.01;
    }
    .appear.appearActive {
      opacity: 1;
      transition: opacity 1.5s ease-in;
    }
  2. ArnoBuschmann commented on Nov 12, 2015

    @ArnoBuschmann
    Author

    Thanks Mark for your quick response. Good point. But hm, still not working, anything else I can try?

  3. ArnoBuschmann commented on Nov 12, 2015

    @ArnoBuschmann
    Author

    Maybe a problem with the webpack configuration?
    loader: 'style!css?modules&localIdentName=[name]---[local]---[hash:base64:5]!postcss'

  4. ArnoBuschmann commented on Nov 12, 2015

    @ArnoBuschmann
    Author

    Ok, seems the effect was just not obvious now, changing the timeouts from

    <CSSTransitionGroup transitionAppear={true} transitionAppearTimeout={500} transitionEnterTimeout={500} transitionLeaveTimeout={300} transitionName={styles}>

    to

    <CSSTransitionGroup transitionAppear={true} transitionAppearTimeout={2000} transitionEnterTimeout={2000} transitionLeaveTimeout={2000} transitionName={styles}>

    finally did the trick.

  5. shogochiai commented on Feb 25, 2016

    @shogochiai

    I was also same situation and I solved by :global scope ()

    // window.jsx
      render (){
        return (
          <div>
            <CSSTransitionGroup
              transitionName={"window-blur"}
              transitionAppear={true}
              transitionAppearTimeout={1000}
              transitionEnterTimeout={1000}
              transitionLeaveTimeout={1000}
              >
              <Header />
              <Body />
              <Footer />
            </CSSTransitionGroup>
          </div>
        );
      }
    
    
    //window.scss
    
    :global {
      .window-blur-enter {
        opacity: 0.01;
      }
    
      .window-blur-enter.window-blur-enter-active {
        opacity: 1;
        transition: opacity 500ms ease-in;
      }
    
      .window-blur-leave {
        opacity: 1;
      }
    
      .window-blur-leave.window-blur-leave-active {
        opacity: 0.01;
        transition: opacity 300ms ease-in;
      }
      .window-blur-appear {
        opacity: 0.01;
      }
    
      .window-blur-appear.window-blur-appear-active {
        opacity: 1;
        transition: opacity .5s ease-in;
      }
    }
    
    // webpack loaders
      {
        test: /\.css$/,
        loader: [
          'style-loader',
          'css-loader?sourceMap&modules&localIdentName=[name]__[local]__[hash:base64:5]',
          'postcss-loader'
        ].join('!')
      },
    
      {
        test: /\.scss$/,
        loader: [
          'style-loader',
          'css-loader?sourceMap&modules&localIdentName=[name]__[local]__[hash:base64:5]',
          'postcss-loader',
          'sass-loader?' + sassParams.join('&')
        ].join('!')
      },
    
    

    This worked yet it is global scope so that transitionName should not be too simple.

  6. Andreyco commented on May 2, 2016

    @Andreyco

    Has anyone managed to do this without having transition name specified :global namespace?

  7. OttlikG commented on May 7, 2016

    @OttlikG

    As you saw in the first example, you can do with this technique:
    https://facebook.github.io/react/docs/animation.html#custom-classes

    Just apply the imported object to the transitionName property:

    import styles from '../assets/css/app.css';
    <CSSTransitionGroup transitionName={styles} transitionEnterTimeout={500} transitionLeaveTimeout={300}>
       { content }
    </CSSTransitionGroup>
  8. elrumordelaluz commented on Jun 17, 2016

    @elrumordelaluz

    @Andreyco I solved specifying the transitionName Object prop. Something like:

    import style from './Styles.css';
    // ...
    <ReactCSSTransitionGroup
      transitionName={{
        enter: style.enter,
        enterActive: style.enterActive,
        leave: style.leave,
        leaveActive: style.leaveActive,
      }}
      transitionEnterTimeout={250}
      transitionLeaveTimeout={250}>
      { /*... */ }
    </ReactCSSTransitionGroup>

    and in Styles.css:

    .enter {
      transition: all 250ms;
      /* ... */
    }
    .enterActive { /* ... */ }
    .leave { /* ... */ }
    .leaveActive {  /* ... */  }
  9. SimoNonnis commented on Aug 16, 2016

    @SimoNonnis

    Does anyone managed to make react-addons-css-transition-group works with css-modules?
    Tried all the solution above with no luck.

  10. amilajack commented on Aug 27, 2016

    @amilajack

    @markdalgleish Please update your answer, as camel casing has been deprecated in favor of kebab-case

    This is the example taken from the docs:

    .example-enter {
      opacity: 0.01;
    }
    
    .example-enter.example-enter-active {
      opacity: 1;
      transition: opacity 500ms ease-in;
    }
    
    .example-leave {
      opacity: 1;
    }
    
    .example-leave.example-leave-active {
      opacity: 0.01;
      transition: opacity 300ms ease-in;
    }
  11. amilajack commented on Aug 27, 2016

    @amilajack

    @SimoNonnis check if your css animation duration lasts as long as your transitionLeaveTimeout and transitionEnterTimeout durations.

  12. iam-peekay commented on Sep 1, 2016

    @iam-peekay

    @elrumordelaluz thank you! worked like a charm.

  13. dace commented on Oct 20, 2016

    @dace

    Using version 15.3.2, when I would pass in an object, like so:

    transitionName={{
        enter: style.enter,
        enterActive: style.enterActive,
        leave: style.leave,
        leaveActive: style.leaveActive,
      }}
    

    I found that I was getting an error that read:

    Uncaught Error: CSSCore.addClass takes only a single class name. "[object Object]-appear" contains multiple classes.

    So I had to pass my css into my ReactCSSTransitionGroupName as such - as just the imported name that points to my css file. This worked for me:

    Login.js Component

    import React from 'react';
    import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
    import PageTitle from '../PageTitle';
    import LoginForm from '../LoginForm';
    import transitions from '../../styles/css/transitions.css';
    
    const Login = () =>
      <div>
        <ReactCSSTransitionGroup
          transitionName={transitions}
          transitionAppearTimeout={500}
          transitionAppear={true}
          transitionEnterTimeout={500}
          transitionLeaveTimeout={300}
        >
          <PageTitle title="Login" />
          <LoginForm />
        </ReactCSSTransitionGroup>
      </div>;
    
    export default Login;
    
    

    transitions.css

    .enter {
      opacity: 0.01;
    
      &.enterActive {
        transition: opacity 0.5s ease-in;
        opacity: 1;
      }
    }
    
    .leave {
      opacity: 1;
    
      &.leaveActive {
        transition: opacity 0.3s ease-in;
        opacity: 0.01;
      }
    }
    
    .appear {
      opacity: 0.01;
    
      &.appearActive {
        transition: opacity 0.5s ease-in;
        opacity: 1;
      }
    }
    
    

    The classes only seemed to work if they were camel-cased (could not get working in kebab-case). Hope this helps.

  14. reznord commented on Dec 1, 2016

    @reznord

    @elrumordelaluz thanks a ton !! works perfect

  15. reznord commented on Dec 20, 2016

    @reznord

    @elrumordelaluz have you implemented page transition? If so, can you show a gist how did you do that?

  16. elrumordelaluz commented on Dec 20, 2016

    @elrumordelaluz

    @reznord didn't implemented, could not work the same way?

  17. reznord commented on Dec 20, 2016

    @reznord

    @elrumordelaluz Regarding the page transitions, I did a bit of homework and implemented it.

    import styles from './styles/animate.css;
    
    export default class Test extends React.Component {
      render() {
        <ReactCSSTransitionGroup 
             transitionAppear={true} 
             transitionName={styles} 
             transitionEnterTimeout={300} 
             transitionLeaveTimeout={300} 
             transitionAppearTimeout={300}
         >
      }
    }
    

    and this is what I did in animate.css:

    .enter {
      opacity: 0.01;
    
      &.enterActive {
        transition: opacity 0.3s ease-in;
        opacity: 1;
      }
    }
    
    .leave {
      opacity: 1;
    
      &.leaveActive {
        transition: opacity 0.3s ease-out;
        opacity: 0.01;
      }
    }
    
    .appear {
      opacity: 0.01;
    
      &.appearActive {
        transition: opacity 0.3s ease-in;
        opacity: 1;
      }
    }
    
    

    but the leave animation is not working for some reason !!

  18. alisonailea commented on Apr 22, 2017

    @alisonailea

    @hidace

    I found that I was getting an error that read: Uncaught Error: CSSCore.addClass takes only a single class name. "[object Object]-appear" contains multiple classes.
    I noticed you didn't include the renaming of the "appear" class.

    have you tried something like this?

    transitionName={{
        enter: style.enter,
        enterActive: style.enterActive,
        leave: style.leave,
        leaveActive: style.leaveActive,
        appear: style.appear,
        appearActive: style.appearActive,
      }}
    
  19. daise commented on Apr 29, 2017

    @daise

    i got the same issue @reznord mentioned, any idea to make leave transition work?
    Please let me know.

  20. willowcheng commented on Jun 8, 2017

    @willowcheng

    To get rid of camel case CSS selectors, I find it also works by

    transitionName={{
        enter: style['enter'],
        enterActive: style['enter-active'],
        leave: style['leave'],
        leaveActive: style['leave-active'],
        appear: style['appear'],
        appearActive: style['appear-active'],
      }}
  21. loukitzanna commented on Jan 17, 2018

    @loukitzanna

    If you're having the same issue as @reznord and @daise it was exit not leave for me

    .enter {
      opacity: 0.01;
    
      &.enterActive {
        transition: opacity 0.3s ease-in;
        opacity: 1;
      }
    }
    
    .exit {
      opacity: 1;
    
      &.exitActive {
        transition: opacity 0.3s ease-out;
        opacity: 0.01;
      }
    }
  22. thanhtutzaw commented on Feb 17, 2023

    @thanhtutzaw

    Any Update ? I was using CSSTransitions with Nextjs and CSS module . Cannot set enter style in module css . BUt it works in global.css , How can I do in css module .

    Updat: Now I can set class like that -> Drawer_drawer___Ga5i-enter-done

    But I can't set css in Drawer.module.css . How can I style ?
    I have tried these and did not work.
    .drawer-enterDone
    and
    .drawer-enter-done

  23. thanhtutzaw commented on Feb 18, 2023

    @thanhtutzaw
    transitionEnterTimeout

    This doesn't work in 2023 . Doc and Example are not enough .

    Now enterDone (camel case only work for me )

  24. BloqX3 commented on Sep 29, 2024

    @BloqX3

    i'm using "react-transition-group": "^4.4.5", the following configuration worked for me:

    <CSSTransition ...
                classNames={{
                    enterActive: styles.transitionEnterActive,
                    enter: styles.transitionEnter,
                    exitActive: styles.transitionExitActive,
                    exit: styles.transitionExit
                  }}
    >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions