Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ReactCSSTransitionGroup with styled-component

I'm using styled-components instead of tradition way of css. But I don't know how it can work together with ReactCSSTransitionGroup.

Basically, ReactCSSTransitionGroup looks for certain classnames in css resource, then apply to a component throughout its lifecycle. However, with styled-components, there are not any class names, styles are applied to components directly.

I know I can choose not to use ReactCSSTransitionGroup because the two technique doesn't look compatible. But when I use only styled-components, seems I can't render any animation when a component is unmounted - it's pure css, can't access component's lifecycle.

Any help or recommendation is appreciated.

like image 417
Stanley Luo Avatar asked Mar 08 '17 00:03

Stanley Luo


People also ask

Can I pass props to styled-component?

To pass props to React components created with styled-components, we can interpolate functions into the string that creates the component. We create the ArrowStyled component with the styled. div tag. Then string that we pass into the tag has the rotate value set from a prop.

Can you use sass with styled-components?

While you can reference global CSS variables within a styled-component, you can't reference sass variables nor global CSS variables containing strings.

How do I use SCSS styled-component?

In SCSS, you can use variables and formulas for expressing colors, font sizes, widths and spacing variants. However, when you switch from SCSS to styled components, the syntax isn't valid anymore. Styled component files are CSS-in-JS, so they are actually JavaScript files.

Can styled-components be a dev dependency?

Marking styled-components as external in your package dependencies. Moving styled-components to devDependencies will guarantee that it wouldn't be installed along with your library (npm install or yarn add will ignore devDependencies when a library is installed).


1 Answers

I didn't want to use injectGlobal as suggested in another answer because I needed to make the transitions different per component.

It turns out to be pretty easy - just nest the transition classes in the styling for the component:

import React from "react"; import CSSTransitionGroup from 'react-transition-group/CSSTransitionGroup'; import styled from 'styled-components';  const appearDuration = 500; const transitionName = `example`;  const Container = styled.section`         font-size: 1.5em;         padding: 0;         margin: 0;          &.${transitionName}-appear {             opacity: 0.01;         }          &.${transitionName}-appear-active {             opacity: 1;             transition: opacity ${appearDuration}ms ease-out;         }`;  export default () => {      return (         <CSSTransitionGroup             transitionName={transitionName}             transitionAppear={true}             transitionAppearTimeout={appearDuration}>             <Container>                 This will have the appear transition applied!             </Container>         </CSSTransitionGroup>     ); }; 

Note that I'm using the newer CSSTransitionGroup, rather than ReactCSSTransitionGroup, but it should work for that too.

like image 108
Mike Goatly Avatar answered Oct 03 '22 21:10

Mike Goatly