@react-stately/layout 3.0.0-nightly-641446f65-240905

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.
@@ -0,0 +1,144 @@
1
+ import { DropTarget, DropTargetDelegate, ItemDropTarget, Key, Node, Collection } from "@react-types/shared";
2
+ import { Layout, LayoutInfo, Rect, Size, InvalidationContext } from "@react-stately/virtualizer";
3
+ import { GridNode } from "@react-types/grid";
4
+ import { TableCollection } from "@react-types/table";
5
+ export interface GridLayoutOptions {
6
+ /**
7
+ * The minimum item size.
8
+ * @default 200 x 200
9
+ */
10
+ minItemSize?: Size;
11
+ /**
12
+ * The maximum item size.
13
+ * @default Infinity
14
+ */
15
+ maxItemSize?: Size;
16
+ /**
17
+ * The minimum space required between items.
18
+ * @default 18 x 18
19
+ */
20
+ minSpace?: Size;
21
+ /**
22
+ * The maximum number of columns.
23
+ * @default Infinity
24
+ */
25
+ maxColumns?: number;
26
+ /**
27
+ * The thickness of the drop indicator.
28
+ * @default 2
29
+ */
30
+ dropIndicatorThickness?: number;
31
+ }
32
+ export class GridLayout<T, O = any> extends Layout<Node<T>, O> implements DropTargetDelegate {
33
+ protected minItemSize: Size;
34
+ protected maxItemSize: Size;
35
+ protected minSpace: Size;
36
+ protected maxColumns: number;
37
+ protected dropIndicatorThickness: number;
38
+ protected itemSize: Size;
39
+ protected numColumns: number;
40
+ protected horizontalSpacing: number;
41
+ protected layoutInfos: LayoutInfo[];
42
+ constructor(options: GridLayoutOptions);
43
+ update(): void;
44
+ getVisibleLayoutInfos(rect: Rect): LayoutInfo[];
45
+ protected getIndexAtPoint(x: number, y: number): number;
46
+ getLayoutInfo(key: Key): LayoutInfo | null;
47
+ protected getLayoutInfoForNode(node: Node<T>): LayoutInfo;
48
+ getContentSize(): Size;
49
+ getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget;
50
+ getDropTargetLayoutInfo(target: ItemDropTarget): LayoutInfo;
51
+ }
52
+ export interface ListLayoutOptions {
53
+ /** The fixed height of a row in px. */
54
+ rowHeight?: number;
55
+ /** The estimated height of a row, when row heights are variable. */
56
+ estimatedRowHeight?: number;
57
+ /** The fixed height of a section header in px. */
58
+ headingHeight?: number;
59
+ /** The estimated height of a section header, when the height is variable. */
60
+ estimatedHeadingHeight?: number;
61
+ /** The fixed height of a loader element in px. This loader is specifically for
62
+ * "load more" elements rendered when loading more rows at the root level or inside nested row/sections.
63
+ */
64
+ loaderHeight?: number;
65
+ /** The thickness of the drop indicator. */
66
+ dropIndicatorThickness?: number;
67
+ }
68
+ export interface LayoutNode {
69
+ node?: Node<unknown>;
70
+ layoutInfo: LayoutInfo;
71
+ children?: LayoutNode[];
72
+ validRect: Rect;
73
+ index?: number;
74
+ }
75
+ /**
76
+ * The ListLayout class is an implementation of a virtualizer {@link Layout}.
77
+ * To configure a ListLayout, you can use the properties to define the
78
+ * layouts and/or use the method for defining indentation.
79
+ * The {@link ListKeyboardDelegate} extends the existing virtualizer
80
+ * delegate with an additional method to do this (it uses the same delegate object as
81
+ * the virtualizer itself).
82
+ */
83
+ export class ListLayout<T, O = any> extends Layout<Node<T>, O> implements DropTargetDelegate {
84
+ protected rowHeight: number;
85
+ protected estimatedRowHeight: number;
86
+ protected headingHeight: number;
87
+ protected estimatedHeadingHeight: number;
88
+ protected loaderHeight: number;
89
+ protected dropIndicatorThickness: number;
90
+ protected layoutNodes: Map<Key, LayoutNode>;
91
+ protected contentSize: Size;
92
+ protected collection: Collection<Node<T>>;
93
+ protected rootNodes: LayoutNode[];
94
+ /** The rectangle containing currently valid layout infos. */
95
+ protected validRect: Rect;
96
+ /** The rectangle of requested layout infos so far. */
97
+ protected requestedRect: Rect;
98
+ /**
99
+ * Creates a new ListLayout with options. See the list of properties below for a description
100
+ * of the options that can be provided.
101
+ */
102
+ constructor(options?: ListLayoutOptions);
103
+ getLayoutInfo(key: Key): LayoutInfo;
104
+ getVisibleLayoutInfos(rect: Rect): LayoutInfo[];
105
+ protected layoutIfNeeded(rect: Rect): void;
106
+ protected isVisible(node: LayoutNode, rect: Rect): boolean;
107
+ protected shouldInvalidateEverything(invalidationContext: InvalidationContext<O>): boolean;
108
+ update(invalidationContext: InvalidationContext<O>): void;
109
+ protected buildCollection(y?: number): LayoutNode[];
110
+ protected isValid(node: Node<T>, y: number): boolean;
111
+ protected buildChild(node: Node<T>, x: number, y: number, parentKey: Key | null): LayoutNode;
112
+ protected buildNode(node: Node<T>, x: number, y: number): LayoutNode;
113
+ protected buildLoader(node: Node<T>, x: number, y: number): LayoutNode;
114
+ protected buildSection(node: Node<T>, x: number, y: number): LayoutNode;
115
+ protected buildSectionHeader(node: Node<T>, x: number, y: number): LayoutNode;
116
+ protected buildItem(node: Node<T>, x: number, y: number): LayoutNode;
117
+ updateItemSize(key: Key, size: Size): boolean;
118
+ getContentSize(): Size;
119
+ getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget;
120
+ getDropTargetLayoutInfo(target: ItemDropTarget): LayoutInfo;
121
+ }
122
+ export interface TableLayoutProps {
123
+ columnWidths?: Map<Key, number>;
124
+ }
125
+ export class TableLayout<T, O extends TableLayoutProps = TableLayoutProps> extends ListLayout<T, O> {
126
+ protected collection: TableCollection<T>;
127
+ constructor(options: ListLayoutOptions);
128
+ update(invalidationContext: InvalidationContext<O>): void;
129
+ protected buildCollection(): LayoutNode[];
130
+ protected buildTableHeader(): LayoutNode;
131
+ protected buildHeaderRow(headerRow: GridNode<T>, x: number, y: number): LayoutNode;
132
+ protected getEstimatedRowHeight(): number;
133
+ protected buildColumn(node: GridNode<T>, x: number, y: number): LayoutNode;
134
+ protected isStickyColumn(node: GridNode<T>): boolean;
135
+ protected buildBody(y: number): LayoutNode;
136
+ protected buildNode(node: GridNode<T>, x: number, y: number): LayoutNode;
137
+ protected buildRow(node: GridNode<T>, x: number, y: number): LayoutNode;
138
+ protected buildCell(node: GridNode<T>, x: number, y: number): LayoutNode;
139
+ getVisibleLayoutInfos(rect: Rect): LayoutInfo[];
140
+ getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget;
141
+ getDropTargetLayoutInfo(target: ItemDropTarget): LayoutInfo;
142
+ }
143
+
144
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;AAeA;IACE;;;OAGG;IACH,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB;;;OAGG;IACH,WAAW,CAAC,EAAE,IAAI,CAAC;IACnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,IAAI,CAAC;IAChB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAChC;AAED,wBAAwB,CAAC,EAAE,CAAC,GAAG,GAAG,CAAE,SAAQ,OAAO,KAAK,CAAC,CAAC,EAAE,CAAC,CAAE,YAAW,kBAAkB;IAC1F,SAAS,CAAC,WAAW,EAAE,IAAI,CAAC;IAC5B,SAAS,CAAC,WAAW,EAAE,IAAI,CAAC;IAC5B,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC;IACzB,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;IAC7B,SAAS,CAAC,sBAAsB,EAAE,MAAM,CAAC;IACzC,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC;IACzB,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;IAC7B,SAAS,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACpC,SAAS,CAAC,WAAW,EAAE,UAAU,EAAE,CAAC;gBAExB,OAAO,EAAE,iBAAiB;IAStC,MAAM,IAAI,IAAI;IAoCd,qBAAqB,CAAC,IAAI,EAAE,IAAI,GAAG,UAAU,EAAE;IAyB/C,SAAS,CAAC,eAAe,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM;IAW9C,aAAa,CAAC,GAAG,EAAE,GAAG,GAAG,UAAU,GAAG,IAAI;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AIzC,SAAS,CAAC,WAAW,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU;IAmB1E,SAAS,CAAC,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IAI1C,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,UAAU;IA8C1C,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU;IAkBxE,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU;IAyCvE,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU;IAiBxE,qBAAqB,CAAC,IAAI,EAAE,IAAI;IA4KhC,sBAAsB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,iBAAiB,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,OAAO,GAAG,UAAU;IAgD5G,uBAAuB,CAAC,MAAM,EAAE,cAAc,GAAG,UAAU;CAK5D","sources":["packages/@react-stately/layout/src/packages/@react-stately/layout/src/GridLayout.ts","packages/@react-stately/layout/src/packages/@react-stately/layout/src/ListLayout.ts","packages/@react-stately/layout/src/packages/@react-stately/layout/src/TableLayout.ts","packages/@react-stately/layout/src/packages/@react-stately/layout/src/index.ts","packages/@react-stately/layout/src/index.ts"],"sourcesContent":[null,null,null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\nexport type {GridLayoutOptions} from './GridLayout';\nexport type {ListLayoutOptions, LayoutNode} from './ListLayout';\nexport type {TableLayoutProps} from './TableLayout';\nexport {GridLayout} from './GridLayout';\nexport {ListLayout} from './ListLayout';\nexport {TableLayout} from './TableLayout';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@react-stately/layout",
3
+ "version": "3.0.0-nightly-641446f65-240905",
4
+ "description": "Spectrum UI components in React",
5
+ "license": "Apache-2.0",
6
+ "main": "dist/main.js",
7
+ "module": "dist/module.js",
8
+ "exports": {
9
+ "types": "./dist/types.d.ts",
10
+ "import": "./dist/import.mjs",
11
+ "require": "./dist/main.js"
12
+ },
13
+ "types": "dist/types.d.ts",
14
+ "source": "src/index.ts",
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "sideEffects": false,
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/adobe/react-spectrum"
23
+ },
24
+ "dependencies": {
25
+ "@react-stately/collections": "^3.0.0-nightly-641446f65-240905",
26
+ "@react-stately/table": "^3.0.0-nightly-641446f65-240905",
27
+ "@react-stately/virtualizer": "^3.0.0-nightly-641446f65-240905",
28
+ "@react-types/grid": "^3.0.0-nightly-641446f65-240905",
29
+ "@react-types/shared": "^3.0.0-nightly-641446f65-240905",
30
+ "@react-types/table": "^3.0.0-nightly-641446f65-240905",
31
+ "@swc/helpers": "^0.5.0"
32
+ },
33
+ "peerDependencies": {
34
+ "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "stableVersion": "4.0.2"
40
+ }
@@ -0,0 +1,223 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {DropTarget, DropTargetDelegate, ItemDropTarget, Key, Node} from '@react-types/shared';
14
+ import {Layout, LayoutInfo, Rect, Size} from '@react-stately/virtualizer';
15
+
16
+ export interface GridLayoutOptions {
17
+ /**
18
+ * The minimum item size.
19
+ * @default 200 x 200
20
+ */
21
+ minItemSize?: Size,
22
+ /**
23
+ * The maximum item size.
24
+ * @default Infinity
25
+ */
26
+ maxItemSize?: Size,
27
+ /**
28
+ * The minimum space required between items.
29
+ * @default 18 x 18
30
+ */
31
+ minSpace?: Size,
32
+ /**
33
+ * The maximum number of columns.
34
+ * @default Infinity
35
+ */
36
+ maxColumns?: number,
37
+ /**
38
+ * The thickness of the drop indicator.
39
+ * @default 2
40
+ */
41
+ dropIndicatorThickness?: number
42
+ }
43
+
44
+ export class GridLayout<T, O = any> extends Layout<Node<T>, O> implements DropTargetDelegate {
45
+ protected minItemSize: Size;
46
+ protected maxItemSize: Size;
47
+ protected minSpace: Size;
48
+ protected maxColumns: number;
49
+ protected dropIndicatorThickness: number;
50
+ protected itemSize: Size;
51
+ protected numColumns: number;
52
+ protected horizontalSpacing: number;
53
+ protected layoutInfos: LayoutInfo[];
54
+
55
+ constructor(options: GridLayoutOptions) {
56
+ super();
57
+ this.minItemSize = options.minItemSize || new Size(200, 200);
58
+ this.maxItemSize = options.maxItemSize || new Size(Infinity, Infinity);
59
+ this.minSpace = options.minSpace || new Size(18, 18);
60
+ this.maxColumns = options.maxColumns || Infinity;
61
+ this.dropIndicatorThickness = options.dropIndicatorThickness || 2;
62
+ }
63
+
64
+ update(): void {
65
+ let visibleWidth = this.virtualizer.visibleRect.width;
66
+
67
+ // The max item width is always the entire viewport.
68
+ // If the max item height is infinity, scale in proportion to the max width.
69
+ let maxItemWidth = Math.min(this.maxItemSize.width, visibleWidth);
70
+ let maxItemHeight = Number.isFinite(this.maxItemSize.height)
71
+ ? this.maxItemSize.height
72
+ : Math.floor((this.minItemSize.height / this.minItemSize.width) * maxItemWidth);
73
+
74
+ // Compute the number of rows and columns needed to display the content
75
+ let columns = Math.floor(visibleWidth / (this.minItemSize.width + this.minSpace.width));
76
+ this.numColumns = Math.max(1, Math.min(this.maxColumns, columns));
77
+
78
+ // Compute the available width (minus the space between items)
79
+ let width = visibleWidth - (this.minSpace.width * Math.max(0, this.numColumns));
80
+
81
+ // Compute the item width based on the space available
82
+ let itemWidth = Math.floor(width / this.numColumns);
83
+ itemWidth = Math.max(this.minItemSize.width, Math.min(maxItemWidth, itemWidth));
84
+
85
+ // Compute the item height, which is proportional to the item width
86
+ let t = ((itemWidth - this.minItemSize.width) / Math.max(1, maxItemWidth - this.minItemSize.width));
87
+ let itemHeight = this.minItemSize.height + Math.floor((maxItemHeight - this.minItemSize.height) * t);
88
+ itemHeight = Math.max(this.minItemSize.height, Math.min(maxItemHeight, itemHeight));
89
+ this.itemSize = new Size(itemWidth, itemHeight);
90
+
91
+ // Compute the horizontal spacing and content height
92
+ this.horizontalSpacing = Math.floor((visibleWidth - this.numColumns * this.itemSize.width) / (this.numColumns + 1));
93
+
94
+ this.layoutInfos = [];
95
+ for (let node of this.virtualizer.collection) {
96
+ this.layoutInfos.push(this.getLayoutInfoForNode(node));
97
+ }
98
+ }
99
+
100
+ getVisibleLayoutInfos(rect: Rect): LayoutInfo[] {
101
+ let firstVisibleItem = this.getIndexAtPoint(rect.x, rect.y);
102
+ let lastVisibleItem = this.getIndexAtPoint(rect.maxX, rect.maxY);
103
+ let result = this.layoutInfos.slice(firstVisibleItem, lastVisibleItem + 1);
104
+ let persistedIndices: number[] = [];
105
+ for (let key of this.virtualizer.persistedKeys) {
106
+ let item = this.virtualizer.collection.getItem(key);
107
+ if (item?.index != null) {
108
+ persistedIndices.push(item.index);
109
+ }
110
+ }
111
+ persistedIndices.sort((a, b) => a - b);
112
+
113
+ let persistedBefore: LayoutInfo[] = [];
114
+ for (let index of persistedIndices) {
115
+ if (index < firstVisibleItem) {
116
+ persistedBefore.push(this.layoutInfos[index]);
117
+ } else if (index > lastVisibleItem) {
118
+ result.push(this.layoutInfos[index]);
119
+ }
120
+ }
121
+ result.unshift(...persistedBefore);
122
+ return result;
123
+ }
124
+
125
+ protected getIndexAtPoint(x: number, y: number) {
126
+ let itemHeight = this.itemSize.height + this.minSpace.height;
127
+ let itemWidth = this.itemSize.width + this.horizontalSpacing;
128
+ return Math.max(0,
129
+ Math.min(
130
+ this.virtualizer.collection.size - 1,
131
+ Math.floor(y / itemHeight) * this.numColumns + Math.floor((x - this.horizontalSpacing) / itemWidth)
132
+ )
133
+ );
134
+ }
135
+
136
+ getLayoutInfo(key: Key): LayoutInfo | null {
137
+ let node = this.virtualizer.collection.getItem(key);
138
+ return node ? this.layoutInfos[node.index] : null;
139
+ }
140
+
141
+ protected getLayoutInfoForNode(node: Node<T>): LayoutInfo {
142
+ let idx = node.index;
143
+ let row = Math.floor(idx / this.numColumns);
144
+ let column = idx % this.numColumns;
145
+ let x = this.horizontalSpacing + column * (this.itemSize.width + this.horizontalSpacing);
146
+ let y = this.minSpace.height + row * (this.itemSize.height + this.minSpace.height);
147
+ let rect = new Rect(x, y, this.itemSize.width, this.itemSize.height);
148
+ return new LayoutInfo(node.type, node.key, rect);
149
+ }
150
+
151
+ getContentSize(): Size {
152
+ let numRows = Math.ceil(this.virtualizer.collection.size / this.numColumns);
153
+ let contentHeight = this.minSpace.height + numRows * (this.itemSize.height + this.minSpace.height);
154
+ return new Size(this.virtualizer.visibleRect.width, contentHeight);
155
+ }
156
+
157
+ getDropTargetFromPoint(x: number, y: number, isValidDropTarget: (target: DropTarget) => boolean): DropTarget {
158
+ if (this.layoutInfos.length === 0) {
159
+ return {type: 'root'};
160
+ }
161
+
162
+ x += this.virtualizer.visibleRect.x;
163
+ y += this.virtualizer.visibleRect.y;
164
+ let index = this.getIndexAtPoint(x, y);
165
+
166
+ let layoutInfo = this.layoutInfos[index];
167
+ let target: DropTarget = {
168
+ type: 'item',
169
+ key: layoutInfo.key,
170
+ dropPosition: 'on'
171
+ };
172
+
173
+ let pos = this.numColumns === 1 ? y : x;
174
+ let layoutInfoPos = this.numColumns === 1 ? layoutInfo.rect.y : layoutInfo.rect.x;
175
+ let size = this.numColumns === 1 ? layoutInfo.rect.height : layoutInfo.rect.width;
176
+ if (isValidDropTarget(target)) {
177
+ // If dropping on the item is accepted, try the before/after positions
178
+ // if within 5px of the start or end of the item.
179
+ if (pos < layoutInfoPos + 5) {
180
+ target.dropPosition = 'before';
181
+ } else if (pos > layoutInfoPos + size - 5) {
182
+ target.dropPosition = 'after';
183
+ }
184
+ } else {
185
+ // If dropping on the item isn't accepted, try the target before or after depending on the position.
186
+ let mid = layoutInfoPos + size / 2;
187
+ if (pos <= mid && isValidDropTarget({...target, dropPosition: 'before'})) {
188
+ target.dropPosition = 'before';
189
+ } else if (pos >= mid && isValidDropTarget({...target, dropPosition: 'after'})) {
190
+ target.dropPosition = 'after';
191
+ }
192
+ }
193
+
194
+ return target;
195
+ }
196
+
197
+ getDropTargetLayoutInfo(target: ItemDropTarget): LayoutInfo {
198
+ let layoutInfo = this.getLayoutInfo(target.key);
199
+ let rect: Rect;
200
+ if (this.numColumns === 1) {
201
+ // Flip from vertical to horizontal if only one column is visible.
202
+ rect = new Rect(
203
+ layoutInfo.rect.x,
204
+ target.dropPosition === 'before'
205
+ ? layoutInfo.rect.y - this.minSpace.height / 2 - this.dropIndicatorThickness / 2
206
+ : layoutInfo.rect.maxY + this.minSpace.height / 2 - this.dropIndicatorThickness / 2,
207
+ layoutInfo.rect.width,
208
+ this.dropIndicatorThickness
209
+ );
210
+ } else {
211
+ rect = new Rect(
212
+ target.dropPosition === 'before'
213
+ ? layoutInfo.rect.x - this.horizontalSpacing / 2 - this.dropIndicatorThickness / 2
214
+ : layoutInfo.rect.maxX + this.horizontalSpacing / 2 - this.dropIndicatorThickness / 2,
215
+ layoutInfo.rect.y,
216
+ this.dropIndicatorThickness,
217
+ layoutInfo.rect.height
218
+ );
219
+ }
220
+
221
+ return new LayoutInfo('dropIndicator', target.key + ':' + target.dropPosition, rect);
222
+ }
223
+ }