Files
site-builder/craft/src/panels/right/styles/responsive-panel.tsx
T
shadowdao c2a90cd7a7 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
2026-10-07 11:51:53 -07:00

97 lines
4.2 KiB
TypeScript

import React, { CSSProperties, useCallback, useMemo } from 'react';
import { useEditor } from '@craftjs/core';
import { useDevice } from '../../../state/DeviceContext';
import type { DeviceMode } from '../../../types';
import {
RESPONSIVE_STYLE_KEYS,
hasOverride,
resolveStyleForDevice,
type ResponsiveProps,
} from '../../../utils/responsive-style';
import { clearResponsiveStyleProp } from './shared';
/* ==========================================================================
Per-device panel helpers (#45). Panels show the EFFECTIVE value for the
current device (desktop → style, tablet → style+styleTablet, mobile →
style+styleTablet+styleMobile) and write through the device-aware
useNodeProp setters in shared.tsx, so editing tablet/mobile never touches
desktop.
========================================================================== */
/** Dot groups: one dot per spacing control, set when ANY side (or the
* legacy shorthand) is overridden; a reset clears the whole group. */
export const MARGIN_KEYS = ['marginTop', 'marginRight', 'marginBottom', 'marginLeft', 'margin'] as const;
export const PADDING_KEYS = ['paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'padding'] as const;
const DEVICE_LABEL: Record<DeviceMode, string> = { desktop: 'Desktop', tablet: 'Tablet', mobile: 'Mobile' };
/** The node's style as rendered on the current device. */
export function useResolvedStyle(nodeProps: Record<string, any>): CSSProperties {
const { device } = useDevice();
return useMemo(
() => resolveStyleForDevice(nodeProps as ResponsiveProps, device),
// The three style objects are replaced (never mutated) on every write.
// eslint-disable-next-line react-hooks/exhaustive-deps
[nodeProps.style, nodeProps.styleTablet, nodeProps.styleMobile, device],
);
}
export interface OverrideState {
/** The current (tablet/mobile) device stores its own value for the key(s). */
overridden: boolean;
/** Non-desktop, responsive key, no own value: it follows the larger device. */
inherited: boolean;
device: DeviceMode;
/** What a reset falls back to: 'tablet' when mobile inherits a tablet value. */
resetsTo: 'desktop' | 'tablet';
/** Removes this device's override of the key(s): one undo step. */
reset: () => void;
}
/**
* Override status of `key` (or a group, e.g. the four padding sides -- the
* group counts as overridden when ANY member is) on the current device.
* `selectedId` is needed for `reset`.
*/
export function useOverrideState(
nodeProps: Record<string, any>,
key: string | readonly string[],
selectedId: string,
): OverrideState {
const { device } = useDevice();
const { actions } = useEditor();
const keys = typeof key === 'string' ? [key] : key;
const props = nodeProps as ResponsiveProps;
const responsive = keys.some((k) => RESPONSIVE_STYLE_KEYS.has(k));
const overridden = device !== 'desktop' && keys.some((k) => hasOverride(props, device, k));
const inherited = device !== 'desktop' && responsive && !overridden;
const resetsTo = device === 'mobile' && keys.some((k) => hasOverride(props, 'tablet', k)) ? 'tablet' : 'desktop';
const keyId = keys.join('|');
const reset = useCallback(
() => clearResponsiveStyleProp(actions, selectedId, device, keyId.split('|')),
[actions, selectedId, device, keyId],
);
return { overridden, inherited, device, resetsTo, reset };
}
/** "You are editing tablet/mobile" strip under the panel's type header.
* Renders nothing on desktop. */
export const DeviceBanner: React.FC = () => {
const { device, setDevice } = useDevice();
if (device === 'desktop') return null;
const name = DEVICE_LABEL[device];
const scope = device === 'tablet' ? 'tablet and below' : 'mobile only';
return (
<div className="wsb-device-banner" role="status" data-testid="device-banner">
<i className={`fa fa-${device === 'tablet' ? 'tablet' : 'mobile'}`} aria-hidden />
<span>
<strong>Editing {name}</strong> - spacing, size and alignment changes apply to {scope}.
{' '}Colours and text apply everywhere.
</span>
<button type="button" className="wsb-device-banner-back" onClick={() => setDevice('desktop')}>
Back to desktop
</button>
</div>
);
};