Files
site-builder/craft/src/state/DeviceContext.tsx
T
shadowdaoandClaude Opus 5.5 5f99df23a7 feat(site-builder): per-breakpoint style model, device context and responsive export
- utils/breakpoints.ts: single source for 767/768/991/992; head
  VISIBILITY/RESPONSIVE CSS built from it (flex-stacking rule moved from
  768 to the 767 mobile boundary).
- utils/responsive-style.ts: styleTablet/styleMobile partial overrides,
  cascade resolution, pure setters, scoped @media CSS per node.
- state/DeviceContext.tsx: real DeviceProvider (owns the one-time mobile
  forcing from EditorShell); TopBar, overflow menu, ReportIssueModal and
  Canvas read useDevice() instead of props.
- editor/RenderNode.tsx: resolves the effective style for the current
  device in one place and marks overridden nodes with data-wsb-overridden.
  Craft passes an opaque <DefaultRender/> to onRender, so the component
  element is rebuilt with the resolved style (cloneElement cannot work).
- utils/html-export.ts: hidden nodes skipped; overrides emitted as one
  body-level <style data-wsb-responsive> block + data-wsb-r scope attr;
  injectAttrs skips leading <style> elements.
- test-utils/editorHarness.tsx: optional onRender/wrap options.

Refs #45
Refs #37

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 11:28:44 -07:00

52 lines
2.1 KiB
TypeScript

import React, { createContext, useContext, useEffect, useMemo, useRef, useState } from 'react';
import type { DeviceMode } from '../types';
import { useIsMobile } from '../hooks/useIsMobile';
/**
* The editor's current device mode (desktop / tablet / mobile canvas).
*
* Shared state rather than props threaded from EditorShell: the TopBar
* switcher, the Canvas frame width, RenderNode's per-device style resolution
* (#45) and the command registry all read the same value.
*
* Without a provider (unit tests, standalone component mounts) the default
* is `desktop` with a no-op setter, so consumers never need a guard.
*/
export interface DeviceContextValue {
device: DeviceMode;
setDevice: (d: DeviceMode) => void;
}
const DEFAULT_VALUE: DeviceContextValue = { device: 'desktop', setDevice: () => {} };
const DeviceCtx = createContext<DeviceContextValue>(DEFAULT_VALUE);
export const useDevice = (): DeviceContextValue => useContext(DeviceCtx);
interface DeviceProviderProps {
children: React.ReactNode;
/** Starting device (tests). Defaults to `desktop`. */
initialDevice?: DeviceMode;
}
export const DeviceProvider: React.FC<DeviceProviderProps> = ({ children, initialDevice = 'desktop' }) => {
const [device, setDevice] = useState<DeviceMode>(initialDevice);
const isMobile = useIsMobile();
// Default the canvas to the "mobile" preview width the first time a mobile
// viewport is detected, so the frame fits without the user reaching for the
// device switcher (tucked in TopBar's overflow menu on mobile). Fires once,
// and only while the device is still `desktop` -- it must not fight a device
// the user has already picked on a later re-render.
const mobileDeviceAppliedRef = useRef(false);
useEffect(() => {
if (isMobile && !mobileDeviceAppliedRef.current) {
mobileDeviceAppliedRef.current = true;
setDevice((current) => (current === 'desktop' ? 'mobile' : current));
}
}, [isMobile]);
const value = useMemo(() => ({ device, setDevice }), [device]);
return <DeviceCtx.Provider value={value}>{children}</DeviceCtx.Provider>;
};