-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dashboard][labs] Defer loading panels below the fold
- Loading branch information
1 parent
ae08154
commit 2d8efd7
Showing
17 changed files
with
294 additions
and
74 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
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
122 changes: 122 additions & 0 deletions
122
src/plugins/dashboard/public/application/embeddable/grid/dashboard_grid_item.tsx
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,122 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useState, useRef, useEffect, FC } from 'react'; | ||
import { EuiLoadingChart } from '@elastic/eui'; | ||
import classNames from 'classnames'; | ||
|
||
import { EmbeddableChildPanel } from '../../../services/embeddable'; | ||
import { useLabs } from '../../../services/presentation_util'; | ||
import { DashboardPanelState } from '../types'; | ||
|
||
type PanelProps = Pick<EmbeddableChildPanel, 'container' | 'PanelComponent'>; | ||
type DivProps = Pick<React.HTMLAttributes<HTMLDivElement>, 'className' | 'style' | 'children'>; | ||
|
||
interface Props extends PanelProps, DivProps { | ||
id: DashboardPanelState['explicitInput']['id']; | ||
type: DashboardPanelState['type']; | ||
focusedPanelId?: string; | ||
expandedPanelId?: string; | ||
key: string; | ||
isRenderable?: boolean; | ||
} | ||
|
||
const Item = React.forwardRef<HTMLDivElement, Props>( | ||
( | ||
{ | ||
container, | ||
expandedPanelId, | ||
focusedPanelId, | ||
id, | ||
PanelComponent, | ||
type, | ||
isRenderable = true, | ||
// The props below are passed from ReactGridLayoutn and need to be merged with their counterparts. | ||
// https://github.com/react-grid-layout/react-grid-layout/issues/1241#issuecomment-658306889 | ||
children, | ||
className, | ||
style, | ||
...rest | ||
}, | ||
ref | ||
) => { | ||
const expandPanel = expandedPanelId !== undefined && expandedPanelId === id; | ||
const hidePanel = expandedPanelId !== undefined && expandedPanelId !== id; | ||
const classes = classNames({ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'dshDashboardGrid__item--expanded': expandPanel, | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
'dshDashboardGrid__item--hidden': hidePanel, | ||
}); | ||
|
||
return ( | ||
<div | ||
style={{ ...style, zIndex: focusedPanelId === id ? 2 : 'auto' }} | ||
className={[classes, className].join(' ')} | ||
data-test-subj="dashboardPanel" | ||
ref={ref} | ||
{...rest} | ||
> | ||
{isRenderable ? ( | ||
<> | ||
<EmbeddableChildPanel | ||
// This key is used to force rerendering on embeddable type change while the id remains the same | ||
key={type} | ||
embeddableId={id} | ||
{...{ container, PanelComponent }} | ||
/> | ||
{children} | ||
</> | ||
) : ( | ||
<div className="embPanel embPanel-isLoading"> | ||
<EuiLoadingChart size="l" mono /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
export const ObservedItem: FC<Props> = (props: Props) => { | ||
const [intersection, updateIntersection] = useState<IntersectionObserverEntry>(); | ||
const [isRenderable, setIsRenderable] = useState(false); | ||
const panelRef = useRef<HTMLDivElement>(null); | ||
|
||
const observerRef = useRef( | ||
new window.IntersectionObserver(([value]) => updateIntersection(value), { | ||
root: panelRef.current, | ||
}) | ||
); | ||
|
||
useEffect(() => { | ||
const { current: currentObserver } = observerRef; | ||
currentObserver.disconnect(); | ||
const { current } = panelRef; | ||
|
||
if (current) { | ||
currentObserver.observe(current); | ||
} | ||
|
||
return () => currentObserver.disconnect(); | ||
}, [panelRef]); | ||
|
||
useEffect(() => { | ||
if (intersection?.isIntersecting && !isRenderable) { | ||
setIsRenderable(true); | ||
} | ||
}, [intersection, isRenderable]); | ||
|
||
return <Item ref={panelRef} isRenderable={isRenderable} {...props} />; | ||
}; | ||
|
||
export const DashboardGridItem: FC<Props> = (props: Props) => { | ||
const { isProjectEnabled } = useLabs(); | ||
const isEnabled = isProjectEnabled('labs:dashboard:deferBelowFold'); | ||
|
||
return isEnabled ? <ObservedItem {...props} /> : <Item {...props} />; | ||
}; |
Oops, something went wrong.