Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
87 changes: 3 additions & 84 deletions docs/utilities/animations.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,95 +152,14 @@ Ionic Animations allows you to control the intermediate steps in an animation us

Hyphenated CSS properties should be written using camel case when writing keyframes. For example, `border-radius` should be written as `borderRadius`. This also applies to the `fromTo()`, `from(),` and `to()` methods.

### Usage

````mdx-code-block
<Tabs
groupId="framework"
defaultValue="javascript"
values={[
{ value: 'javascript', label: 'JavaScript' },
{ value: 'angular', label: 'Angular' },
{ value: 'react', label: 'React' },
{ value: 'vue', label: 'Vue' },
]
}>
<TabItem value="javascript">

```javascript
createAnimation()
.addElement(document.querySelector('.square'))
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]);
```
</TabItem>
<TabItem value="angular">

```javascript
this.animationCtrl.create()
.addElement(this.square.nativeElement)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]);
```
</TabItem>
<TabItem value="react">
import Keyframes from '@site/static/usage/v7/animations/keyframes/index.md';

```tsx
<CreateAnimation
duration={3000}
iterations={Infinity}
keyframes={[
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]}
>
...
</CreateAnimation>
```
</TabItem>
<TabItem value="vue">
<Keyframes />

```javascript
import { createAnimation } from '@ionic/vue';
import { ref } from 'vue';

...

const squareRef = ref();

...

createAnimation()
.addElement(squareRef.value)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'green' }
]);
```
</TabItem>
</Tabs>
````

In the example above, the `.square` element will transition from a red background color, to a background color defined by the `--background` variable, and then transition on to a green background color.
In the example above, the `.square` element will transition from a red background color, to a background color defined by the `--background` variable, and then transition on to an aqua background color.

Each keyframe object contains an `offset` property. `offset` is a value between 0 and 1 that defines the keyframe step. Offset values must go in ascending order and cannot repeat.

<Codepen user="ionic" slug="YzKLEzR" />

## Grouped Animations

Multiple elements can be animated at the same time and controlled via a single parent animation object. Child animations inherit properties such as duration, easing, and iterations unless otherwise specified. A parent animation's `onFinish` callback will not be called until all child animations have completed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```html
<div #square style="width: 100px; height: 100px; background: red; --background: yellow;"></div>
<ion-button (click)="play()">Play</ion-button>
<ion-button (click)="pause()">Pause</ion-button>
<ion-button (click)="stop()">Stop</ion-button>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
```ts
import { Component, ElementRef, ViewChild } from '@angular/core';
import type { Animation } from '@ionic/angular';
import { AnimationController } from '@ionic/angular';

@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
})
export class ExampleComponent {
@ViewChild('square', { static: true }) square: ElementRef;

private animation: Animation;

constructor(private animationCtrl: AnimationController) {}

ngAfterViewInit() {
this.animation = this.animationCtrl
.create()
.addElement(this.square.nativeElement)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'aqua' },
]);
}

play() {
this.animation.play();
}

pause() {
this.animation.pause();
}

stop() {
this.animation.stop();
}
}
```
61 changes: 61 additions & 0 deletions static/usage/v7/animations/keyframes/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Keyframe Animations</title>
<link rel="stylesheet" href="../../../common.css" />
<script src="../../../common.js"></script>
<script type="module" src="https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/ionic.esm.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ionic/core@7/css/ionic.bundle.css" />
<script type="module">
import { createAnimation } from 'https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/index.esm.js';

const animation = createAnimation()
.addElement(document.querySelector('.square'))
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'aqua' },
]);

document.querySelector('#play').addEventListener('click', () => {
animation.play();
});

document.querySelector('#pause').addEventListener('click', () => {
animation.pause();
});

document.querySelector('#stop').addEventListener('click', () => {
animation.stop();
});
</script>

<style>
.container {
flex-direction: column;
}

