Files
ncd-fe/packages/pl-fe/src/components/inline-style.tsx
nicole mikołajczyk 8efed59d52 pl-fe: use adoptedStyleSheets and break a bunch of other stuff
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
2025-06-29 19:30:11 +02:00

47 lines
1.2 KiB
TypeScript

import React, { useEffect, useRef } from 'react';
interface IInlineStyle {
children: string;
}
const InlineStyle: React.FC<IInlineStyle> = ({ children }) => {
// eslint-disable-next-line compat/compat
const sheet = useRef(document.adoptedStyleSheets ? new CSSStyleSheet() : document.createElement('style'));
useEffect(() => {
if (sheet.current) {
const stylesheet = sheet.current;
if (stylesheet instanceof CSSStyleSheet) {
stylesheet.replaceSync(children);
} else {
stylesheet.textContent = children;
}
}
}, [children]);
useEffect(() => {
const stylesheet = sheet.current;
if (stylesheet) {
if (stylesheet instanceof CSSStyleSheet) {
document.adoptedStyleSheets = [...document.adoptedStyleSheets, stylesheet];
} else {
document.head.appendChild(stylesheet);
}
}
return () => {
if (stylesheet) {
if (stylesheet instanceof CSSStyleSheet) {
document.adoptedStyleSheets = document.adoptedStyleSheets.filter(s => s !== stylesheet);
} else {
document.head.removeChild(stylesheet);
}
}
};
}, []);
return <></>;
};
export { InlineStyle as default };