Skip to content

Commit 5543c56

Browse files
committed
Memoized Floating UI options in TableHandlesController
1 parent 1defbc6 commit 5543c56

File tree

1 file changed

+119
-82
lines changed

1 file changed

+119
-82
lines changed

packages/react/src/components/TableHandles/TableHandlesController.tsx

Lines changed: 119 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { FC, useMemo, useState } from "react";
1212
import { offset, size } from "@floating-ui/react";
1313
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js";
1414
import { useExtensionState } from "../../hooks/useExtension.js";
15+
import { FloatingUIOptions } from "../Popovers/FloatingUIOptions.js";
1516
import {
1617
GenericPopover,
1718
GenericPopoverReference,
@@ -143,6 +144,119 @@ export const TableHandlesController = <
143144
};
144145
}, [editor, state]);
145146

147+
const floatingUIOptions = useMemo<
148+
| {
149+
rowTableHandle: FloatingUIOptions;
150+
columnTableHandle: FloatingUIOptions;
151+
tableCellHandle: FloatingUIOptions;
152+
extendRowsButton: FloatingUIOptions;
153+
extendColumnsButton: FloatingUIOptions;
154+
}
155+
| undefined
156+
>(
157+
() =>
158+
state !== undefined
159+
? {
160+
rowTableHandle: {
161+
useFloatingOptions: {
162+
open:
163+
state.show &&
164+
state.rowIndex !== undefined &&
165+
(!onlyShownElement || onlyShownElement === "rowTableHandle"),
166+
placement: "left",
167+
middleware: [offset(-10)],
168+
},
169+
elementProps: {
170+
style: {
171+
zIndex: 10,
172+
},
173+
},
174+
},
175+
columnTableHandle: {
176+
useFloatingOptions: {
177+
open:
178+
state.show &&
179+
state.colIndex !== undefined &&
180+
(!onlyShownElement ||
181+
onlyShownElement === "columnTableHandle"),
182+
placement: "top",
183+
middleware: [offset(-12)],
184+
},
185+
elementProps: {
186+
style: {
187+
zIndex: 10,
188+
},
189+
},
190+
},
191+
tableCellHandle: {
192+
useFloatingOptions: {
193+
open:
194+
state.show &&
195+
state.rowIndex !== undefined &&
196+
state.colIndex !== undefined &&
197+
(!onlyShownElement || onlyShownElement === "tableCellHandle"),
198+
placement: "top-end",
199+
middleware: [offset({ mainAxis: -15, crossAxis: -1 })],
200+
},
201+
elementProps: {
202+
style: {
203+
zIndex: 10,
204+
},
205+
},
206+
},
207+
extendRowsButton: {
208+
useFloatingOptions: {
209+
open:
210+
state.show &&
211+
state.showAddOrRemoveRowsButton &&
212+
(!onlyShownElement ||
213+
onlyShownElement === "extendRowsButton"),
214+
placement: "bottom",
215+
middleware: [
216+
size({
217+
apply({ rects, elements }) {
218+
Object.assign(elements.floating.style, {
219+
width: `${rects.reference.width}px`,
220+
});
221+
},
222+
}),
223+
],
224+
},
225+
elementProps: {
226+
style: {
227+
zIndex: 10,
228+
},
229+
},
230+
},
231+
extendColumnsButton: {
232+
useFloatingOptions: {
233+
open:
234+
state.show &&
235+
state.showAddOrRemoveColumnsButton &&
236+
(!onlyShownElement ||
237+
onlyShownElement === "extendColumnsButton"),
238+
placement: "right",
239+
middleware: [
240+
size({
241+
apply({ rects, elements }) {
242+
Object.assign(elements.floating.style, {
243+
height: `${rects.reference.height}px`,
244+
});
245+
},
246+
}),
247+
],
248+
},
249+
elementProps: {
250+
style: {
251+
zIndex: 10,
252+
},
253+
},
254+
},
255+
}
256+
: undefined,
257+
[onlyShownElement, state],
258+
);
259+
146260
if (!state) {
147261
return null;
148262
}
@@ -155,19 +269,7 @@ export const TableHandlesController = <
155269
<>
156270
<GenericPopover
157271
reference={references?.rowReference}
158-
useFloatingOptions={{
159-
open:
160-
state.show &&
161-
state.rowIndex !== undefined &&
162-
(!onlyShownElement || onlyShownElement === "rowTableHandle"),
163-
placement: "left",
164-
middleware: [offset(-10)],
165-
}}
166-
elementProps={{
167-
style: {
168-
zIndex: 10,
169-
},
170-
}}
272+
{...floatingUIOptions?.rowTableHandle}
171273
>
172274
{state.show &&
173275
state.rowIndex !== undefined &&
@@ -182,19 +284,7 @@ export const TableHandlesController = <
182284
</GenericPopover>
183285
<GenericPopover
184286
reference={references?.columnReference}
185-
useFloatingOptions={{
186-
open:
187-
state.show &&
188-
state.colIndex !== undefined &&
189-
(!onlyShownElement || onlyShownElement === "columnTableHandle"),
190-
placement: "top",
191-
middleware: [offset(-12)],
192-
}}
193-
elementProps={{
194-
style: {
195-
zIndex: 10,
196-
},
197-
}}
287+
{...floatingUIOptions?.columnTableHandle}
198288
>
199289
{state.show &&
200290
state.colIndex !== undefined &&
@@ -209,20 +299,7 @@ export const TableHandlesController = <
209299
</GenericPopover>
210300
<GenericPopover
211301
reference={references?.cellReference}
212-
useFloatingOptions={{
213-
open:
214-
state.show &&
215-
state.rowIndex !== undefined &&
216-
state.colIndex !== undefined &&
217-
(!onlyShownElement || onlyShownElement === "tableCellHandle"),
218-
placement: "top-end",
219-
middleware: [offset({ mainAxis: -15, crossAxis: -1 })],
220-
}}
221-
elementProps={{
222-
style: {
223-
zIndex: 10,
224-
},
225-
}}
302+
{...floatingUIOptions?.tableCellHandle}
226303
>
227304
{state.show &&
228305
state.rowIndex !== undefined &&
@@ -237,27 +314,7 @@ export const TableHandlesController = <
237314
</GenericPopover>
238315
<GenericPopover
239316
reference={references?.tableReference}
240-
useFloatingOptions={{
241-
open:
242-
state.show &&
243-
state.showAddOrRemoveRowsButton &&
244-
(!onlyShownElement || onlyShownElement === "extendRowsButton"),
245-
placement: "bottom",
246-
middleware: [
247-
size({
248-
apply({ rects, elements }) {
249-
Object.assign(elements.floating.style, {
250-
width: `${rects.reference.width}px`,
251-
});
252-
},
253-
}),
254-
],
255-
}}
256-
elementProps={{
257-
style: {
258-
zIndex: 10,
259-
},
260-
}}
317+
{...floatingUIOptions?.extendRowsButton}
261318
>
262319
{state.show &&
263320
state.showAddOrRemoveRowsButton &&
@@ -272,27 +329,7 @@ export const TableHandlesController = <
272329
</GenericPopover>
273330
<GenericPopover
274331
reference={references?.tableReference}
275-
useFloatingOptions={{
276-
open:
277-
state.show &&
278-
state.showAddOrRemoveColumnsButton &&
279-
(!onlyShownElement || onlyShownElement === "extendColumnsButton"),
280-
placement: "right",
281-
middleware: [
282-
size({
283-
apply({ rects, elements }) {
284-
Object.assign(elements.floating.style, {
285-
height: `${rects.reference.height}px`,
286-
});
287-
},
288-
}),
289-
],
290-
}}
291-
elementProps={{
292-
style: {
293-
zIndex: 10,
294-
},
295-
}}
332+
{...floatingUIOptions?.extendColumnsButton}
296333
>
297334
{state.show &&
298335
state.showAddOrRemoveColumnsButton &&

0 commit comments

Comments
 (0)