Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/layout/DocsLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import ArrowRight from '../icons/ArrowRight';
import IconMenuCenter from '../icons/CenterMenu';
import DocsMobileMenu from '../navigation/DocsMobileMenu';
import DocsNavWrapper from '../navigation/DocsNavWrapper';
import DocsNavigationShortcuts from '../navigation/DocsNavigationShortcuts';
import TOC from '../TOC';
import Heading from '../typography/Heading';

Expand Down Expand Up @@ -126,6 +127,7 @@ export default function DocsLayout({ post, navItems = {}, children }: IDocsLayou
{sidebar}
<div className='flex w-0 max-w-full flex-1 flex-col lg:max-w-(screen-16)'>
<main className='relative z-0 pb-6 pt-2 focus:outline-none md:py-6' tabIndex={0}>
<DocsNavigationShortcuts post={post} />
{!showMenu && (
<div className='lg:hidden'>
<button
Expand Down
89 changes: 89 additions & 0 deletions components/navigation/DocsNavigationShortcuts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useEffect, useMemo } from 'react';

import type { IDocs } from '@/types/post';

import ArrowLeft from '../icons/ArrowLeft';
import ArrowRight from '../icons/ArrowRight';

interface IDocsNavigationShortcutsProps {
post: IDocs[number];
}

const INTERACTIVE_TAGS = new Set(['input', 'textarea', 'select', 'button']);

export default function DocsNavigationShortcuts({ post }: IDocsNavigationShortcutsProps) {
const router = useRouter();

const navigationTargets = useMemo(
() => ({
prev: post?.prevPage?.href,
next: post?.nextPage?.href,
}),
[post?.prevPage?.href, post?.nextPage?.href],
);

useEffect(() => {
if (!navigationTargets.prev && !navigationTargets.next) return;

function handleKeyDown(event: KeyboardEvent) {
if (event.defaultPrevented) return;
if (event.metaKey || event.ctrlKey || event.altKey) return;
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;

const target = event.target as HTMLElement | null;
const tagName = target?.tagName?.toLowerCase();
const isEditable = target?.isContentEditable;

if (isEditable || (tagName && INTERACTIVE_TAGS.has(tagName))) return;

if (event.key === 'ArrowLeft' && navigationTargets.prev) {
router.push(navigationTargets.prev);
event.preventDefault();
} else if (event.key === 'ArrowRight' && navigationTargets.next) {
router.push(navigationTargets.next);
event.preventDefault();
}
}

window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [navigationTargets, router]);
Comment on lines +27 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Refine useEffect for consistent-return + padding rules and clearer cleanup

The early return plus returning a cleanup function in another path is triggering the consistent-return warning, and the linter also wants a blank line before the cleanup return. You can address both and make the cleanup more explicit like this:

  useEffect(() => {
-    if (!navigationTargets.prev && !navigationTargets.next) return;
+    if (!navigationTargets.prev && !navigationTargets.next) {
+      return undefined;
+    }
@@
-    window.addEventListener('keydown', handleKeyDown);
-    return () => window.removeEventListener('keydown', handleKeyDown);
+    window.addEventListener('keydown', handleKeyDown);
+
+    return () => {
+      window.removeEventListener('keydown', handleKeyDown);
+    };
   }, [navigationTargets, router]);

This keeps behavior the same while satisfying the lint rules and making the lifecycle a bit clearer.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
if (!navigationTargets.prev && !navigationTargets.next) return;
function handleKeyDown(event: KeyboardEvent) {
if (event.defaultPrevented) return;
if (event.metaKey || event.ctrlKey || event.altKey) return;
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
const target = event.target as HTMLElement | null;
const tagName = target?.tagName?.toLowerCase();
const isEditable = target?.isContentEditable;
if (isEditable || (tagName && INTERACTIVE_TAGS.has(tagName))) return;
if (event.key === 'ArrowLeft' && navigationTargets.prev) {
router.push(navigationTargets.prev);
event.preventDefault();
} else if (event.key === 'ArrowRight' && navigationTargets.next) {
router.push(navigationTargets.next);
event.preventDefault();
}
}
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [navigationTargets, router]);
useEffect(() => {
if (!navigationTargets.prev && !navigationTargets.next) {
return undefined;
}
function handleKeyDown(event: KeyboardEvent) {
if (event.defaultPrevented) return;
if (event.metaKey || event.ctrlKey || event.altKey) return;
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
const target = event.target as HTMLElement | null;
const tagName = target?.tagName?.toLowerCase();
const isEditable = target?.isContentEditable;
if (isEditable || (tagName && INTERACTIVE_TAGS.has(tagName))) return;
if (event.key === 'ArrowLeft' && navigationTargets.prev) {
router.push(navigationTargets.prev);
event.preventDefault();
} else if (event.key === 'ArrowRight' && navigationTargets.next) {
router.push(navigationTargets.next);
event.preventDefault();
}
}
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [navigationTargets, router]);
🧰 Tools
🪛 GitHub Actions: PR testing - if Node project

[warning] 30-30: Missing JSDoc comment.


[warning] 51-51: Padding and formatting rule: Expected blank line before this statement.


[warning] 51-51: Consistent return: Arrow function should return a value or explicitly return null.

🤖 Prompt for AI Agents
In components/navigation/DocsNavigationShortcuts.tsx around lines 27 to 52, the
effect currently early-returns in one path and returns a cleanup function in
another which trips the consistent-return rule and the linter wants a blank line
before the cleanup return; change the early exit to return an explicit no-op
cleanup (e.g., return () => {}), add a blank line immediately before the final
return that removes the event listener, and keep the rest of the handler logic
intact so the effect always returns a function and the cleanup is clearly
separated.


if (!post?.prevPage && !post?.nextPage) return null;

const buttonBaseStyles =
'pointer-events-auto inline-flex max-w-xs items-center gap-2 rounded-full border border-gray-200 bg-white/90 px-3 py-2 text-sm font-medium text-gray-700 shadow focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-secondary-500 hover:border-secondary-200 hover:text-secondary-600';

return (
<div className='pointer-events-none fixed inset-y-0 left-0 right-0 z-30 hidden md:flex md:items-center md:justify-between md:px-4 lg:px-8'>
<div className='flex-1'>
{post?.prevPage && (
<Link
href={post.prevPage.href}
className={`${buttonBaseStyles} justify-start`}
aria-label={`Previous documentation page: ${post.prevPage.title}`}
>
<ArrowLeft className='size-4' />
<span className='hidden lg:inline'>{post.prevPage.title}</span>
<span className='lg:hidden'>Previous</span>
</Link>
)}
</div>
<div className='flex-1 text-right'>
{post?.nextPage && (
<Link
href={post.nextPage.href}
className={`${buttonBaseStyles} justify-end`}
aria-label={`Next documentation page: ${post.nextPage.title}`}
>
<span className='hidden lg:inline'>{post.nextPage.title}</span>
<span className='lg:hidden'>Next</span>
<ArrowRight className='size-4' />
</Link>
)}
</div>
</div>
);
}
Loading