Skip to content

Instantly share code, notes, and snippets.

@oncomouse
Last active April 15, 2021 05:24
Show Gist options
  • Select an option

  • Save oncomouse/08b1920f6b42d90a01a0e296980386a4 to your computer and use it in GitHub Desktop.

Select an option

Save oncomouse/08b1920f6b42d90a01a0e296980386a4 to your computer and use it in GitHub Desktop.
Using CSSTransitionGroup with Emotion.js
import React from 'react';
import styled from '@emotion/styled';
import { css } from '@emotion/core';
import { CSSTransition } from 'react-transition-group';
const has = (key, obj) => Object.prototype.hasOwnProperty.call(obj, key);
const keyframes = [
'appear',
'enter',
'exit',
'appear-active',
'enter-active',
'exit-active',
'appear-done',
'enter-done',
'exit-done',
]
const StyledTransition = styled(({ transitions, className, ...props }) => <CSSTransition className={className} classNames={className} {...props} />)`
${({transitions}) => keyframes.map(keyframe => {
const objectKey = keyframe.replace(/(-[a-z])/, v => v.slice(1).toUpperCase());
if (has(objectKey, transitions)) {
return css`
&-${keyframe} {
${transitions[objectKey]}
}
`;
}
return null;
}).filter(x => x !== null)}
`;
export default StyledTransition;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment