-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove react-sizeme and repalce with our own HOC
- Loading branch information
1 parent
1c71ba2
commit 1afb28a
Showing
6 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import PropTypes from 'prop-types'; | ||
import { withSize } from '../../../src/extend/withSize'; | ||
|
||
/** Mock ResizeObserver */ | ||
class ResizeObserver { | ||
/** */ | ||
constructor(callback) { | ||
this.callback = callback; | ||
} | ||
|
||
/** */ | ||
observe(element) { | ||
// Fake a resize event | ||
setTimeout(() => { | ||
this.callback([{ contentRect: { height: 300, width: 400 } }]); | ||
}, 0); | ||
} | ||
|
||
/** */ | ||
disconnect() { jest.fn(); } // eslint-disable-line | ||
} | ||
|
||
// Replace the global ResizeObserver with the mock | ||
global.ResizeObserver = ResizeObserver; | ||
|
||
/** */ | ||
const TestComponent = ({ size }) => ( | ||
<div> | ||
{size.width} | ||
{size.height} | ||
</div> | ||
); | ||
|
||
TestComponent.propTypes = { | ||
size: PropTypes.shape({ | ||
height: PropTypes.number, | ||
width: PropTypes.number, | ||
}).isRequired, | ||
}; | ||
|
||
const WrappedTestComponent = withSize()(TestComponent); | ||
|
||
test('it should render with size', async () => { | ||
render(<WrappedTestComponent />); | ||
|
||
// Assert that the updated size is reflected | ||
expect(await screen.findByText(/400/)).toBeInTheDocument(); | ||
expect(await screen.findByText(/300/)).toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** This file was written to replace https://github.com/ctrlplusb/react-sizeme | ||
* when its dependencies went out of date and is very much inspired by its code. | ||
*/ | ||
|
||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
/** */ | ||
export function withSize() { | ||
return function WrapComponent(WrappedComponent) { | ||
/** */ | ||
const SizeAwareComponent = (props) => { | ||
const [size, setSize] = useState({ height: undefined, width: undefined }); | ||
const elementRef = useRef(null); | ||
const observerRef = useRef(null); | ||
|
||
useEffect(() => { | ||
/** */ | ||
const handleResize = (entries) => { | ||
for (const entry of entries) { | ||
const { width, height } = entry.contentRect; | ||
setSize({ height, width }); | ||
} | ||
}; | ||
|
||
observerRef.current = new ResizeObserver(handleResize); | ||
|
||
if (elementRef.current) { | ||
observerRef.current.observe(elementRef.current); | ||
} | ||
|
||
return () => { | ||
if (observerRef.current) { | ||
observerRef.current.disconnect(); | ||
} | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div ref={elementRef}> | ||
<WrappedComponent size={size} {...props} /> | ||
</div> | ||
); | ||
}; | ||
|
||
return SizeAwareComponent; | ||
}; | ||
} |