fix: mobile UX improvements - scroll restoration, OTP autofill, swipe direction, back button
- ClientLayout: implement proper manual scroll restoration using module-level _wasPopNavigation flag and sessionStorage. Fixes scroll jumping to top/bottom on Back navigation. Scroll position saved on scroll (debounced) and cleanup, restored via double-rAF on popstate navigation. - AuthModal/LoginModal: fix Android back button to push only ONE history sentinel on modal open (not on every subView/step change). Prevents stacking of extra history entries. Back in sub-views re-pushes sentinel for next back press. - AuthModal/LoginModal: remove navigator.credentials.get (WebOTP API) which was showing an intrusive permission modal. The native autoComplete=one-time-code on the first OTP input provides the suggestion bar above the keyboard instead. - AuthModal/LoginModal: add isVerifyingRef guard to prevent double toast on successful login. Both onComplete callback and form submit were calling triggerVerifyOtp/handleVerifyOtpWithCode simultaneously. - AuthModal: remove triggerVerifyOtp from handleOtpChange to eliminate the second duplicate call (only onComplete now triggers verification). - OtpInput5: remove maxLength=1 restriction from first input so browser can inject the full 5-digit OTP code. Multi-char input is already handled by handleDigitChange which distributes digits across all boxes. - ProductPage: fix swipe gesture direction in media gallery. Swipe right -> next item, swipe left -> previous item (matches RTL intuition for Persian users).
This commit is contained in:
1 parent
1d2d141514
commit
86e8101d8c
16 files changed
+10175
-9886
No files matched your search
@@ -21,6 +21,12 @@ import { useUIStore } from "../lib/store/uiStore";
|
||||
import MaintenancePage from "../components/MaintenancePage";
|
||||
import { NavigationTarget } from "../lib/types";
|
||||
|
||||
// Module-level flag: survives across effect re-runs because it lives outside the component.
|
||||
// When the user presses Back/Forward, popstate fires BEFORE pathname changes.
|
||||
// The flag is read (and reset) in the pathname effect which runs AFTER.
|
||||
let _wasPopNavigation = false;
|
||||
|
||||
|
||||
export default function ClientLayout({ children }: { children: React.ReactNode }) {
|
||||
const isCartOpen = useUIStore((state) => state.isCartOpen);
|
||||
const isLoginModalOpen = useUIStore((state) => state.isLoginModalOpen);
|
||||
@@ -52,36 +58,68 @@ export default function ClientLayout({ children }: { children: React.ReactNode }
|
||||
useUserStore.getState().logout();
|
||||
}
|
||||
|
||||
// Preserve scroll restoration on back/forward
|
||||
// Let the browser handle scroll restoration on Back/Forward navigation.
|
||||
// We separately implement push-navigation scroll-to-top below.
|
||||
if (typeof window !== 'undefined' && 'scrollRestoration' in window.history) {
|
||||
window.history.scrollRestoration = 'auto';
|
||||
window.history.scrollRestoration = 'manual';
|
||||
}
|
||||
|
||||
// Permanent listener: set the flag whenever any popstate fires.
|
||||
// This includes modal sentinel pops - those don't change pathname so they
|
||||
// are harmless (the pathname effect won't run, flag stays set until the
|
||||
// next real back navigation that DOES change pathname).
|
||||
const handlePopState = () => { _wasPopNavigation = true; };
|
||||
window.addEventListener('popstate', handlePopState);
|
||||
return () => window.removeEventListener('popstate', handlePopState);
|
||||
}, [fetchProfile, fetchSettings]);
|
||||
|
||||
// Track navigation type (push vs pop) to allow smooth scroll restoration on Back/Forward
|
||||
|
||||
// Scroll-to-top only on PUSH navigation, not on Back/Forward.
|
||||
// _wasPopNavigation is set by the permanent popstate listener above
|
||||
// BEFORE this effect runs for the new pathname.
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
let popstateTriggered = false;
|
||||
const handlePopState = () => {
|
||||
popstateTriggered = true;
|
||||
};
|
||||
|
||||
window.addEventListener('popstate', handlePopState);
|
||||
|
||||
// Give microtask/frame for popstate event to register before checking
|
||||
const timeoutId = setTimeout(() => {
|
||||
if (!popstateTriggered) {
|
||||
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
|
||||
if (_wasPopNavigation) {
|
||||
// Back/Forward: restore the previously saved scroll position.
|
||||
_wasPopNavigation = false;
|
||||
const savedY = sessionStorage.getItem(`__scroll_${pathname}`);
|
||||
if (savedY) {
|
||||
const y = parseInt(savedY, 10);
|
||||
// Double rAF: first waits for React commit, second waits for paint
|
||||
requestAnimationFrame(() =>
|
||||
requestAnimationFrame(() =>
|
||||
window.scrollTo({ top: y, behavior: 'instant' })
|
||||
)
|
||||
);
|
||||
}
|
||||
}, 0);
|
||||
return;
|
||||
}
|
||||
// Push navigation (link click, router.push): scroll to top immediately.
|
||||
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
|
||||
}, [pathname]);
|
||||
|
||||
// Save current scroll position to sessionStorage (debounced + on cleanup)
|
||||
// so it can be restored on Back navigation.
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') return;
|
||||
const key = `__scroll_${pathname}`;
|
||||
let debounceTimer: ReturnType<typeof setTimeout>;
|
||||
const handleScroll = () => {
|
||||
clearTimeout(debounceTimer);
|
||||
debounceTimer = setTimeout(() => {
|
||||
sessionStorage.setItem(key, String(window.scrollY));
|
||||
}, 100);
|
||||
};
|
||||
window.addEventListener('scroll', handleScroll, { passive: true });
|
||||
return () => {
|
||||
window.removeEventListener('popstate', handlePopState);
|
||||
clearTimeout(timeoutId);
|
||||
clearTimeout(debounceTimer);
|
||||
// Save immediately on exit (before pathname changes)
|
||||
sessionStorage.setItem(key, String(window.scrollY));
|
||||
window.removeEventListener('scroll', handleScroll);
|
||||
};
|
||||
}, [pathname]);
|
||||
|
||||
|
||||
// Check Maintenance Mode (Admin / Partner bypass)
|
||||
const isMaintenanceMode = getText('MAINTENANCE_MODE', 'false') === 'true' || getText('maintenance_mode', 'false') === 'true';
|
||||
const isAdmin = role === 'User_Partner' || (typeof window !== 'undefined' && Boolean(localStorage.getItem('adminToken')));
|
||||
|
||||
Reference in new issue
Block a user