Skip to content

Commit a1297ba

Browse files
authored
Remove clsx dependency (#3883)
1 parent 330d0f5 commit a1297ba

File tree

9 files changed

+44
-28
lines changed

9 files changed

+44
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ The DataGrid component is designed to handle large datasets efficiently while of
2222

2323
- [React 19.2+](package.json) support
2424
- Evergreen browsers and server-side rendering support
25-
- Tree-shaking support and only [one npm dependency](package.json) to keep your bundles slim
25+
- Tree-shaking support with no external dependencies to keep your bundles slim
2626
- Great performance thanks to virtualization: columns and rows outside the viewport are not rendered
2727
- Strictly typed with TypeScript
2828
- [Keyboard accessibility](https://comcast.github.io/react-data-grid/#/CommonFeatures)

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
"prepublishOnly": "npm install && node --run build",
4949
"postpublish": "git push --follow-tags origin HEAD"
5050
},
51-
"dependencies": {
52-
"clsx": "^2.0.0"
53-
},
5451
"devDependencies": {
5552
"@babel/preset-typescript": "^7.27.1",
5653
"@biomejs/biome": "2.2.5",
@@ -72,6 +69,7 @@
7269
"@vitest/eslint-plugin": "^1.3.4",
7370
"@wyw-in-js/rollup": "^0.7.0",
7471
"@wyw-in-js/vite": "^0.7.0",
72+
"clsx": "^2.1.1",
7573
"eslint": "^9.36.0",
7674
"eslint-plugin-jest-dom": "^5.5.0",
7775
"eslint-plugin-react": "^7.37.5",

src/DataGrid.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from 'react';
99
import type { Key, KeyboardEvent } from 'react';
1010
import { flushSync } from 'react-dom';
11-
import clsx from 'clsx';
1211

1312
import {
1413
HeaderRowSelectionChangeContext,
@@ -26,6 +25,7 @@ import {
2625
abs,
2726
assertIsValidKeyGetter,
2827
canExitGrid,
28+
classnames,
2929
createCellEvent,
3030
getCellStyle,
3131
getColSpan,
@@ -991,7 +991,10 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
991991
return (
992992
<div
993993
style={dragHandleStyle}
994-
className={clsx(cellDragHandleClassname, column.frozen && cellDragHandleFrozenClassname)}
994+
className={classnames(
995+
cellDragHandleClassname,
996+
column.frozen && cellDragHandleFrozenClassname
997+
)}
995998
onPointerDown={handleDragHandlePointerDown}
996999
onPointerMove={isDragging ? handleDragHandlePointerMove : undefined}
9971000
onLostPointerCapture={isDragging ? handleDragHandleLostPointerCapture : undefined}
@@ -1185,7 +1188,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
11851188
// Scrollable containers without tabIndex are keyboard focusable in Chrome only if there is no focusable element inside
11861189
// whereas they are always focusable in Firefox. We need to set tabIndex to have a consistent behavior across browsers.
11871190
tabIndex={-1}
1188-
className={clsx(
1191+
className={classnames(
11891192
rootClassname,
11901193
{
11911194
[viewportDraggingClassname]: isDragging
@@ -1329,7 +1332,7 @@ export function DataGrid<R, SR = unknown, K extends Key = Key>(props: DataGridPr
13291332
<div
13301333
ref={focusSinkRef}
13311334
tabIndex={isGroupRowFocused ? 0 : -1}
1332-
className={clsx(focusSinkClassname, {
1335+
className={classnames(focusSinkClassname, {
13331336
[focusSinkHeaderAndSummaryClassname]: !isRowIdxWithinViewportBounds(
13341337
selectedPosition.rowIdx
13351338
),

src/GroupRow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { memo, useMemo } from 'react';
22
import { css } from '@linaria/core';
3-
import clsx from 'clsx';
43

54
import { RowSelectionContext, type RowSelectionContextValue } from './hooks';
6-
import { getRowStyle } from './utils';
5+
import { classnames, getRowStyle } from './utils';
76
import type { BaseRenderRowProps, GroupRow } from './types';
87
import { SELECT_COLUMN_KEY } from './Columns';
98
import GroupCell from './GroupCell';
@@ -65,7 +64,7 @@ function GroupedRow<R, SR>({
6564
aria-setsize={row.setSize}
6665
aria-posinset={row.posInSet + 1} // aria-posinset is 1-based
6766
aria-expanded={row.isExpanded}
68-
className={clsx(
67+
className={classnames(
6968
rowClassname,
7069
groupRowClassname,
7170
`rdg-row-${rowIdx % 2 === 0 ? 'even' : 'odd'}`,

src/GroupedColumnHeaderCell.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import clsx from 'clsx';
2-
31
import { useRovingTabIndex } from './hooks';
4-
import { getHeaderCellRowSpan, getHeaderCellStyle } from './utils';
2+
import { classnames, getHeaderCellRowSpan, getHeaderCellStyle } from './utils';
53
import type { CalculatedColumnParent } from './types';
64
import type { GroupedColumnHeaderRowProps } from './GroupedColumnHeaderRow';
75
import { cellClassname } from './style/cell';
@@ -39,7 +37,7 @@ export default function GroupedColumnHeaderCell<R, SR>({
3937
aria-rowspan={rowSpan}
4038
aria-selected={isCellSelected}
4139
tabIndex={tabIndex}
42-
className={clsx(cellClassname, column.headerCellClass)}
40+
className={classnames(cellClassname, column.headerCellClass)}
4341
style={{
4442
...getHeaderCellStyle(column, rowIdx, rowSpan),
4543
gridColumnStart: index,

src/HeaderRow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { memo, useState } from 'react';
22
import { css } from '@linaria/core';
3-
import clsx from 'clsx';
43

5-
import { getColSpan } from './utils';
4+
import { classnames, getColSpan } from './utils';
65
import type { CalculatedColumn, Direction, Maybe, Position, ResizedWidth } from './types';
76
import type { DataGridProps } from './DataGrid';
87
import HeaderCell from './HeaderCell';
@@ -97,7 +96,7 @@ function HeaderRow<R, SR, K extends React.Key>({
9796
<div
9897
role="row"
9998
aria-rowindex={rowIdx} // aria-rowindex is 1 based
100-
className={clsx(
99+
className={classnames(
101100
headerRowClassname,
102101
{
103102
[rowSelectedClassname]: selectedCellIdx === -1

src/Row.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { memo, useMemo } from 'react';
2-
import clsx from 'clsx';
32

43
import { RowSelectionContext, useLatestFunc, type RowSelectionContextValue } from './hooks';
5-
import { getColSpan, getRowStyle } from './utils';
4+
import { classnames, getColSpan, getRowStyle } from './utils';
65
import type { CalculatedColumn, RenderRowProps } from './types';
76
import { useDefaultRenderers } from './DataGridDefaultRenderersContext';
87
import { rowClassname, rowSelectedClassname } from './style/row';
@@ -35,7 +34,7 @@ function Row<R, SR>({
3534
onRowChange(column, rowIdx, newRow);
3635
});
3736

38-
className = clsx(
37+
className = classnames(
3938
rowClassname,
4039
`rdg-row-${rowIdx % 2 === 0 ? 'even' : 'odd'}`,
4140
{

src/SummaryRow.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { memo } from 'react';
22
import { css } from '@linaria/core';
3-
import clsx from 'clsx';
43

5-
import { getColSpan, getRowStyle } from './utils';
4+
import { classnames, getColSpan, getRowStyle } from './utils';
65
import type { RenderRowProps } from './types';
76
import { cell, cellFrozen } from './style/cell';
87
import {
@@ -90,7 +89,7 @@ function SummaryRow<R, SR>({
9089
<div
9190
role="row"
9291
aria-rowindex={ariaRowIndex}
93-
className={clsx(
92+
className={classnames(
9493
rowClassname,
9594
`rdg-row-${rowIdx % 2 === 0 ? 'even' : 'odd'}`,
9695
summaryRowClassname,

src/utils/styleUtils.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { CSSProperties } from 'react';
2-
import clsx from 'clsx';
32

4-
import type { CalculatedColumn, CalculatedColumnOrColumnGroup } from '../types';
3+
import type { CalculatedColumn, CalculatedColumnOrColumnGroup, Maybe } from '../types';
54
import { cellClassname, cellFrozenClassname } from '../style/cell';
65

76
export function getRowStyle(rowIdx: number): CSSProperties {
@@ -45,11 +44,33 @@ export function getCellStyle<R, SR>(
4544
};
4645
}
4746

47+
type ClassValue = Maybe<string> | Record<string, boolean> | false;
48+
49+
export function classnames(...args: readonly ClassValue[]) {
50+
let classname = '';
51+
52+
for (const arg of args) {
53+
if (arg) {
54+
if (typeof arg === 'string') {
55+
classname += ` ${arg}`;
56+
} else if (typeof arg === 'object') {
57+
for (const key in arg) {
58+
if (arg[key]) {
59+
classname += ` ${key}`;
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
return classname.trimStart();
67+
}
68+
4869
export function getCellClassname<R, SR>(
4970
column: CalculatedColumn<R, SR>,
50-
...extraClasses: Parameters<typeof clsx>
71+
...extraClasses: readonly ClassValue[]
5172
): string {
52-
return clsx(
73+
return classnames(
5374
cellClassname,
5475
{
5576
[cellFrozenClassname]: column.frozen

0 commit comments

Comments
 (0)