diff --git a/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridItemRenderer.tsx b/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridItemRenderer.tsx index d1d895dfbf5..43566124f97 100644 --- a/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridItemRenderer.tsx +++ b/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridItemRenderer.tsx @@ -7,7 +7,7 @@ import { useStyles2 } from '@grafana/ui'; import { useDashboardState, useIsConditionallyHidden } from '../../utils/utils'; import { AutoGridItem } from './ResponsiveGridItem'; -import { DRAGGED_ITEM_HEIGHT, DRAGGED_ITEM_LEFT, DRAGGED_ITEM_TOP, DRAGGED_ITEM_WIDTH } from './ResponsiveGridLayout'; +import { DRAGGED_ITEM_HEIGHT, DRAGGED_ITEM_LEFT, DRAGGED_ITEM_TOP, DRAGGED_ITEM_WIDTH } from './const'; export function AutoGridItemRenderer({ model }: SceneComponentProps) { const { body, repeatedPanels, key } = model.useState(); diff --git a/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayout.tsx b/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayout.tsx index 5d370d2a884..72fd8a488d6 100644 --- a/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayout.tsx +++ b/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayout.tsx @@ -1,4 +1,4 @@ -import { CSSProperties, PointerEvent as ReactPointerEvent } from 'react'; +import { createRef, CSSProperties, PointerEvent as ReactPointerEvent } from 'react'; import { SceneLayout, SceneObjectBase, SceneObjectState, VizPanel } from '@grafana/scenes'; @@ -6,6 +6,7 @@ import { getLayoutOrchestratorFor } from '../../utils/utils'; import { AutoGridItem } from './ResponsiveGridItem'; import { AutoGridLayoutRenderer } from './ResponsiveGridLayoutRenderer'; +import { DRAGGED_ITEM_HEIGHT, DRAGGED_ITEM_LEFT, DRAGGED_ITEM_TOP, DRAGGED_ITEM_WIDTH } from './const'; export interface AutoGridLayoutState extends SceneObjectState, AutoGridLayoutOptions { children: AutoGridItem[]; @@ -58,7 +59,7 @@ export interface AutoGridLayoutOptions { export class AutoGridLayout extends SceneObjectBase implements SceneLayout { public static Component = AutoGridLayoutRenderer; - private _containerRef: HTMLDivElement | null = null; + public containerRef = createRef(); private _draggedGridItem: AutoGridItem | null = null; private _initialGridItemPosition: { pageX: number; @@ -111,10 +112,6 @@ export class AutoGridLayout extends SceneObjectBase impleme }; } - public setRef(ref: HTMLDivElement | null) { - this._containerRef = ref; - } - private _canDrag(evt: ReactPointerEvent): boolean { if (!this.isDraggable()) { return false; @@ -214,24 +211,27 @@ export class AutoGridLayout extends SceneObjectBase impleme } private _updatePanelPosition(top: number, left: number) { - this._containerRef?.style.setProperty(DRAGGED_ITEM_TOP, `${top}px`); - this._containerRef?.style.setProperty(DRAGGED_ITEM_LEFT, `${left}px`); + this._setContainerStyle(DRAGGED_ITEM_TOP, `${top}px`); + this._setContainerStyle(DRAGGED_ITEM_LEFT, `${left}px`); } private _updatePanelSize(width: number, height: number) { - this._containerRef?.style.setProperty(DRAGGED_ITEM_WIDTH, `${Math.floor(width)}px`); - this._containerRef?.style.setProperty(DRAGGED_ITEM_HEIGHT, `${Math.floor(height)}px`); + this._setContainerStyle(DRAGGED_ITEM_WIDTH, `${Math.floor(width)}px`); + this._setContainerStyle(DRAGGED_ITEM_HEIGHT, `${Math.floor(height)}px`); } private _resetPanelPositionAndSize() { - this._containerRef?.style.removeProperty(DRAGGED_ITEM_TOP); - this._containerRef?.style.removeProperty(DRAGGED_ITEM_LEFT); - this._containerRef?.style.removeProperty(DRAGGED_ITEM_WIDTH); - this._containerRef?.style.removeProperty(DRAGGED_ITEM_HEIGHT); + this._removeContainerStyle(DRAGGED_ITEM_TOP); + this._removeContainerStyle(DRAGGED_ITEM_LEFT); + this._removeContainerStyle(DRAGGED_ITEM_WIDTH); + this._removeContainerStyle(DRAGGED_ITEM_HEIGHT); + } + + private _setContainerStyle(name: string, value: string) { + this.containerRef.current?.style.setProperty(name, value); + } + + private _removeContainerStyle(name: string) { + this.containerRef.current?.style.removeProperty(name); } } - -export const DRAGGED_ITEM_TOP = '--responsive-grid-dragged-item-top'; -export const DRAGGED_ITEM_LEFT = '--responsive-grid-dragged-item-left'; -export const DRAGGED_ITEM_WIDTH = '--responsive-grid-dragged-item-width'; -export const DRAGGED_ITEM_HEIGHT = '--responsive-grid-dragged-item-height'; diff --git a/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayoutRenderer.tsx b/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayoutRenderer.tsx index 738ead16493..0721f1fe704 100644 --- a/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayoutRenderer.tsx +++ b/public/app/features/dashboard-scene/scene/layout-responsive-grid/ResponsiveGridLayoutRenderer.tsx @@ -25,6 +25,7 @@ export function AutoGridLayoutRenderer({ model }: SceneComponentProps {children.map((item) => isLazy ? ( diff --git a/public/app/features/dashboard-scene/scene/layout-responsive-grid/const.ts b/public/app/features/dashboard-scene/scene/layout-responsive-grid/const.ts new file mode 100644 index 00000000000..7148c24ad53 --- /dev/null +++ b/public/app/features/dashboard-scene/scene/layout-responsive-grid/const.ts @@ -0,0 +1,4 @@ +export const DRAGGED_ITEM_TOP = '--responsive-grid-dragged-item-top'; +export const DRAGGED_ITEM_LEFT = '--responsive-grid-dragged-item-left'; +export const DRAGGED_ITEM_WIDTH = '--responsive-grid-dragged-item-width'; +export const DRAGGED_ITEM_HEIGHT = '--responsive-grid-dragged-item-height';