Skip to content

docs(animations): add React and Angular example for preference animations #1712

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

Closed
wants to merge 2 commits into from
Closed
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
34 changes: 24 additions & 10 deletions src/pages/utilities/animations.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,15 +843,16 @@ Developers can also tailor their animations to user preferences such as `prefers
width: 100px;
height: 100px;
opacity: 0.5;
background: blue;
margin: 10px;

--background: red;
background: var(--background);
--translate-position: translateX(50px)
}

@media (prefers-color-scheme: dark) {
.square {
--background: green;
--translate-position: translateX(250px)
}
}
```
Expand All @@ -865,7 +866,8 @@ createAnimation()
.duration(1500)
.iterations(Infinity)
.direction('alternate')
.fromTo('background', 'blue', 'var(--background)');
.fromTo("background", "red", "var(--background)")
.fromTo("transform", "translateX(0)", "var(--translate-position)");
```
</docs-tab>
<docs-tab tab="angular">
Expand All @@ -876,7 +878,8 @@ this.animationCtrl.create()
.duration(1500)
.iterations(Infinity)
.direction('alternate')
.fromTo('background', 'blue', 'var(--background)');
.fromTo("background", "red", "var(--background)")
.fromTo("transform", "translateX(0)", "var(--translate-position)");
```
</docs-tab>
<docs-tab tab="react">
Expand All @@ -886,18 +889,27 @@ this.animationCtrl.create()
duration={1500}
iterations={Infinity}
direction='alternate'
fromTo={{
property: 'background',
fromValue: 'blue',
toValue: 'var(--background)'
}}
fromTo={[
{
property: "background",
fromValue: "red",
toValue: "var(--background)"
},
{
property: "transform",
fromValue: "translateX(0)",
toValue: "var(--translate-position)"
}
]}
>
<div className="square"></div>
</CreateAnimation>
```
</docs-tab>
</docs-tabs>

You can view a live example of this in Angular [here](https://stackblitz.com/edit/ionic-angular-preference-based-animations) and in React [here](https://stackblitz.com/edit/ionic-react-preference-based-animations).

This method works in all supported browsers when creating animations for the first time. Most browsers are also capable of dynamically updating keyframe animations as the CSS Variables change.

Safari does not currently support dynamically updating keyframe animations. For developers who need this kind of support in Safari, they can use [MediaQueryList.addListener()](https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/addListener).
Expand Down Expand Up @@ -1019,7 +1031,7 @@ export class ModalExample {

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

export const ModalExample: React.FC = () => {
const [showModal, setShowModal] = useState(false);
Expand Down Expand Up @@ -1061,6 +1073,8 @@ export const ModalExample: React.FC = () => {
</docs-tab>
</docs-tabs>

You can view a live example of this in Angular [here](https://stackblitz.com/edit/ionic-angular-animation-override) and in React [here](https://stackblitz.com/edit/ionic-react-animation-overwride).

<docs-codepen user="ionic" slug="ExapZBZ"></docs-codepen>


Expand Down