diff --git a/packages/pl-fe/src/components/inline-style.tsx b/packages/pl-fe/src/components/inline-style.tsx new file mode 100644 index 000000000..3f6d5ed70 --- /dev/null +++ b/packages/pl-fe/src/components/inline-style.tsx @@ -0,0 +1,46 @@ +import React, { useEffect, useRef } from 'react'; + +interface IInlineStyle { + children: string; +} + +const InlineStyle: React.FC = ({ 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 }; diff --git a/packages/pl-fe/src/features/pl-fe-config/components/site-preview.tsx b/packages/pl-fe/src/features/pl-fe-config/components/site-preview.tsx index bb159e55e..cb80ffcaf 100644 --- a/packages/pl-fe/src/features/pl-fe-config/components/site-preview.tsx +++ b/packages/pl-fe/src/features/pl-fe-config/components/site-preview.tsx @@ -2,10 +2,10 @@ import clsx from 'clsx'; import React from 'react'; import { FormattedMessage } from 'react-intl'; +import InlineStyle from 'pl-fe/components/inline-style'; import BackgroundShapes from 'pl-fe/features/ui/components/background-shapes'; import { useSystemTheme } from 'pl-fe/hooks/use-system-theme'; import { useThemeCss } from 'pl-fe/hooks/use-theme-css'; -import { useSettingsStore } from 'pl-fe/stores/settings'; import type { PlFeConfig } from 'pl-fe/normalizers/pl-fe/pl-fe-config'; @@ -16,9 +16,7 @@ interface ISitePreview { /** Renders a preview of the website's style with the configuration applied. */ const SitePreview: React.FC = ({ plFe }) => { - const { defaultSettings } = useSettingsStore(); - - const userTheme = defaultSettings.themeMode; + const userTheme = plFe.defaultSettings.themeMode; const systemTheme = useSystemTheme(); const dark = ['dark', 'black'].includes(userTheme as string) || (userTheme === 'system' && systemTheme === 'black'); @@ -32,13 +30,14 @@ const SitePreview: React.FC = ({ plFe }) => { 'h-40 overflow-hidden rounded-lg', { 'bg-white': !dark, - 'bg-gray-900': dark, + 'bg-gray-900': dark && userTheme !== 'black', + 'bg-black': userTheme === 'black', }); return (
- - + {`.site-preview {${themeCss}}`} +