Skip to content

Commit af0d739

Browse files
authored
fix: support SSR environments where Element is not defined (#251) (#252)
The `draggableOpts.offsetParent` prop type used `PropTypes.instanceOf(Element)` which throws a ReferenceError in SSR/Node.js environments where the DOM global `Element` doesn't exist. This was introduced in a prior release that added the draggableOpts prop type definitions. The fix checks if Element is defined before using instanceOf.
1 parent deb5fe7 commit af0d739

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

__tests__/ssr.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
// #251 - Test SSR compatibility where Element is not defined
5+
6+
describe('SSR compatibility', () => {
7+
test('propTypes.js can be imported in Node.js environment without Element global', () => {
8+
// In Node.js environment (jest-environment: node), Element is not defined
9+
expect(typeof Element).toBe('undefined');
10+
11+
// This should not throw ReferenceError: Element is not defined
12+
expect(() => {
13+
require('../lib/propTypes');
14+
}).not.toThrow();
15+
});
16+
17+
test('Resizable.js can be imported in Node.js environment without Element global', () => {
18+
expect(typeof Element).toBe('undefined');
19+
20+
// This should not throw ReferenceError: Element is not defined
21+
expect(() => {
22+
require('../lib/Resizable');
23+
}).not.toThrow();
24+
});
25+
26+
test('ResizableBox.js can be imported in Node.js environment without Element global', () => {
27+
expect(typeof Element).toBe('undefined');
28+
29+
// This should not throw ReferenceError: Element is not defined
30+
expect(() => {
31+
require('../lib/ResizableBox');
32+
}).not.toThrow();
33+
});
34+
});

lib/propTypes.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ export const resizableProps: Object = {
7676
children: PropTypes.node,
7777
disabled: PropTypes.bool,
7878
enableUserSelectHack: PropTypes.bool,
79-
offsetParent: PropTypes.instanceOf(Element),
79+
// #251: Check for Element to support SSR environments where DOM globals don't exist
80+
offsetParent: typeof Element !== 'undefined' ? PropTypes.instanceOf(Element) : PropTypes.any,
8081
grid: PropTypes.arrayOf(PropTypes.number),
8182
handle: PropTypes.string,
8283
nodeRef: PropTypes.object,

0 commit comments

Comments
 (0)