Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): virtualization #4285

Open
wants to merge 27 commits into
base: canary
Choose a base branch
from

Conversation

vinroger
Copy link
Contributor

@vinroger vinroger commented Dec 9, 2024

Closes #3697

Results: Demo for 10000 rows + Full Page Refresh (Delay reduced significantly)

table.virtualized.mp4

📝 Description

This PR introduces virtualization support for the Table component, enabling efficient rendering and handling of large datasets by only rendering visible items in the viewport. The implementation utilizes the @tanstack/react-virtual library and adds several customizable props to enhance usability and performance.

⛳️ Current behavior (updates)

The current Table component renders all rows in the DOM, which can lead to significant performance issues when displaying large datasets.

🚀 New behavior

  • Virtualization Support:

    • Added isVirtualized prop to enable virtualization.
    • Introduced new props:
      • rowHeight: Specifies the height of each row (default: 40px).
      • maxTableHeight: Sets the maximum height of the table (default: 600px).
  • Documentation and Examples:

    • Added examples demonstrating:
      • Virtualized table with 10,000 rows.
      • Custom rowHeight for variable row sizes.
      • Custom maxTableHeight configurations.
  • Performance Improvements:

    • Virtualization efficiently renders only visible rows, reducing DOM node usage and improving performance.

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

This feature is powered by the @tanstack/react-virtual library for efficient list rendering. All new props and behavior changes are backward-compatible, ensuring seamless integration with existing implementations.

Summary by CodeRabbit

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced virtualization support for the Table component, allowing efficient rendering of large datasets.
    • Added new props: isVirtualized, rowHeight, and maxTableHeight for enhanced configurability.
    • New routes added in documentation for "NextUI to HeroUI," "Figma," and "Laravel."
    • New examples in documentation demonstrating virtualization with 500 and 10,000 rows.
  • Bug Fixes

    • Updated type handling for the TableRowGroup component to improve type safety.
  • Documentation

    • Enhanced documentation for the Table component with new sections on virtualization and updated API details.
    • Updated documentation to reflect new routes and props.
  • Chores

    • Updated dependencies in the package configuration.

Copy link

linear bot commented Dec 9, 2024

Copy link

changeset-bot bot commented Dec 9, 2024

🦋 Changeset detected

Latest commit: f73a7a5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@heroui/table Patch
@heroui/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Dec 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
heroui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 1, 2025 10:27am
heroui-sb ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 1, 2025 10:27am
nextui-docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 1, 2025 10:27am
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 1, 2025 10:27am

Copy link
Contributor

coderabbitai bot commented Dec 9, 2024

Walkthrough

The changes in this pull request involve updates to the documentation and functionality of various table components within the NextUI framework. Key modifications include the introduction of virtualization features to enhance performance when rendering large datasets. The routes.json configuration has been updated to reflect new and updated content for documentation, while several new React components have been created to support virtualized table rendering. Additionally, the documentation has been enhanced to include new props related to virtualization, ensuring comprehensive guidance for users.

Changes

File Path Change Summary
apps/docs/config/routes.json Updated multiple routes with updated and newPost flags; added new routes for "NextUI to HeroUI," "Figma," and "Laravel."
apps/docs/content/components/table/index.ts Added imports for virtualization-related features and updated tableContent export.
apps/docs/content/components/table/virtualization-custom-max-table-height.raw.jsx Introduced a new component for a virtualized table with a max height of 300px.
apps/docs/content/components/table/virtualization-custom-max-table-height.ts Created a module to export the new virtualized table component.
apps/docs/content/components/table/virtualization-custom-row-height.raw.jsx Added a new component for a virtualized table with a max height of 500px.
apps/docs/content/components/table/virtualization-custom-row-height.ts Created a module to export the new virtualized table component.
apps/docs/content/components/table/virtualization-ten-thousand.raw.jsx Introduced a new component for a virtualized table capable of displaying 10,000 rows.
apps/docs/content/components/table/virtualization-ten-thousand.ts Created a module to export the new virtualized table component.
apps/docs/content/components/table/virtualization.raw.jsx Added a new component for rendering a virtualized table with 500 rows.
apps/docs/content/components/table/virtualization.ts Created a module to export the new virtualized table component.
apps/docs/content/docs/components/table.mdx Enhanced documentation with new props for virtualization and examples for large datasets.
packages/components/table/package.json Added a new dependency on @tanstack/react-virtual.
packages/components/table/src/table-row-group.tsx Updated the type of the forwarded ref in TableRowGroup from "thead" to HTMLTableSectionElement.
packages/components/table/src/table.tsx Modified TableProps to include virtualization-related props and updated rendering logic.
packages/components/table/src/virtualized-table-body.tsx Introduced VirtualizedTableBody component for rendering virtualized table bodies.
packages/components/table/src/virtualized-table.tsx Created VirtualizedTable component to enhance rendering performance through virtualization.
packages/components/table/stories/table.stories.tsx Added new story templates for virtualized table scenarios with 500 and 10,000 rows.

