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(animations): add playground for before and after hooks #3035

Merged
merged 5 commits into from
Jul 12, 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
113 changes: 4 additions & 109 deletions docs/utilities/animations.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,118 +255,13 @@ import Group from '@site/static/usage/v7/animations/group/index.md';

Ionic Animations provides hooks that let you alter an element before an animation runs and after an animation completes. These hooks can be used to perform DOM reads and writes as well as add or remove classes and inline styles.

### 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(2000)
.beforeStyles({
opacity: 0.2
})
.afterStyles({
background: 'rgba(0, 255, 0, 0.5)'
})
.afterClearStyles(['opacity'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' }
])
```
</TabItem>
<TabItem value="angular">

```javascript
this.animationCtrl.create()
.addElement(this.square.nativeElement)
.duration(2000)
.beforeStyles({
opacity: 0.2
})
.afterStyles({
background: 'rgba(0, 255, 0, 0.5)'
})
.afterClearStyles(['opacity'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' }
])
```
</TabItem>
<TabItem value="react">

```tsx
<CreateAnimation
duration={2000}
beforeStyles={{
opacity: 0.2
}}
afterStyles={{
background: 'rgba(0, 255, 0, 0.5)'
}}
afterClearStyles={['opacity']}
keyframes={[
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' }
]}
>
...
</CreateAnimation>
```
</TabItem>
<TabItem value="vue">

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

...

const squareRef = ref();

...

createAnimation()
.addElement(squareRef.value)
.duration(2000)
.beforeStyles({
opacity: 0.2
})
.afterStyles({
background: 'rgba(0, 255, 0, 0.5)'
})
.afterClearStyles(['opacity'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' }
])
```
</TabItem>
</Tabs>
````

In this example, an inline opacity of 0.2 is set on the `.square` element prior to the animation starting. Once the animation finishes, the background color of the element is set to `rgba(0, 255, 0, 0.5)`, and the inline opacity is cleared.
This example sets an inline filter which inverts the background color of the card by `75%` prior to the animation starting. Once the animation finishes, the box shadow on the element is set to `rgba(255, 0, 50, 0.4) 0px 4px 16px 6px`, a red glow, and the inline filter is cleared. The animation must be stopped in order to remove the box shadow and play it with the filter again.

See [Methods](#methods) for a complete list of hooks.

<Codepen user="ionic" slug="BaBxmwo" />
import BeforeAndAfterHooks from '@site/static/usage/v7/animations/before-and-after-hooks/index.md';

<BeforeAndAfterHooks />

## Chained Animations

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
```html
<ion-card>
<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,52 @@
```ts
import { Component, ElementRef, ViewChildren } from '@angular/core';
import type { QueryList } from '@angular/core';
import type { Animation } from '@ionic/angular';
import { AnimationController, IonCard } from '@ionic/angular';

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

private animation: Animation;

constructor(private animationCtrl: AnimationController) {}

ngAfterViewInit() {
const card = this.animationCtrl
.create()
.addElement(this.cardElements.get(0).nativeElement)
.duration(2000)
.beforeStyles({
filter: 'invert(75%)',
})
.beforeClearStyles(['box-shadow'])
.afterStyles({
'box-shadow': 'rgba(255, 0, 50, 0.4) 0px 4px 16px 6px',
})
.afterClearStyles(['filter'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' },
]);

this.animation = this.animationCtrl.create().duration(2000).addAnimation([card]);
}

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

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

stop() {
this.animation.stop();
}
}
```
74 changes: 74 additions & 0 deletions static/usage/v7/animations/before-and-after-hooks/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>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';
window.createAnimation = createAnimation;

const card = createAnimation()
.addElement(document.querySelector('#card'))
.duration(2000)
.beforeStyles({
filter: 'invert(75%)',
})
.beforeClearStyles(['box-shadow'])
.afterStyles({
'box-shadow': 'rgba(255, 0, 50, 0.4) 0px 4px 16px 6px',
})
.afterClearStyles(['filter'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' },
]);

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

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

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

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

ion-card {
width: 70%;
}
</style>
</head>

<body>
<ion-app>
<ion-content>
<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>
</ion-content>
</ion-app>
</body>
</html>
25 changes: 25 additions & 0 deletions static/usage/v7/animations/before-and-after-hooks/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/before-and-after-hooks/demo.html"
devicePreview={true}
/>
28 changes: 28 additions & 0 deletions static/usage/v7/animations/before-and-after-hooks/javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```html
<ion-card id="card">
<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(2000)
.beforeStyles({
filter: 'invert(75%)',
})
.beforeClearStyles(['box-shadow'])
.afterStyles({
'box-shadow': 'rgba(255, 0, 50, 0.4) 0px 4px 16px 6px',
})
.afterClearStyles(['filter'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' },
]);
</script>
```
61 changes: 61 additions & 0 deletions static/usage/v7/animations/before-and-after-hooks/react.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
```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 card = useRef<Animation>();

useEffect(() => {
if (!card.current) {
card.current = createAnimation()
.addElement(cardEl.current!)
.duration(2000)
.beforeStyles({
filter: 'invert(75%)',
})
.beforeClearStyles(['box-shadow'])
.afterStyles({
'box-shadow': 'rgba(255, 0, 50, 0.4) 0px 4px 16px 6px',
})
.afterClearStyles(['filter'])
.keyframes([
{ offset: 0, transform: 'scale(1)' },
{ offset: 0.5, transform: 'scale(1.5)' },
{ offset: 1, transform: 'scale(1)' },
]);
}
}, [cardEl]);

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

return (
<>
<IonCard ref={cardEl}>
<IonCardContent>Card</IonCardContent>
</IonCard>

<IonButton id="play" onClick={play}>
Play
</IonButton>
<IonButton id="pause" onClick={pause}>
Pause
</IonButton>
<IonButton id="stop" onClick={stop}>
Stop
</IonButton>
</>
);
}
export default Example;
```
Loading