Skip to content

Commit e50070e

Browse files
committed
try switching to resize observer
1 parent 1bcb0d3 commit e50070e

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import { useCallback, useRef, useState } from 'react';
4+
5+
export function useAppLayoutVisible() {
6+
const observerRef = useRef<ResizeObserver | null>(null);
7+
const [isVisible, setVisible] = useState(true);
8+
const ref = useCallback((node: HTMLElement | null) => {
9+
if (observerRef.current) {
10+
observerRef.current.disconnect();
11+
}
12+
if (node && node.ownerDocument.defaultView && node.ownerDocument.defaultView.ResizeObserver) {
13+
observerRef.current = new node.ownerDocument.defaultView!.ResizeObserver(([entry]) => {
14+
setVisible(entry.borderBoxSize[0].inlineSize > 0 && entry.borderBoxSize[0].blockSize > 0);
15+
});
16+
observerRef.current.observe(node);
17+
}
18+
}, []);
19+
20+
return { ref, isVisible };
21+
}

src/app-layout/visual-refresh-toolbar/state/use-app-layout.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { useMergeRefs, useStableCallback, useUniqueId } from '@cloudscape-design
77
import { SplitPanelSideToggleProps } from '../../../internal/context/split-panel-context';
88
import { fireNonCancelableEvent } from '../../../internal/events';
99
import { useControllable } from '../../../internal/hooks/use-controllable';
10-
import { useIntersectionObserver } from '../../../internal/hooks/use-intersection-observer';
1110
import { useMobile } from '../../../internal/hooks/use-mobile';
1211
import { useGetGlobalBreadcrumbs } from '../../../internal/plugins/helpers/use-global-breadcrumbs';
1312
import globalVars from '../../../internal/styles/global-vars';
@@ -26,6 +25,7 @@ import {
2625
} from '../compute-layout';
2726
import { AppLayoutState } from '../interfaces';
2827
import { AppLayoutInternalProps, AppLayoutInternals } from '../interfaces';
28+
import { useAppLayoutVisible } from './use-app-layout-visible';
2929

3030
export const useAppLayout = (
3131
hasToolbar: boolean,
@@ -259,9 +259,8 @@ export const useAppLayout = (
259259
stickyNotifications: resolvedStickyNotifications,
260260
});
261261

262-
const { ref: intersectionObserverRef, isIntersecting } = useIntersectionObserver({ initialState: true });
263-
264-
const rootRef = useMergeRefs(rootRefInternal, intersectionObserverRef, onMountRootRef);
262+
const { ref: visibilityObserverRef, isVisible } = useAppLayoutVisible();
263+
const rootRef = useMergeRefs(rootRefInternal, visibilityObserverRef, onMountRootRef);
265264

266265
const discoveredBreadcrumbs = useGetGlobalBreadcrumbs(hasToolbar && !breadcrumbs);
267266

@@ -431,7 +430,7 @@ export const useAppLayout = (
431430

432431
return {
433432
rootRef,
434-
isIntersecting,
433+
isIntersecting: isVisible,
435434
appLayoutInternals,
436435
splitPanelInternals,
437436
widgetizedState: {

0 commit comments

Comments
 (0)