Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Jun 21, 2024
1 parent a4e6249 commit 5b44ae1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,10 @@ export const TreeViewVirtualScrollbar = React.forwardRef<HTMLDivElement, {}>(
const scrollbarRef = React.useRef<HTMLDivElement>(null);
const contentRef = React.useRef<HTMLDivElement>(null);

// TODO: Make dynamic
const dimensions = {};

const contentSize =
dimensions.minimumSize.height + (dimensions.hasScrollX ? dimensions.scrollbarSize : 0);

const scrollbarSize = dimensions.viewportInnerSize.height;

const scrollbarInnerSize = scrollbarSize * (contentSize / dimensions.viewportOuterSize.height);
const dimensions = instance.getDimensions();
const scrollbarHeight = dimensions.viewportHeight;
const scrollbarInnerSize =
scrollbarHeight * (dimensions.contentSize / dimensions.viewportHeight);

const onScrollerScroll = useEventCallback(() => {
const scroller = instance.virtualScrollerRef.current!;
Expand All @@ -66,7 +61,7 @@ export const TreeViewVirtualScrollbar = React.forwardRef<HTMLDivElement, {}>(
}
isLocked.current = true;

const value = scroller.scrollTop / contentSize;
const value = scroller.scrollTop / dimensions.contentSize;
scrollbar.scrollTop = value * scrollbarInnerSize;

lastPosition.current = scroller.scrollTop;
Expand All @@ -83,7 +78,7 @@ export const TreeViewVirtualScrollbar = React.forwardRef<HTMLDivElement, {}>(
isLocked.current = true;

const value = scrollbar.scrollTop / scrollbarInnerSize;
scroller.scrollTop = value * contentSize;
scroller.scrollTop = value * dimensions.contentSize;
});

useOnMount(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const useTreeViewVirtualization: TreeViewPlugin<UseTreeViewVirtualization
return {
instance: {
virtualScrollerRef,
getDimensions: () => ({}) as any,
},
};
};
Expand All @@ -17,4 +18,8 @@ useTreeViewVirtualization.getDefaultizedParams = (params) => ({
enableVirtualization: params.enableVirtualization ?? false,
});

useTreeViewVirtualization.getInitialState = () => ({
virtualization: { contentSize: 0, viewportHeight: 0 },
});

useTreeViewVirtualization.params = { enableVirtualization: true };
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultizedProps, TreeViewPluginSignature } from '@mui/x-tree-view/inte

export interface UseTreeViewVirtualizationInstance {
virtualScrollerRef: React.RefObject<HTMLUListElement>;
getDimensions: () => UseTreeViewVirtualizationState['virtualization'];
}

export interface UseTreeViewVirtualizationParameters {
Expand All @@ -14,9 +15,23 @@ export type UseTreeViewVirtualizationDefaultizedParameters = DefaultizedProps<
'enableVirtualization'
>;

interface UseTreeViewVirtualizationState {
virtualization: {
/**
* The viewport height.
*/
viewportHeight: number;
/**
* The minimum size to display all the items.
*/
contentSize: number;
};
}

export type UseTreeViewVirtualizationSignature = TreeViewPluginSignature<{
params: UseTreeViewVirtualizationParameters;
defaultizedParams: UseTreeViewVirtualizationDefaultizedParameters;
instance: UseTreeViewVirtualizationInstance;
state: UseTreeViewVirtualizationState;
dependencies: [];
}>;

0 comments on commit 5b44ae1

Please sign in to comment.