mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-15 09:12:24 +02:00
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
147e0a822b
commit
a12c6f8119
3 changed files with 20 additions and 17 deletions
|
@ -54,7 +54,9 @@ function DropdownNavbarItemDesktop({
|
||||||
const [showDropdown, setShowDropdown] = useState(false);
|
const [showDropdown, setShowDropdown] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
|
const handleClickOutside = (
|
||||||
|
event: MouseEvent | TouchEvent | FocusEvent,
|
||||||
|
) => {
|
||||||
if (
|
if (
|
||||||
!dropdownRef.current ||
|
!dropdownRef.current ||
|
||||||
dropdownRef.current.contains(event.target as Node)
|
dropdownRef.current.contains(event.target as Node)
|
||||||
|
@ -66,10 +68,12 @@ function DropdownNavbarItemDesktop({
|
||||||
|
|
||||||
document.addEventListener('mousedown', handleClickOutside);
|
document.addEventListener('mousedown', handleClickOutside);
|
||||||
document.addEventListener('touchstart', handleClickOutside);
|
document.addEventListener('touchstart', handleClickOutside);
|
||||||
|
document.addEventListener('focusin', handleClickOutside);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
document.removeEventListener('mousedown', handleClickOutside);
|
document.removeEventListener('mousedown', handleClickOutside);
|
||||||
document.removeEventListener('touchstart', handleClickOutside);
|
document.removeEventListener('touchstart', handleClickOutside);
|
||||||
|
document.removeEventListener('focusin', handleClickOutside);
|
||||||
};
|
};
|
||||||
}, [dropdownRef]);
|
}, [dropdownRef]);
|
||||||
|
|
||||||
|
@ -100,22 +104,6 @@ function DropdownNavbarItemDesktop({
|
||||||
{items.map((childItemProps, i) => (
|
{items.map((childItemProps, i) => (
|
||||||
<NavbarItem
|
<NavbarItem
|
||||||
isDropdownItem
|
isDropdownItem
|
||||||
onKeyDown={(e) => {
|
|
||||||
if (i === items.length - 1 && e.key === 'Tab') {
|
|
||||||
e.preventDefault();
|
|
||||||
setShowDropdown(false);
|
|
||||||
const nextNavbarItem = dropdownRef.current!.nextElementSibling;
|
|
||||||
if (nextNavbarItem) {
|
|
||||||
const targetItem =
|
|
||||||
nextNavbarItem instanceof HTMLAnchorElement
|
|
||||||
? nextNavbarItem
|
|
||||||
: // Next item is another dropdown; focus on the inner
|
|
||||||
// anchor element instead so there's outline
|
|
||||||
nextNavbarItem.querySelector('a')!;
|
|
||||||
targetItem.focus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
activeClassName="dropdown__link--active"
|
activeClassName="dropdown__link--active"
|
||||||
{...childItemProps}
|
{...childItemProps}
|
||||||
key={i}
|
key={i}
|
||||||
|
|
11
website/_dogfooding/_pages tests/navbar-dropdown-tests.mdx
Normal file
11
website/_dogfooding/_pages tests/navbar-dropdown-tests.mdx
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# navbar-dropdown-tests
|
||||||
|
|
||||||
|
<div id="navbar-dropdown-tests">
|
||||||
|
|
||||||
|
1. Make sure that the theme switcher is placed immediately after the language switcher in the navbar.
|
||||||
|
2. Press `Tab` several times to focus language switcher.
|
||||||
|
3. Press `Enter` to open language switcher.
|
||||||
|
4. Press `Tab` several times to exit language switcher.
|
||||||
|
5. Check if the theme selection button is in focus immediately after the last item.
|
||||||
|
|
||||||
|
</div>
|
|
@ -24,3 +24,7 @@ html.plugin-pages.plugin-id-pages-tests .navbar {
|
||||||
#z-index-test {
|
#z-index-test {
|
||||||
z-index: 100;
|
z-index: 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
html:has(#navbar-dropdown-tests) .navbar__item.dropdown ~ a {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue