Skip to content

Commit ba4ff10

Browse files
committed
fix: implement review comments
1 parent dafc52d commit ba4ff10

File tree

3 files changed

+29
-44
lines changed

3 files changed

+29
-44
lines changed

packages/pluggableWidgets/skiplink-web/src/SkipLink.editorPreview.tsx

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,7 @@ export const preview = (props: SkipLinkPreviewProps): ReactElement => {
77
<div style={{ position: "relative", height: 40 }}>
88
<a
99
href={`#${props.mainContentId}`}
10-
style={{
11-
position: "absolute",
12-
top: 0,
13-
left: 0,
14-
background: "#fff",
15-
color: "#0078d4",
16-
padding: "8px 16px",
17-
zIndex: 1000,
18-
textDecoration: "none",
19-
border: "2px solid #0078d4",
20-
borderRadius: 4,
21-
fontWeight: "bold"
22-
}}
10+
style={props.styleObject}
2311
>
2412
{props.linkText}
2513
</a>
@@ -29,19 +17,7 @@ export const preview = (props: SkipLinkPreviewProps): ReactElement => {
2917
return (
3018
<a
3119
href={`#${props.mainContentId}`}
32-
style={{
33-
position: "absolute",
34-
top: 0,
35-
left: 0,
36-
background: "#fff",
37-
color: "#0078d4",
38-
padding: "8px 16px",
39-
zIndex: 1000,
40-
textDecoration: "none",
41-
border: "2px solid #0078d4",
42-
borderRadius: 4,
43-
fontWeight: "bold"
44-
}}
20+
style={props.styleObject}
4521
>
4622
{props.linkText}
4723
</a>
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { MouseEvent, useEffect, useRef } from "react";
1+
import { MouseEvent, useState } from "react";
2+
import { createPortal } from 'react-dom';
23
import "./ui/SkipLink.scss";
34
import { SkipLinkContainerProps } from "typings/SkipLinkProps";
45

@@ -7,23 +8,32 @@ import { SkipLinkContainerProps } from "typings/SkipLinkProps";
78
* When activated, focus is programmatically set to the main content.
89
*/
910
export function SkipLink(props: SkipLinkContainerProps) {
10-
const skipLinkRef = useRef<HTMLAnchorElement | null>(null);
11-
12-
useEffect(() => {
13-
// Insert as the first child of the element with ID 'root'
14-
const root = document.getElementById("root");
15-
if (root && skipLinkRef.current) {
16-
root.insertBefore(skipLinkRef.current, root.firstChild);
11+
const [linkRoot] = useState(() => {
12+
const link = document.createElement('div');
13+
const root = document.getElementById("root");
14+
// Insert as first child immediately
15+
if (root && root.firstElementChild) {
16+
root.insertBefore(link, root.firstElementChild);
17+
} else if (root) {
18+
root.appendChild(link);
19+
} else{
20+
console.error("No root element found on page");
1721
}
18-
}, [skipLinkRef.current]);
22+
return link;
23+
})
1924

2025
function handleClick(event: MouseEvent): void {
2126
event.preventDefault();
2227
let main: HTMLElement;
23-
const mainByID = document.getElementById(props.mainContentId);
24-
if (props.mainContentId !== "" && mainByID !== null) {
25-
main = mainByID;
26-
} else {
28+
if(props.mainContentId !== "") {
29+
const mainByID = document.getElementById(props.mainContentId);
30+
if (mainByID !== null) {
31+
main = mainByID;
32+
} else{
33+
console.error(`Element with id: ${props.mainContentId} not found on page`);
34+
return;
35+
}
36+
} else{
2737
main = document.getElementsByTagName("main")[0];
2838
}
2939

@@ -44,15 +54,14 @@ export function SkipLink(props: SkipLinkContainerProps) {
4454
}
4555
}
4656

47-
return (
57+
return createPortal(
4858
<a
49-
ref={skipLinkRef}
5059
className={`widget-skip-link ${props.class}`}
5160
href={`#${props.mainContentId}`}
5261
tabIndex={props.tabIndex}
5362
onClick={handleClick}
5463
>
5564
{props.linkText}
56-
</a>
65+
</a>, linkRoot
5766
);
5867
}

packages/pluggableWidgets/skiplink-web/src/ui/SkipLink.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.widget.skip-link {
1+
.widget-skip-link {
22
position: absolute;
33
top: 0;
44
left: 0;
@@ -14,7 +14,7 @@
1414
font-weight: bold;
1515
}
1616

17-
.widget.skip-link:focus {
17+
.widget-skip-link:focus {
1818
transform: translateY(0);
1919
outline: none;
2020
}

0 commit comments

Comments
 (0)