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

fix(animation): add property conversions for CSS Animations #20252

Merged
merged 3 commits into from Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/components/modal/animations/ios.enter.ts
Expand Up @@ -43,8 +43,8 @@ export const iosEnterAnimation = (
.beforeAddWrite(() => bodyEl.style.setProperty('background-color', 'black'))
.addElement(presentingEl)
.keyframes([
{ offset: 0, transform: 'translateY(0px) scale(1)', 'border-radius': '0px' },
{ offset: 1, transform: finalTransform, 'border-radius': '10px 10px 0 0' }
{ offset: 0, transform: 'translateY(0px) scale(1)', borderRadius: '0px' },
{ offset: 1, transform: finalTransform, borderRadius: '10px 10px 0 0' }
]);

baseAnimation.addAnimation(presentingAnimation);
Expand Down
4 changes: 2 additions & 2 deletions core/src/components/modal/animations/ios.leave.ts
Expand Up @@ -44,8 +44,8 @@ export const iosLeaveAnimation = (
}
})
.keyframes([
{ offset: 0, transform: `translateY(${modalTransform}) scale(${currentPresentingScale})`, 'border-radius': '10px 10px 0 0' },
{ offset: 1, transform: 'translateY(0px) scale(1)', 'border-radius': '0px' }
{ offset: 0, transform: `translateY(${modalTransform}) scale(${currentPresentingScale})`, borderRadius: '10px 10px 0 0' },
{ offset: 1, transform: 'translateY(0px) scale(1)', borderRadius: '0px' }
]);

baseAnimation.addAnimation(presentingAnimation);
Expand Down
21 changes: 15 additions & 6 deletions core/src/utils/animation/animation-utils.ts
Expand Up @@ -7,21 +7,30 @@ import { AnimationKeyFrames } from './animation-interface';
export const processKeyframes = (keyframes: AnimationKeyFrames) => {
keyframes.forEach(keyframe => {
for (const key in keyframe) {
if (keyframe.hasOwnProperty(key) && key.includes('-')) {
if (keyframe.hasOwnProperty(key)) {
const value = keyframe[key];
const newKey = convertHyphenToCamelCase(key);

keyframe[newKey] = value;
delete keyframe[key];
if (key === 'easing') {
const newKey = 'animation-timing-function';
keyframe[newKey] = value;
delete keyframe[key];
} else {
const newKey = convertCamelCaseToHypen(key);

if (newKey !== key) {
keyframe[newKey] = value;
delete keyframe[key];
}
}
}
}
});

return keyframes;
};

const convertHyphenToCamelCase = (str: string) => {
return str.replace(/-([a-z])/ig, g => g[1].toUpperCase());
const convertCamelCaseToHypen = (str: string) => {
return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
};

let animationPrefix: string | undefined;
Expand Down
9 changes: 4 additions & 5 deletions core/src/utils/animation/animation.ts
Expand Up @@ -494,9 +494,10 @@ export const createAnimation = (animationId?: string): Animation => {
const initializeCSSAnimation = (toggleAnimationName = true) => {
cleanUpStyleSheets();

const processedKeyframes = processKeyframes(_keyframes);
elements.forEach(element => {
if (_keyframes.length > 0) {
const keyframeRules = generateKeyframeRules(_keyframes);
if (processedKeyframes.length > 0) {
const keyframeRules = generateKeyframeRules(processedKeyframes);
keyframeName = (animationId !== undefined) ? animationId : generateKeyframeName(keyframeRules);
const stylesheet = createKeyframeStylesheet(keyframeName, keyframeRules, element);
stylesheets.push(stylesheet);
Expand Down Expand Up @@ -526,10 +527,8 @@ export const createAnimation = (animationId?: string): Animation => {
};

const initializeWebAnimation = () => {
const processedKeyframes = processKeyframes(_keyframes);

elements.forEach(element => {
const animation = element.animate(processedKeyframes, {
const animation = element.animate(_keyframes, {
id,
delay: getDelay(),
duration: getDuration(),
Expand Down
13 changes: 13 additions & 0 deletions core/src/utils/animation/test/animation.spec.ts
@@ -1,4 +1,5 @@
import { createAnimation } from '../animation';
import { processKeyframes } from '../animation-utils';
import { getTimeGivenProgression } from '../cubic-bezier';
import { Animation } from '../animation-interface';

Expand Down Expand Up @@ -82,6 +83,18 @@ describe('Animation Class', () => {

expect(animation.getKeyframes().length).toEqual(3);
});

it('should convert properties for CSS Animations', () => {
const processedKeyframes = processKeyframes([
{ borderRadius: '0px', easing: 'ease-in' , offset: 0 },
{ borderRadius: '4px', easing: 'ease-out', offset: 1 }
]);

expect(processedKeyframes).toEqual([
{ 'border-radius': '0px', 'animation-timing-function': 'ease-in', offset: 0 },
{ 'border-radius': '4px', 'animation-timing-function': 'ease-out', offset: 1 }
]);
});

it('should set the from keyframe properly', () => {
animation
Expand Down
8 changes: 4 additions & 4 deletions core/src/utils/animation/test/basic/index.html
Expand Up @@ -29,10 +29,10 @@
.iterations(2)
.easing('linear')
.keyframes([
{ background: 'rgba(255, 0, 0, 0.5)', offset: 0, 'border-radius': '0px' },
{ background: 'rgba(0, 255, 0, 0.5)', offset: 0.33, 'border-radius': '10px' },
{ background: 'rgba(0, 0, 255, 0.5)', offset: 0.66, 'border-radius': '20px' },
{ background: 'rgba(255, 0, 0, 0.5)', offset: 1, 'border-radius': '30px' }
{ background: 'rgba(255, 0, 0, 0.5)', offset: 0, borderRadius: '0px' },
{ background: 'rgba(0, 255, 0, 0.5)', offset: 0.33, borderRadius: '10px' },
{ background: 'rgba(0, 0, 255, 0.5)', offset: 0.66, borderRadius: '20px' },
{ background: 'rgba(255, 0, 0, 0.5)', offset: 1, borderRadius: '30px' }
])
.onFinish(() => {
const ev = new CustomEvent('ionAnimationFinished');
Expand Down