-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial scroll position improvements (#902)
* WIP * Naming * Fix build * Add story
- Loading branch information
Showing
4 changed files
with
190 additions
and
68 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
95 changes: 95 additions & 0 deletions
95
packages/core/src/data-editor/use-initial-scroll-offset.ts
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,95 @@ | ||
import * as React from "react"; | ||
import type { VisibleRegion } from "./visible-region.js"; | ||
import type { DataEditorCoreProps } from "../index.js"; | ||
import { useStateWithReactiveInput } from "../common/utils.js"; | ||
|
||
function useCallbackRef<T>(initialValue: T, callback: (newVal: T) => void) { | ||
const realRef = React.useRef<T>(initialValue); | ||
const cbRef = React.useRef(callback); | ||
cbRef.current = callback; | ||
|
||
return React.useMemo( | ||
() => ({ | ||
get current() { | ||
return realRef.current; | ||
}, | ||
set current(value: T) { | ||
if (realRef.current !== value) { | ||
realRef.current = value; | ||
cbRef.current(value); | ||
} | ||
}, | ||
}), | ||
[] | ||
); | ||
} | ||
|
||
export function useInitialScrollOffset( | ||
scrollOffsetX: number | undefined, | ||
scrollOffsetY: number | undefined, | ||
rowHeight: NonNullable<DataEditorCoreProps["rowHeight"]>, | ||
visibleRegionRef: React.MutableRefObject<VisibleRegion>, | ||
onDidScroll: () => void | ||
) { | ||
const [visibleRegionY, visibleRegionTy] = React.useMemo(() => { | ||
return [ | ||
scrollOffsetY !== undefined && typeof rowHeight === "number" ? Math.floor(scrollOffsetY / rowHeight) : 0, | ||
scrollOffsetY !== undefined && typeof rowHeight === "number" ? -(scrollOffsetY % rowHeight) : 0, | ||
]; | ||
}, [scrollOffsetY, rowHeight]); | ||
|
||
const visibleRegionInput = React.useMemo<VisibleRegion>( | ||
() => ({ | ||
x: visibleRegionRef.current.x, | ||
y: visibleRegionY, | ||
width: visibleRegionRef.current.width ?? 1, | ||
height: visibleRegionRef.current.height ?? 1, | ||
// tx: 'TODO', | ||
ty: visibleRegionTy, | ||
}), | ||
[visibleRegionRef, visibleRegionTy, visibleRegionY] | ||
); | ||
|
||
const [visibleRegion, setVisibleRegion, empty] = useStateWithReactiveInput<VisibleRegion>(visibleRegionInput); | ||
|
||
const onDidScrollRef = React.useRef(onDidScroll); | ||
onDidScrollRef.current = onDidScroll; | ||
|
||
const scrollRef = useCallbackRef<HTMLDivElement | null>(null, newVal => { | ||
if (newVal !== null && scrollOffsetY !== undefined) { | ||
newVal.scrollTop = scrollOffsetY; | ||
} else if (newVal !== null && scrollOffsetX !== undefined) { | ||
newVal.scrollLeft = scrollOffsetX; | ||
} | ||
}); | ||
|
||
const vScrollReady = (visibleRegion.height ?? 1) > 1; | ||
React.useLayoutEffect(() => { | ||
if (scrollOffsetY !== undefined && scrollRef.current !== null && vScrollReady) { | ||
if (scrollRef.current.scrollTop === scrollOffsetY) return; | ||
scrollRef.current.scrollTop = scrollOffsetY; | ||
if (scrollRef.current.scrollTop !== scrollOffsetY) { | ||
empty(); | ||
} | ||
onDidScrollRef.current(); | ||
} | ||
}, [scrollOffsetY, vScrollReady, empty, scrollRef]); | ||
|
||
const hScrollReady = (visibleRegion.width ?? 1) > 1; | ||
React.useLayoutEffect(() => { | ||
if (scrollOffsetX !== undefined && scrollRef.current !== null && hScrollReady) { | ||
if (scrollRef.current.scrollLeft === scrollOffsetX) return; | ||
scrollRef.current.scrollLeft = scrollOffsetX; | ||
if (scrollRef.current.scrollLeft !== scrollOffsetX) { | ||
empty(); | ||
} | ||
onDidScrollRef.current(); | ||
} | ||
}, [scrollOffsetX, hScrollReady, empty, scrollRef]); | ||
|
||
return { | ||
visibleRegion, | ||
setVisibleRegion, | ||
scrollRef, | ||
}; | ||
} |
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,20 @@ | ||
import { type Rectangle, type Item } from "../internal/data-grid/data-grid-types.js"; | ||
|
||
export type VisibleRegion = Rectangle & { | ||
/** value in px */ | ||
tx?: number; | ||
/** value in px */ | ||
ty?: number; | ||
extras?: { | ||
selected?: Item; | ||
/** | ||
* @deprecated | ||
*/ | ||
freezeRegion?: Rectangle; | ||
|
||
/** | ||
* All visible freeze regions | ||
*/ | ||
freezeRegions?: readonly Rectangle[]; | ||
}; | ||
}; |
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,49 @@ | ||
import React from "react"; | ||
import { DataEditorAll as DataEditor } from "../../data-editor-all.js"; | ||
import { | ||
BeautifulWrapper, | ||
Description, | ||
PropName, | ||
useMockDataGenerator, | ||
defaultProps, | ||
} from "../../data-editor/stories/utils.js"; | ||
import { SimpleThemeWrapper } from "../../stories/story-utils.js"; | ||
import _ from "lodash"; | ||
|
||
export default { | ||
title: "Glide-Data-Grid/DataEditor Demos", | ||
|
||
decorators: [ | ||
(Story: React.ComponentType) => ( | ||
<SimpleThemeWrapper> | ||
<BeautifulWrapper | ||
title="Scroll Offset" | ||
description={ | ||
<Description> | ||
The <PropName>rowGrouping</PropName> prop can be used to group and even fold rows. | ||
</Description> | ||
}> | ||
<Story /> | ||
</BeautifulWrapper> | ||
</SimpleThemeWrapper> | ||
), | ||
], | ||
}; | ||
|
||
export const ScrollOffset: React.VFC<any> = () => { | ||
const { cols, getCellContent } = useMockDataGenerator(100); | ||
const rows = 1000; | ||
|
||
return ( | ||
<DataEditor | ||
{...defaultProps} | ||
height="100%" | ||
rowMarkers="both" | ||
scrollOffsetY={400} | ||
getCellContent={getCellContent} | ||
columns={cols} | ||
// verticalBorder={false} | ||
rows={rows} | ||
/> | ||
); | ||
}; |