Skip to content

docs(animation): add playground for override example #3018

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

Merged
merged 9 commits into from
Jul 6, 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
231 changes: 2 additions & 229 deletions docs/utilities/animations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1279,236 +1279,9 @@ Certain Ionic components allow developers to provide custom animations. All anim

### Modals

````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
customElements.define('modal-page', class extends HTMLElement {
connectedCallback() {
this.innerHTML = `
<ion-header>
<ion-toolbar>
<ion-title>Modal Header</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
Modal Content
</ion-content>
`;
}
});

function presentModal() {
const enterAnimation = (baseEl: any) => {
const root = baseEl.shadowRoot;

const backdropAnimation = createAnimation()
.addElement(root.querySelector('ion-backdrop')!)
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

const wrapperAnimation = createAnimation()
.addElement(root.querySelector('.modal-wrapper')!)
.keyframes([
{ offset: 0, opacity: '0', transform: 'scale(0)' },
{ offset: 1, opacity: '0.99', transform: 'scale(1)' }
]);

return createAnimation()
.addElement(baseEl)
.easing('ease-out')
.duration(500)
.addAnimation([backdropAnimation, wrapperAnimation]);
}

const leaveAnimation = (baseEl: any) => {
return enterAnimation(baseEl).direction('reverse');
}

// create the modal with the `modal-page` component
const modalElement = document.createElement('ion-modal');
modalElement.component = 'modal-page';
modalElement.enterAnimation = enterAnimation;
modalElement.leaveAnimation = leaveAnimation;

// present the modal
document.body.appendChild(modalElement);
return modalElement.present();
}
```
</TabItem>
<TabItem value="angular">

```tsx
import { Component } from '@angular/core';
import { ModalController, AnimationController } from '@ionic/angular';
import { ModalPage } from '../modal/modal.page';

@Component({
selector: 'modal-example',
templateUrl: 'modal-example.html',
styleUrls: ['./modal-example.css']
})
export class ModalExample {
constructor(public modalController: ModalController,
public animationCtrl: AnimationController) { }

async presentModal() {
const enterAnimation = (baseEl: any) => {
const root = baseEl.shadowRoot;

const backdropAnimation = this.animationCtrl.create()
.addElement(root.querySelector('ion-backdrop')!)
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

const wrapperAnimation = this.animationCtrl.create()
.addElement(root.querySelector('.modal-wrapper')!)
.keyframes([
{ offset: 0, opacity: '0', transform: 'scale(0)' },
{ offset: 1, opacity: '0.99', transform: 'scale(1)' }
]);

return this.animationCtrl.create()
.addElement(baseEl)
.easing('ease-out')
.duration(500)
.addAnimation([backdropAnimation, wrapperAnimation]);
}

const leaveAnimation = (baseEl: any) => {
return enterAnimation(baseEl).direction('reverse');
}

const modal = await this.modalController.create({
component: ModalPage,
enterAnimation,
leaveAnimation
});
return await modal.present();
}
}
```
</TabItem>
<TabItem value="react">

```jsx
import React, { useState } from 'react';
import { createAnimation, IonModal, IonButton, IonContent } from '@ionic/react';

export const ModalExample: React.FC = () => {
const [showModal, setShowModal] = useState(false);

const enterAnimation = (baseEl: any) => {
const root = baseEl.shadowRoot;

const backdropAnimation = createAnimation()
.addElement(root.querySelector('ion-backdrop')!)
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

const wrapperAnimation = createAnimation()
.addElement(root.querySelector('.modal-wrapper')!)
.keyframes([
{ offset: 0, opacity: '0', transform: 'scale(0)' },
{ offset: 1, opacity: '0.99', transform: 'scale(1)' }
]);

return createAnimation()
.addElement(baseEl)
.easing('ease-out')
.duration(500)
.addAnimation([backdropAnimation, wrapperAnimation]);
}

const leaveAnimation = (baseEl: any) => {
return enterAnimation(baseEl).direction('reverse');
}

return (
<IonContent>
<IonModal isOpen={showModal} enterAnimation={enterAnimation} leaveAnimation={leaveAnimation}>
<p>This is modal content</p>
<IonButton onClick={() => setShowModal(false)}>Close Modal</IonButton>
</IonModal>
<IonButton onClick={() => setShowModal(true)}>Show Modal</IonButton>
</IonContent>
);
};
```
</TabItem>
<TabItem value="vue">

```jsx
<template>
<ion-page>
<ion-content>
<ion-modal
:is-open="isModalOpen"
:enter-animation="enterAnimation"
:leave-animation="leaveAnimation"
@didDismiss="setModalOpen(false)"
>
Modal content goes here.
</ion-modal>

