@mui/x-virtualizer 0.2.10 → 0.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/CHANGELOG.md +248 -0
  2. package/constants.d.ts +22 -0
  3. package/constants.js +26 -0
  4. package/esm/constants.d.ts +22 -0
  5. package/esm/constants.js +20 -0
  6. package/esm/features/colspan.d.ts +4 -4
  7. package/esm/features/dimensions.d.ts +14 -8
  8. package/esm/features/dimensions.js +26 -7
  9. package/esm/features/index.d.ts +1 -1
  10. package/esm/features/index.js +1 -1
  11. package/esm/features/keyboard.d.ts +3 -3
  12. package/esm/features/keyboard.js +1 -1
  13. package/esm/features/rowspan.d.ts +4 -4
  14. package/esm/features/virtualization/index.d.ts +2 -0
  15. package/esm/features/virtualization/index.js +2 -0
  16. package/esm/features/virtualization/layout.d.ts +129 -0
  17. package/esm/features/virtualization/layout.js +152 -0
  18. package/esm/features/{virtualization.d.ts → virtualization/virtualization.d.ts} +35 -58
  19. package/esm/features/{virtualization.js → virtualization/virtualization.js} +97 -134
  20. package/esm/index.d.ts +2 -1
  21. package/esm/index.js +3 -2
  22. package/esm/models/core.d.ts +7 -0
  23. package/esm/models/core.js +7 -0
  24. package/esm/models/dimensions.d.ts +8 -0
  25. package/esm/useVirtualizer.d.ts +25 -69
  26. package/esm/useVirtualizer.js +21 -4
  27. package/features/colspan.d.ts +4 -4
  28. package/features/dimensions.d.ts +14 -8
  29. package/features/dimensions.js +26 -7
  30. package/features/index.d.ts +1 -1
  31. package/features/keyboard.d.ts +3 -3
  32. package/features/rowspan.d.ts +4 -4
  33. package/features/virtualization/index.d.ts +2 -0
  34. package/features/virtualization/index.js +27 -0
  35. package/features/virtualization/layout.d.ts +129 -0
  36. package/features/virtualization/layout.js +163 -0
  37. package/features/{virtualization.d.ts → virtualization/virtualization.d.ts} +35 -58
  38. package/features/{virtualization.js → virtualization/virtualization.js} +97 -134
  39. package/index.d.ts +2 -1
  40. package/index.js +12 -1
  41. package/models/core.d.ts +7 -0
  42. package/models/core.js +8 -1
  43. package/models/dimensions.d.ts +8 -0
  44. package/package.json +2 -2
  45. package/useVirtualizer.d.ts +25 -69
  46. package/useVirtualizer.js +20 -3
