Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 12s
Build & Deploy / 🧪 QA (push) Successful in 4m40s
Build & Deploy / 🏗️ Build (push) Failing after 31s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🧪 Smoke Test (push) Has been skipped
Build & Deploy / ⚡ Lighthouse (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
interface BlogPaginationProps {
|
|
currentPage: number;
|
|
totalPages: number;
|
|
locale: string;
|
|
}
|
|
|
|
export function BlogPaginationKeyboardObserver({
|
|
currentPage,
|
|
totalPages,
|
|
locale,
|
|
}: BlogPaginationProps) {
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
const handleKeyDown = (e: KeyboardEvent) => {
|
|
// Don't trigger if user is typing in an input
|
|
if (
|
|
document.activeElement?.tagName === 'INPUT' ||
|
|
document.activeElement?.tagName === 'TEXTAREA' ||
|
|
document.activeElement?.tagName === 'SELECT'
|
|
) {
|
|
return;
|
|
}
|
|
|
|
if (e.key === 'ArrowLeft' && currentPage > 1) {
|
|
router.push(`/${locale}/blog?page=${currentPage - 1}`);
|
|
} else if (e.key === 'ArrowRight' && currentPage < totalPages) {
|
|
router.push(`/${locale}/blog?page=${currentPage + 1}`);
|
|
}
|
|
};
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
|
}, [currentPage, totalPages, locale, router]);
|
|
|
|
return null;
|
|
}
|