<ion-button @click="setModalOpen(true)">Show Modal</ion-button>
</ion-content>
</ion-page>
</template>

<script lang="ts">
import { createAnimation, IonButton, IonContent, IonModal, IonPage } from '@ionic/vue';
import { defineComponent, ref } from 'vue';

export default defineComponent({
components: { IonButton, IonContent, IonModal, IonPage },
setup() {
const isModalOpen = ref(false);
const setModalOpen = (state) => isModalOpen.value = state;

const enterAnimation = (baseEl: any) => {
const root = baseEl.shadowRoot;

const backdropAnimation = createAnimation()
.addElement(root.querySelector('ion-backdrop')!)
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

const wrapperAnimation = createAnimation()
.addElement(root.querySelector('.modal-wrapper')!)
.keyframes([
{ offset: 0, opacity: '0', transform: 'scale(0)' },
{ offset: 1, opacity: '0.99', transform: 'scale(1)' }
]);

return createAnimation()
.addElement(baseEl)
.easing('ease-out')
.duration(500)
.addAnimation([backdropAnimation, wrapperAnimation]);
}

const leaveAnimation = (baseEl: any) => {
return enterAnimation(baseEl).direction('reverse');
}

return { isModalOpen, setModalOpen, enterAnimation, leaveAnimation }
}
})
</script>
```
</TabItem>
</Tabs>
````
import ModalOverride from '@site/static/usage/v7/animations/modal-override/index.md';

<Codepen user="ionic" slug="ExapZBZ" />
<ModalOverride />

## Performance Considerations

Expand Down
3 changes: 2 additions & 1 deletion static/code/stackblitz/v7/html/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineCustomElements } from '@ionic/core/loader';

import { loadingController, modalController, pickerController, toastController } from '@ionic/core';
import { createAnimation, loadingController, modalController, pickerController, toastController } from '@ionic/core';

/* Core CSS required for Ionic components to work properly */
import '@ionic/core/css/core.css';
Expand All @@ -27,3 +27,4 @@ defineCustomElements();
(window as any).modalController = modalController;
(window as any).pickerController = pickerController;
(window as any).toastController = toastController;
(window as any).createAnimation = createAnimation;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
```html
<ion-header>
<ion-toolbar>
<ion-title>Page</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<ion-button id="modal-trigger">Present Modal</ion-button>
<ion-modal trigger="modal-trigger" #modal>
<ng-template>
<ion-header>
<ion-toolbar>
<ion-title>Modal</ion-title>
<ion-buttons slot="end">
<ion-button (click)="closeModal()">Close</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding"> Modal Content </ion-content>
</ng-template>
</ion-modal>
</ion-content>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
```ts
import { Component, ViewChild } from '@angular/core';
import type { IonModal } from '@ionic/angular';
import { AnimationController } from '@ionic/angular';

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

constructor(private animationCtrl: AnimationController) {}

ngOnInit() {
const enterAnimation = (baseEl: HTMLElement) => {
const root = baseEl.shadowRoot;

const backdropAnimation = this.animationCtrl
.create()
.addElement(root.querySelector('ion-backdrop'))
.fromTo('opacity', '0.01', 'var(--backdrop-opacity)');

const wrapperAnimation = this.animationCtrl
.create()
.addElement(root.querySelector('.modal-wrapper'))
.keyframes([
{ offset: 0, opacity: '0', transform: 'scale(0)' },
{ offset: 1, opacity: '0.99', transform: 'scale(1)' },
]);

return this.animationCtrl
.create()
.addElement(baseEl)
.easing('ease-out')
.duration(500)
.addAnimation([backdropAnimation, wrapperAnimation]);
};

const leaveAnimation = (baseEl: HTMLElement) => {
return enterAnimation(baseEl).direction('reverse');
};

this.modal.enterAnimation = enterAnimation;
this.modal.leaveAnimation = leaveAnimation;
}

closeModal() {
this.modal.dismiss();
}
}
```
Loading