-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
sarayourfriend
wants to merge
5
commits into
WordPress:master
from
sarayourfriend:refactor/use-css-in-js-for-animate
Closed
Animate: Use CSS-in-JS #25754
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb5b278
[@wordpress/components] Use CSS-in-JS for Animate
sarayourfriend dc9e406
Update snapshot tests
sarayourfriend 5b02810
Deprecate "middle" value for y-axis origin
sarayourfriend 63d7482
Make animations RTL safe
sarayourfriend f886962
Clean up RTL safe rules
sarayourfriend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
packages/components/src/animate/styles/animate-keyframe-styles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
`; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.