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

docs(animation): add playground example for keyframes #3039

Merged
merged 7 commits into from
Jul 17, 2023
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.
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 card element will transition from its initial width, to a width defined by the `--width` variable, and then transition on to the final width.

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,8 @@
```html
<ion-card #card style="width: 80px; --width: 160px;">
<ion-card-content>Card</ion-card-content>
</ion-card>
<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, IonCard, IonCardContent } from '@ionic/angular';

@Component({
selector: 'app-example',
templateUrl: 'example.component.html',
})
export class ExampleComponent {
@ViewChild(IonCard, { read: ElementRef }) card: ElementRef<HTMLIonCardElement>;

private animation: Animation;

constructor(private animationCtrl: AnimationController) {}

ngAfterViewInit() {
this.animation = this.animationCtrl
.create()
.addElement(this.card.nativeElement)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, width: '80px' },
{ offset: 0.72, width: 'var(--width)' },
{ offset: 1, width: '240px' },
]);
}

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('#card'))
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, width: '80px' },
{ offset: 0.72, width: 'var(--width)' },
{ offset: 1, width: '240px' },
]);

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;
}

ion-card {
width: 80px;
--width: 160px;
}
</style>
</head>

<body>
<div class="container">
<ion-card id="card">
<ion-card-content>Card</ion-card-content>
</ion-card>
<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>
25 changes: 25 additions & 0 deletions static/usage/v7/animations/keyframes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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"
devicePreview={true}
/>
20 changes: 20 additions & 0 deletions static/usage/v7/animations/keyframes/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
```html
<ion-card id="card" style="width: 80px; --width: 160px;">
<ion-card-content>Card</ion-card-content>
</ion-card>
<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('#card'))
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, width: '80px' },
{ offset: 0.72, width: 'var(--width)' },
{ offset: 1, width: '240px' },
]);
</script>
```
47 changes: 47 additions & 0 deletions static/usage/v7/animations/keyframes/react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
```tsx
import React, { useEffect, useRef } from 'react';
import { IonButton, IonCard, IonCardContent, createAnimation } from '@ionic/react';
import type { Animation } from '@ionic/react';

function Example() {
const cardEl = useRef<HTMLIonCardElement | null>(null);

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

useEffect(() => {
if (animation.current === null) {
animation.current = createAnimation()
.addElement(cardEl.current!)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, width: '80px' },
{ offset: 0.72, width: 'var(--width)' },
{ offset: 1, width: '240px' },
]);
}
}, [cardEl]);

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

return (
<>
<IonCard ref={cardEl} style={{ width: '80px', '--width': '160px' } as React.CSSProperties}>
<IonCardContent>Card</IonCardContent>
</IonCard>
<IonButton onClick={play}>Play</IonButton>
<IonButton onClick={pause}>Pause</IonButton>
<IonButton onClick={stop}>Stop</IonButton>
</>
);
}
export default Example;
```
60 changes: 60 additions & 0 deletions static/usage/v7/animations/keyframes/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
```html
<template>
<ion-card ref="cardEl">
<ion-card-content>Card</ion-card-content>
</ion-card>
<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, IonCard, IonCardContent, createAnimation } from '@ionic/vue';
import type { Animation } from '@ionic/vue';

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

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

let animation: Animation;

onMounted(() => {
animation = createAnimation()
.addElement(cardEl.value.$el)
.duration(3000)
.iterations(Infinity)
.keyframes([
{ offset: 0, width: '80px' },
{ offset: 0.72, width: 'var(--width)' },
{ offset: 1, width: '240px' },
]);
});

const play = () => animation.play();
const pause = () => animation.pause();
const stop = () => animation.stop();

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

<style>
ion-card {
width: 80px;
--width: 160px;
}
</style>
```