From 297c112678c7beb9d64ba6ac8f8ff0e3eeeb0e0c Mon Sep 17 00:00:00 2001 From: zbeyens Date: Thu, 26 Dec 2024 14:59:49 +0100 Subject: [PATCH] perf --- .../content/docs/en/components/changelog.mdx | 4 +- .../default/plate-ui/table-cell-element.tsx | 248 +++++++++++------- .../TableCellElement/useTableCellElement.ts | 5 +- .../useTableCellElementResizable.ts | 66 ++--- 4 files changed, 203 insertions(+), 120 deletions(-) diff --git a/apps/www/content/docs/en/components/changelog.mdx b/apps/www/content/docs/en/components/changelog.mdx index 5d690f750a..d32fe89a86 100644 --- a/apps/www/content/docs/en/components/changelog.mdx +++ b/apps/www/content/docs/en/components/changelog.mdx @@ -18,7 +18,9 @@ v42 - Move icons to `table-icons` - Remove `colgroup`, col width is now set in `table-cell-element` - `table-row-element`: remove `hideBorder` prop -- `table-cell-element`, `table-cell-element-static`: column hover/resizing state is now using Tailwind instead of JS +- `table-cell-element`, `table-cell-element-static`: + - column hover/resizing state is now using Tailwind instead of JS + - React.memo - `table-dropdown-menu`: - dynamic table insert - merge/split cells diff --git a/apps/www/src/registry/default/plate-ui/table-cell-element.tsx b/apps/www/src/registry/default/plate-ui/table-cell-element.tsx index 84afcab3c3..d2e3ab1163 100644 --- a/apps/www/src/registry/default/plate-ui/table-cell-element.tsx +++ b/apps/www/src/registry/default/plate-ui/table-cell-element.tsx @@ -2,6 +2,8 @@ import React from 'react'; +import type { BorderStylesDefault } from '@udecode/plate-table'; + import { cn, withProps, withRef } from '@udecode/cn'; import { useEditorPlugin, useElement } from '@udecode/plate-common/react'; import { useBlockSelected } from '@udecode/plate-selection/react'; @@ -24,11 +26,7 @@ export const TableCellElement = withRef< isHeader?: boolean; } >(({ children, className, isHeader, style, ...props }, ref) => { - const { element } = props; - const { api } = useEditorPlugin(TablePlugin); - const readOnly = useReadOnly(); const rowElement = useElement(TableRowPlugin.key); - const isSelectingRow = useBlockSelected(rowElement.id as string); const { borders, @@ -49,100 +47,174 @@ export const TableCellElement = withRef< }); return ( - + {children} + + ); +}); + +const TableCellElementMemo = React.memo( + React.forwardRef< + React.ComponentRef, + React.ComponentPropsWithoutRef & { + borders: BorderStylesDefault; + bottomProps: React.ComponentPropsWithoutRef; + colIndex: number; + colSpan: number; + hiddenLeft: boolean; + isSelectingCell: boolean; + leftProps: React.ComponentPropsWithoutRef; + rightProps: React.ComponentPropsWithoutRef; + rowId: string; + rowIndex: number; + selected: boolean; + width: number | string; + isHeader?: boolean; + minHeight?: number | string; + } + >( + ( + { + borders, + bottomProps, + children, className, - 'relative h-full overflow-visible border-none bg-background p-0', - element.background ? 'bg-[--cellBackground]' : 'bg-background', - - cn( - isHeader && 'text-left [&_>_*]:m-0', - 'before:size-full', - selected && 'before:z-10 before:bg-muted', - "before:absolute before:box-border before:select-none before:content-['']", - borders && + colIndex, + hiddenLeft, + isHeader, + isSelectingCell, + leftProps, + minHeight, + rightProps, + rowId, + rowIndex, + selected, + style, + width, + ...props + }, + ref + ) => { + const { api } = useEditorPlugin(TablePlugin); + const readOnly = useReadOnly(); + const isSelectingRow = useBlockSelected(rowId); + + const element = useElement(); + + return ( + _*]:m-0', + 'before:size-full', + selected && 'before:z-10 before:bg-muted', + "before:absolute before:box-border before:select-none before:content-['']", + borders && + cn( + borders.bottom?.size && + `before:border-b before:border-b-border`, + borders.right?.size && + `before:border-r before:border-r-border`, + borders.left?.size && + `before:border-l before:border-l-border`, + borders.top?.size && `before:border-t before:border-t-border` + ) ) - ) - )} - style={ - { - '--cellBackground': element.background, - maxWidth: width || 240, - minWidth: width || 120, - ...style, - } as React.CSSProperties - } - {...{ - colSpan: api.table.getColSpan(element), - rowSpan: api.table.getRowSpan(element), - }} - {...props} - > -
- {children} -
- - {!isSelectingCell && ( -
- {!readOnly && ( - <> - - - {!hiddenLeft && ( - - )} +
+ {children} +
+ + {!isSelectingCell && ( +
+ {!readOnly && ( + <> + + + {!hiddenLeft && ( + + )} -