-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add floating docs navigation shortcuts #4632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yash-kurosaki
wants to merge
1
commit into
asyncapi:master
Choose a base branch
from
Yash-kurosaki:feature/docs-nav-shortcuts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]); | ||
|
|
||
| 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> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refine
useEffectforconsistent-return+ padding rules and clearer cleanupThe early
returnplus returning a cleanup function in another path is triggering theconsistent-returnwarning, and the linter also wants a blank line before the cleanupreturn. 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
🧰 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