.square {
width: 100px;
height: 100px;
background: red;
--background: yellow;
}
</style>
</head>

<body>
<div class="container">
<div class="square"></div>
Copy link
Member

Choose a reason for hiding this comment

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

Could we use an ion-card here like the other demos? Example: https://ionicframework.com/docs/utilities/animations#basic-animations

<div>
<ion-button id="play">Play</ion-button>
<ion-button id="pause">Pause</ion-button>
<ion-button id="stop">Stop</ion-button>
</div>
</div>
</body>
</html>
24 changes: 24 additions & 0 deletions static/usage/v7/animations/keyframes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Playground from '@site/src/components/global/Playground';

import javascript from './javascript.md';
import react from './react.md';
import vue from './vue.md';

import angular_example_component_html from './angular/example_component_html.md';
import angular_example_component_ts from './angular/example_component_ts.md';

<Playground
version="7"
code={{
javascript,
react,
vue,
angular: {
files: {
'src/app/example.component.html': angular_example_component_html,
'src/app/example.component.ts': angular_example_component_ts,
},
},
}}
src="usage/v7/animations/keyframes/demo.html"
/>
18 changes: 18 additions & 0 deletions static/usage/v7/animations/keyframes/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
```html
<div class="square" style="width: 100px; height: 100px; background: red; --background: yellow;"></div>
<ion-button onclick="animation.play()">Play</ion-button>
<ion-button onclick="animation.pause()">Pause</ion-button>
<ion-button onclick="animation.stop()">Stop</ion-button>

<script>
var animation = createAnimation()
.addElement(document.querySelector('.square'))
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'aqua' },
]);
</script>
```
55 changes: 55 additions & 0 deletions static/usage/v7/animations/keyframes/react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
```tsx
import React, { useEffect, useRef } from 'react';
import { IonButton, createAnimation } from '@ionic/react';
import type { Animation } from '@ionic/react';

function Example() {
const squareEl = useRef<HTMLDivElement | null>(null);

const animation = useRef<Animation | null>(null);

useEffect(() => {
if (animation.current === null) {
animation.current = createAnimation()
.addElement(squareEl.current!)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'aqua' },
]);
}
}, [squareEl]);

const play = () => {
animation.current?.play();
};
const pause = () => {
animation.current?.pause();
};
const stop = () => {
animation.current?.stop();
};

return (
<>
<div
ref={squareEl}
style={
{
width: '100px',
height: '100px',
background: 'red',
'--background': 'yellow',
} as React.CSSProperties
}
></div>
<IonButton onClick={play}>Play</IonButton>
<IonButton onClick={pause}>Pause</IonButton>
<IonButton onClick={stop}>Stop</IonButton>
</>
);
}
export default Example;
```
65 changes: 65 additions & 0 deletions static/usage/v7/animations/keyframes/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
```html
<template>
<div ref="squareRef" class="square"></div>
<ion-button @click="play()">Play</ion-button>
<ion-button @click="pause()">Pause</ion-button>
<ion-button @click="stop()">Stop</ion-button>
</template>

<script lang="ts">
import { IonButton, createAnimation } from '@ionic/vue';
import type { Animation } from '@ionic/vue';

import { defineComponent, ref, onMounted } from 'vue';

export default defineComponent({
components: {
IonButton,
},
setup() {
const squareRef = ref(null);

let animation: Animation | undefined;

onMounted(() => {
console.log(squareRef);
animation = createAnimation()
.addElement(squareRef.value)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, background: 'red' },
{ offset: 0.72, background: 'var(--background)' },
{ offset: 1, background: 'aqua' },
]);
});

const play = async () => {
await animation.play();
};
const pause = () => {
animation?.pause();
};
const stop = () => {
animation?.stop();
};

return {
play,
pause,
stop,
squareRef,
};
},
});
</script>

<style>
.square {
width: 100px;
height: 100px;
background: red;
--background: yellow;
}
</style>
```