Nicolium: language dropdown keyboard handling
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -73,7 +73,11 @@ const DropdownMenuContent: React.FC<IDropdownMenuContent> = ({
|
||||
() => (e: KeyboardEvent) => {
|
||||
if (!ref.current) return;
|
||||
|
||||
const elements = Array.from(ref.current.querySelectorAll('a, button'));
|
||||
const elements = Array.from(
|
||||
ref.current.querySelectorAll<HTMLElement>(
|
||||
'a, button:not([disabled]), input:not([disabled])',
|
||||
),
|
||||
).filter((element) => !element.hasAttribute('aria-hidden'));
|
||||
const index = elements.indexOf(document.activeElement as any);
|
||||
|
||||
let element = null;
|
||||
@ -119,7 +123,7 @@ const DropdownMenuContent: React.FC<IDropdownMenuContent> = ({
|
||||
}
|
||||
|
||||
if (element) {
|
||||
(element as HTMLAnchorElement).focus();
|
||||
(element as HTMLElement).focus();
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ const getLanguageDropdown =
|
||||
);
|
||||
|
||||
const node = useRef<HTMLDivElement>(null);
|
||||
const focusedItem = useRef<HTMLButtonElement>(null);
|
||||
const input = useRef<HTMLInputElement>(null);
|
||||
|
||||
const [searchValue, setSearchValue] = useState('');
|
||||
|
||||
@ -156,9 +156,13 @@ const getLanguageDropdown =
|
||||
handleMenuClose();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!language) input.current?.focus();
|
||||
}, [input.current]);
|
||||
|
||||
useEffect(() => {
|
||||
if (node.current) {
|
||||
(node.current?.querySelector('div[aria-selected=true]') as HTMLDivElement)?.focus();
|
||||
(node.current?.querySelector('button[aria-selected=true]') as HTMLButtonElement)?.focus();
|
||||
}
|
||||
}, [node.current]);
|
||||
|
||||
@ -173,6 +177,7 @@ const getLanguageDropdown =
|
||||
<span>{intl.formatMessage(messages.search)}</span>
|
||||
|
||||
<Input
|
||||
ref={input}
|
||||
className='w-64'
|
||||
type='text'
|
||||
value={searchValue}
|
||||
@ -199,7 +204,7 @@ const getLanguageDropdown =
|
||||
/>
|
||||
</button>
|
||||
</label>
|
||||
<div className='⁂-language-dropdown__options' ref={node} tabIndex={-1}>
|
||||
<div className='⁂-language-dropdown__options' tabIndex={-1} ref={node} role='listbox'>
|
||||
{results.map(([code, name]) => {
|
||||
const active = code === language;
|
||||
const modified = code === modifiedLanguage;
|
||||
@ -218,7 +223,6 @@ const getLanguageDropdown =
|
||||
'⁂-language-dropdown__option--active': active,
|
||||
})}
|
||||
aria-selected={active}
|
||||
ref={active ? focusedItem : null}
|
||||
>
|
||||
<div className='⁂-language-dropdown__option__name'>{name}</div>
|
||||
{features.multiLanguage &&
|
||||
|
||||
Reference in New Issue
Block a user