Skip to content

Commit 75647ec

Browse files
committed
fix(frontend) Anchor link scroll focus to element
1 parent 1270ce2 commit 75647ec

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

frontend/app/components/links.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function AnchorLink({ anchorElementId, children, onClick, ...restProps }:
129129
*/
130130
function handleOnSkipLinkClick(event: MouseEvent<HTMLAnchorElement>) {
131131
event.preventDefault();
132-
scrollAndFocusFromAnchorLink(event.currentTarget.href);
132+
scrollAndFocusToElement(anchorElementId);
133133
onClick?.(event);
134134
}
135135

@@ -168,21 +168,15 @@ export function InlineLink({ className, children, file, hash, params, search, to
168168
}
169169

170170
/**
171-
* Scrolls and focuses on the element identified by the anchor link's hash.
171+
* Scrolls and focuses on the element identified.
172172
*
173-
* @param href - The anchor link URL.
173+
* @param id - The element ID to scroll to and focus on.
174174
*/
175-
function scrollAndFocusFromAnchorLink(href: string): void {
176-
if (URL.canParse(href)) {
177-
const { hash } = new URL(href);
178-
179-
if (hash) {
180-
const targetElement = document.getElementById(hash.replace('#', ''));
181-
182-
if (targetElement) {
183-
targetElement.scrollIntoView({ behavior: 'smooth' });
184-
targetElement.focus();
185-
}
186-
}
175+
function scrollAndFocusToElement(id: string): void {
176+
const targetElement = document.getElementById(id);
177+
178+
if (targetElement) {
179+
targetElement.scrollIntoView({ behavior: 'smooth' });
180+
targetElement.focus();
187181
}
188182
}

0 commit comments

Comments
 (0)