@@ -0,0 +1,129 @@
1
+ import * as React from 'react';
2
+ import { Store } from '@mui/x-internals/store';
3
+ import { Dimensions } from "../dimensions.js";
4
+ import { Virtualization, type VirtualizationLayoutParams } from "./virtualization.js";
5
+ import type { BaseState, ParamsWithDefaults } from "../../useVirtualizer.js";
6
+ type RequiredAPI = Dimensions.API;
7
+ type BaseElements = {
8
+ scroller: React.RefObject<HTMLElement | null>;
9
+ container: React.RefObject<HTMLElement | null>;
10
+ };
11
+ type AnyElements = BaseElements & Record<string, React.RefObject<HTMLElement | null>>;
12
+ export declare abstract class Layout<E extends AnyElements = AnyElements> {
13
+ static elements: readonly (keyof AnyElements)[];
14
+ refs: E;
15
+ constructor(refs: E);
16
+ abstract use(store: Store<BaseState>, params: ParamsWithDefaults, api: RequiredAPI, layoutParams: VirtualizationLayoutParams): any;
17
+ refSetter(name: keyof E): (node: HTMLDivElement | null) => void;
18
+ }
19
+ type DataGridElements = BaseElements & {
20
+ scrollbarVertical: React.RefObject<HTMLElement | null>;
21
+ scrollbarHorizontal: React.RefObject<HTMLElement | null>;
22
+ };
23
+ export declare class LayoutDataGrid extends Layout<DataGridElements> {
24
+ static elements: readonly ["scroller", "container", "content", "positioner", "scrollbarVertical", "scrollbarHorizontal"];
25
+ use(store: Store<BaseState>, _params: ParamsWithDefaults, _api: RequiredAPI, layoutParams: VirtualizationLayoutParams): void;
26
+ static selectors: {
27
+ containerProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
28
+ ref: any;
29
+ };
30
+ scrollerProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
31
+ ref: any;
32
+ style: {
33
+ overflowX: string | undefined;
34
+ overflowY: string | undefined;
35
+ };
36
+ role: string;
37
+ tabIndex: number | undefined;
38
+ };
39
+ contentProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
40
+ style: React.CSSProperties;
41
+ role: string;
42
+ };
43
+ positionerProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
44
+ style: {
45
+ transform: string;
46
+ };
47
+ };
48
+ scrollbarHorizontalProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
49
+ ref: any;
50
+ scrollPosition: {
51
+ current: import("../../models/index.js").ScrollPosition;
52
+ };
53
+ };
54
+ scrollbarVerticalProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
55
+ ref: any;
56
+ scrollPosition: {
57
+ current: import("../../models/index.js").ScrollPosition;
58
+ };
59
+ };
60
+ scrollAreaProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
61
+ scrollPosition: {
62
+ current: import("../../models/index.js").ScrollPosition;
63
+ };
64
+ };
65
+ };
66
+ }
67
+ export declare class LayoutDataGridLegacy extends LayoutDataGrid {
68
+ use(store: Store<BaseState>, _params: ParamsWithDefaults, _api: RequiredAPI, layoutParams: VirtualizationLayoutParams): {
69
+ getContainerProps: () => {
70
+ ref: any;
71
+ };
72
+ getScrollerProps: () => {
73
+ ref: any;
74
+ style: {
75
+ overflowX: string | undefined;
76
+ overflowY: string | undefined;
77
+ };
78
+ role: string;
79
+ tabIndex: number | undefined;
80
+ };
81
+ getContentProps: () => {
82
+ style: React.CSSProperties;
83
+ role: string;
84
+ };
85
+ getPositionerProps: () => {
86
+ style: {
87
+ transform: string;
88
+ };
89
+ };
90
+ getScrollbarVerticalProps: () => {
91
+ ref: any;
92
+ scrollPosition: {
93
+ current: import("../../models/index.js").ScrollPosition;
94
+ };
95
+ };
96
+ getScrollbarHorizontalProps: () => {
97
+ ref: any;
98
+ scrollPosition: {
99
+ current: import("../../models/index.js").ScrollPosition;
100
+ };
101
+ };
102
+ getScrollAreaProps: () => {
103
+ scrollPosition: {
104
+ current: import("../../models/index.js").ScrollPosition;
105
+ };
106
+ };
107
+ };
108
+ }
109
+ type ListElements = BaseElements;
110
+ export declare class LayoutList extends Layout<ListElements> {
111
+ static elements: readonly ["scroller", "container", "content", "positioner"];
112
+ use(store: Store<BaseState>, _params: ParamsWithDefaults, _api: RequiredAPI, layoutParams: VirtualizationLayoutParams): void;
113
+ static selectors: {
114
+ containerProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
115
+ ref: any;
116
+ style: React.CSSProperties;
117
+ role: string;
118
+ tabIndex: number | undefined;
119
+ };
120
+ contentProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
121
+ style: React.CSSProperties;
122
+ role: string;
123
+ };
124
+ positionerProps: (args_0: Virtualization.State<Layout<AnyElements>> & Dimensions.State) => {
125
+ style: React.CSSProperties;
126
+ };
127
+ };
128
+ }
129
+ export {};
@@ -0,0 +1,152 @@
1
+ import useForkRef from '@mui/utils/useForkRef';
2
+ import useEventCallback from '@mui/utils/useEventCallback';
3
+ import * as platform from '@mui/x-internals/platform';
4
+ import { createSelectorMemoized } from '@mui/x-internals/store';
5
+ import { Dimensions } from "../dimensions.js";
6
+ import { Virtualization } from "./virtualization.js";
7
+
8
+ /* eslint-disable react-hooks/rules-of-hooks */
9
+
10
+ export class Layout {
11
+ static elements = ['scroller', 'container'];
12
+ constructor(refs) {
13
+ this.refs = refs;
14
+ }
15
+ refSetter(name) {
16
+ return node => {
17
+ if (node && this.refs[name].current !== node) {
18
+ this.refs[name].current = node;
19
+ }
20
+ };
21
+ }
22
+ }
23
+ export class LayoutDataGrid extends Layout {
24
+ static elements = (() => ['scroller', 'container', 'content', 'positioner', 'scrollbarVertical', 'scrollbarHorizontal'])();
25
+ use(store, _params, _api, layoutParams) {
26
+ const {
27
+ scrollerRef,
28
+ containerRef
29
+ } = layoutParams;
30
+ const scrollbarVerticalRef = useEventCallback(this.refSetter('scrollbarVertical'));
31
+ const scrollbarHorizontalRef = useEventCallback(this.refSetter('scrollbarHorizontal'));
32
+ store.state.virtualization.context = {
33
+ scrollerRef,
34
+ containerRef,
35
+ scrollbarVerticalRef,
36
+ scrollbarHorizontalRef
37
+ };
38
+ }
39
+ static selectors = (() => ({
40
+ containerProps: createSelectorMemoized(Virtualization.selectors.context, context => ({
41
+ ref: context.containerRef
42
+ })),
43
+ scrollerProps: createSelectorMemoized(Virtualization.selectors.context, Dimensions.selectors.autoHeight, Dimensions.selectors.needsHorizontalScrollbar, (context, autoHeight, needsHorizontalScrollbar) => ({
44
+ ref: context.scrollerRef,
45
+ style: {
46
+ overflowX: !needsHorizontalScrollbar ? 'hidden' : undefined,
47
+ overflowY: autoHeight ? 'hidden' : undefined
48
+ },
49
+ role: 'presentation',
50
+ // `tabIndex` shouldn't be used along role=presentation, but it fixes a Firefox bug
51
+ // https://github.com/mui/mui-x/pull/13891#discussion_r1683416024
52
+ tabIndex: platform.isFirefox ? -1 : undefined
53
+ })),
54
+ contentProps: createSelectorMemoized(Dimensions.selectors.contentHeight, Dimensions.selectors.minimalContentHeight, Dimensions.selectors.columnsTotalWidth, Dimensions.selectors.needsHorizontalScrollbar, (contentHeight, minimalContentHeight, columnsTotalWidth, needsHorizontalScrollbar) => ({
55
+ style: {
56
+ width: needsHorizontalScrollbar ? columnsTotalWidth : 'auto',
57
+ flexBasis: contentHeight === 0 ? minimalContentHeight : contentHeight,
58
+ flexShrink: 0
59
+ },
60
+ role: 'presentation'
61
+ })),
62
+ positionerProps: createSelectorMemoized(Virtualization.selectors.offsetTop, offsetTop => ({
63
+ style: {
64
+ transform: `translate3d(0, ${offsetTop}px, 0)`
65
+ }
66
+ })),
67
+ scrollbarHorizontalProps: createSelectorMemoized(Virtualization.selectors.context, Virtualization.selectors.scrollPosition, (context, scrollPosition) => ({
68
+ ref: context.scrollbarHorizontalRef,
69
+ scrollPosition
70
+ })),
71
+ scrollbarVerticalProps: createSelectorMemoized(Virtualization.selectors.context, Virtualization.selectors.scrollPosition, (context, scrollPosition) => ({
72
+ ref: context.scrollbarVerticalRef,
73
+ scrollPosition
74
+ })),
75
+ scrollAreaProps: createSelectorMemoized(Virtualization.selectors.scrollPosition, scrollPosition => ({
76
+ scrollPosition
77
+ }))
78
+ }))();
79
+ }
80
+
81
+ // The current virtualizer API is exposed on one of the DataGrid slots, so we need to keep
82
+ // the old API for backward compatibility. This API prevents using fine-grained reactivity
83
+ // as all props are returned in a single object, so everything re-renders on any change.
84
+ //
85
+ // TODO(v9): Remove the legacy API.
86
+ export class LayoutDataGridLegacy extends LayoutDataGrid {
87
+ use(store, _params, _api, layoutParams) {
88
+ super.use(store, _params, _api, layoutParams);
89
+ const containerProps = store.use(LayoutDataGrid.selectors.containerProps);
90
+ const scrollerProps = store.use(LayoutDataGrid.selectors.scrollerProps);
91
+ const contentProps = store.use(LayoutDataGrid.selectors.contentProps);
92
+ const positionerProps = store.use(LayoutDataGrid.selectors.positionerProps);
93
+ const scrollbarVerticalProps = store.use(LayoutDataGrid.selectors.scrollbarVerticalProps);
94
+ const scrollbarHorizontalProps = store.use(LayoutDataGrid.selectors.scrollbarHorizontalProps);
95
+ const scrollAreaProps = store.use(LayoutDataGrid.selectors.scrollAreaProps);
96
+ return {
97
+ getContainerProps: () => containerProps,
98
+ getScrollerProps: () => scrollerProps,
99
+ getContentProps: () => contentProps,
100
+ getPositionerProps: () => positionerProps,
101
+ getScrollbarVerticalProps: () => scrollbarVerticalProps,
102
+ getScrollbarHorizontalProps: () => scrollbarHorizontalProps,
103
+ getScrollAreaProps: () => scrollAreaProps
104
+ };
105
+ }
106
+ }
107
+ export class LayoutList extends Layout {
108
+ static elements = (() => ['scroller', 'container', 'content', 'positioner'])();
109
+ use(store, _params, _api, layoutParams) {
110
+ const {
111
+ scrollerRef,
112
+ containerRef
113
+ } = layoutParams;
114
+ const mergedRef = useForkRef(scrollerRef, containerRef);
115
+ store.state.virtualization.context = {
116
+ mergedRef
117
+ };
118
+ }
119
+ static selectors = (() => ({
120
+ containerProps: createSelectorMemoized(Virtualization.selectors.context, Dimensions.selectors.autoHeight, Dimensions.selectors.needsHorizontalScrollbar, (context, autoHeight, needsHorizontalScrollbar) => ({
121
+ ref: context.mergedRef,
122
+ style: {
123
+ overflowX: !needsHorizontalScrollbar ? 'hidden' : undefined,
124
+ overflowY: autoHeight ? 'hidden' : undefined,
125
+ position: 'relative'
126
+ },
127
+ role: 'presentation',
128
+ // `tabIndex` shouldn't be used along role=presentation, but it fixes a Firefox bug
129
+ // https://github.com/mui/mui-x/pull/13891#discussion_r1683416024
130
+ tabIndex: platform.isFirefox ? -1 : undefined
131
+ })),
132
+ contentProps: createSelectorMemoized(Dimensions.selectors.contentHeight, contentHeight => {
133
+ return {
134
+ style: {
135
+ position: 'absolute',
136
+ display: 'inline-block',
137
+ width: '100%',
138
+ height: contentHeight,
139
+ top: 0,
140
+ left: 0,
141
+ zIndex: -1
142
+ },
143
+ role: 'presentation'
144
+ };
145
+ }),
146
+ positionerProps: createSelectorMemoized(Virtualization.selectors.offsetTop, offsetTop => ({
147
+ style: {
148
+ height: offsetTop
149
+ }
150
+ }))
151
+ }))();
152
+ }
@@ -1,10 +1,11 @@
1
1
  import * as React from 'react';
