Skip to content

Commit 0624fc8

Browse files
committed
Check for ResizeObserver availability in flyout using hasResizeObserver
1 parent ef70c47 commit 0624fc8

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/components/collapsible_nav_beta/__snapshots__/collapsible_nav_beta.test.tsx.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
exports[`EuiCollapsibleNavBeta renders 1`] = `
44
<body
55
class="euiBody--hasFlyout"
6-
style="padding-inline-start: 0px;"
76
>
87
<div>
98
<div
@@ -56,7 +55,6 @@ exports[`EuiCollapsibleNavBeta renders 1`] = `
5655
exports[`EuiCollapsibleNavBeta renders initialIsCollapsed 1`] = `
5756
<body
5857
class="euiBody--hasFlyout"
59-
style="padding-inline-start: 0px;"
6058
>
6159
<div>
6260
<div

src/components/flyout/flyout.test.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ jest.mock('../portal', () => ({
2525
EuiPortal: ({ children }: { children: any }) => children,
2626
}));
2727

28-
global.ResizeObserver = class MockedResizeObserver {
29-
observe = jest.fn();
30-
unobserve = jest.fn();
31-
disconnect = jest.fn();
32-
};
33-
3428
describe('EuiFlyout', () => {
3529
shouldRenderCustomStyles(
3630
<EuiFlyout {...requiredProps} onClose={() => {}} />,

src/components/flyout/flyout.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
useGeneratedHtmlId,
3333
} from '../../services';
3434
import { logicalStyle } from '../../global_styling';
35+
import { hasResizeObserver } from '../observer/resize_observer/resize_observer';
3536

3637
import { CommonProps, PropsOfElement } from '../common';
3738
import { EuiFocusTrap, EuiFocusTrapProps } from '../focus_trap';
@@ -221,25 +222,29 @@ export const EuiFlyout = forwardRef(
221222

222223
// We manually set up a resize observer here because useResizeObserver calls getBoundingClientRect
223224
// which results in layout thrashing when the width changes frequently, such as with a resizable flyout
224-
const observer = new ResizeObserver((entries) => {
225-
const width = entries[0]?.borderBoxSize?.[0]?.inlineSize;
226-
227-
/**
228-
* Accomodate for the `isPushed` state by adding padding to the body equal to the width of the element
229-
*/
230-
if (isPushed) {
231-
if (side === 'right') {
232-
document.body.style.paddingInlineEnd = `${width}px`;
233-
} else if (side === 'left') {
234-
document.body.style.paddingInlineStart = `${width}px`;
225+
let observer: ResizeObserver | undefined;
226+
227+
if (hasResizeObserver) {
228+
observer = new ResizeObserver((entries) => {
229+
const width = entries[0]?.borderBoxSize?.[0]?.inlineSize;
230+
231+
/**
232+
* Accomodate for the `isPushed` state by adding padding to the body equal to the width of the element
233+
*/
234+
if (isPushed) {
235+
if (side === 'right') {
236+
document.body.style.paddingInlineEnd = `${width}px`;
237+
} else if (side === 'left') {
238+
document.body.style.paddingInlineStart = `${width}px`;
239+
}
235240
}
236-
}
237-
});
241+
});
238242

239-
observer.observe(resizeRef);
243+
observer.observe(resizeRef);
244+
}
240245

241246
return () => {
242-
observer.disconnect();
247+
observer?.disconnect();
243248

244249
if (isPushed) {
245250
if (side === 'right') {

0 commit comments

Comments
 (0)