Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: apply margin to wrapperStyles when drag wrapper is enabled [ALT-1293] #784

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.cf-divider {
display: contents;
position: relative;
width: 100%;
height: 100%;
}

.cf-divider hr {
border: none;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import React from 'react';
import './ContentfulDivider.css';
import { css, cx } from 'emotion';

const styles = {
hr: css({
border: 'none',
}),
};
Comment on lines -5 to -9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The components package doesn't have emotion installed. I moved this style to the component's css file instead.


export type ContentfulDividerProps = {
className?: string;
};

export const ContentfulDivider = (props: ContentfulDividerProps) => {
const { className } = props;
export const ContentfulDivider = ({ className = '', ...props }: ContentfulDividerProps) => {
Comment on lines -15 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This subtle change makes it so className will not be spread via ...props to the div.cf-divider element

return (
<div className="cf-divider" {...props}>
<hr className={cx(styles.hr, className)} />
<hr className={className} />
</div>
);
};
8 changes: 5 additions & 3 deletions packages/components/src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const Image: React.FC<ImageProps> = ({ className = '', src, cfImageAsset,
);
}

const imageClasses = combineClasses('cf-image', className);

if (typeof cfImageAsset === 'string') {
return <img src={cfImageAsset} className={'cf-image ' + className} {...props} />;
return <img src={cfImageAsset} className={imageClasses} {...props} />;
}

if (cfImageAsset) {
Expand All @@ -41,13 +43,13 @@ export const Image: React.FC<ImageProps> = ({ className = '', src, cfImageAsset,
srcSet={cfImageAsset.srcSet?.length ? cfImageAsset.srcSet?.join(', ') : undefined}
sizes={cfImageAsset.sizes ? cfImageAsset.sizes : undefined}
loading={cfImageAsset.loading}
className={'cf-image ' + className}
className={imageClasses}
{...props}
/>
);
}

if (src) {
return <img src={src} className={'cf-image ' + className} {...props} />;
return <img src={src} className={imageClasses} {...props} />;
}
};
12 changes: 6 additions & 6 deletions packages/visual-editor/src/hooks/useComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export const useComponent = ({
}, [node]);

const componentId = node.data.id;
const isAssembly = node.type === 'assembly';
const isStructureComponent = isContentfulStructureComponent(node.data.blockId);
const requiresDragWrapper =
!isAssembly && !isStructureComponent && !componentRegistration?.options?.wrapComponent;

const { componentProps, wrapperStyles } = useComponentProps({
node,
Expand All @@ -72,6 +76,7 @@ export const useComponent = ({
renderDropzone,
definition: componentRegistration?.definition,
userIsDragging,
requiresDragWrapper,
});

const elementToRender = (props?: { dragProps?: DragWrapperProps; rest?: unknown }) => {
Expand All @@ -88,14 +93,9 @@ export const useComponent = ({
...customComponentProps
} = componentProps;

const isStructureComponent = isContentfulStructureComponent(node.data.blockId);
const isAssembly = node.type === 'assembly';
const modifiedProps =
isStructureComponent || isAssembly ? componentProps : customComponentProps;

const requiresDragWrapper =
!isStructureComponent && componentRegistration.options?.wrapComponent === false;

const element = React.createElement(
ImportedComponentErrorBoundary,
{ componentId: node.data.blockId },
Expand All @@ -105,7 +105,7 @@ export const useComponent = ({
}),
);

if (!requiresDragWrapper || isAssembly) {
if (!requiresDragWrapper) {
return element;
}

Expand Down
49 changes: 44 additions & 5 deletions packages/visual-editor/src/hooks/useComponentProps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ASSEMBLY_NODE_TYPE } from '@contentful/experiences-core/constants';
import { ASSEMBLY_NODE_TYPE, CONTENTFUL_COMPONENTS } from '@contentful/experiences-core/constants';
import { useComponentProps } from './useComponentProps';
import { ComponentDefinition, ExperienceTreeNode } from '@contentful/experiences-core/types';
import { vi } from 'vitest';
Expand Down Expand Up @@ -102,8 +102,8 @@ describe('useComponentProps', () => {

describe('structure components', () => {
const definition: ComponentDefinition = {
id: 'contentful-section',
name: 'Section',
id: CONTENTFUL_COMPONENTS.section.id,
name: CONTENTFUL_COMPONENTS.section.name,
variables: {
cfWidth: { type: 'Text' },
cfHeight: { type: 'Text' },
Expand All @@ -113,7 +113,7 @@ describe('useComponentProps', () => {
data: {
id: 'id',
// This block id will identify the component as a structure component
blockId: 'contentful-section',
blockId: CONTENTFUL_COMPONENTS.section.id,
props: {
cfWidth: {
type: 'DesignValue',
Expand Down Expand Up @@ -160,6 +160,8 @@ describe('useComponentProps', () => {
variables: {
cfWidth: { type: 'Text' },
cfHeight: { type: 'Text' },
cfMaxWidth: { type: 'Text' },
cfMargin: { type: 'Text' },
},
};
const node: ExperienceTreeNode = {
Expand All @@ -180,6 +182,18 @@ describe('useComponentProps', () => {
[desktop.id]: '50%',
},
},
cfMaxWidth: {
type: 'DesignValue',
valuesByBreakpoint: {
[desktop.id]: '50%',
},
},
cfMargin: {
type: 'DesignValue',
valuesByBreakpoint: {
[desktop.id]: '10px 0 10px 0',
},
},
},
unboundValues: {},
dataSource: {},
Expand All @@ -189,22 +203,47 @@ describe('useComponentProps', () => {
type: 'block',
};

it('should set the component size in wrapperStyles and set 100% size in componentStyles', () => {
it('should set the component size in wrapperStyles when drag wrapper is enabled', () => {
const { result } = renderHook(() =>
useComponentProps({
node,
areEntitiesFetched,
resolveDesignValue,
renderDropzone,
definition,
requiresDragWrapper: true,
userIsDragging,
}),
);

expect(result.current.wrapperStyles.width).toEqual('50%');
expect(result.current.wrapperStyles.height).toEqual('50%');
expect(result.current.wrapperStyles.maxWidth).toEqual('50%');
expect(result.current.wrapperStyles.margin).toEqual('10px 0 10px 0');

expect(result.current.componentStyles.width).toEqual('100%');
expect(result.current.componentStyles.height).toEqual('100%');
expect(result.current.componentStyles.maxWidth).toEqual('none');
expect(result.current.componentStyles.margin).toEqual('0');
});

it('should set the component size in componentStyles when drag wrapper is disabled', () => {
const { result } = renderHook(() =>
useComponentProps({
node,
areEntitiesFetched,
resolveDesignValue,
renderDropzone,
definition,
requiresDragWrapper: false,
userIsDragging,
}),
);

expect(result.current.componentStyles.width).toEqual('50%');
expect(result.current.componentStyles.height).toEqual('50%');
expect(result.current.componentStyles.maxWidth).toEqual('50%');
expect(result.current.componentStyles.margin).toEqual('10px 0 10px 0');
});
});
});
21 changes: 16 additions & 5 deletions packages/visual-editor/src/hooks/useComponentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type UseComponentProps = {
renderDropzone: RenderDropzoneFunction;
userIsDragging: boolean;
slotId?: string;
requiresDragWrapper?: boolean;
};

export const useComponentProps = ({
Expand All @@ -60,6 +61,7 @@ export const useComponentProps = ({
renderDropzone,
definition,
userIsDragging,
requiresDragWrapper,
}: UseComponentProps) => {
const unboundValues = useEditorStore((state) => state.unboundValues);
const hyperlinkPattern = useEditorStore((state) => state.hyperLinkPattern);
Expand Down Expand Up @@ -223,18 +225,27 @@ export const useComponentProps = ({

// Move size styles to the wrapping div and override the component styles
const overrideStyles: CSSProperties = {};
const wrapperStyles: CSSProperties = {
width: cfStyles.width,
maxWidth: cfStyles.maxWidth,
};
if (!isStructureComponent) {
const wrapperStyles: CSSProperties = {};
if (requiresDragWrapper) {
if (cfStyles.height) {
wrapperStyles.height = cfStyles.height;
overrideStyles.height = '100%';
}

if (cfStyles.width) {
wrapperStyles.width = cfStyles.width;
overrideStyles.width = '100%';
}

if (cfStyles.maxWidth) {
wrapperStyles.maxWidth = cfStyles.maxWidth;
overrideStyles.maxWidth = 'none';
}

if (cfStyles.margin) {
wrapperStyles.margin = cfStyles.margin;
overrideStyles.margin = '0';
}
}

// Styles that will be applied to the component element
Expand Down
Loading