Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcho committed Jan 21, 2025
1 parent 103d457 commit 106af34
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions src/hooks/useStyledComponentsTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ function isSCTarget(node: Node): node is HTMLStyleElement {
* When styled-components, which has already been initialized, is re-added to the head, for example `document.head.innerHTML += ''`, the styles may not render correctly.
* Therefore, the target is moved to the body tag.
* Similarly, the issue could also rise in below cases and the hook handles them accordingly:
* - If <head> is removed, switch to <body>.
* - If styles are removed from <head>, switch to <body>.
* This is a short-term solution, and in the long run, we plan to remove styled-components altogether.
* */
Expand All @@ -19,14 +18,8 @@ export function useStyledComponentsTarget() {

useLayoutEffect(() => {
const observer = new MutationObserver((mutations) => {
// Case 1: Detect if <head> is removed
if (!document.head) {
console.warn('document.head was removed, switching to <body>');
setTarget(document.body);
return;
}
mutations.forEach((mutation) => {
// Case 2: Detect if styles are added to <head>
// Case 1: Detect if styles are added to <head>
if (mutation.target === document.head && mutation.addedNodes.length > 0) {
for (const node of mutation.addedNodes) {
if (isSCTarget(node)) {
Expand All @@ -36,7 +29,7 @@ export function useStyledComponentsTarget() {
}
}
}
// Case 3: Detect if styles are removed from <head>
// Case 2: Detect if styles are removed from <head>
if (mutation.target === document.head && mutation.removedNodes.length > 0) {
for (const node of mutation.removedNodes) {
if (isSCTarget(node)) {
Expand Down

0 comments on commit 106af34

Please sign in to comment.