Skip to content

Commit

Permalink
fix(sdk-react): Contentlets with empty HTML content needs to have min…
Browse files Browse the repository at this point in the history
…-height to edit it in UVE (#30122)

### Proposed Changes



https://github.com/user-attachments/assets/0873e554-def3-41fb-aafd-42510a2ab236

---------

Co-authored-by: Kevin Davila <[email protected]>
  • Loading branch information
KevinDavilaDotCMS and kevindaviladev authored Sep 25, 2024
1 parent 030f9f8 commit b5e9fac
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContext } from 'react';

import { PageContext } from '../../contexts/PageContext';
import { useCheckHaveContent } from '../../hooks/useCheckHaveContent';
import { DotCMSPageContext } from '../../models';
import { getContainersData } from '../../utils/utils';

Expand Down Expand Up @@ -46,6 +47,8 @@ export function Container({ containerRef }: ContainerProps) {

const { identifier, uuid } = containerRef;

const { haveContent, contentletDivRef } = useCheckHaveContent();

// Get the containers from the global context
const {
pageAsset: { containers },
Expand Down Expand Up @@ -96,7 +99,9 @@ export function Container({ containerRef }: ContainerProps) {
data-dot-type={contentlet.contentType}
data-dot-container={JSON.stringify(container)}
data-dot-on-number-of-pages={contentlet.onNumberOfPages}
key={contentlet.identifier}>
key={contentlet.identifier}
ref={contentletDivRef}
style={{ minHeight: haveContent ? undefined : '4rem' }}>
<Component {...contentlet} />
</div>
) : (
Expand Down
48 changes: 48 additions & 0 deletions core-web/libs/sdk/react/src/lib/hooks/useCheckHaveContent.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { renderHook } from '@testing-library/react';
import React from 'react';

import { useCheckHaveContent } from './useCheckHaveContent';

describe('useCheckHaveContent', () => {
it('should return true when there is content', () => {
const div = document.createElement('div');
const child = document.createElement('div');
div.appendChild(child);

jest.spyOn(child, 'getBoundingClientRect').mockImplementation(() => {
return {
bottom: 0,
height: 100,
left: 0,
right: 0,
top: 0,
width: 0,
x: 0,
y: 0,
toJSON: () => ({})
};
});

jest.spyOn(React, 'useRef').mockReturnValue({
current: div
});

const { result } = renderHook(() => useCheckHaveContent());

expect(result.current.haveContent).toBe(true);
});

it('should return false when there is no content', () => {
const div = document.createElement('div');
const child = document.createElement('div');
div.appendChild(child);

jest.spyOn(React, 'useRef').mockReturnValue({
current: div
});

const { result } = renderHook(() => useCheckHaveContent());

expect(result.current.haveContent).toBe(false);
});
});
30 changes: 30 additions & 0 deletions core-web/libs/sdk/react/src/lib/hooks/useCheckHaveContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useRef, useState } from 'react';

export const useCheckHaveContent = () => {
const contentletDivRef = useRef<HTMLDivElement | null>(null);
const [haveContent, setHaveContent] = useState<boolean>(false);

useEffect(() => {
if (!contentletDivRef.current) {
return;
}

const childElement = contentletDivRef.current.firstElementChild;

if (!childElement) {
return;
}

const height = childElement.getBoundingClientRect().height;

if (height > 0) {
setHaveContent(true);

return;
}

setHaveContent(false);
}, [contentletDivRef]);

return { contentletDivRef, haveContent };
};
6 changes: 3 additions & 3 deletions examples/nextjs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b5e9fac

Please sign in to comment.