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

Animate: Use CSS-in-JS #25754

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions packages/components/src/animate/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,69 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { isFunction } from 'lodash';

/**
* WordPress dependencies
*/
import deprecated from '@wordpress/deprecated';

/**
* Internal dependencies
*/
import {
AppearingWrapper,
SlidingWrapper,
LoadingWrapper,
} from './styles/animate-styles';

function Animate( { type, options = {}, children } ) {
/**
* For backwards compatability we need to handle when
* children as a function is passed in. By passing an
* empty string for className we should prevent any
* conventional usage of this component from before the
* change from breaking.
*/
const safeChildren = isFunction( children )
? () => children( { className: '' } )
: () => children;

if ( type === 'appear' ) {
const { origin = 'top' } = options;
const [ yAxis, xAxis = 'center' ] = origin.split( ' ' );

return children( {
className: classnames( 'components-animate__appear', {
[ 'is-from-' + xAxis ]: xAxis !== 'center',
[ 'is-from-' + yAxis ]: yAxis !== 'middle',
} ),
} );
if ( yAxis === 'middle' ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this value was used for consistency with the Popover position props but I may be wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, should it be kept and still filtered out then? It does appear to line up with the Popover position props.

deprecated( 'Using "middle" for the y-axis value of origin', {
alternative: 'center',
} );
}

return (
<AppearingWrapper
yAxis={ yAxis === 'middle' ? 'center' : yAxis }
xAxis={ xAxis }
>
{ safeChildren() }
</AppearingWrapper>
);
}

if ( type === 'slide-in' ) {
const { origin = 'left' } = options;

return children( {
className: classnames(
'components-animate__slide-in',
'is-from-' + origin
),
} );
return (
<SlidingWrapper origin={ origin }>
{ safeChildren() }
</SlidingWrapper>
);
}

if ( type === 'loading' ) {
return children( {
className: classnames( 'components-animate__loading' ),
} );
return <LoadingWrapper>{ safeChildren() }</LoadingWrapper>;
}

return children( {} );
return safeChildren( {} );
}

export default Animate;
24 changes: 18 additions & 6 deletions packages/components/src/animate/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ export const _default = () => (
// Unexported helper for different origins.
const Appear = ( { origin } ) => (
<Animate type="appear" options={ { origin } }>
{ ( { className } ) => (
<Notice className={ className } status="success">
<p>Appear animation. Origin: { origin }.</p>
</Notice>
) }
<Notice status="success">
<p>Appear animation. Origin: { origin }.</p>
</Notice>
</Animate>
);

export const appearTopLeft = () => <Appear origin="top left" />;
export const appearTopRight = () => <Appear origin="top right" />;
export const appearBottomLeft = () => <Appear origin="bottom left" />;
export const appearBottomRight = () => <Appear origin="bottom right" />;
export const appearCenterCenter = () => <Appear origin="center center" />;
export const deprecatedAppearMiddleCenter = () => (
<Appear origin="middle center" />
);

export const loading = () => (
<Animate type="loading">
Expand All @@ -42,7 +44,7 @@ export const loading = () => (
</Animate>
);

export const slideIn = () => (
export const slideInLeft = () => (
<Animate type="slide-in">
{ ( { className } ) => (
<Notice className={ className } status="success">
Expand All @@ -51,3 +53,13 @@ export const slideIn = () => (
) }
</Animate>
);

export const slideInRight = () => (
<Animate type="slide-in" options={ { origin: 'right' } }>
{ ( { className } ) => (
<Notice className={ className } status="success">
<p>Slide-in animation.</p>
</Notice>
) }
</Animate>
);
65 changes: 0 additions & 65 deletions packages/components/src/animate/style.scss

This file was deleted.

31 changes: 31 additions & 0 deletions packages/components/src/animate/styles/animate-keyframe-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* External dependencies
*/
import { keyframes } from '@emotion/core';

export const appear = keyframes`
from {
transform: translateY(-2em) scaleY(0) scaleX(0);
}
to {
transform: translateY(0%) scaleY(1) scaleX(1);
}
`;

export const slideIn = keyframes`
100% {
transform: translateX(0%);
}
`;

export const loading = keyframes`
0% {
opacity: 0.5;
}
50% {
opacity: 1;
}
100% {
opacity: 0.5;
}
`;
33 changes: 33 additions & 0 deletions packages/components/src/animate/styles/animate-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* External dependencies
*/
import styled from '@emotion/styled';

/**
* Internal dependencies
*/
import * as animations from './animate-keyframe-styles';
import { reduceMotion } from '../../utils';
import { appearTransform, appearTransformOrigin } from './utils';

export const AppearingWrapper = styled.div`
animation: ${ animations.appear } 0.1s cubic-bezier( 0, 0, 0.2, 1 ) 0s;
animation-fill-mode: forwards;

${ reduceMotion( 'animation' ) }

${ appearTransformOrigin }
`;

export const SlidingWrapper = styled.div`
animation: ${ animations.slideIn } 0.1s cubic-bezier( 0, 0, 0.2, 1 );
animation-fill-mode: forwards;

${ reduceMotion( 'animation' ) }

${ appearTransform }
`;

export const LoadingWrapper = styled.div`
animation: ${ animations.loading } 1.6s ease-in-out infinite;
`;
45 changes: 45 additions & 0 deletions packages/components/src/animate/styles/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { css } from '@emotion/core';

/**
* Internal dependencies
*/
import { getRTL } from '../../utils';

const RTL_TRANSLATE_X = {
'-100%': '+100%',
'+100%': '-100%',
};

export const appearTransform = ( { origin } ) => {
const isRTL = getRTL();

let translateX = origin === 'right' ? '+100%' : '-100%';
if ( isRTL ) {
translateX = RTL_TRANSLATE_X[ translateX ];
}

return css`
transform: translateX( ${ translateX } );
`;
};

const RTL_DIRECTION = {
left: 'right',
right: 'left',
};

export const appearTransformOrigin = ( { yAxis, xAxis } ) => {
const isRTL = getRTL();

if ( isRTL ) {
// Use opposite values when one exists, otherwise default to the value itself (as in the case of `center`).
xAxis = RTL_DIRECTION[ xAxis ] || xAxis;
}

return css`
transform-origin: ${ yAxis } ${ xAxis };
`;
};
Loading