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 1 commit
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
Prev Previous commit
Clean up RTL safe rules
  • Loading branch information
sarayourfriend committed Oct 3, 2020
commit f8869623b913067ad821f8608391867af38969a9
8 changes: 4 additions & 4 deletions packages/components/src/animate/styles/animate-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import styled from '@emotion/styled';
*/
import * as animations from './animate-keyframe-styles';
import { reduceMotion } from '../../utils';
import { getRTLSafeTranslateX, getRTLSafeXAxis } 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' ) }
transform-origin: ${ ( p ) => p.yAxis } ${ getRTLSafeXAxis };

${ appearTransformOrigin }
`;

export const SlidingWrapper = styled.div`
Expand All @@ -25,7 +25,7 @@ export const SlidingWrapper = styled.div`

${ reduceMotion( 'animation' ) }

transform: translateX( ${ getRTLSafeTranslateX } );
${ appearTransform }
`;

export const LoadingWrapper = styled.div`
Expand Down
40 changes: 27 additions & 13 deletions packages/components/src/animate/styles/utils.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
/**
* External dependencies
*/
import { css } from '@emotion/core';

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

export const getRTLSafeTranslateX = ( { origin } ) => {
const RTL_TRANSLATE_X = {
'-100%': '+100%',
'+100%': '-100%',
};

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

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

return origin === 'right' ? '+100%' : '-100%';
return css`
transform: translateX( ${ translateX } );
`;
};

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

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

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

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