Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 5d70226

Browse files
fix: improve swipe and scroll code handling on mobile devices
1 parent 829bf78 commit 5d70226

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

src/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export namespace Components {
5555
'anchor'?: string;
5656
'anchorZoom'?: string;
5757
'hideAnchor'?: boolean;
58+
'onScrolling'?: (event: CustomEvent<void>) => void;
5859
'onSlideDidLoad'?: (event: CustomEvent<void>) => void;
5960
'srcFile'?: string;
6061
}

src/components/deck/deckdeckgo-deck/deckdeckgo-deck.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export class DeckdeckgoDeck {
2222

2323
private startX: number = null;
2424
private deckTranslateX: number = 0;
25-
private autoSwipeRatio: number = 4;
25+
private autoSwipeRatio: number = 10;
26+
27+
private blockSlide: boolean = false;
2628

2729
@State()
2830
private activeIndex: number = 0;
@@ -103,6 +105,11 @@ export class DeckdeckgoDeck {
103105
this.startX = null;
104106
}
105107

108+
@Listen('scrolling')
109+
scrolling() {
110+
this.blockSlide = true;
111+
}
112+
106113
private unify(e) {
107114
return e.changedTouches ? e.changedTouches[0] : e;
108115
}
@@ -113,6 +120,10 @@ export class DeckdeckgoDeck {
113120

114121
private async move(e: Event) {
115122

123+
if (this.blockSlide) {
124+
return;
125+
}
126+
116127
const deltaX: DeltaX = await this.getDeltaX(e);
117128

118129
if (!deltaX) {
@@ -126,6 +137,11 @@ export class DeckdeckgoDeck {
126137
}
127138

128139
private async stop(e: Event) {
140+
if (this.blockSlide) {
141+
this.blockSlide = false;
142+
return;
143+
}
144+
129145
const deltaX: DeltaX = await this.getDeltaX(e);
130146

131147
await this.swipeSlide(deltaX);

src/components/slides/deckdeckgo-slide-code/deckdeckgo-slide-code.tsx

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import {Component, Element, Event, EventEmitter, Method, Prop, State} from '@ste
22

33
import {DeckdeckgoSlide, DeckDeckGoSlideUtils} from '../deckdeckgo-slide';
44

5+
enum DeckdeckgoSlideCodeAction {
6+
SWIPE,
7+
SCROLL
8+
}
9+
510
@Component({
611
tag: 'deckgo-slide-code',
712
styleUrl: 'deckdeckgo-slide-code.scss',
@@ -13,6 +18,8 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
1318

1419
@Event() slideDidLoad: EventEmitter<void>;
1520

21+
@Event() scrolling: EventEmitter<void>;
22+
1623
@Prop() srcFile: string;
1724

1825
@Prop() anchor: string = '// DeckDeckGo';
@@ -24,6 +31,10 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
2431
@State()
2532
private code: string[] = [];
2633

34+
private startX: number = null;
35+
private detectThreshold: number = 10;
36+
private action: DeckdeckgoSlideCodeAction = null;
37+
2738
async componentDidLoad() {
2839
this.slideDidLoad.emit();
2940

@@ -134,12 +145,61 @@ export class DeckdeckgoSlideCode implements DeckdeckgoSlide {
134145

135146
// DeckDeckGo
136147
render() {
137-
return <div class="deckgo-slide">
148+
return <div class="deckgo-slide"
149+
onTouchStart={(event: TouchEvent) => this.touchScrollStart(event)}
150+
onTouchMove={(event: TouchEvent) => this.touchScrollMove(event)}
151+
onTouchEnd={() => this.touchScrollEnd()}>
138152
<slot name="title"></slot>
139-
<div class="deckgo-code-container"><code>{this.renderCode()}</code></div>
153+
<div class="deckgo-code-container" onScroll={() => this.emitScrolling()}><code>{this.renderCode()}</code></div>
140154
</div>;
141155
}
142156

157+
private touchScrollStart(event: TouchEvent) {
158+
this.startX = event.changedTouches ? event.changedTouches[0].clientX : null;
159+
}
160+
161+
private touchScrollMove(event: TouchEvent) {
162+
if (this.action) {
163+
return;
164+
}
165+
166+
const currentX: number = event.changedTouches ? event.changedTouches[0].clientX : null;
167+
168+
const swipeLeft: boolean = this.startX > currentX + this.detectThreshold;
169+
const swipeRight: boolean = this.startX < currentX - this.detectThreshold;
170+
171+
this.action = swipeLeft || swipeRight ? DeckdeckgoSlideCodeAction.SWIPE : DeckdeckgoSlideCodeAction.SCROLL;
172+
173+
if (!swipeLeft && !swipeRight) {
174+
this.scrolling.emit();
175+
this.unlockScroll();
176+
} else {
177+
this.lockScroll();
178+
}
179+
}
180+
181+
private touchScrollEnd() {
182+
this.action = null;
183+
184+
this.unlockScroll();
185+
}
186+
187+
private lockScroll() {
188+
const container: HTMLElement = this.el.shadowRoot.querySelector('div.deckgo-code-container');
189+
container.style.setProperty('overflow-y', 'hidden');
190+
}
191+
192+
private unlockScroll() {
193+
const container: HTMLElement = this.el.shadowRoot.querySelector('div.deckgo-code-container');
194+
container.style.removeProperty('overflow-y');
195+
}
196+
197+
private emitScrolling() {
198+
if (this.action === DeckdeckgoSlideCodeAction.SCROLL) {
199+
this.scrolling.emit();
200+
}
201+
}
202+
143203
private renderCode() {
144204
return (
145205
this.code.map((line: string) => {

0 commit comments

Comments
 (0)