Skip to content

Commit

Permalink
Optimize scroll-triggered rerendering
Browse files Browse the repository at this point in the history
  • Loading branch information
icflorescu committed Apr 12, 2024
1 parent 90aaa2f commit 28e8249
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ Minor versions that are not listed in the changelog are bug fixes and small impr

## 7.8.0 (2024-04-12)

- Update dev dependencies to ensure compatibility with Mantine 7.8 and Next.js 14.2.0
- Update peer dependencies to Mantine 7.8
- Remove `useDragToggleColumns` hook, previously deprecated in favor of `useDataTableColumns`
- Make the scroll shadows gentler, especially in dark mode
- Optimize scroll-triggered re-renders by using a `debouncedProcessScrolling` method
- Implement `maxHeight` property

## 7.7.0 (2024-04-04)

Expand Down
6 changes: 4 additions & 2 deletions app/examples/complex-usage-scenario/ComplexUsageExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Employee } from '~/data';
import { getEmployeesAsync } from '~/data/async';
import classes from './ComplexUsageExample.module.css';

const PAGE_SIZE = 20;
const PAGE_SIZE = 100;

export function ComplexUsageExample() {
const { showContextMenu, hideContextMenu } = useContextMenu();
Expand Down Expand Up @@ -227,7 +227,9 @@ export function ComplexUsageExample() {

return (
<DataTable
height={400}
height="70dvh"
minHeight={400}
maxHeight={1000}
withTableBorder
highlightOnHover
borderRadius="sm"
Expand Down
1 change: 1 addition & 0 deletions app/examples/empty-state/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async function EmptyStateExamplePage() {
return (
<>
<PageTitle of={PATH} />
<Code hidden>minHeight</Code>
<Txt>
If <Code>records</Code> property points to an empty array, the <Code>DataTable</Code> component will indicate
its empty state by displaying a customizable icon and text, like so:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
"webpack": "^5.91.0"
},
"peerDependencies": {
"@mantine/core": "7 <= 7.7.0 || >= 7.7.2",
"@mantine/hooks": "7 <= 7.7.0 || >= 7.7.2",
"@mantine/core": ">=7.8",
"@mantine/hooks": ">=7.8",
"clsx": ">=2",
"react": ">=18.2"
}
Expand Down
10 changes: 7 additions & 3 deletions package/DataTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Table, type MantineSize } from '@mantine/core';
import { useMergedRef } from '@mantine/hooks';
import { useDebounceCallback, useMergedRef } from '@mantine/hooks';
import clsx from 'clsx';
import { useCallback, useMemo, useState } from 'react';
import { DataTableColumnsProvider } from './DataTableDragToggleProvider';
Expand Down Expand Up @@ -29,6 +29,7 @@ export function DataTable<T>({
textSelectionDisabled,
height = '100%',
minHeight,
maxHeight,
shadow,
verticalAlign = 'center',
fetching,
Expand Down Expand Up @@ -204,12 +205,14 @@ export function DataTable<T>({

useIsomorphicLayoutEffect(processScrolling, [processScrolling]);

const debouncedProcessScrolling = useDebounceCallback(processScrolling, 50);

const handleScrollPositionChange = useCallback(
(e: { x: number; y: number }) => {
onScroll?.(e);
processScrolling();
debouncedProcessScrolling();
},
[processScrolling, onScroll]
[debouncedProcessScrolling, onScroll]
);

const handlePageChange = useCallback(
Expand Down Expand Up @@ -282,6 +285,7 @@ export function DataTable<T>({
boxShadow: theme.shadows[shadow as MantineSize] || shadow,
height,
minHeight,
maxHeight,
}),
style,
styles?.root,
Expand Down
5 changes: 5 additions & 0 deletions package/types/DataTableProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export type DataTableProps<T = Record<string, unknown>> = {
*/
minHeight?: string | number;

/**
* Maximum table height.
*/
maxHeight?: string | number;

/**
* DataTable component shadow.
*/
Expand Down

0 comments on commit 28e8249

Please sign in to comment.