fix(editor): header/footer zone previews show the active device's overrides

The zone previews render the exported HTML, whose <style data-wsb-responsive>
@media rules evaluate against the browser viewport, so tablet/mobile modes
showed desktop styling for header/footer. ZonePreview now applies the active
device's override rules unconditionally (applyResponsiveCssForDevice) and
drops the rest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-10-07 13:40:54 -07:00
1 parent e20c604413
commit c62a333257
3 files changed
+89 -4

No files matched your search

+7 -3
View File
@@ -5,6 +5,7 @@ import { usePages } from '../state/PageContext';
import { useDevice } from '../state/DeviceContext';
import { DEVICE_WIDTHS } from '../constants/presets';
import { exportBodyHtml } from '../utils/html-export';
import { applyResponsiveCssForDevice } from '../utils/responsive-style';
import { useIsMobile } from '../hooks/useIsMobile';
import { useMobileChrome } from '../state/MobileChromeContext';
import { useSitesmithModal } from '../state/SitesmithContext';
@@ -21,21 +22,24 @@ interface CanvasProps {
* Renders the actual header/footer content from the Craft.js state as a
* non-interactive preview. This is the user's own authored content.
*/
const ZonePreview: React.FC<{ craftState: string | null; zone: 'header' | 'footer' }> = ({
export const ZonePreview: React.FC<{ craftState: string | null; zone: 'header' | 'footer' }> = ({
craftState,
zone,
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const { device } = useDevice();
// Per-device overrides follow the editor's device mode, not the browser
// viewport the exported @media rules would evaluate against.
const renderedHtml = useMemo(() => {
if (!craftState) return null;
try {
const result = exportBodyHtml(craftState, zone);
return result.html || null;
return result.html ? applyResponsiveCssForDevice(result.html, device) : null;
} catch {
return null;
}
}, [craftState, zone]);
}, [craftState, zone, device]);
// Set the rendered HTML into the container via ref (user-authored content)
useEffect(() => {
@@ -0,0 +1,62 @@
import React from 'react';
import { describe, test, expect, afterEach } from 'vitest';
import { createRoot, Root } from 'react-dom/client';
import { act } from 'react-dom/test-utils';
import { DeviceProvider } from '../state/DeviceContext';
import { ZonePreview } from './Canvas';
import type { DeviceMode } from '../types';
/**
* Minor finding: tablet/mobile editor modes did not show per-device overrides in
* the header/footer zone previews -- the exported `<style data-wsb-responsive>`
* @media rules evaluate against the BROWSER viewport, not the emulated device.
* ZonePreview must apply the rules for the active device mode instead.
*/
const headerState = JSON.stringify({
ROOT: {
type: { resolvedName: 'Container' }, isCanvas: true, displayName: 'Container', custom: {}, hidden: false,
props: { tag: 'header', cssId: 'hdr', style: { paddingTop: '40px' }, styleTablet: { paddingTop: '20px' }, styleMobile: { paddingTop: '8px' } },
nodes: [], linkedNodes: {},
},
});
let root: Root | null = null;
let el: HTMLDivElement | null = null;
afterEach(() => {
act(() => root?.unmount());
el?.remove();
root = null; el = null;
});
function previewCss(device: DeviceMode): string {
el = document.createElement('div');
document.body.appendChild(el);
act(() => {
root = createRoot(el!);
root.render(<DeviceProvider initialDevice={device}><ZonePreview craftState={headerState} zone="header" /></DeviceProvider>);
});
const style = el.querySelector('style[data-wsb-responsive]');
return style?.textContent ?? '';
}
describe('ZonePreview applies the active device mode\'s overrides', () => {
test('mobile: tablet + mobile rules apply unconditionally (no viewport @media)', () => {
const css = previewCss('mobile');
expect(css).not.toContain('@media');
expect(css).toContain('padding-top:20px!important');
expect(css).toContain('padding-top:8px!important');
expect(css.indexOf('20px')).toBeLessThan(css.indexOf('8px'));
});
test('tablet: only the tablet rule applies', () => {
const css = previewCss('tablet');
expect(css).not.toContain('@media');
expect(css).toContain('padding-top:20px!important');
expect(css).not.toContain('8px');
});
test('desktop: no override rules (even on a narrow browser viewport)', () => {
const css = previewCss('desktop');
expect(css).not.toContain('@media');
expect(css).not.toContain('20px');
expect(css).not.toContain('8px');
});
});
+20 -1
View File
@@ -1,7 +1,7 @@
import type { CSSProperties } from 'react';
import type { DeviceMode } from '../types';
import { cssPropsToString } from './style-helpers';
import { mediaQueryFor } from './breakpoints';
import { DEVICE_ORDER, mediaQueryFor } from './breakpoints';
/**
* Per-breakpoint style overrides (site-builder #45 / #37).
@@ -273,3 +273,22 @@ export function responsiveCssForNode(scopeAttr: string, props: ResponsiveProps,
}
return css;
}
/**
* Editor device emulation for exported HTML shown in the canvas (header/footer
* zone previews): the leading `<style data-wsb-responsive>` block's @media rules
* would evaluate against the BROWSER viewport, so apply the active device's
* rules unconditionally instead (tablet: tablet rules; mobile: tablet then
* mobile, as the cascade) and drop the rest. Rule bodies never contain braces
* (`declaration` strips them), so one level of `{}` nesting is exact.
*/
export function applyResponsiveCssForDevice(html: string, device: DeviceMode): string {
return html.replace(/^(<style data-wsb-responsive>)([\s\S]*?)(<\/style>)/, (_m, open: string, css: string, close: string) => {
const scoped = css.replace(/@media ([^{]+)\{((?:[^{}]*\{[^{}]*\})*)\}/g, (block: string, query: string, rules: string) => {
const ruleDevice = (['tablet', 'mobile'] as const).find((d) => mediaQueryFor(d) === query);
if (!ruleDevice) return block;
return DEVICE_ORDER.indexOf(device) >= DEVICE_ORDER.indexOf(ruleDevice) ? rules : '';
});
return open + scoped + close;
});
}