-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (56 loc) · 1.66 KB
/
index.js
File metadata and controls
65 lines (56 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, { useCallback, useEffect, useRef } from "react";
const GitHubButton = React.memo(({ children, ...props }) => {
const containerRef = useRef(null);
const buttonRef = useRef(null);
const paint = useCallback(() => {
if (!containerRef.current) return;
const tempSpan = document.createElement("span");
containerRef.current.appendChild(tempSpan);
import(/* webpackMode: "eager" */ "github-buttons")
.then(({ render }) => {
if (
buttonRef.current &&
containerRef.current &&
containerRef.current.lastChild === tempSpan
) {
render(tempSpan.appendChild(buttonRef.current), (el) => {
if (
containerRef.current &&
containerRef.current.lastChild === tempSpan
) {
containerRef.current.replaceChild(el, tempSpan);
}
});
}
})
.catch((error) => {
console.error("Error loading github-buttons:", error);
if (
containerRef.current &&
containerRef.current.lastChild === tempSpan
) {
containerRef.current.removeChild(tempSpan);
}
});
}, []);
useEffect(() => {
paint();
return () => {
if (containerRef.current) {
const lastChild = containerRef.current.lastChild;
if (lastChild && lastChild !== buttonRef.current) {
containerRef.current.removeChild(lastChild);
}
}
};
}, [paint]);
return (
<span ref={containerRef}>
<a {...props} ref={buttonRef}>
{children}
</a>
</span>
);
});
GitHubButton.displayName = "GitHubButton";
export default GitHubButton;