Assessment against linked issues

Objective Addressed Explanation
Support virtualization for large datasets (#3697)
Provide options to enable virtualization in the table component (#3697)
Enhance performance when rendering large tables (#3697)

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f8cd39 and f73a7a5.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (3)
  • apps/docs/package.json (2 hunks)
  • package.json (1 hunks)
  • packages/components/table/src/table-row-group.tsx (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • apps/docs/package.json
  • packages/components/table/src/table-row-group.tsx
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: TypeScript
  • GitHub Check: Build
🔇 Additional comments (1)
package.json (1)

136-136: TypeScript Version Update Verification

The TypeScript version in the devDependencies has been updated from "^5.5.0" to "^5.7.3". This upgrade is beneficial for leveraging the latest TypeScript enhancements and ensuring compatibility with other recent tooling improvements. Please verify that no build or type-checking issues arise from this change and that it remains consistent with the versions used in other updated parts of the repository.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
packages/components/table/src/virtualized-table-body.tsx (1)

128-129: ⚠️ Potential issue

Correct the transform calculation for proper row positioning.

The transform calculation is incorrect and can lead to improper row positioning.

-              transform: `translateY(${virtualRow.start - index * virtualRow.size}px)`,
+              transform: `translateY(${virtualRow.start}px)`,
🧹 Nitpick comments (6)
packages/components/table/src/virtualized-table-body.tsx (2)

25-25: Consider improving type safety for rowVirtualizer.

The rowVirtualizer prop uses any type which could lead to type safety issues. Consider using a more specific type from @tanstack/react-virtual.

-  rowVirtualizer: Virtualizer<any, Element>;
+  rowVirtualizer: Virtualizer<unknown, Element>;

64-96: Consider memoizing empty and loading content.

The empty and loading content are recreated on every render. Consider using useMemo to optimize performance.

+import {useMemo} from "react";
+
 const VirtualizedTableBody = forwardRef<"tbody", VirtualizedTableBodyProps>((props, ref) => {
   // ...existing code...
-  let emptyContent;
-  let loadingContent;
+  const emptyContent = useMemo(() => {
+    if (collection.size === 0 && bodyProps.emptyContent) {
+      return (
+        <tr role="row">
+          <td
+            className={slots?.emptyWrapper({class: classNames?.emptyWrapper})}
+            colSpan={collection.columnCount}
+            role="gridcell"
+          >
+            {!isLoading && bodyProps.emptyContent}
+          </td>
+        </tr>
+      );
+    }
+    return null;
+  }, [collection.size, bodyProps.emptyContent, slots, classNames, isLoading, collection.columnCount]);
packages/components/table/src/virtualized-table.tsx (4)

39-39: Add prop validation for rowHeight and maxTableHeight.

Consider adding validation to ensure these values are positive numbers and documenting the default values in JSDoc.

+/**
+ * @property {number} [rowHeight=40] - Height of each row in pixels
+ * @property {number} [maxTableHeight=600] - Maximum height of the table in pixels
+ */
 export interface TableProps<T = object>
   extends Omit<UseTableProps<T>, "isSelectable" | "isMultiSelectable"> {
   isVirtualized?: boolean;
-  rowHeight?: number;
-  maxTableHeight?: number;
+  rowHeight?: number & Record<never, never>;
+  maxTableHeight?: number & Record<never, never>;
 }

 const VirtualizedTable = forwardRef<"table", TableProps>((props, ref) => {
+  const {rowHeight = 40, maxTableHeight = 600} = props;
+  
+  if (rowHeight <= 0 || maxTableHeight <= 0) {
+    throw new Error('rowHeight and maxTableHeight must be positive numbers');
+  }

71-75: Improve header height calculation resilience.

The current implementation might miss header height updates if the header content changes dynamically.

 useLayoutEffect(() => {
+  const updateHeaderHeight = () => {
     if (headerRef.current) {
       setHeaderHeight(headerRef.current.getBoundingClientRect().height);
     }
+  };
+
+  updateHeaderHeight();
+
+  // Optional: Setup ResizeObserver for dynamic content
+  const resizeObserver = new ResizeObserver(updateHeaderHeight);
+  if (headerRef.current) {
+    resizeObserver.observe(headerRef.current);
+  }
+
+  return () => resizeObserver.disconnect();
 }, [headerRef]);

77-82: Consider making overscan configurable.

The overscan value determines how many rows to render outside the visible area. Different use cases might benefit from different values.

 export interface TableProps<T = object>
   extends Omit<UseTableProps<T>, "isSelectable" | "isMultiSelectable"> {
   isVirtualized?: boolean;
   rowHeight?: number;
   maxTableHeight?: number;
+  overscan?: number;
 }

 const VirtualizedTable = forwardRef<"table", TableProps>((props, ref) => {
-  const {rowHeight = 40, maxTableHeight = 600} = props;
+  const {rowHeight = 40, maxTableHeight = 600, overscan = 5} = props;

   const rowVirtualizer = useVirtualizer({
     count,
     getScrollElement: () => parentRef.current,
     estimateSize: () => rowHeight,
-    overscan: 5,
+    overscan,
   });

51-52: Enhance documentation for the display property requirement.

The inline comment about display property is crucial for maintaining scroll functionality. Consider making it more visible in the component's documentation.

+/**
+ * @important The wrapper must have display: "block" to maintain scroll position
+ * when virtualizing rows. Changing this value will break the virtualization.
+ */
 return (
   <BaseComponent
     {...getWrapperProps()}
     ref={parentRef}
-    /* Display must be block to maintain the scroll "progress" */
     style={{height: maxTableHeight, display: "block"}}
   >
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2ebb53 and 022ed88.

📒 Files selected for processing (2)
  • packages/components/table/src/virtualized-table-body.tsx (1 hunks)
  • packages/components/table/src/virtualized-table.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: TypeScript
  • GitHub Check: Build

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

♻️ Duplicate comments (1)
packages/components/table/src/virtualized-table-body.tsx (1)

128-129: ⚠️ Potential issue

Correct the transform calculation for proper row positioning

The transform property in the row's style should use virtualRow.start directly. Subtracting index * virtualRow.size can lead to incorrect row positioning.

Apply this diff to fix the calculation:

-                  transform: `translateY(${virtualRow.start - index * virtualRow.size}px)`,
+                  transform: `translateY(${virtualRow.start}px)`,
🧹 Nitpick comments (1)
packages/components/table/src/virtualized-table.tsx (1)

77-82: Add a comment explaining the overscan value.

Consider adding a comment explaining why the overscan value is set to 5. This helps other developers understand the performance implications of this configuration.

Apply this diff to add the comment:

   const rowVirtualizer = useVirtualizer({
     count,
     getScrollElement: () => parentRef.current,
     estimateSize: () => rowHeight,
+    // Pre-render 5 items above and below the visible area for smooth scrolling
     overscan: 5,
   });
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc5e2f7 and 9f051e6.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (4)
  • packages/components/table/package.json (1 hunks)
  • packages/components/table/src/virtualized-table-body.tsx (1 hunks)
  • packages/components/table/src/virtualized-table.tsx (1 hunks)
  • packages/components/table/stories/table.stories.tsx (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/components/table/package.json
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: TypeScript
  • GitHub Check: Build
🔇 Additional comments (5)
packages/components/table/src/virtualized-table-body.tsx (1)

14-26: Well-defined props interface!

The VirtualizedTableBodyProps interface is well-structured and properly extends HTMLHeroUIProps<"tbody">. The props cover all necessary aspects of virtualization, selection, and styling.

packages/components/table/src/virtualized-table.tsx (1)

13-18: Well-defined props interface!

The TableProps interface properly extends the base props and adds virtualization-specific properties with appropriate types.

packages/components/table/stories/table.stories.tsx (3)

123-129: LGTM!

The generateRows utility function is well-implemented, using Array.from to efficiently generate test data with unique keys.


923-953: Well-structured story template!

The VirtualizedTemplate is well-implemented with proper virtualization settings and test data generation.


1154-1170: Consider consolidating duplicate story implementations.

The Virtualized and TenThousandRows stories share almost identical implementation, differing only in row count. Consider refactoring to reduce code duplication.

Apply this diff to consolidate the stories:

+const VirtualizedTableStory = ({rowCount, ...args}: TableProps & {rowCount: number}) => {
+  const rows = generateRows(rowCount);
+  const columns = [
+    {key: "name", label: "Name"},
+    {key: "value", label: "Value"},
+  ];
+
+  return (
+    <div>
+      <Table
+        aria-label={`Example of virtualized table with ${rowCount} rows`}
+        {...args}
+        isVirtualized
+        maxTableHeight={300}
+        rowHeight={40}
+      >
+        <TableHeader columns={columns}>
+          {(column) => <TableColumn key={column.key}>{column.label}</TableColumn>}
+        </TableHeader>
+        <TableBody items={rows}>
+          {(item) => (
+            <TableRow key={item.key}>
+              {(columnKey) => <TableCell>{item[columnKey]}</TableCell>}
+            </TableRow>
+          )}
+        </TableBody>
+      </Table>
+    </div>
+  );
+};
+
+export const Virtualized = {
+  render: (args: TableProps) => <VirtualizedTableStory rowCount={500} {...args} />,
+  args: {
+    ...defaultProps,
+    className: "max-w-3xl",
+  },
+};
+
+export const TenThousandRows = {
+  render: (args: TableProps) => <VirtualizedTableStory rowCount={10000} {...args} />,
+  args: {
+    ...defaultProps,
+    className: "max-w-3xl",
+  },
+};

@wingkwong wingkwong assigned vinroger and unassigned wingkwong Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Table support Virtualization to improve performance
3 participants