feat(site-builder): tablet/mobile style overrides that never touch desktop

On tablet/mobile the useNodeProp style setters now route responsive keys
(spacing, font size, line height, letter spacing, alignment, size, flex
layout) into styleTablet/styleMobile; colours and other keys still write
style. An empty value removes the override. Continuous-burst keys include
the device so a desktop drag and a mobile drag never merge.

- shared.tsx: device-aware setters, new setResponsiveStyleProp /
  clearResponsiveStyleProp exports, setPropStylesContinuous
- responsive-panel.tsx: useResolvedStyle, useOverrideState, DeviceBanner
- OverrideDot: filled dot resets the override (one undo step), hollow dot
  marks an inherited value; wired into Text/Button/Container/Image/Media/
  BackgroundSection panels and both box-model sections
- Columns: "Stack on this device" toggle (flexDirection override)
- GuidedStyles: device banner under the type header
- RenderNode: every component node renders through one stable
  ResolvedRender so gaining/losing an override or switching device no
  longer remounts the node (the remount dropped inline-edit focus/caret
  and blanked a selected Heading)

Refs #45
This commit is contained in:
shadowdao committed 2026-10-07 11:51:53 -07:00
1 parent d8ff295093
commit c2a90cd7a7
15 files changed
+806 -78

No files matched your search

+47 -34
View File
@@ -1,8 +1,9 @@
import React, { useCallback, useEffect, useRef } from 'react';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { createPortal } from 'react-dom';
import { NodeElement, useEditor, useNode } from '@craftjs/core';
import { useDevice } from '../state/DeviceContext';
import { hasAnyOverride, hasActiveOverride, resolveStyleForDevice, type ResponsiveProps } from '../utils/responsive-style';
import type { DeviceMode } from '../types';
interface RenderNodeProps {
render: React.ReactElement;
@@ -49,29 +50,60 @@ interface RenderNodeProps {
* needs to know about devices. Craft hands `onRender` an opaque
* `<DefaultRender/>` element that reads the node's props itself, so the
* override cannot be applied with `cloneElement(render, {style})` -- instead
* the component element is rebuilt here exactly as Craft's DefaultRender
* does (`createElement(type, props, <NodeElement/> children)`) with the
* resolved `style`. Nodes without overrides, and every node on desktop,
* keep Craft's own `render` untouched.
* every component node renders through `ResolvedRender`, a copy of Craft's
* DefaultRender (`createElement(type, props, <NodeElement/> children)`) that
* swaps in the resolved `style`. It is used on EVERY device and whether or
* not the node has overrides, so the element type never changes: switching
* device or adding the first override only updates the style, it never
* remounts the component (#45 -- a remount dropped inline-edit focus/caret
* and blanked a selected Heading/Text, whose text is only written to the DOM
* while unselected). Raw-tag nodes keep Craft's own `render`.
*/
const ResolvedRender: React.FC<{ device: DeviceMode }> = ({ device }) => {
const { type, props, childIds, hydrationTimestamp } = useNode((node) => ({
type: node.data.type,
props: node.data.props,
childIds: node.data.nodes,
hydrationTimestamp: (node as any)._hydrationTimestamp,
}));
// Only nodes with overrides depend on the device, so a device switch
// re-renders just those (plain nodes keep their memoised element).
const styleDevice: DeviceMode = hasAnyOverride(props as ResponsiveProps) ? device : 'desktop';
return useMemo(() => {
const children =
childIds && childIds.length > 0 ? (
<>
{childIds.map((childId: string) => (
<NodeElement id={childId} key={childId} />
))}
</>
) : (
props.children
);
const resolved =
styleDevice !== 'desktop' ? { ...props, style: resolveStyleForDevice(props as ResponsiveProps, styleDevice) } : props;
return React.createElement(type as React.ElementType, resolved, children);
}, [type, props, childIds, hydrationTimestamp, styleDevice]);
};
export const RenderNode: React.FC<RenderNodeProps> = ({ render }) => {
const { actions } = useEditor();
const { device } = useDevice();
const { id, isSelected, dom, name, parent, isCanvas, isHovered, overridden, hideDesktop, hideTablet, hideMobile } = useNode((node) => {
// Keep the collector cheap: the node's type/props/children are only
// collected for the (rare) nodes that actually carry overrides.
const { id, isSelected, dom, name, parent, isCanvas, isHovered, isComponent, tabletOverride, mobileOverride, hideDesktop, hideTablet, hideMobile } = useNode((node) => {
// Keep the collector cheap (booleans only); ResolvedRender collects the
// node's type/props/children itself, as Craft's DefaultRender does.
const props = node.data.props as ResponsiveProps | undefined;
const overridden = hasAnyOverride(props)
? { type: node.data.type, props: node.data.props, childIds: node.data.nodes }
: null;
const type = node.data.type as unknown;
return {
isComponent: !!type && typeof type !== 'string',
tabletOverride: hasActiveOverride(props, 'tablet'),
mobileOverride: hasActiveOverride(props, 'mobile'),
isSelected: node.events.selected,
isHovered: node.events.hovered,
dom: node.dom,
name: (node.data.props?.aiName as string) || node.data.displayName,
parent: node.data.parent,
isCanvas: node.data.isCanvas,
overridden,
hideDesktop: !!node.data.props?.hideOnDesktop,
hideTablet: !!node.data.props?.hideOnTablet,
hideMobile: !!node.data.props?.hideOnMobile,
@@ -79,8 +111,7 @@ export const RenderNode: React.FC<RenderNodeProps> = ({ render }) => {
});
// Overrides that apply on the current device (mobile inherits tablet's).
const activeOverride =
!!overridden && hasActiveOverride(overridden.props as ResponsiveProps, device);
const activeOverride = device === 'tablet' ? tabletOverride : device === 'mobile' ? mobileOverride : false;
const badgeRef = useRef<HTMLDivElement>(null);
const active = isSelected && id !== 'ROOT' && !!dom;
@@ -149,28 +180,10 @@ export const RenderNode: React.FC<RenderNodeProps> = ({ render }) => {
}
}, [dom, activeOverride]);
let output: React.ReactElement = render;
// String-typed (plain DOM) nodes go through Craft's own SimpleElement
// connector wrapper, which is not exported -- leave those untouched. Every
// resolver component is a React component, so this only skips raw tags.
if (device !== 'desktop' && overridden && typeof overridden.type !== 'string') {
const { type, props, childIds } = overridden;
const children =
childIds && childIds.length > 0 ? (
<>
{childIds.map((childId: string) => (
<NodeElement id={childId} key={childId} />
))}
</>
) : (
props.children
);
output = React.createElement(
type as React.ElementType,
{ ...props, style: resolveStyleForDevice(props as ResponsiveProps, device) },
children,
);
}
// resolver component is a React component (see ResolvedRender above).
const output: React.ReactElement = isComponent ? <ResolvedRender device={device} /> : render;
if (!active) return <>{output}</>;