-
-
Notifications
You must be signed in to change notification settings - Fork 14
Keyboard List/Menu navigation #310
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,13 @@ | ||
| import React, { FC, forwardRef, InputHTMLAttributes, LegacyRef } from 'react'; | ||
| import React, { | ||
| Children, | ||
| cloneElement, | ||
| FC, | ||
| forwardRef, | ||
| InputHTMLAttributes, | ||
| isValidElement, | ||
| LegacyRef, | ||
| useRef | ||
| } from 'react'; | ||
| import { twMerge } from 'tailwind-merge'; | ||
| import { useComponentTheme } from '@/utils'; | ||
| import { ListTheme } from './ListTheme'; | ||
|
|
@@ -22,14 +31,55 @@ export const List: FC<ListProps & ListRef> = forwardRef< | |
| ListProps | ||
| >(({ className, children, theme: customTheme, ...rest }, ref) => { | ||
| const theme: ListTheme = useComponentTheme('list', customTheme); | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im not sure I agree this should be in List ( or at least not in the base list component ). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not in the List? It is have the same functionality as menu and user should have ability to navigate it in the same way/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, it works only for items with tab index (simplly say which have onClick callback) |
||
| // Remove the first invisible item when navigating with keyboard is started | ||
| if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { | ||
| containerRef.current | ||
| ?.querySelector<HTMLElement>('[data-first-item="true"]') | ||
| ?.remove(); | ||
| } | ||
| const focusableItems = Array.from( | ||
| containerRef.current?.querySelectorAll<HTMLElement>( | ||
| '[tabindex]:not(.disabled)' | ||
| ) || [] | ||
| ); | ||
| const currentIndex = focusableItems.indexOf( | ||
| document.activeElement as HTMLElement | ||
| ); | ||
|
|
||
| if (event.key === 'ArrowDown') { | ||
| event.preventDefault(); | ||
| const nextIndex = (currentIndex + 1) % focusableItems.length; | ||
| focusableItems[nextIndex]?.focus(); | ||
| } else if (event.key === 'ArrowUp') { | ||
| event.preventDefault(); | ||
| const prevIndex = | ||
| (currentIndex - 1 + focusableItems.length) % focusableItems.length; | ||
| focusableItems[prevIndex]?.focus(); | ||
| } else if (event.key === 'Enter') { | ||
| event.preventDefault(); | ||
| (document.activeElement as HTMLElement)?.click(); | ||
| } | ||
| }; | ||
|
|
||
| const focusableChildren = Children.map(children, child => | ||
| isValidElement(child) ? cloneElement(child, { tabIndex: 0 }) : child | ||
| ); | ||
|
|
||
| return ( | ||
| <div | ||
| {...rest} | ||
| ref={ref} | ||
| role="list" | ||
| className={twMerge(theme.base, className)} | ||
| > | ||
| {children} | ||
| <div ref={containerRef} onKeyDown={handleKeyDown}> | ||
| {/* First invisible item which takes focus for correct keyboard navigation*/} | ||
| <div data-first-item="true" tabIndex={0} /> | ||
| <div | ||
| {...rest} | ||
| ref={ref} | ||
| role="list" | ||
| className={twMerge(theme.base, className)} | ||
| > | ||
| {focusableChildren} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }); | ||
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.
Wouldn't escape close the menu all together? I know the ExitListener often is handling this. Just calling this out.
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.
It is closing, but all levels together. But we need to give user ability get back to previous level