Skip to content
Merged
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
44 changes: 12 additions & 32 deletions src/components/accessibility/skip_link/skip_link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
* Side Public License, v 1.
*/

import React, {
FunctionComponent,
Ref,
useState,
useEffect,
useCallback,
} from 'react';
import React, { FunctionComponent, Ref, useCallback } from 'react';
import classNames from 'classnames';
import { isTabbable } from 'tabbable';
import { useEuiTheme } from '../../../services';
Expand Down Expand Up @@ -81,31 +75,17 @@ export const EuiSkipLink: FunctionComponent<EuiSkipLinkProps> = ({
position !== 'static' ? styles[position] : undefined,
];

const [destinationEl, setDestinationEl] = useState<HTMLElement | null>(null);
const [hasValidId, setHasValidId] = useState(true);

useEffect(() => {
const idEl = document.getElementById(destinationId);
if (idEl) {
setHasValidId(true);
setDestinationEl(idEl);
return;
}
setHasValidId(false);

// If no valid element via ID is available, use the fallback query selectors
if (fallbackDestination) {
const fallbackEl = document.querySelector<HTMLElement>(
fallbackDestination
);
if (fallbackEl) {
setDestinationEl(fallbackEl);
}
}
}, [destinationId, fallbackDestination]);

const onClick = useCallback(
(e: React.MouseEvent<HTMLAnchorElement>) => {
let destinationEl: HTMLElement | null = null;
// Check if the destination ID is valid
destinationEl = document.getElementById(destinationId);
const hasValidId = !!destinationEl;
// Check the fallback destination if not
if (!destinationEl && fallbackDestination) {
destinationEl = document.querySelector(fallbackDestination);
}

if ((overrideLinkBehavior || !hasValidId) && destinationEl) {
e.preventDefault();

Expand All @@ -124,7 +104,7 @@ export const EuiSkipLink: FunctionComponent<EuiSkipLinkProps> = ({
destinationEl.tabIndex = -1;
destinationEl.addEventListener(
'blur',
() => destinationEl.removeAttribute('tabindex'),
() => destinationEl?.removeAttribute('tabindex'),
{ once: true }
);
}
Expand All @@ -134,7 +114,7 @@ export const EuiSkipLink: FunctionComponent<EuiSkipLinkProps> = ({

_onClick?.(e);
},
[overrideLinkBehavior, hasValidId, destinationEl, _onClick]
[overrideLinkBehavior, destinationId, fallbackDestination, _onClick]
);

return (
Expand Down
3 changes: 3 additions & 0 deletions upcoming_changelogs/6613.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Bug fixes**

- Fixed an `EuiSkipLink` bug where main content loading in progressively/dynamically after the skip link rendered was not being correctly focused