22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
44import { useCallback } from 'react' ;
5+ import type React from 'react' ;
56import { handleCardSelection } from '../utils/selection' ;
67import type { Layout } from '../utils/cardPosition' ;
78import type { ViewMode } from '../components/Toolbar' ;
@@ -20,6 +21,8 @@ interface UseCardSelectionParams<TItem extends object> {
2021 zoomLevel : number ;
2122 viewMode : ViewMode ;
2223 layout : Layout ;
24+ containerRef : React . RefObject < HTMLDivElement | null > ;
25+ spacerRef : React . RefObject < HTMLDivElement | null > ;
2326 containerDimensions : { width : number ; height : number } ;
2427 scrollPosition : { x : number ; y : number } ;
2528 preSelectionState : { zoom : number ; scrollLeft : number ; scrollTop : number } | null ;
@@ -39,6 +42,8 @@ export function useCardSelection<TItem extends object>({
3942 zoomLevel,
4043 viewMode,
4144 layout,
45+ containerRef,
46+ spacerRef,
4247 containerDimensions,
4348 scrollPosition,
4449 preSelectionState,
@@ -53,8 +58,8 @@ export function useCardSelection<TItem extends object>({
5358 return useCallback ( ( item : TItem , e : MouseEvent , id ?: number | string ) => {
5459 if ( isPanning ) return ;
5560
56- // Get container element from event target
57- const container = ( e . target as Element ) ?. closest ( '.pv-main' ) ?. parentElement as HTMLDivElement | null ;
61+ // Use the containerRef directly as the scrollable viewport
62+ const container = containerRef . current ;
5863 if ( ! container ) return ;
5964
6065 // Resolve item ID
@@ -111,7 +116,7 @@ export function useCardSelection<TItem extends object>({
111116 targetCardPosition,
112117 getCardPositionAtZoom : callbacks . getCardPositionAtZoom ,
113118 getLayoutSizeAtZoom : callbacks . getLayoutSizeAtZoom ,
114- spacer : container . querySelector ( '.pv-spacer' ) as HTMLDivElement ,
119+ spacer : spacerRef . current ,
115120 preSelectionState,
116121 startScrollPosition : { x : scrollPosition . x , y : scrollPosition . y } ,
117122 setZoomLevel,
@@ -122,5 +127,5 @@ export function useCardSelection<TItem extends object>({
122127 zoomLevel,
123128 totalHeight : targetTotalHeight ,
124129 } ) ;
125- } , [ isPanning , selectedItem , zoomLevel , preSelectionState , viewMode , resolveId , setZoomLevel , layout , grouping , containerDimensions , scrollPosition , data , getItemId ] ) ;
130+ } , [ isPanning , selectedItem , zoomLevel , preSelectionState , viewMode , resolveId , setZoomLevel , layout , grouping , containerRef , spacerRef , containerDimensions , scrollPosition , data , getItemId ] ) ;
126131}
0 commit comments