diff --git a/craft/src/editor/RenderNode.tsx b/craft/src/editor/RenderNode.tsx index 67d31e5..8d15a55 100644 --- a/craft/src/editor/RenderNode.tsx +++ b/craft/src/editor/RenderNode.tsx @@ -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 * `` 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, 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, 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) => ( + + ))} + + ) : ( + 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 = ({ 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.custom?.label as string) || (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 = ({ 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(null); const active = isSelected && id !== 'ROOT' && !!dom; @@ -149,28 +180,10 @@ export const RenderNode: React.FC = ({ 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) => ( - - ))} - - ) : ( - 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 ? : render; if (!active) return <>{output}; diff --git a/craft/src/panels/right/GuidedStyles.tsx b/craft/src/panels/right/GuidedStyles.tsx index b0087b9..d307fa3 100644 --- a/craft/src/panels/right/GuidedStyles.tsx +++ b/craft/src/panels/right/GuidedStyles.tsx @@ -19,6 +19,7 @@ import { GenericPropsEditor, HtmlStylePanel, } from './styles'; +import { DeviceBanner } from './styles/responsive-panel'; /* ================================================================ IMPORTANT: None of these panels use useNode(). @@ -140,6 +141,7 @@ export const GuidedStyles: React.FC<{ Ask Sitesmith + {/* TEXT */} {isText && } diff --git a/craft/src/panels/right/styles/BackgroundSectionStylePanel.tsx b/craft/src/panels/right/styles/BackgroundSectionStylePanel.tsx index 390c9f7..7da56b8 100644 --- a/craft/src/panels/right/styles/BackgroundSectionStylePanel.tsx +++ b/craft/src/panels/right/styles/BackgroundSectionStylePanel.tsx @@ -16,13 +16,16 @@ import { sectionGap, useNodeProp, } from './shared'; +import { useResolvedStyle, PADDING_KEYS } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; import { AssetPicker } from '../../../ui/AssetPicker'; /* ---------- BACKGROUND SECTION ---------- */ export const BackgroundSectionStylePanel: React.FC = ({ selectedId, nodeProps }) => { const { setProp, setPropStyle, setPropContinuous } = useNodeProp(selectedId); - const style = nodeProps.style || {}; + const style = useResolvedStyle(nodeProps); + const dot = (key: string | readonly string[]) => ; return ( <> @@ -54,7 +57,7 @@ export const BackgroundSectionStylePanel: React.FC = ({ selecte onChange={(e) => setPropContinuous('innerMaxWidth', e.target.value)} style={inputStyle} />
- Padding + Padding{dot(PADDING_KEYS)} setPropStyle('padding', v)} />
diff --git a/craft/src/panels/right/styles/ButtonStylePanel.tsx b/craft/src/panels/right/styles/ButtonStylePanel.tsx index a57cf74..842da48 100644 --- a/craft/src/panels/right/styles/ButtonStylePanel.tsx +++ b/craft/src/panels/right/styles/ButtonStylePanel.tsx @@ -29,6 +29,8 @@ import { continuousSetProp, discreteSetProp, } from './shared'; +import { useResolvedStyle, MARGIN_KEYS, PADDING_KEYS } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; function capitalize(s: string): string { return s.charAt(0).toUpperCase() + s.slice(1); @@ -55,8 +57,9 @@ function opacityPercent(v: unknown): number { /* ---------- BUTTON ---------- */ export const ButtonStylePanel: React.FC = ({ selectedId, nodeProps }) => { const { actions } = useEditor(); - const style: CSSProperties = nodeProps.style || {}; + const style: CSSProperties = useResolvedStyle(nodeProps); const { design } = useSiteDesign(); + const dot = (key: string | readonly string[]) => ; const { setProp, setPropStyle, setProps, setPropContinuous, setPropStyleContinuous } = useNodeProp(selectedId); @@ -123,7 +126,7 @@ export const ButtonStylePanel: React.FC = ({ selectedId, nodePr />
- Padding + Padding{dot(PADDING_KEYS)} = ({ selectedId, nodePr {/* Box model + border/effects + animation/visibility rollout */} Margin{dot(MARGIN_KEYS)}} value={{ top: style.marginTop as string, right: style.marginRight as string, bottom: style.marginBottom as string, left: style.marginLeft as string }} onChange={(side: SpacingSide, v: string) => setPropStyleContinuous(`margin${capitalize(side)}`, v)} /> diff --git a/craft/src/panels/right/styles/ContainerStylePanel.tsx b/craft/src/panels/right/styles/ContainerStylePanel.tsx index c55fa22..a82987f 100644 --- a/craft/src/panels/right/styles/ContainerStylePanel.tsx +++ b/craft/src/panels/right/styles/ContainerStylePanel.tsx @@ -1,5 +1,4 @@ import React, { CSSProperties } from 'react'; -import { useEditor } from '@craftjs/core'; import { BG_COLORS, RADIUS_PRESETS, @@ -15,8 +14,11 @@ import { inputStyle, sectionGap, useNodeProp, - continuousSetProp, } from './shared'; +import { useResolvedStyle } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; +import { useDevice } from '../../../state/DeviceContext'; +import { resolveStyleForDevice, withoutOverride } from '../../../utils/responsive-style'; import { BoxModelSection, BorderEffectsSection, AnimVisSection } from './containerBoxModel'; // Vertical Alignment options shown to the user identically regardless of @@ -32,10 +34,11 @@ const VERTICAL_ALIGN_OPTIONS: { label: string; value: string }[] = [ /* ---------- CONTAINER / SECTION / COLUMNS ---------- */ export const ContainerStylePanel: React.FC = ({ selectedId, nodeProps }) => { - const style: CSSProperties = nodeProps.style || {}; + const style: CSSProperties = useResolvedStyle(nodeProps); + const dot = (key: string | readonly string[]) => ; + const { device } = useDevice(); - const { actions } = useEditor(); - const { setProp, setPropStyle, setProps, setPropContinuous, setPropStyleContinuous } = useNodeProp(selectedId); + const { setProp, setPropStyle, setProps, setPropContinuous, setPropStyleContinuous, setPropStylesContinuous } = useNodeProp(selectedId); // ColumnLayout only ever carries `columns`/`split` props -- Container and // Section never set them -- so checking either alone distinguishes the @@ -43,12 +46,19 @@ export const ContainerStylePanel: React.FC = ({ selectedId, nod // column heights) from the flex-COLUMN case (Container/Section, which // vertically center/position their OWN content via justify-content, // paired with a Min Height control so centering is meaningful). - const setPropStylesContinuous = (patch: Record, controlKey: string) => - continuousSetProp(actions, selectedId, controlKey, (p: any) => { p.style = { ...p.style, ...patch }; }); - const isColumns = nodeProps.columns !== undefined || nodeProps.split !== undefined; const vAlignKey = isColumns ? 'alignItems' : 'justifyContent'; + // "Stack on this device" (Columns, tablet/mobile only): a flexDirection + // override. Turning it off removes this device's override; if the columns + // would still inherit a stack (mobile under a stacked tablet), it writes + // 'row' instead so the toggle really un-stacks this device. + const stacked = style.flexDirection === 'column'; + const inheritsStack = + device !== 'desktop' && + resolveStyleForDevice(withoutOverride(nodeProps, device, 'flexDirection'), device).flexDirection === 'column'; + const toggleStack = () => setPropStyle('flexDirection', stacked ? (inheritsStack ? 'row' : '') : 'column'); + return ( <> {nodeProps.cssId !== undefined && ( @@ -99,7 +109,7 @@ export const ContainerStylePanel: React.FC = ({ selectedId, nod />
- Alignment + Alignment{dot('textAlign')}
{(['left', 'center', 'right', 'justify'] as const).map((a) => (
- Vertical Alignment + Vertical Alignment{dot(vAlignKey)} setPropStyle(vAlignKey, v)} />
+ {isColumns && device !== 'desktop' && ( +
+ Columns Layout{dot('flexDirection')} + +
+ )} {!isColumns && (
- Min Height + Min Height{dot('minHeight')} setPropStyleContinuous('minHeight', v)} @@ -134,7 +153,7 @@ export const ContainerStylePanel: React.FC = ({ selectedId, nod )} {/* Box model + border/effects + animation/visibility rollout */} - + diff --git a/craft/src/panels/right/styles/FormStylePanel.tsx b/craft/src/panels/right/styles/FormStylePanel.tsx index 6905b16..17380eb 100644 --- a/craft/src/panels/right/styles/FormStylePanel.tsx +++ b/craft/src/panels/right/styles/FormStylePanel.tsx @@ -28,6 +28,8 @@ import { useNodeProp, continuousSetProp, } from './shared'; +import { useResolvedStyle, MARGIN_KEYS, PADDING_KEYS } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; /* The full sanitizeInputType (utils/escape.ts) allowlist, plus the two fake "types" (textarea/select) that take their own ContactForm render @@ -175,7 +177,10 @@ export const FormStylePanel: React.FC = ({ selectedId, nodeProp const { actions } = useEditor(); const { setProp, setPropStyle, setProps, setPropContinuous, setPropStyleContinuous } = useNodeProp(selectedId); - const style = nodeProps.style || {}; + // Effective value for the current device (#45): the setters write + // tablet/mobile overrides, so the fields must read them back. + const style = useResolvedStyle(nodeProps) as Record; + const dot = (key: string | readonly string[]) => ; // ContactForm only: FormContainer/SubscribeForm have no destinationType prop, // so their relay controls stay exactly as they were. const isWebhook = nodeProps.destinationType === 'webhook'; @@ -484,7 +489,7 @@ export const FormStylePanel: React.FC = ({ selectedId, nodeProp setPropStyle('backgroundColor', v)} />
- Padding + Padding{dot(PADDING_KEYS)} setPropStyle('padding', v)} />
@@ -498,12 +503,12 @@ export const FormStylePanel: React.FC = ({ selectedId, nodeProp they all spread `style` onto their root element. */} Margin{dot(MARGIN_KEYS)}} value={{ top: style.marginTop, right: style.marginRight, bottom: style.marginBottom, left: style.marginLeft }} onChange={(side, v) => setPropStyleContinuous(`margin${SPACING_SIDE_KEYS.find((s) => s.side === side)!.suffix}`, v)} /> Padding (per side){dot(PADDING_KEYS)}} value={{ top: style.paddingTop, right: style.paddingRight, bottom: style.paddingBottom, left: style.paddingLeft }} onChange={(side, v) => setPropStyleContinuous(`padding${SPACING_SIDE_KEYS.find((s) => s.side === side)!.suffix}`, v)} /> diff --git a/craft/src/panels/right/styles/GenericPropsEditor.tsx b/craft/src/panels/right/styles/GenericPropsEditor.tsx index 8cd48b4..f056f3b 100644 --- a/craft/src/panels/right/styles/GenericPropsEditor.tsx +++ b/craft/src/panels/right/styles/GenericPropsEditor.tsx @@ -16,6 +16,8 @@ import { sectionGap, useNodeProp, } from './shared'; +import { useResolvedStyle, PADDING_KEYS } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; import { ArrayItemFieldsEditor } from './ArrayItemFields'; import { HtmlCodeField } from './HtmlCodeField'; @@ -35,7 +37,10 @@ export const GenericPropsEditor: React.FC<{ selectedId: string; nodeProps: Recor const stringProps = allProps.filter(([key, val]) => typeof val === 'string' && !/color/i.test(key)); const arrayProps = allProps.filter(([_, val]) => Array.isArray(val)); - const style = nodeProps.style || {}; + // Effective value for the current device (#45): the setters write + // tablet/mobile overrides, so the fields must read them back. + const style = useResolvedStyle(nodeProps) as Record; + const dot = (key: string | readonly string[]) => ; const hasCodeProp = typeof nodeProps.code === 'string'; return ( @@ -117,11 +122,11 @@ export const GenericPropsEditor: React.FC<{ selectedId: string; nodeProps: Recor setStyleValue('color', v)} />
- Padding + Padding{dot(PADDING_KEYS)} setStyleValue('padding', v)} />
- Text Alignment + Text Alignment{dot('textAlign')}
{(['left', 'center', 'right'] as const).map((a) => (
{/* Box model + animation/visibility rollout */} - + diff --git a/craft/src/panels/right/styles/MediaStylePanel.tsx b/craft/src/panels/right/styles/MediaStylePanel.tsx index 372d1b8..edc98ff 100644 --- a/craft/src/panels/right/styles/MediaStylePanel.tsx +++ b/craft/src/panels/right/styles/MediaStylePanel.tsx @@ -21,6 +21,8 @@ import { useNodeProp, continuousSetProp, } from './shared'; +import { useResolvedStyle } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; import { AssetPicker } from '../../../ui/AssetPicker'; import { BoxModelSection, BorderEffectsSection, AnimVisSection } from './mediaBoxModel'; @@ -36,7 +38,8 @@ export const MediaStylePanel: React.FC = ({ selectedId, nodePro const { actions } = useEditor(); const { setProp, setPropStyle, setProps, setPropContinuous, setPropStyleContinuous, setPropStyles } = useNodeProp(selectedId); - const style = nodeProps.style || {}; + const style = useResolvedStyle(nodeProps); + const dot = (key: string | readonly string[]) => ; // Same crop-fills-by-default + no stale-height-conflict treatment as // ImageStylePanel (see there for the full rationale): applying a ratio @@ -72,7 +75,7 @@ export const MediaStylePanel: React.FC = ({ selectedId, nodePro {nodeProps.videoUrl !== undefined && ( <> Width{dot('width')}} value={(style.width as string) || ''} onChange={(v) => setPropStyleContinuous('width', v)} /> @@ -276,7 +279,7 @@ export const MediaStylePanel: React.FC = ({ selectedId, nodePro {/* Box model + animation/visibility rollout (Video/Gallery/Map/Slider all carry these props now, so it's safe to render unconditionally). */} - + diff --git a/craft/src/panels/right/styles/OverrideDot.tsx b/craft/src/panels/right/styles/OverrideDot.tsx new file mode 100644 index 0000000..1cadf29 --- /dev/null +++ b/craft/src/panels/right/styles/OverrideDot.tsx @@ -0,0 +1,48 @@ +import React from 'react'; +import { useOverrideState } from './responsive-panel'; + +const DEVICE_NAME = { desktop: 'Desktop', tablet: 'Tablet', mobile: 'Mobile' } as const; + +export interface OverrideDotProps { + nodeProps: Record; + selectedId: string; + /** One style key, or a group shown as one dot (e.g. all four margin sides). */ + styleKey: string | readonly string[]; +} + +/** + * Per-device marker next to a field label (#45). On tablet/mobile: + * - filled dot = this device has its own value; click resets it (one undo step); + * - hollow dot = the field follows the larger device, so an edit here + * creates a device-specific value. + * Nothing on desktop, nor for keys that are never per-device (colours...). + */ +export const OverrideDot: React.FC = ({ nodeProps, selectedId, styleKey }) => { + const { overridden, inherited, device, resetsTo, reset } = useOverrideState(nodeProps, styleKey, selectedId); + if (device === 'desktop') return null; + const name = DEVICE_NAME[device]; + if (overridden) { + const title = `${name} override - click to reset to ${resetsTo}`; + return ( +
- Padding + Padding{dot(PADDING_KEYS)} setPropStyle('padding', v)} />
- Text Alignment + Text Alignment{dot('textAlign')}
{(['left', 'center', 'right'] as const).map((a) => (
- Padding + Padding{dot(PADDING_KEYS)} setPropStyle('padding', v)} />
@@ -262,7 +267,7 @@ export const SocialStylePanel: React.FC = ({ selectedId, nodePr {/* Box model: margin, padding (per-side), border, shadow, opacity */} Margin{dot(MARGIN_KEYS)}} value={{ top: style.marginTop as string, right: style.marginRight as string, bottom: style.marginBottom as string, left: style.marginLeft as string, @@ -270,7 +275,7 @@ export const SocialStylePanel: React.FC = ({ selectedId, nodePr onChange={(side, v) => setPropStyleContinuous(`margin${capSide(side)}`, v)} /> Padding{dot(PADDING_KEYS)}} value={{ top: style.paddingTop as string, right: style.paddingRight as string, bottom: style.paddingBottom as string, left: style.paddingLeft as string, diff --git a/craft/src/panels/right/styles/TextStylePanel.tsx b/craft/src/panels/right/styles/TextStylePanel.tsx index 7a980c2..2151847 100644 --- a/craft/src/panels/right/styles/TextStylePanel.tsx +++ b/craft/src/panels/right/styles/TextStylePanel.tsx @@ -26,6 +26,8 @@ import { labelStyle, useNodeProp, } from './shared'; +import { useResolvedStyle, MARGIN_KEYS, PADDING_KEYS } from './responsive-panel'; +import { OverrideDot } from './OverrideDot'; /* Text-transform is a small fixed enum with no natural home in the shared foundation presets (constants/presets.ts is import-only for this @@ -61,7 +63,8 @@ function opacityPercent(v: unknown): number { /* ---------- TEXT ---------- */ export const TextStylePanel: React.FC = ({ selectedId, nodeProps }) => { - const style: CSSProperties = nodeProps.style || {}; + const style: CSSProperties = useResolvedStyle(nodeProps); + const dot = (key: string | readonly string[]) => ; const { setProp, setPropStyle, setProps, setPropStyleContinuous } = useNodeProp(selectedId); @@ -87,7 +90,7 @@ export const TextStylePanel: React.FC = ({ selectedId, nodeProp />
- Text Size + Text Size{dot('fontSize')} = ({ selectedId, nodeProp />
- Line Height + Line Height{dot('lineHeight')} = ({ selectedId, nodeProp />
- Letter Spacing + Letter Spacing{dot('letterSpacing')} = ({ selectedId, nodeProp />
- Alignment + Alignment{dot('textAlign')}
{(['left', 'center', 'right', 'justify'] as const).map((a) => (
+ ); + }; + const btn = (id: string) => h!.container.querySelector(`[data-testid="${id}"]`) as HTMLElement; + + test('setPropStyles with mixed keys on tablet splits in ONE setProp (one undo step)', () => { + mount('tablet'); + h!.mountChild(); + act(() => { h!.actions.history.clear(); }); + click(btn('mixed')); + expect(props().styleTablet).toEqual({ fontSize: '20px', paddingTop: '4px' }); + expect(props().style.color).toBe('#ff0000'); + expect(props().style.fontFamily).toBe('Georgia'); + expect(props().style.fontSize).toBe('36px'); + expect(props().style.paddingTop).toBeUndefined(); + expect(undoDepth()).toBe(1); + expect(props().styleTablet).toBeUndefined(); + expect(props().style.color).toBe('#000000'); + }); + + test('a desktop drag then a mobile drag inside the burst window are separate undo steps', () => { + vi.useFakeTimers(); + mount('desktop'); + h!.mountChild(); + act(() => { h!.actions.history.clear(); }); + click(btn('cont-a')); + switchDevice('mobile'); + act(() => { vi.advanceTimersByTime(CONTINUOUS_EDIT_IDLE_MS / 5); }); + click(btn('cont-b')); + expect(props().style.fontSize).toBe('21px'); + expect(props().styleMobile.fontSize).toBe('22px'); + expect(undoDepth()).toBe(2); + }); + + test('exported helpers: set (continuous) + clear on tablet, desktop clear is a no-op', () => { + mount('desktop'); + act(() => { h!.actions.history.clear(); }); + act(() => setResponsiveStyleProp(h!.actions, 'h-1', 'tablet', 'width', '50%', { continuous: true, controlKey: 'drag' })); + act(() => setResponsiveStyleProp(h!.actions, 'h-1', 'tablet', 'width', '60%', { continuous: true, controlKey: 'drag' })); + expect(props().styleTablet).toEqual({ width: '60%' }); + expect(props().style.width).toBeUndefined(); + closeContinuousBurst(); + act(() => clearResponsiveStyleProp(h!.actions, 'h-1', 'desktop', 'width')); + act(() => clearResponsiveStyleProp(h!.actions, 'h-1', 'tablet', 'width')); + expect(props().styleTablet).toEqual({}); + expect(undoDepth()).toBe(2); // the drag burst + the clear + }); +}); + +describe('Columns "Stack on this device"', () => { + test('hidden on desktop; on mobile it writes flexDirection:column to the mobile override only', () => { + mountPanel('desktop', ContainerStylePanel, 'cols'); + expect(h!.container.querySelector('[data-testid="stack-columns"]')).toBeNull(); + switchDevice('mobile'); + const box = h!.container.querySelector('[data-testid="stack-columns"]') as HTMLInputElement; + click(box); + expect(props('cols').styleMobile).toEqual({ flexDirection: 'column' }); + expect(props('cols').style.flexDirection).toBeUndefined(); + click(h!.container.querySelector('[data-testid="stack-columns"]')!); + expect(props('cols').styleMobile).toEqual({}); + }); + + test('un-stacking mobile under a stacked tablet writes row', () => { + const s = JSON.parse(state()); + s.cols.props.styleTablet = { flexDirection: 'column' }; + mountPanel('mobile', ContainerStylePanel, 'cols', JSON.stringify(s)); + const box = h!.container.querySelector('[data-testid="stack-columns"]') as HTMLInputElement; + expect(box.checked).toBe(true); + click(box); + expect(props('cols').styleMobile).toEqual({ flexDirection: 'row' }); + }); +}); + +describe('DeviceBanner in GuidedStyles', () => { + test('shown on tablet/mobile only; "Back to desktop" switches the device', () => { + mount('desktop'); + h!.mountChild(); + act(() => { h!.actions.selectNode('h-1'); }); + expect(h!.container.querySelector('[data-testid="device-banner"]')).toBeNull(); + switchDevice('tablet'); + const banner = h!.container.querySelector('[data-testid="device-banner"]')!; + expect(banner.textContent).toContain('Editing Tablet'); + expect(banner.textContent).toContain('Colours and text apply everywhere'); + click(banner.querySelector('button')!); + expect(h!.container.querySelector('[data-testid="device-banner"]')).toBeNull(); + }); +}); + +describe('every useNodeProp panel reads back what it writes per device (fix round 1)', () => { + // Panels that edit responsive keys through the device-aware setters must + // show the RESOLVED value and a dot, or a mobile edit looks like a no-op. + const blockState = () => + JSON.stringify({ + ROOT: node('Container', { tag: 'div', style: {} }, ['blk']), + blk: node('Container', { tag: 'div', style: { padding: '8px', marginTop: '0', marginRight: '0', marginBottom: '0', marginLeft: '0', textAlign: 'left' } }, [], { parent: 'ROOT' }), + }); + const withType = (P: React.FC, typeName: string): React.FC => (p) =>

; + const panels: [string, React.FC, { preset: boolean; spacing: boolean; align?: string }][] = [ + ['SocialStylePanel', SocialStylePanel, { preset: true, spacing: true }], + ['SectionTypePanel', withType(SectionTypePanel, 'Accordion'), { preset: true, spacing: true, align: 'Text Alignment' }], + ['PricingStylePanel', PricingStylePanel, { preset: false, spacing: true }], + ['FormStylePanel', FormStylePanel, { preset: true, spacing: true }], + ['GenericPropsEditor', withType(GenericPropsEditor, 'Thing'), { preset: true, spacing: false, align: 'Text Alignment' }], + ]; + + const marginControl = () => + Array.from(h!.container.querySelectorAll('[data-testid="spacing-control"]')) + .find((el) => (el.querySelector('.guided-section-label')?.textContent || '').startsWith('Margin')) as HTMLElement; + + test.each(panels)('%s: mobile edits land in styleMobile and the panel shows them with a dot', (_n, Panel, has) => { + mountPanel('mobile', Panel, 'blk', blockState()); + if (has.preset) { + expect(activePreset('Padding')).toBe('S'); + expect(hollowDot('Padding')).not.toBeNull(); + click(presetBtn('Padding', 'L')); + expect(props('blk').styleMobile?.padding).toBe('24px'); + expect(props('blk').style.padding).toBe('8px'); + expect(activePreset('Padding')).toBe('L'); + expect(filledDot('Padding')).not.toBeNull(); + } + if (has.spacing) { + const m = marginControl(); + expect(m.querySelector('button.preset-btn.active')?.textContent).toBe('None'); + click(Array.from(m.querySelectorAll('button.preset-btn')).find((b) => b.textContent === 'M')!); + expect(props('blk').styleMobile?.marginTop).toBe('16px'); + expect(props('blk').style.marginTop).toBe('0'); + expect(marginControl().querySelector('button.preset-btn.active')?.textContent).toBe('M'); + const dot = marginControl().querySelector('[data-testid="override-dot"]') as HTMLElement; + expect(dot).not.toBeNull(); + click(dot); + expect(props('blk').styleMobile?.marginTop).toBeUndefined(); + expect(marginControl().querySelector('button.preset-btn.active')?.textContent).toBe('None'); + } + if (has.align) { + click(section(has.align).querySelector('button[title="center"]')!); + expect(props('blk').styleMobile?.textAlign).toBe('center'); + expect(props('blk').style.textAlign).toBe('left'); + expect(section(has.align).querySelector('button.preset-btn.active')?.getAttribute('title')).toBe('center'); + expect(filledDot(has.align)).not.toBeNull(); + } + }); +}); + +describe('end to end: export', () => { + test('a mobile font-size edit exports as a scoped max-width:767px rule', () => { + mountPanel('mobile'); + click(presetBtn('Text Size', 'XL')); + const { html } = exportBodyHtml(h!.getSerialized()); + expect(html).toContain('@media (max-width:767px)'); + expect(html).toContain('font-size:24px!important'); + expect(html).toMatch(/]*style="[^"]*font-size:\s*36px/); // desktop inline style unchanged + }); +}); + +describe('canvas: overrides never remount the node (inline editing keeps focus/caret)', () => { + const ResetProbe: React.FC = () => { + const { setPropStyle } = useNodeProp('h-1'); + return +

+ ); +}; diff --git a/craft/src/panels/right/styles/shared.tsx b/craft/src/panels/right/styles/shared.tsx index 03c912f..1b0aed4 100644 --- a/craft/src/panels/right/styles/shared.tsx +++ b/craft/src/panels/right/styles/shared.tsx @@ -12,6 +12,9 @@ import { getPref, setPref } from '../../../utils/local-prefs'; import { useSiteDesign, DEFAULT_SITE_DESIGN, SiteDesign } from '../../../state/SiteDesignContext'; import { COLOR_TOKENS, resolveTokenValue, tokenKeyOf } from '../../../utils/tokens'; import { TokenSwatchRow } from './TokenSwatchRow'; +import { useDevice } from '../../../state/DeviceContext'; +import { RESPONSIVE_STYLE_KEYS } from '../../../utils/responsive-style'; +import type { DeviceMode } from '../../../types'; /* ---------- Helper: auto text color for bg ---------- `bg` may be a token reference (`var(--wsb-primary)`); it is resolved through @@ -516,23 +519,96 @@ export function discreteSetProp(actions: any, nodeId: string, updater: (p: any) actions.setProp(nodeId, updater); } +/* ---------- Per-device style writes (#45) ---------- + On tablet/mobile a RESPONSIVE_STYLE_KEYS key is written into that device's + partial (`styleTablet` / `styleMobile`) and never into `style`, so editing + a smaller device can't change desktop. Every other key (colours, fonts, + borders...) is not per-device and still writes `style`. An empty value on + tablet/mobile REMOVES the override (falls back to the inherited value) + rather than storing a `''` that overrides nothing -- same rule as + `withResponsiveValue` in utils/responsive-style.ts. */ +const OVERRIDE_FIELD: Record<'tablet' | 'mobile', 'styleTablet' | 'styleMobile'> = { + tablet: 'styleTablet', + mobile: 'styleMobile', +}; + +const isEmptyStyleValue = (v: unknown) => v === undefined || v === null || v === ''; + +/** Mutates the Craft props draft `p`: routes each key of `patch` to `style` + * or to `device`'s override partial (see above). One call = one setProp. */ +function applyStylePatch(p: any, device: DeviceMode, patch: Record) { + let base: Record | null = null; + let over: Record | null = null; + const field = device === 'desktop' ? null : OVERRIDE_FIELD[device]; + for (const [k, v] of Object.entries(patch)) { + if (field && RESPONSIVE_STYLE_KEYS.has(k)) { + const o: Record = (over ??= { ...(p[field] || {}) }); + if (isEmptyStyleValue(v)) delete o[k]; + else o[k] = v; + } else { + (base ??= { ...p.style })[k] = v; + } + } + if (base) p.style = base; + if (field && over) p[field] = over; +} + +/** Burst identity for a continuous style edit: includes the device, so a + * desktop drag followed by a mobile drag never merges into one undo step. */ +const styleControlKey = (device: DeviceMode, controlKey: string) => `${device}:${controlKey}`; + +/** Writes one style key for `device` (desktop → `style`, tablet/mobile → + * that device's override for responsive keys). Discrete by default (its own + * undo step); `continuous` joins the undo burst of `controlKey` + * (default `style.`). */ +export function setResponsiveStyleProp( + actions: any, + nodeId: string, + device: DeviceMode, + key: string, + value: unknown, + opts: { continuous?: boolean; controlKey?: string } = {}, +) { + const update = (p: any) => applyStylePatch(p, device, { [key]: value }); + if (opts.continuous) continuousSetProp(actions, nodeId, styleControlKey(device, opts.controlKey ?? `style.${key}`), update); + else discreteSetProp(actions, nodeId, update); +} + +/** Removes `device`'s override of `key` (or of every key in a group, e.g. the + * four padding sides) in ONE discrete setProp = one undo step. No-op on + * desktop: the desktop value is the base, there is nothing to reset to. */ +export function clearResponsiveStyleProp(actions: any, nodeId: string, device: DeviceMode, key: string | readonly string[]) { + if (device === 'desktop') return; + const field = OVERRIDE_FIELD[device]; + const keys = typeof key === 'string' ? [key] : key; + discreteSetProp(actions, nodeId, (p: any) => { + const next = { ...(p[field] || {}) }; + keys.forEach((k) => { delete next[k]; }); + p[field] = next; + }); +} + /* ---------- useNodeProp ---------- Shared setProp/setPropStyle boilerplate repeated across many *StylePanel.tsx files. Only adopted where a panel's inline definitions were byte-equivalent to this (some panels use different param names or extra logic and keep - their own local versions). */ + their own local versions). The style setters are device-aware (#45): see + applyStylePatch above. */ export function useNodeProp(selectedId: string) { const { actions } = useEditor(); + const { device } = useDevice(); // Default setters are plain: one call = one undo step. Continuous inputs // (colour pickers, sliders, typing) use the *Continuous variants. const setProp = useCallback((key: string, value: any) => discreteSetProp(actions, selectedId, (p: any) => { p[key] = value; }), [actions, selectedId]); - const setPropStyle = useCallback((prop: string, value: string) => discreteSetProp(actions, selectedId, (p: any) => { p.style = { ...p.style, [prop]: value }; }), [actions, selectedId]); + const setPropStyle = useCallback((prop: string, value: string) => setResponsiveStyleProp(actions, selectedId, device, prop, value), [actions, selectedId, device]); // Several props / style keys in ONE setProp call = exactly one undo step. const setProps = useCallback((patch: Record) => discreteSetProp(actions, selectedId, (p: any) => { Object.assign(p, patch); }), [actions, selectedId]); - const setPropStyles = useCallback((patch: Record) => discreteSetProp(actions, selectedId, (p: any) => { p.style = { ...p.style, ...patch }; }), [actions, selectedId]); + const setPropStyles = useCallback((patch: Record) => discreteSetProp(actions, selectedId, (p: any) => applyStylePatch(p, device, patch)), [actions, selectedId, device]); const setPropContinuous = useCallback((key: string, value: any) => continuousSetProp(actions, selectedId, key, (p: any) => { p[key] = value; }), [actions, selectedId]); - const setPropStyleContinuous = useCallback((prop: string, value: string) => continuousSetProp(actions, selectedId, `style.${prop}`, (p: any) => { p.style = { ...p.style, [prop]: value }; }), [actions, selectedId]); - return { setProp, setPropStyle, setProps, setPropStyles, setPropContinuous, setPropStyleContinuous }; + const setPropStyleContinuous = useCallback((prop: string, value: string) => setResponsiveStyleProp(actions, selectedId, device, prop, value, { continuous: true }), [actions, selectedId, device]); + // Several style keys as one continuous burst (`controlKey` names the control). + const setPropStylesContinuous = useCallback((patch: Record, controlKey: string) => continuousSetProp(actions, selectedId, styleControlKey(device, controlKey), (p: any) => applyStylePatch(p, device, patch)), [actions, selectedId, device]); + return { setProp, setPropStyle, setProps, setPropStyles, setPropContinuous, setPropStyleContinuous, setPropStylesContinuous }; } /* ---------- Array Prop Editor (reusable for features, items, plans, etc.) ---------- */ @@ -681,7 +757,7 @@ export const NumericUnitInput: React.FC = ({ (label="Width" / label="Height") for components needing both, e.g. ImageBlock / VideoBlock sizing. */ export interface SizeControlProps { - label: string; + label: React.ReactNode; /** CSS value for this one dimension, e.g. "100%" / "400px" / "auto" / "". */ value: string; onChange: (value: string) => void; @@ -800,7 +876,7 @@ export type SpacingSide = 'top' | 'right' | 'bottom' | 'left'; export interface SpacingSideValues { top?: string; right?: string; bottom?: string; left?: string; } const SPACING_SIDES: SpacingSide[] = ['top', 'right', 'bottom', 'left']; export interface SpacingControlProps { - label: string; + label: React.ReactNode; value: SpacingSideValues; /** Called per-side. Linked edits/presets call this once for each of the * 4 sides (in top/right/bottom/left order) with the same value. */ diff --git a/craft/src/styles/editor.css b/craft/src/styles/editor.css index 64f434a..926a411 100644 --- a/craft/src/styles/editor.css +++ b/craft/src/styles/editor.css @@ -2662,3 +2662,72 @@ body { border: 1px solid var(--color-accent); border-radius: 3px; } + +/* #45 overrides */ +.wsb-override-dot { + display: inline-block; + width: 8px; + height: 8px; + margin-left: 6px; + padding: 0; + vertical-align: middle; + border-radius: 50%; + box-sizing: border-box; +} + +.wsb-override-dot.is-set { + background: var(--color-accent); + border: 1px solid var(--color-accent); + cursor: pointer; +} + +.wsb-override-dot.is-set:hover, +.wsb-override-dot.is-set:focus-visible { + background: var(--color-danger); + border-color: var(--color-danger); + outline: none; +} + +.wsb-override-dot.is-inherited { + background: transparent; + border: 1px solid var(--color-text-dim); +} + +.wsb-device-banner { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 6px 8px; + margin: 0 0 8px; + padding: 8px 10px; + font-size: 11px; + line-height: 1.4; + color: var(--color-text); + background: var(--color-accent-subtle); + border: 1px solid var(--color-accent); + border-radius: var(--radius-md); +} + +.wsb-device-banner > span { + flex: 1 1 180px; +} + +.wsb-device-banner-back { + padding: 0; + font: inherit; + font-weight: 600; + color: var(--color-accent); + background: none; + border: 0; + cursor: pointer; + text-decoration: underline; +} + +.wsb-stack-toggle { + display: flex; + align-items: center; + gap: 6px; + font-size: 11px; + color: var(--color-text); + cursor: pointer; +}