fix(cursor): completely remove liquid glass lens on phones and tablets

This commit is contained in:
2026-11-05 12:19:12 +05:30
parent 583a1fe5a0
commit 001628e233
+40 -96
View File
@@ -1,21 +1,17 @@
'use client';
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
/**
* LiquidGlassLensCursor:
* Pure Optical Liquid Glass Lens cursor with 100% transparent crystal body,
* sharp specular water-drop glints, dynamic velocity squash & stretch,
* and universal responsiveness across all devices (Desktop, Mobile, Tablet, Trackpad).
*
* Key Capabilities:
* - Ultra-transparent liquid glass (Zero frosting, zero milky haze, pure optical clarity)
* - Sharp meniscus rim & caustic reflections matching brand theme (#2563EB, #06B6D4)
* - True multi-device responsiveness: Mouse on desktop + fluid touch lens on mobile/tablets
* - Hardware-accelerated GPU transforms (locked 60-120fps, zero layout reflows)
* - Strictly pointer-events-none: never blocks taps, clicks, scrolls, or dragging
* sharp specular water-drop glints, dynamic velocity squash & stretch.
*
* Exclusively active on Desktop & Laptop computers with precise mouse / trackpad pointers.
* Completely disabled and removed on Phones, Tablets, and Touchscreen devices.
*/
export function SmoothCursor() {
const [isEnabled, setIsEnabled] = useState(false);
const lensRef = useRef<HTMLDivElement>(null);
const dotRef = useRef<HTMLDivElement>(null);
const causticRef = useRef<HTMLDivElement>(null);
@@ -23,6 +19,28 @@ export function SmoothCursor() {
useEffect(() => {
if (typeof window === 'undefined') return;
// Detect phones, tablets, or touchscreen devices
const isMobileOrTablet =
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Tablet/i.test(navigator.userAgent) ||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches) ||
(window.matchMedia && !window.matchMedia('(hover: hover)').matches) ||
('ontouchstart' in window && window.innerWidth < 1024) ||
(navigator.maxTouchPoints > 0 && window.innerWidth < 1024);
const hasFinePointer =
window.matchMedia && window.matchMedia('(hover: hover) and (pointer: fine)').matches;
// Completely disable and remove on phones and tablets
if (isMobileOrTablet || !hasFinePointer) {
return;
}
setIsEnabled(true);
}, []);
useEffect(() => {
if (!isEnabled) return;
const lens = lensRef.current;
const dot = dotRef.current;
const caustic = causticRef.current;
@@ -42,16 +60,12 @@ export function SmoothCursor() {
let isHovered = false;
let isPressed = false;
let isVisible = false;
let isTouchActive = false;
let fadeTimeout: NodeJS.Timeout | null = null;
let animId = 0;
// 1. Mouse & Pointer Movement (Desktop & Trackpads)
// Desktop Mouse & Pointer Movement
const onMouseMove = (e: MouseEvent) => {
isTouchActive = false;
mouseX = e.clientX;
mouseY = e.clientY;
if (fadeTimeout) clearTimeout(fadeTimeout);
if (!isVisible) {
isVisible = true;
@@ -62,7 +76,6 @@ export function SmoothCursor() {
};
const onMouseLeave = () => {
if (isTouchActive) return;
isVisible = false;
lens.style.opacity = '0';
dot.style.opacity = '0';
@@ -70,7 +83,6 @@ export function SmoothCursor() {
};
const onMouseEnter = () => {
if (isTouchActive) return;
isVisible = true;
lens.style.opacity = '1';
dot.style.opacity = '1';
@@ -85,63 +97,6 @@ export function SmoothCursor() {
isPressed = false;
};
// 2. Touch Handlers (Full Mobile, Tablet, iPad & Touchscreen Compatibility)
const onTouchStart = (e: TouchEvent) => {
try {
if (!e.touches || e.touches.length === 0) return;
isTouchActive = true;
if (fadeTimeout) clearTimeout(fadeTimeout);
const touch = e.touches[0];
if (!touch) return;
mouseX = touch.clientX;
mouseY = touch.clientY;
// If opening fresh, snap directly to finger touch point
if (!isVisible) {
lensX = mouseX;
lensY = mouseY;
prevMouseX = mouseX;
prevMouseY = mouseY;
isVisible = true;
lens.style.opacity = '1';
if (caustic) caustic.style.opacity = '1';
}
// On touch, hide the tiny dot (the finger is right there)
dot.style.opacity = '0';
isPressed = true;
} catch {
// Safe failover
}
};
const onTouchMove = (e: TouchEvent) => {
try {
if (!e.touches || e.touches.length === 0) return;
const touch = e.touches[0];
if (!touch) return;
mouseX = touch.clientX;
mouseY = touch.clientY;
} catch {
// Safe failover
}
};
const onTouchEnd = () => {
try {
isPressed = false;
// Graceful liquid fade out after touch interaction completes
if (fadeTimeout) clearTimeout(fadeTimeout);
fadeTimeout = setTimeout(() => {
isVisible = false;
if (lens) lens.style.opacity = '0';
if (caustic) caustic.style.opacity = '0';
}, 350);
} catch {
// Safe failover
}
};
// Throttled interactive element detection for magnetic lens scaling
let lastCheck = 0;
const onMouseOver = (e: MouseEvent) => {
@@ -160,7 +115,6 @@ export function SmoothCursor() {
// High performance RAF loop (locked 60-120fps, 100% GPU composited)
const loop = () => {
// Calculate velocity vector of pointer movement
const vx = mouseX - prevMouseX;
const vy = mouseY - prevMouseY;
prevMouseX = mouseX;
@@ -195,16 +149,13 @@ export function SmoothCursor() {
lens.style.transform = `translate3d(${lensX}px, ${lensY}px, 0) translate3d(-50%, -50%, 0) scale(${baseScale})`;
}
// Precision pinpoint dot (Desktop only, hidden on touch or hover)
if (!isTouchActive) {
const dotScale = isHovered ? 0 : (isPressed ? 0.7 : 1);
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`;
}
// Precision pinpoint dot
const dotScale = isHovered ? 0 : (isPressed ? 0.7 : 1);
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`;
animId = requestAnimationFrame(loop);
};
// Desktop Pointer Listeners
window.addEventListener('mousemove', onMouseMove, { passive: true });
window.addEventListener('mouseleave', onMouseLeave);
window.addEventListener('mouseenter', onMouseEnter);
@@ -212,33 +163,26 @@ export function SmoothCursor() {
window.addEventListener('mouseup', onMouseUp);
document.addEventListener('mouseover', onMouseOver, { passive: true });
// Touch Device Listeners
window.addEventListener('touchstart', onTouchStart, { passive: true });
window.addEventListener('touchmove', onTouchMove, { passive: true });
window.addEventListener('touchend', onTouchEnd, { passive: true });
window.addEventListener('touchcancel', onTouchEnd, { passive: true });
animId = requestAnimationFrame(loop);
return () => {
cancelAnimationFrame(animId);
if (fadeTimeout) clearTimeout(fadeTimeout);
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseleave', onMouseLeave);
window.removeEventListener('mouseenter', onMouseEnter);
window.removeEventListener('mousedown', onMouseDown);
window.removeEventListener('mouseup', onMouseUp);
document.removeEventListener('mouseover', onMouseOver);
window.removeEventListener('touchstart', onTouchStart);
window.removeEventListener('touchmove', onTouchMove);
window.removeEventListener('touchend', onTouchEnd);
window.removeEventListener('touchcancel', onTouchEnd);
};
}, []);
}, [isEnabled]);
// Completely removed on phones and tablets
if (!isEnabled) {
return null;
}
return (
<div className="pointer-events-none fixed inset-0 z-[99999] overflow-hidden select-none">
<div className="pointer-events-none fixed inset-0 z-[99999] overflow-hidden select-none hidden md:block">
{/* 1. Ambient Caustic Aura (Fluid blue light dispersion) */}
<div
ref={causticRef}
@@ -252,7 +196,7 @@ export function SmoothCursor() {
{/* 2. Pure Liquid Glass Lens (100% Crystal Transparent Body, Sharp Specular Glints, Zero Frosting) */}
<div
ref={lensRef}
className="fixed top-0 left-0 w-11 h-11 sm:w-12 sm:h-12 rounded-full pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden] transition-opacity duration-200"
className="fixed top-0 left-0 w-12 h-12 rounded-full pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden] transition-opacity duration-200"
style={{
// Optical clear glass: ZERO blur (eliminates all frosted haze) + optical contrast & saturation pop
backdropFilter: 'contrast(1.16) brightness(1.05) saturate(1.28)',