Skip to content

Commit 23a1ecb

Browse files
Copilotbrunoborges
andcommitted
Add swipe and tap support for mobile card interactions
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent c8ba932 commit 23a1ecb

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

app.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,71 @@
181181
const isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
182182
if (!isTouchDevice) return;
183183

184+
// Update hover hints for touch devices
185+
document.querySelectorAll('.hover-hint').forEach(hint => {
186+
hint.textContent = '👆 tap or swipe →';
187+
});
188+
184189
document.querySelectorAll('.tip-card').forEach(card => {
190+
let touchStartX = 0;
191+
let touchStartY = 0;
192+
let touchEndX = 0;
193+
let touchEndY = 0;
194+
let isSwiping = false;
195+
196+
// Track touch start
197+
card.addEventListener('touchstart', (e) => {
198+
// Only track touches on the card-code area
199+
if (!e.target.closest('.card-code')) return;
200+
201+
touchStartX = e.changedTouches[0].screenX;
202+
touchStartY = e.changedTouches[0].screenY;
203+
isSwiping = false;
204+
}, { passive: true });
205+
206+
// Track touch move to detect swipe
207+
card.addEventListener('touchmove', (e) => {
208+
if (!e.target.closest('.card-code')) return;
209+
isSwiping = true;
210+
}, { passive: true });
211+
212+
// Handle touch end for swipe or tap
213+
card.addEventListener('touchend', (e) => {
214+
// Only handle touches on the card-code area
215+
if (!e.target.closest('.card-code')) return;
216+
217+
touchEndX = e.changedTouches[0].screenX;
218+
touchEndY = e.changedTouches[0].screenY;
219+
220+
const deltaX = touchEndX - touchStartX;
221+
const deltaY = touchEndY - touchStartY;
222+
const absDeltaX = Math.abs(deltaX);
223+
const absDeltaY = Math.abs(deltaY);
224+
225+
// Determine if it's a swipe (horizontal movement > 50px and more horizontal than vertical)
226+
const isHorizontalSwipe = absDeltaX > 50 && absDeltaX > absDeltaY;
227+
228+
if (isHorizontalSwipe) {
229+
// Swipe left = show modern, swipe right = show old
230+
if (deltaX < 0) {
231+
// Swipe left - show modern
232+
card.classList.add('toggled');
233+
} else {
234+
// Swipe right - show old
235+
card.classList.remove('toggled');
236+
}
237+
} else if (!isSwiping) {
238+
// It's a tap (no significant movement)
239+
card.classList.toggle('toggled');
240+
}
241+
}, { passive: true });
242+
243+
// Prevent click events from propagating when on touch devices
244+
// to avoid navigation when toggling
185245
card.addEventListener('click', (e) => {
186-
// Don't toggle if clicking a link inside the card
187-
if (e.target.closest('a')) return;
188-
card.classList.toggle('toggled');
246+
if (e.target.closest('.card-code')) {
247+
e.preventDefault();
248+
}
189249
});
190250
});
191251
};

styles.css

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,13 @@ nav {
478478
overflow-y: auto;
479479
}
480480

481-
.tip-card:hover .old-layer {
481+
.tip-card:hover .old-layer,
482+
.tip-card.toggled .old-layer {
482483
opacity: 0;
483484
}
484485

485-
.tip-card:hover .modern-layer {
486+
.tip-card:hover .modern-layer,
487+
.tip-card.toggled .modern-layer {
486488
opacity: 1;
487489
}
488490

@@ -523,7 +525,8 @@ nav {
523525
transition: opacity 0.3s;
524526
}
525527

526-
.tip-card:hover .hover-hint {
528+
.tip-card:hover .hover-hint,
529+
.tip-card.toggled .hover-hint {
527530
opacity: 0;
528531
}
529532

0 commit comments

Comments
 (0)