2
2
  import type { integer } from '@mui/x-internals/types';
3
3
  import { Store } from '@mui/x-internals/store';
4
- import type { CellColSpanInfo } from "../models/colspan.js";
5
- import { Dimensions } from "./dimensions.js";
6
- import type { BaseState, VirtualizerParams } from "../useVirtualizer.js";
7
- import { PinnedRowPosition, RenderContext, ColumnsRenderContext, ColumnWithWidth, RowId, RowEntry } from "../models/index.js";
4
+ import type { CellColSpanInfo } from "../../models/colspan.js";
5
+ import { Dimensions } from "../dimensions.js";
6
+ import type { BaseState, ParamsWithDefaults } from "../../useVirtualizer.js";
7
+ import type { Layout } from "./layout.js";
8
+ import { RenderContext, ColumnsRenderContext, ColumnWithWidth, RowId, ScrollPosition } from "../../models/index.js";
8
9
  export type VirtualizationParams = {
9
10
  /** @default false */
10
11
  isRtl?: boolean;
@@ -15,11 +16,16 @@ export type VirtualizationParams = {
15
16
  * @default 150 */
16
17
  columnBufferPx?: number;
17
18
  };
18
- export type VirtualizationState = {
19
+ export type VirtualizationState<K extends string = string> = {
19
20
  enabled: boolean;
20
21
  enabledForRows: boolean;
21
22
  enabledForColumns: boolean;
22
23
  renderContext: RenderContext;
24
+ props: Record<K, Record<string, any>>;
25
+ context: Record<string, any>;
26
+ scrollPosition: {
27
+ current: ScrollPosition;
28
+ };
23
29
  };
24
30
  export declare const EMPTY_RENDER_CONTEXT: {
25
31
  firstRowIndex: number;
@@ -31,20 +37,31 @@ export declare const Virtualization: {
31
37
  initialize: typeof initializeState;
32
38
  use: typeof useVirtualization;
33
39
  selectors: {
34
- store: (state: BaseState) => VirtualizationState;
40
+ store: (state: BaseState) => VirtualizationState<string>;
35
41
  renderContext: (state: BaseState) => RenderContext;
36
42
  enabledForRows: (state: BaseState) => boolean;
37
43
  enabledForColumns: (state: BaseState) => boolean;
44
+ offsetTop: (args_0: Virtualization.State<Layout<{
45
+ scroller: React.RefObject<HTMLElement | null>;
46
+ container: React.RefObject<HTMLElement | null>;
47
+ } & Record<string, React.RefObject<HTMLElement | null>>>> & Dimensions.State) => number;
48
+ context: (state: BaseState) => Record<string, any>;
49
+ scrollPosition: (state: BaseState) => {
50
+ current: ScrollPosition;
51
+ };
38
52
  };
39
53
  };
40
54
  export declare namespace Virtualization {
41
- type State = {
42
- virtualization: VirtualizationState;
55
+ type State<L extends Layout> = {
56
+ virtualization: VirtualizationState<L extends Layout<infer E> ? keyof E : string>;
43
57
  getters: ReturnType<typeof useVirtualization>['getters'];
44
58
  };
45
59
  type API = ReturnType<typeof useVirtualization>;
46
60
  }
47
- declare function initializeState(params: VirtualizerParams): Virtualization.State;
61
+ declare function initializeState(params: ParamsWithDefaults): Virtualization.State<Layout<{
62
+ scroller: React.RefObject<HTMLElement | null>;
63
+ container: React.RefObject<HTMLElement | null>;
64
+ } & Record<string, React.RefObject<HTMLElement | null>>>>;
48
65
  /** APIs to override for colspan/rowspan */
49
66
  type AbstractAPI = {
50
67
  getCellColSpanInfo: (rowId: RowId, columnIndex: integer) => CellColSpanInfo;
@@ -52,58 +69,18 @@ type AbstractAPI = {
52
69
  getHiddenCellsOrigin: () => Record<RowId, Record<number, number>>;
53
70
  };
54
71
  type RequiredAPI = Dimensions.API & AbstractAPI;
55
- declare function useVirtualization(store: Store<BaseState>, params: VirtualizerParams, api: RequiredAPI): {
56
- getters: {
57
- setPanels: React.Dispatch<React.SetStateAction<Readonly<Map<any, React.ReactNode>>>>;
58
- getOffsetTop: () => number;
59
- getRows: (rowParams?: {
60
- rows?: RowEntry[];
61
- position?: PinnedRowPosition;
62
- renderContext?: RenderContext;
63
- }, unstable_rowTree?: Record<RowId, any>) => React.ReactNode[];
64
- rows: RowEntry[];
65
- getContainerProps: () => {
66
- ref: (node: HTMLDivElement | null) => (() => void | undefined) | undefined;
67
- };
68
- getScrollerProps: () => {
69
- ref: (node: HTMLDivElement | null) => (() => void | undefined) | undefined;
70
- style: React.CSSProperties;
71
- role: string;
72
- tabIndex: number | undefined;
73
- };
74
- getContentProps: () => {
75
- ref: (node: HTMLDivElement | null) => void;
76
- style: React.CSSProperties;
77
- role: string;
78
- };
79
- getScrollbarVerticalProps: () => {
80
- ref: (node: HTMLDivElement | null) => void;
81
- scrollPosition: React.RefObject<{
82
- top: number;
83
- left: number;
84
- }>;
85
- };
86
- getScrollbarHorizontalProps: () => {
87
- ref: (node: HTMLDivElement | null) => void;
88
- scrollPosition: React.RefObject<{
89
- top: number;
90
- left: number;
91
- }>;
92
- };
93
- getScrollAreaProps: () => {
94
- scrollPosition: React.RefObject<{
95
- top: number;
96
- left: number;
97
- }>;
98
- };
99
- };
100
- useVirtualization: () => BaseState;
101
- setPanels: React.Dispatch<React.SetStateAction<Readonly<Map<any, React.ReactNode>>>>;
102
- forceUpdateRenderContext: () => void;
103
- scheduleUpdateRenderContext: () => void;
72
+ export type VirtualizationLayoutParams = {
73
+ containerRef: (node: HTMLDivElement | null) => void;
74
+ scrollerRef: (node: HTMLDivElement | null) => void;
75
+ };
76
+ declare function useVirtualization(store: Store<BaseState>, params: ParamsWithDefaults, api: RequiredAPI): {
104
77
  getCellColSpanInfo: (rowId: RowId, columnIndex: integer) => CellColSpanInfo;
105
78
  calculateColSpan: (rowId: RowId, minFirstColumn: integer, maxLastColumn: integer, columns: ColumnWithWidth[]) => void;
106
79
  getHiddenCellsOrigin: () => Record<RowId, Record<number, number>>;
80
+ getters: any;
81
+ setPanels: React.Dispatch<React.SetStateAction<Readonly<Map<any, React.ReactNode>>>>;
82
+ forceUpdateRenderContext: () => void;
83
+ scheduleUpdateRenderContext: () => void;
107
84
  };
108
85
  export declare function areRenderContextsEqual(context1: RenderContext, context2: RenderContext): boolean;
109
86
  export declare function computeOffsetLeft(columnPositions: number[], renderContext: ColumnsRenderContext, pinnedLeftLength: number): number;