Skip to content

Commit 6512617

Browse files
authored
Expand styling and customization docs (#3933)
* Expand styling and customization docs * tweaks * static rowHeight
1 parent b59a7a1 commit 6512617

File tree

3 files changed

+240
-15
lines changed

3 files changed

+240
-15
lines changed

README.md

Lines changed: 234 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,120 @@ function App() {
120120
}
121121
```
122122

123-
## Theming
123+
## Styling and Customization
124124

125-
Set `--rdg-color-scheme: light/dark` at the `:root` to control the color theme. The light or dark themes can be enforced using the `rdg-light` or `rdg-dark` classes.
125+
The DataGrid provides multiple ways to customize its appearance and behavior.
126+
127+
### Light/Dark Themes
128+
129+
The DataGrid supports both light and dark color schemes out of the box using CSS's `light-dark()` function. The theme automatically adapts based on the user's system preference when `color-scheme: light dark;` is set.
130+
131+
To enforce a specific theme, we recommend setting the standard `color-scheme` CSS property on the `:root`:
132+
133+
```css
134+
:root {
135+
color-scheme: light; /* or 'dark', or 'light dark' for auto */
136+
}
137+
```
138+
139+
Alternatively, you can add the `rdg-light` or `rdg-dark` class to individual grids:
140+
141+
```tsx
142+
// Force light theme
143+
<DataGrid className="rdg-light" columns={columns} rows={rows} />
144+
145+
// Force dark theme
146+
<DataGrid className="rdg-dark" columns={columns} rows={rows} />
147+
```
148+
149+
### CSS Variables
150+
151+
The DataGrid supports the following CSS variables for customization:
152+
153+
```css
154+
.rdg {
155+
/* Selection */
156+
--rdg-selection-width: 2px;
157+
--rdg-selection-color: hsl(207, 75%, 66%);
158+
159+
/* Typography */
160+
--rdg-font-size: 14px;
161+
162+
/* Colors (using light-dark() for automatic theme switching) */
163+
--rdg-color: light-dark(#000, #ddd);
164+
--rdg-background-color: light-dark(hsl(0deg 0% 100%), hsl(0deg 0% 13%));
165+
166+
/* Header */
167+
--rdg-header-background-color: light-dark(hsl(0deg 0% 97.5%), hsl(0deg 0% 10.5%));
168+
--rdg-header-draggable-background-color: light-dark(hsl(0deg 0% 90.5%), hsl(0deg 0% 17.5%));
169+
170+
/* Rows */
171+
--rdg-row-hover-background-color: light-dark(hsl(0deg 0% 96%), hsl(0deg 0% 9%));
172+
--rdg-row-selected-background-color: light-dark(hsl(207deg 76% 92%), hsl(207deg 76% 42%));
173+
--rdg-row-selected-hover-background-color: light-dark(hsl(207deg 76% 88%), hsl(207deg 76% 38%));
174+
175+
/* Borders */
176+
--rdg-border-width: 1px;
177+
--rdg-border-color: light-dark(#ddd, #444);
178+
--rdg-summary-border-width: calc(var(--rdg-border-width) * 2);
179+
--rdg-summary-border-color: light-dark(#aaa, #555);
180+
181+
/* Frozen columns */
182+
--rdg-cell-frozen-box-shadow: 2px 0 5px -2px rgba(136, 136, 136, 0.3);
183+
184+
/* Checkboxes */
185+
--rdg-checkbox-focus-color: hsl(207deg 100% 69%);
186+
}
187+
```
188+
189+
Example of customizing colors:
190+
191+
```css
192+
.my-custom-grid {
193+
--rdg-background-color: #f0f0f0;
194+
--rdg-selection-color: #ff6b6b;
195+
--rdg-font-size: 16px;
196+
}
197+
```
198+
199+
```tsx
200+
<DataGrid className="my-custom-grid" columns={columns} rows={rows} />
201+
```
202+
203+
### Standard Props
204+
205+
The DataGrid accepts standard [`className`](#classname-string--undefined) and [`style`](#style-cssproperties--undefined) props:
206+
207+
```tsx
208+
<DataGrid
209+
columns={columns}
210+
rows={rows}
211+
className="my-grid custom-theme"
212+
style={{ width: 800, height: 600 }}
213+
/>
214+
```
215+
216+
### Row and Cell Styling
217+
218+
#### Row Heights
219+
220+
Control row heights using the [`rowHeight`](#rowheight-maybenumber--row-r--number), [`headerRowHeight`](#headerrowheight-maybenumber), and [`summaryRowHeight`](#summaryrowheight-maybenumber) props. The `rowHeight` prop supports both fixed heights and dynamic heights per row.
221+
222+
#### Row Classes
223+
224+
Apply custom CSS classes to rows using the [`rowClass`](#rowclass-mayberow-r-rowidx-number--maybestring) prop, and to header rows using the [`headerRowClass`](#headerrowclass-maybestring) prop.
225+
226+
#### Cell Classes
227+
228+
Apply custom CSS classes to cells using the [`cellClass`](#cellclass-maybestring--row-trow--maybestring) property in column definitions. You can also use [`headerCellClass`](#headercellclass-maybestring) and [`summaryCellClass`](#summarycellclass-maybestring--row-tsummaryrow--maybestring) for header and summary cells respectively.
229+
230+
#### Column Widths
231+
232+
Control column widths using the [`width`](#width-maybenumber--string), [`minWidth`](#minwidth-maybenumber), and [`maxWidth`](#maxwidth-maybenumber) properties in column definitions. Enable column resizing using the [`resizable`](#resizable-maybeboolean) property, or use [`defaultColumnOptions`](#defaultcolumnoptions-maybedefaultcolumnoptionsr-sr) to apply it to all columns.
233+
234+
### Custom Renderers
235+
236+
Replace default components with custom implementations using the [`renderers`](#renderers-mayberenderersr-sr) prop. Columns can also have custom renderers using the [`renderCell`](#rendercell-maybeprops-rendercellpropstrow-tsummaryrow--reactnode), [`renderHeaderCell`](#renderheadercell-maybeprops-renderheadercellpropstrow-tsummaryrow--reactnode), [`renderSummaryCell`](#rendersummarycell-maybeprops-rendersummarycellpropstsummaryrow-trow--reactnode), [`renderGroupCell`](#rendergroupcell-maybeprops-rendergroupcellpropstrow-tsummaryrow--reactnode), and [`renderEditCell`](#rendereditcell-maybeprops-rendereditcellpropstrow-tsummaryrow--reactnode) properties.
126237

127238
## API Reference
128239

@@ -214,7 +325,19 @@ function MyGrid() {
214325

215326
Height of each row in pixels. A function can be used to set different row heights.
216327

217-
:warning: **Performance:** When using a function, the height of all rows is calculated upfront on every render. For large datasets (1000+ rows), this can cause performance issues. Consider using a fixed height when possible, or memoize the `rowHeight` function.
328+
```tsx
329+
// Fixed height for all rows
330+
<DataGrid columns={columns} rows={rows} rowHeight={50} />;
331+
332+
// Dynamic height per row
333+
function getRowHeight(row) {
334+
return row.isExpanded ? 100 : 35;
335+
}
336+
337+
<DataGrid columns={columns} rows={rows} rowHeight={getRowHeight} />;
338+
```
339+
340+
:warning: **Performance:** When using a function, the heights of all rows are processed upfront. For large datasets (1000+ rows), this can cause performance issues if the identity of the function changes and invalidates internal memoization. Consider using a static function when possible, or memoize the `rowHeight` function.
218341

219342
###### `headerRowHeight?: Maybe<number>`
220343

@@ -228,6 +351,17 @@ Height of the header row in pixels.
228351

229352
Height of each summary row in pixels.
230353

354+
```tsx
355+
<DataGrid
356+
columns={columns}
357+
rows={rows}
358+
rowHeight={35}
359+
headerRowHeight={45}
360+
summaryRowHeight={40}
361+
topSummaryRows={topSummaryRows}
362+
/>
363+
```
364+
231365
###### `columnWidths?: Maybe<ColumnWidths>`
232366

233367
A map of column widths containing both measured and resized widths. If not provided then an internal state is used.
@@ -508,7 +642,40 @@ interface Renderers<TRow, TSummaryRow> {
508642
}
509643
```
510644

511-
For example, the default `<Row />` component can be wrapped via the `renderRow` prop to add contexts or tweak props
645+
Example of replacing default components:
646+
647+
```tsx
648+
import { DataGrid, type Renderers } from 'react-data-grid';
649+
650+
const customRenderers: Renderers<Row, SummaryRow> = {
651+
// Custom row render function
652+
renderRow(key, props) {
653+
return <CustomRow key={key} {...props} />;
654+
},
655+
656+
// Custom cell render function
657+
renderCell(key, props) {
658+
return <CustomCell key={key} {...props} />;
659+
},
660+
661+
// Custom checkbox render function
662+
renderCheckbox(props) {
663+
return <CustomCheckbox {...props} />;
664+
},
665+
666+
// Custom sort status indicator
667+
renderSortStatus(props) {
668+
return <CustomSortIcon {...props} />;
669+
},
670+
671+
// Custom empty state
672+
noRowsFallback: <div>No data available</div>
673+
};
674+
675+
<DataGrid columns={columns} rows={rows} renderers={customRenderers} />;
676+
```
677+
678+
The default `<Row />` component can be wrapped via the `renderRow` prop to add contexts or tweak props:
512679

513680
```tsx
514681
import { DataGrid, RenderRowProps, Row } from 'react-data-grid';
@@ -533,13 +700,13 @@ Function to apply custom class names to rows.
533700
```tsx
534701
import { DataGrid } from 'react-data-grid';
535702

536-
function MyGrid() {
537-
return <DataGrid columns={columns} rows={rows} rowClass={rowClass} />;
538-
}
539-
540703
function rowClass(row: Row, rowIdx: number) {
541704
return rowIdx % 2 === 0 ? 'even' : 'odd';
542705
}
706+
707+
function MyGrid() {
708+
return <DataGrid columns={columns} rows={rows} rowClass={rowClass} />;
709+
}
543710
```
544711

545712
:warning: **Performance:** Define this function outside your component or memoize it with `useCallback` to avoid re-rendering all rows on every render.
@@ -548,6 +715,10 @@ function rowClass(row: Row, rowIdx: number) {
548715

549716
Custom class name for the header row.
550717

718+
```tsx
719+
<DataGrid columns={columns} rows={rows} headerRowClass="sticky-header" />
720+
```
721+
551722
###### `direction?: Maybe<'ltr' | 'rtl'>`
552723

553724
This property sets the text direction of the grid, it defaults to `'ltr'` (left-to-right). Setting `direction` to `'rtl'` has the following effects:
@@ -1036,6 +1207,31 @@ A unique key to distinguish each column
10361207

10371208
Width can be any valid css grid column value. If not specified, it will be determined automatically based on grid width and specified widths of other columns.
10381209

1210+
```tsx
1211+
const columns: Column<Row>[] = [
1212+
{
1213+
key: 'id',
1214+
name: 'ID',
1215+
width: 80, // Fixed width in pixels
1216+
resizable: false
1217+
},
1218+
{
1219+
key: 'name',
1220+
name: 'Name',
1221+
width: '30%', // Percentage width
1222+
minWidth: 100,
1223+
maxWidth: 400
1224+
},
1225+
{
1226+
key: 'description',
1227+
name: 'Description'
1228+
// No width specified - automatically sized
1229+
}
1230+
];
1231+
```
1232+
1233+
Other examples:
1234+
10391235
```tsx
10401236
width: 80, // pixels
10411237
width: '25%',
@@ -1059,6 +1255,36 @@ Maximum column width in pixels.
10591255

10601256
Class name(s) for cells. Can be a string or a function that returns a class name based on the row.
10611257

1258+
```tsx
1259+
const columns: Column<Row>[] = [
1260+
{
1261+
key: 'status',
1262+
name: 'Status',
1263+
cellClass: (row) => `status-${row.status}`
1264+
},
1265+
{
1266+
key: 'price',
1267+
name: 'Price',
1268+
cellClass: 'text-right' // Static class
1269+
}
1270+
];
1271+
```
1272+
1273+
```css
1274+
.status-active {
1275+
color: green;
1276+
font-weight: bold;
1277+
}
1278+
1279+
.status-inactive {
1280+
color: grey;
1281+
}
1282+
1283+
.text-right {
1284+
text-align: right;
1285+
}
1286+
```
1287+
10621288
##### `headerCellClass?: Maybe<string>`
10631289

10641290
Class name(s) for the header cell.

src/style/core.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ const root = css`
3131
--rdg-checkbox-focus-color: hsl(207deg 100% 69%);
3232
3333
&.rdg-dark {
34-
--rdg-color-scheme: dark;
34+
color-scheme: dark;
3535
}
3636
3737
&.rdg-light {
38-
--rdg-color-scheme: light;
38+
color-scheme: light;
3939
}
4040
41-
color-scheme: var(--rdg-color-scheme, light dark);
42-
4341
&:dir(rtl) {
4442
--rdg-cell-frozen-box-shadow: -2px 0 5px -2px rgba(136, 136, 136, 0.3);
4543
}

website/root.css

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ body {
66
}
77

88
:root {
9+
color-scheme: light dark;
10+
911
&:has([value='light']:checked) {
10-
--rdg-color-scheme: light;
12+
color-scheme: light;
1113
}
1214

1315
&:has([value='dark']:checked) {
14-
--rdg-color-scheme: dark;
16+
color-scheme: dark;
1517
}
1618
}
1719

1820
body {
19-
color-scheme: var(--rdg-color-scheme, light dark);
2021
background-color: light-dark(#fff, hsl(0deg 0% 10%));
2122
color: light-dark(#111, #fff);
2223
}

0 commit comments

Comments
 (0)