feat/fix: improve responsive constraints & add live ThemeListener
This commit is contained in:
parent
5f4d715628
commit
b3648c5bb4
6 changed files with 128 additions and 2 deletions
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { ThemeListener } from './components/layout/ThemeListener';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
|
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
|
||||||
import { Navbar } from './components/layout/Navbar';
|
import { Navbar } from './components/layout/Navbar';
|
||||||
|
|
@ -16,8 +17,9 @@ import { Contact } from './pages/Contact';
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
|
<ThemeListener />
|
||||||
<Analytics />
|
<Analytics />
|
||||||
<div className="flex min-h-screen flex-col font-sans text-gray-800 antialiased selection:bg-brand-gold selection:text-white">
|
<div className="overflow-x-hidden flex min-h-screen flex-col font-sans text-gray-800 antialiased selection:bg-brand-gold selection:text-white">
|
||||||
<Navbar />
|
<Navbar />
|
||||||
<div className="flex-1">
|
<div className="flex-1">
|
||||||
<Routes>
|
<Routes>
|
||||||
|
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
@ -35,3 +35,14 @@ body {
|
||||||
::-webkit-scrollbar-thumb:hover {
|
::-webkit-scrollbar-thumb:hover {
|
||||||
background: #D4AF37;
|
background: #D4AF37;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
overflow-x: hidden;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 100vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { ThemeListener } from './components/layout/ThemeListener';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
|
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
|
||||||
import { ScrollToTop } from './components/ScrollToTop';
|
import { ScrollToTop } from './components/ScrollToTop';
|
||||||
|
|
@ -17,9 +18,10 @@ import { Contact } from './pages/Contact';
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
<Router>
|
<Router>
|
||||||
|
<ThemeListener />
|
||||||
<ScrollToTop />
|
<ScrollToTop />
|
||||||
<Analytics />
|
<Analytics />
|
||||||
<div className="flex min-h-screen flex-col font-sans text-gray-800 antialiased selection:bg-brand-gold selection:text-white">
|
<div className="overflow-x-hidden flex min-h-screen flex-col font-sans text-gray-800 antialiased selection:bg-brand-gold selection:text-white">
|
||||||
<Navbar />
|
<Navbar />
|
||||||
<div className="flex-1">
|
<div className="flex-1">
|
||||||
<Routes>
|
<Routes>
|
||||||
|
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
@ -35,3 +35,14 @@ body {
|
||||||
::-webkit-scrollbar-thumb:hover {
|
::-webkit-scrollbar-thumb:hover {
|
||||||
background: #DDA7A5;
|
background: #DDA7A5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
overflow-x: hidden;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 100vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue