Skip to content

Commit bf251f8

Browse files
authored
fix: removeChild #121
1 parent a0b7597 commit bf251f8

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

packages/preload/loading.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
/**
42
* https://tobiasahlin.com/spinkit
53
* https://connoratherton.com/loaders
@@ -45,12 +43,25 @@ export function useLoading() {
4543

4644
return {
4745
appendLoading() {
48-
document.head.appendChild(oStyle)
49-
document.body.appendChild(oDiv)
46+
safe.append(document.head, oStyle)
47+
safe.append(document.body, oDiv)
5048
},
5149
removeLoading() {
52-
document.head.removeChild(oStyle)
53-
document.body.removeChild(oDiv)
50+
safe.remove(document.head, oStyle)
51+
safe.remove(document.body, oDiv)
5452
},
5553
}
5654
}
55+
56+
const safe = {
57+
append(parent: HTMLElement, child: HTMLElement) {
58+
if (!Array.from(parent.children).find(e => e === child)) {
59+
return parent.appendChild(child)
60+
}
61+
},
62+
remove(parent: HTMLElement, child: HTMLElement) {
63+
if (Array.from(parent.children).find(e => e === child)) {
64+
return parent.removeChild(child)
65+
}
66+
},
67+
}

0 commit comments

Comments
 (0)