Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update useStyledComponentsTarget to support case where head tag is removed. #414

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/self-service/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const version = getWidgetVersion();

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), cssInjectedByJsPlugin()],
plugins: [react(), cssInjectedByJsPlugin({ styleId: 'sendbird-css-inject-id' })],
build: {
outDir: `./dist/${version}`,
rollupOptions: {
Expand Down
45 changes: 27 additions & 18 deletions src/hooks/useStyledComponentsTarget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useLayoutEffect, useState } from 'react';
import { version } from 'styled-components/package.json';

const StyledId = 'sendbird-css-inject-id';

function isSCTarget(node: Node): node is HTMLStyleElement {
return node instanceof HTMLStyleElement && node.getAttribute('data-styled-version') === version;
}
Expand All @@ -17,28 +19,35 @@ export function useStyledComponentsTarget() {
const [target, setTarget] = useState(document.head);

useLayoutEffect(() => {
const moveStyleToBody = (styleElement: HTMLElement) => {
if (styleElement && styleElement.parentElement !== document.body) {
console.warn(`[useStyledComponentsTarget]: Moving style element ${StyledId} to <body>.`);
document.body.appendChild(styleElement);
bang9 marked this conversation as resolved.
Show resolved Hide resolved
setTarget(document.body);
}
};

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
// 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)) {
console.warn('Styled Components styles re-injected, switching to <body>');
setTarget(document.body);
return;
}
// Handle added nodes
Array.from(mutation.addedNodes).forEach((node) => {
if (isSCTarget(node)) {
console.warn('[useStyledComponentsTarget]: Styled Components styles re-injected, switching to <body>');
setTarget(document.body);
} else if (node instanceof HTMLElement && node.id === StyledId) {
moveStyleToBody(node);
bang9 marked this conversation as resolved.
Show resolved Hide resolved
}
}
// 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)) {
console.warn('Styled Components styles removed, switching to <body>');
setTarget(document.body);
return;
}
});

// Handle removed nodes
Array.from(mutation.removedNodes).forEach((node) => {
if (isSCTarget(node)) {
console.warn('[useStyledComponentsTarget]: Styled Components styles removed, switching to <body>');
setTarget(document.body);
} else if (node instanceof HTMLElement && node.id === StyledId) {
moveStyleToBody(node);
}
}
});
});
});

Expand Down
Loading