-
Notifications
You must be signed in to change notification settings - Fork 3.1k
docs(animation): add playground example for double-click gesture #3042
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9036d6
Generate playground
mapsandapps 8911ba9
Import playground
mapsandapps 9d5fcc0
Build out playground
mapsandapps b899fe5
Remove codepen
mapsandapps c834c3f
Apply suggestions from code review
mapsandapps e4d352d
Address PR feedback
mapsandapps File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
static/usage/v7/gestures/double-click/angular/example_component_css.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
```css | ||
ion-card { | ||
transform: translateX(0); | ||
user-select: none; | ||
} | ||
``` |
5 changes: 5 additions & 0 deletions
5
static/usage/v7/gestures/double-click/angular/example_component_html.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
```html | ||
<ion-card #card> | ||
<ion-card-content>Double click me to move the card.</ion-card-content> | ||
</ion-card> | ||
``` |
51 changes: 51 additions & 0 deletions
51
static/usage/v7/gestures/double-click/angular/example_component_ts.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
```ts | ||
import { Component, ElementRef, ViewChild } from '@angular/core'; | ||
import { GestureController, IonCard } from '@ionic/angular'; | ||
|
||
@Component({ | ||
selector: 'app-example', | ||
templateUrl: 'example.component.html', | ||
styleUrls: ['./example.component.css'], | ||
}) | ||
export class ExampleComponent { | ||
@ViewChild('card', { read: ElementRef }) card: ElementRef<HTMLIonCardElement>; | ||
|
||
private currentOffset: number = 0; | ||
private lastOnStart: number = 0; | ||
private DOUBLE_CLICK_THRESHOLD: number = 500; | ||
|
||
constructor(private el: ElementRef, private gestureCtrl: GestureController) {} | ||
|
||
ngAfterViewInit() { | ||
const gesture = this.gestureCtrl.create({ | ||
el: this.card.nativeElement, | ||
threshold: 0, | ||
onStart: () => this.onStart(), | ||
gestureName: 'double-click', | ||
}); | ||
|
||
gesture.enable(); | ||
} | ||
|
||
private onStart() { | ||
const now = Date.now(); | ||
|
||
if (Math.abs(now - this.lastOnStart) <= this.DOUBLE_CLICK_THRESHOLD) { | ||
this.card.nativeElement.style.setProperty('transform', this.getNewTransform()); | ||
this.lastOnStart = 0; | ||
} else { | ||
this.lastOnStart = now; | ||
} | ||
} | ||
|
||
private getNewTransform() { | ||
if (this.currentOffset >= 100) { | ||
this.currentOffset = 0; | ||
} else { | ||
this.currentOffset += 20; | ||
} | ||
|
||
return `translateX(${this.currentOffset}px)`; | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Double-Click Gestures</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 { createGesture } from 'https://cdn.jsdelivr.net/npm/@ionic/core@7/dist/ionic/index.esm.js'; | ||
window.createGesture = createGesture; | ||
|
||
const DOUBLE_CLICK_THRESHOLD = 500; | ||
const card = document.querySelector('ion-card'); | ||
const gesture = createGesture({ | ||
el: card, | ||
threshold: 0, | ||
onStart: () => onStart(), | ||
gestureName: 'double-click', | ||
}); | ||
|
||
gesture.enable(); | ||
|
||
let lastOnStart = 0; | ||
let currentOffset = 0; | ||
|
||
const onStart = () => { | ||
const now = Date.now(); | ||
|
||
if (Math.abs(now - lastOnStart) <= DOUBLE_CLICK_THRESHOLD) { | ||
card.style.setProperty('transform', getNewTransform()); | ||
lastOnStart = 0; | ||
} else { | ||
lastOnStart = now; | ||
} | ||
}; | ||
|
||
const getNewTransform = () => { | ||
if (currentOffset >= 100) { | ||
currentOffset = 0; | ||
} else { | ||
currentOffset += 20; | ||
} | ||
|
||
return `translateX(${currentOffset}px)`; | ||
}; | ||
</script> | ||
|
||
<style> | ||
ion-card { | ||
transform: translateX(0); | ||
user-select: none; | ||
} | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<ion-content> | ||
<ion-card> | ||
<ion-card-content>Double click me to move the card.</ion-card-content> | ||
</ion-card> | ||
</ion-content> | ||
</body> | ||
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import Playground from '@site/src/components/global/Playground'; | ||
|
||
import javascript from './javascript.md'; | ||
|
||
import react_main_tsx from './react/main_tsx.md'; | ||
import react_main_css from './react/main_css.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'; | ||
import angular_example_component_css from './angular/example_component_css.md'; | ||
|
||
<Playground | ||
version="7" | ||
code={{ | ||
javascript, | ||
react: { | ||
files: { | ||
'src/main.tsx': react_main_tsx, | ||
'src/main.css': react_main_css, | ||
}, | ||
}, | ||
vue, | ||
angular: { | ||
files: { | ||
'src/app/example.component.html': angular_example_component_html, | ||
'src/app/example.component.ts': angular_example_component_ts, | ||
'src/app/example.component.css': angular_example_component_css, | ||
}, | ||
}, | ||
}} | ||
src="usage/v7/gestures/double-click/demo.html" | ||
/> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, why was the example changed from using colors to movements?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original PR had both a background (like the pre-PR code) and a transform (unlike the pre-PR code). The author mentioned accessibility concerns, and I agreed. Especially since I wanted the card to have instructions in it, it's now hard to get the text contrast sufficient with different background colors in both light & dark modes, on top of the different background colors potentially being difficult to distinguish from each other. Also, I thought the code was a bit simpler to read having it just increment by n pixels every time instead of having an array of colors to rotate through. I'd be totally fine with changing to something other than
translateX
, but I don't think background color was working well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely agree, thank you for making this change!! I have no issues with
translateX
, it's easy to see the gesture and straight forward code.