feat/fix: improve responsive constraints & add live ThemeListener
This commit is contained in:
parent
144144e5f9
commit
a0582ac8d5
4 changed files with 104 additions and 0 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { ThemeListener } from './components/layout/ThemeListener';
|
||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
@ -11,6 +12,7 @@ import AnimatedRoutes from './components/AnimatedRoutes';
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
|
<ThemeListener />
|
||||||
<ScrollToTop />
|
<ScrollToTop />
|
||||||
<Layout>
|
<Layout>
|
||||||
<AnimatedRoutes />
|
<AnimatedRoutes />
|
||||||
|
|
|
||||||
50
Template-01/src/components/layout/ThemeListener.tsx
Normal file
50
Template-01/src/components/layout/ThemeListener.tsx
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
export function ThemeListener() {
|
||||||
|
useEffect(() => {
|
||||||
|
const handleMessage = (event: MessageEvent) => {
|
||||||
|
// Allow messages from any origin since preview is generic
|
||||||
|
if (event.data?.type === 'UPDATE_APPEARANCE') {
|
||||||
|
const settings = event.data.settings;
|
||||||
|
if (!settings) return;
|
||||||
|
|
||||||
|
const root = document.documentElement;
|
||||||
|
|
||||||
|
// Update Primary Color
|
||||||
|
if (settings.primaryColor) {
|
||||||
|
root.style.setProperty('--color-brand-primary', settings.primaryColor);
|
||||||
|
root.style.setProperty('--color-brand-blue', settings.primaryColor);
|
||||||
|
root.style.setProperty('--color-brand-gold', settings.primaryColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Background Color
|
||||||
|
if (settings.backgroundColor) {
|
||||||
|
root.style.setProperty('--color-brand-bg', settings.backgroundColor);
|
||||||
|
document.body.style.backgroundColor = settings.backgroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Font Family
|
||||||
|
if (settings.fontFamily) {
|
||||||
|
const fontLink = document.getElementById('dynamic-font') as HTMLLinkElement;
|
||||||
|
const fontName = settings.fontFamily.replace(/ /g, '+');
|
||||||
|
if (fontLink) {
|
||||||
|
fontLink.href = `https://fonts.googleapis.com/css2?family=${fontName}:wght@300;400;500;600;700&display=swap`;
|
||||||
|
} else {
|
||||||
|
const link = document.createElement('link');
|
||||||
|
link.id = 'dynamic-font';
|
||||||
|
link.rel = 'stylesheet';
|
||||||
|
link.href = `https://fonts.googleapis.com/css2?family=${fontName}:wght@300;400;500;600;700&display=swap`;
|
||||||
|
document.head.appendChild(link);
|
||||||
|
}
|
||||||
|
root.style.setProperty('--font-sans', `"${settings.fontFamily}", sans-serif`);
|
||||||
|
document.body.style.fontFamily = `"${settings.fontFamily}", sans-serif`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleMessage);
|
||||||
|
return () => window.removeEventListener('message', handleMessage);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { ThemeListener } from './components/layout/ThemeListener';
|
||||||
/**
|
/**
|
||||||
* @license
|
* @license
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
@ -10,6 +11,7 @@ import AnimatedRoutes from './components/AnimatedRoutes';
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
|
<ThemeListener />
|
||||||
<Layout>
|
<Layout>
|
||||||
<AnimatedRoutes />
|
<AnimatedRoutes />
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
||||||
50
Template-02/src/components/layout/ThemeListener.tsx
Normal file
50
Template-02/src/components/layout/ThemeListener.tsx
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
export function ThemeListener() {
|
||||||
|
useEffect(() => {
|
||||||
|
const handleMessage = (event: MessageEvent) => {
|
||||||
|
// Allow messages from any origin since preview is generic
|
||||||
|
if (event.data?.type === 'UPDATE_APPEARANCE') {
|
||||||
|
const settings = event.data.settings;
|
||||||
|
if (!settings) return;
|
||||||
|
|
||||||
|
const root = document.documentElement;
|
||||||
|
|
||||||
|
// Update Primary Color
|
||||||
|
if (settings.primaryColor) {
|
||||||
|
root.style.setProperty('--color-brand-primary', settings.primaryColor);
|
||||||
|
root.style.setProperty('--color-brand-blue', settings.primaryColor);
|
||||||
|
root.style.setProperty('--color-brand-gold', settings.primaryColor);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Background Color
|
||||||
|
if (settings.backgroundColor) {
|
||||||
|
root.style.setProperty('--color-brand-bg', settings.backgroundColor);
|
||||||
|
document.body.style.backgroundColor = settings.backgroundColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update Font Family
|
||||||
|
if (settings.fontFamily) {
|
||||||
|
const fontLink = document.getElementById('dynamic-font') as HTMLLinkElement;
|
||||||
|
const fontName = settings.fontFamily.replace(/ /g, '+');
|
||||||
|
if (fontLink) {
|
||||||
|
fontLink.href = `https://fonts.googleapis.com/css2?family=${fontName}:wght@300;400;500;600;700&display=swap`;
|
||||||
|
} else {
|
||||||
|
const link = document.createElement('link');
|
||||||
|
link.id = 'dynamic-font';
|
||||||
|
link.rel = 'stylesheet';
|
||||||
|
link.href = `https://fonts.googleapis.com/css2?family=${fontName}:wght@300;400;500;600;700&display=swap`;
|
||||||
|
document.head.appendChild(link);
|
||||||
|
}
|
||||||
|
root.style.setProperty('--font-sans', `"${settings.fontFamily}", sans-serif`);
|
||||||
|
document.body.style.fontFamily = `"${settings.fontFamily}", sans-serif`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleMessage);
|
||||||
|
return () => window.removeEventListener('message', handleMessage);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue