fix(ui): drag slider only via arrows handle on phones and make whole trendy bento card clickable

This commit is contained in:
2026-11-05 13:10:05 +05:30
parent 3d147dd3de
commit ee518ff451
5 changed files with 133 additions and 135 deletions
+28 -22
View File
@@ -73,10 +73,11 @@ export function CardBeforeAfterSlider({
}
}, [setSplit]);
// Unified Pointer Handlers (Flawless on iOS/Android touchscreens and desktop mouse)
// Unified Pointer Handlers on Center Pill Handle with Left/Right Arrows
const handlePointerDown = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
if (e.button !== 0 && e.pointerType === 'mouse') return;
e.stopPropagation();
isDraggingRef.current = true;
isInteractedRef.current = true;
@@ -88,13 +89,13 @@ export function CardBeforeAfterSlider({
// Fallback
}
// Cache rect on pointer down to eliminate layout thrashing
const rect = e.currentTarget.getBoundingClientRect();
cachedRectRef.current = { left: rect.left, width: rect.width };
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSplit(percent);
if (containerRef.current) {
const rect = containerRef.current.getBoundingClientRect();
cachedRectRef.current = { left: rect.left, width: rect.width };
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSplit(percent);
}
},
[setSplit]
);
@@ -102,6 +103,7 @@ export function CardBeforeAfterSlider({
const handlePointerMove = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
if (!isDraggingRef.current) return;
e.stopPropagation();
const clientX = e.clientX;
if (rafIdRef.current) {
@@ -127,6 +129,7 @@ export function CardBeforeAfterSlider({
const handlePointerUp = useCallback((e: React.PointerEvent<HTMLDivElement>) => {
if (!isDraggingRef.current) return;
e.stopPropagation();
isDraggingRef.current = false;
cachedRectRef.current = null;
@@ -147,19 +150,11 @@ export function CardBeforeAfterSlider({
ref={containerRef}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
role="slider"
aria-label="Before and after transformation slider"
aria-valuenow={50}
tabIndex={0}
style={{
'--split': '50%',
touchAction: 'none', // Strictly prevents mobile touch scrolling interference
touchAction: 'pan-y', // Allows mobile vertical scrolling across the card and line
} as React.CSSProperties}
className={`relative w-full ${aspectRatio} overflow-hidden cursor-ew-resize select-none bg-slate-900 group/slider transition-shadow duration-300 hover:shadow-cream-lg touch-none`}
className={`relative w-full ${aspectRatio} overflow-hidden select-none bg-slate-900 group/slider transition-shadow duration-300 hover:shadow-cream-lg`}
>
{/* Layer 1: After Image (Transformed Neural Result) */}
{/* eslint-disable-next-line @next/next/no-img-element */}
@@ -186,7 +181,7 @@ export function CardBeforeAfterSlider({
/>
</div>
{/* Layer 3: Vertical Split Divider Line */}
{/* Layer 3: Vertical Split Divider Line (pointer-events-none so it doesn't block scrolling) */}
<div
className="absolute top-0 bottom-0 w-[2.5px] bg-white z-20 pointer-events-none shadow-[0_0_10px_rgba(0,0,0,0.8)] -translate-x-1/2"
style={{ left: 'var(--split)' }}
@@ -194,9 +189,20 @@ export function CardBeforeAfterSlider({
{/* Glowing Aura */}
<div className="absolute -top-4 -bottom-4 -left-1.5 w-3.5 bg-gradient-to-r from-blue-500/30 via-white/70 to-amber-400/30 blur-[2px]" />
{/* Center Pill Handle with Left/Right Arrows & Touch Pad */}
<div className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-9 h-9 sm:w-10 sm:h-10 rounded-full bg-white text-slate-800 shadow-xl border border-slate-900/15 flex items-center justify-center transition-transform group-hover/slider:scale-110 before:absolute before:-inset-3 before:content-['']">
<svg className="w-3.5 h-3.5 text-blue-700" fill="none" stroke="currentColor" viewBox="0 0 24 24">
{/* Center Pill Handle with Left/Right Arrows - Dedicated Drag Trigger with Pointer Capture */}
<div
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
role="slider"
aria-label="Before and after transformation slider"
aria-valuenow={50}
tabIndex={0}
style={{ touchAction: 'none' }}
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-9 h-9 sm:w-10 sm:h-10 rounded-full bg-white text-slate-800 shadow-xl border border-slate-900/15 flex items-center justify-center transition-transform group-hover/slider:scale-110 before:absolute before:-inset-3 before:content-[''] pointer-events-auto cursor-ew-resize touch-none"
>
<svg className="w-3.5 h-3.5 text-blue-700 pointer-events-none" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2.5} d="M8 9l-3 3 3 3m8-6l3 3-3 3" />
</svg>
</div>
+32 -28
View File
@@ -82,32 +82,32 @@ export function Cinematic3DComparison({
};
}, [autoSlide, isDragging, updatePosition]);
// Unified Pointer Handlers (Works with mouse on desktop + touches/pens on mobile/iOS/tablets)
// Unified Pointer Handlers on the Slider Arrows Handle
const handlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
if (e.button !== 0 && e.pointerType === 'mouse') return;
e.stopPropagation();
isPointerDownRef.current = true;
setIsDragging(true);
// Capture pointer so fast drags outside the element bounds never lose tracking
try {
e.currentTarget.setPointerCapture(e.pointerId);
} catch {
// Fallback
}
// Cache rect on pointer down to eliminate layout thrashing during drag
const rect = e.currentTarget.getBoundingClientRect();
cachedRectRef.current = { left: rect.left, width: rect.width };
// Instantly update to pointer position
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
updatePosition(percent);
if (containerRef.current) {
const rect = containerRef.current.getBoundingClientRect();
cachedRectRef.current = { left: rect.left, width: rect.width };
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
updatePosition(percent);
}
};
const handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
const clientX = e.clientX;
if (rafIdRef.current) {
@@ -131,6 +131,7 @@ export function Cinematic3DComparison({
const handlePointerUp = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
isPointerDownRef.current = false;
cachedRectRef.current = null;
setIsDragging(false);
@@ -163,24 +164,13 @@ export function Cinematic3DComparison({
return (
<div className={`w-full perspective-1400 select-none ${className}`}>
{/* 3D Kinetic Tilt Stage with Locked Touch-Action for Flawless Mobile Swiping */}
{/* 3D Kinetic Tilt Stage - Allows smooth page scrolling when touching anywhere except the arrows handle */}
<div
ref={containerRef}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
onKeyDown={handleKeyDown}
role="slider"
aria-label="Before and after comparison"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={50}
tabIndex={0}
className={`relative w-full ${aspectRatio} rounded-2xl sm:rounded-3xl overflow-hidden cursor-ew-resize preserve-3d transition-transform duration-150 ease-out shadow-cinematic-3d hover:shadow-cinematic-hover border border-slate-900/[0.08] bg-slate-950 touch-none`}
className={`relative w-full ${aspectRatio} rounded-2xl sm:rounded-3xl overflow-hidden preserve-3d transition-transform duration-150 ease-out shadow-cinematic-3d hover:shadow-cinematic-hover border border-slate-900/[0.08] bg-slate-950`}
style={{
'--split': '50%',
touchAction: 'none', // Strictly prevents iOS/Android touch scrolling conflict on the slider
touchAction: 'pan-y', // Allows mobile vertical scrolling across the image and line
} as React.CSSProperties}
>
{/* Layer 1: After Image (Full Reconstructed Neural Result) */}
@@ -208,7 +198,7 @@ export function Cinematic3DComparison({
/>
</div>
{/* Layer 3: Illuminated Holographic Laser Split Line */}
{/* Layer 3: Illuminated Holographic Laser Split Line (pointer-events-none so it doesn't block scrolling) */}
<div
className="absolute top-0 bottom-0 w-[2px] bg-gradient-to-b from-blue-400 via-white to-amber-300 z-20 pointer-events-none -translate-x-1/2"
style={{ left: 'var(--split)' }}
@@ -216,10 +206,24 @@ export function Cinematic3DComparison({
{/* Vertical Caustic Glow Beam */}
<div className="absolute -top-4 -bottom-4 -left-1.5 w-3.5 bg-gradient-to-r from-blue-500/20 via-white/40 to-amber-400/20 blur-[3px]" />
{/* 3D Tactile Center Control Handle with generous touch-target area */}
{/* 3D Tactile Center Control Handle with Arrows - Dedicated Drag Trigger with Pointer Capture */}
<div
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-10 h-10 sm:w-11 sm:h-11 rounded-full bg-white/95 backdrop-blur-md border border-slate-900/10 shadow-2xl flex items-center justify-center cursor-ew-resize hover:scale-110 active:scale-95 transition-transform duration-150 preserve-3d before:absolute before:-inset-3 before:content-['']"
style={{ transform: 'translateZ(28px)' }}
onPointerDown={handlePointerDown}
onPointerMove={handlePointerMove}
onPointerUp={handlePointerUp}
onPointerCancel={handlePointerUp}
onKeyDown={handleKeyDown}
role="slider"
aria-label="Before and after comparison slider"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={50}
tabIndex={0}
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-10 h-10 sm:w-11 sm:h-11 rounded-full bg-white/95 backdrop-blur-md border border-slate-900/10 shadow-2xl flex items-center justify-center cursor-ew-resize hover:scale-110 active:scale-95 transition-transform duration-150 preserve-3d before:absolute before:-inset-3 before:content-[''] pointer-events-auto touch-none"
style={{
transform: 'translateZ(28px)',
touchAction: 'none', // Strictly locks drag on the arrow handle only
}}
>
{/* Outer Metallic Ring */}
<div className="absolute inset-0 rounded-full border border-blue-600/30 shadow-[0_0_12px_rgba(37,99,235,0.3)]" />