Skip to content

feat: draw grid lines for selected rows cols #1070

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/core/src/data-editor/data-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ export interface DataEditorProps extends Props, Pick<DataGridSearchProps, "image
readonly gridSelection?: GridSelection;
/**
* Emitted whenever the grid selection changes. Specifying
* this function will make the grids selection controlled, so
* this function will make the grid's selection controlled, so
* so you will need to specify {@link gridSelection} as well. See
* the "Controlled Selection" example for details.
*
Expand Down Expand Up @@ -686,6 +686,22 @@ export interface DataEditorProps extends Props, Pick<DataGridSearchProps, "image
* Allows overriding the default portal element.
*/
readonly portalElementRef?: React.RefObject<HTMLElement>;

/**
* When true (default) draws accent-coloured grid lines around selected columns in the header. Set to false to
* revert to the original behaviour where only the header background is accented.
* @group Style
* @defaultValue true
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

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

The documentation incorrectly states 'true (default)' but the actual default value is false as shown in line 911. The comment should say 'When true draws accent-coloured grid lines...' and '@DefaultValue false'.

Suggested change
* @defaultValue true
* When true draws accent-coloured grid lines around selected columns in the header. Set to false to
* revert to the original behaviour where only the header background is accented.
* @group Style
* @defaultValue false

Copilot uses AI. Check for mistakes.

*/
readonly columnSelectionGridLines?: boolean;

/**
* When true (default) draws accent-coloured grid lines around selected rows in the header. Set to false to
* revert to the original behaviour where only the header background is accented.
* @group Style
* @defaultValue true
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

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

The documentation incorrectly states 'true (default)' but the actual default value is false as shown in line 912. The comment should say 'When true draws accent-coloured grid lines...' and '@DefaultValue false'.

Suggested change
* @defaultValue true
* When true draws accent-coloured grid lines around selected rows in the header. Set to false to
* revert to the original behaviour where only the header background is accented.
* @group Style
* @defaultValue false

Copilot uses AI. Check for mistakes.

*/
readonly rowSelectionGridLines?: boolean;
}

type ScrollToFn = (
Expand Down Expand Up @@ -892,6 +908,8 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
scrollToActiveCell = true,
drawFocusRing: drawFocusRingIn = true,
portalElementRef,
columnSelectionGridLines = false,
rowSelectionGridLines = false,
} = p;

const drawFocusRing = drawFocusRingIn === "no-editor" ? overlay === undefined : drawFocusRingIn;
Expand Down Expand Up @@ -4264,6 +4282,8 @@ const DataEditorImpl: React.ForwardRefRenderFunction<DataEditorRef, DataEditorPr
gridRef={gridRef}
getCellRenderer={getCellRenderer}
resizeIndicator={resizeIndicator}
columnSelectionGridLines={columnSelectionGridLines}
rowSelectionGridLines={rowSelectionGridLines}
/>
{renameGroupNode}
{overlay !== undefined && (
Expand Down
75 changes: 75 additions & 0 deletions packages/core/src/docs/examples/selection-gridlines.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import React from "react";
import { DataEditorAll as DataEditor } from "../../data-editor-all.js";
import {
BeautifulWrapper,
Description,
MoreInfo,
useMockDataGenerator,
defaultProps,
} from "../../data-editor/stories/utils.js";
import { SimpleThemeWrapper } from "../../stories/story-utils.js";

export default {
title: "Glide-Data-Grid/DataEditor Demos",

decorators: [
(Story: React.ComponentType) => (
<SimpleThemeWrapper>
<BeautifulWrapper
title="Column & Row Selection Grid Lines"
description={
<>
<Description>
Demonstrates the <code>columnSelectionGridLines</code> and
<code>rowSelectionGridLines</code> props which control whether accent-coloured grid
lines are drawn around selected columns and rows.
</Description>
<MoreInfo>
Use the story controls to toggle the behaviours on and off.
</MoreInfo>
</>
}>
<Story />
</BeautifulWrapper>
</SimpleThemeWrapper>
),
],
};

interface SelectionGridLineProps {
columnSelectionGridLines: boolean;
rowSelectionGridLines: boolean;
}

export const SelectionGridLine: React.FC<SelectionGridLineProps> = p => {
const { cols, getCellContent } = useMockDataGenerator(10);
return (
<DataEditor
{...defaultProps}
getCellContent={getCellContent}
columns={cols}
rows={1000}
columnSelectionGridLines={p.columnSelectionGridLines}
rowSelectionGridLines={p.rowSelectionGridLines}
rowMarkers="both"
/>
);
};

(SelectionGridLine as any).args = {
columnSelectionGridLines: true,
rowSelectionGridLines: true,
};

(SelectionGridLine as any).argTypes = {
columnSelectionGridLines: {
control: {
type: "boolean",
},
},
rowSelectionGridLines: {
control: {
type: "boolean",
},
},
};
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

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

[nitpick] Trailing whitespace at the end of the file. Consider removing the space after the semicolon.

Suggested change
};
};

Copilot uses AI. Check for mistakes.

2 changes: 2 additions & 0 deletions packages/core/src/internal/data-grid-dnd/data-grid-dnd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ const DataGridDnd: React.FunctionComponent<DataGridDndProps> = p => {
dragAndDropState={dragOffset}
onMouseMoveRaw={onMouseMove}
ref={gridRef}
columnSelectionGridLines={p.columnSelectionGridLines}
rowSelectionGridLines={p.rowSelectionGridLines}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ const DataGridSearch: React.FunctionComponent<DataGridSearchProps> = p => {
smoothScrollX={p.smoothScrollX}
smoothScrollY={p.smoothScrollY}
resizeIndicator={p.resizeIndicator}
columnSelectionGridLines={p.columnSelectionGridLines}
rowSelectionGridLines={p.rowSelectionGridLines}
/>
{searchbox}
</>
Expand Down
49 changes: 37 additions & 12 deletions packages/core/src/internal/data-grid/data-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ export interface DataGridProps {
* @group Style
*/
readonly resizeIndicator: "full" | "header" | "none" | undefined;

/** Enables accent-coloured grid lines for selected columns in the header.
* @defaultValue false
* @group Style
*/
readonly columnSelectionGridLines?: boolean;

/** Enables accent-coloured grid lines for selected rows in the header.
* @defaultValue false
* @group Style
*/
readonly rowSelectionGridLines?: boolean;
}

type DamageUpdateList = readonly {
Expand All @@ -320,7 +332,11 @@ export interface DataGridRef {
focus: () => void;
getBounds: (col?: number, row?: number) => Rectangle | undefined;
damage: (cells: DamageUpdateList) => void;
getMouseArgsForPosition: (posX: number, posY: number, ev?: MouseEvent | TouchEvent) => GridMouseEventArgs | undefined;
getMouseArgsForPosition: (
posX: number,
posY: number,
ev?: MouseEvent | TouchEvent
) => GridMouseEventArgs | undefined;
}

const getRowData = (cell: InnerGridCell, getCellRenderer?: GetCellRendererCallback) => {
Expand Down Expand Up @@ -396,6 +412,8 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
experimental,
getCellRenderer,
resizeIndicator = "full",
columnSelectionGridLines = false,
rowSelectionGridLines = false,
} = p;
const translateX = p.translateX ?? 0;
const translateY = p.translateY ?? 0;
Expand Down Expand Up @@ -446,7 +464,10 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
}, [cellYOffset, cellXOffset, translateX, translateY, enableFirefoxRescaling, enableSafariRescaling]);

const mappedColumns = useMappedColumns(columns, freezeColumns);
const stickyX = React.useMemo(() => fixedShadowX ? getStickyWidth(mappedColumns, dragAndDropState) : 0,[mappedColumns, dragAndDropState, fixedShadowX]);
const stickyX = React.useMemo(
() => (fixedShadowX ? getStickyWidth(mappedColumns, dragAndDropState) : 0),
[mappedColumns, dragAndDropState, fixedShadowX]
);

// row: -1 === columnHeader, -2 === groupHeader
const getBoundsForItem = React.useCallback(
Expand Down Expand Up @@ -829,6 +850,8 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
getCellRenderer,
minimumCellWidth,
resizeIndicator,
columnSelectionGridLines,
rowSelectionGridLines,
};

// This confusing bit of code due to some poor design. Long story short, the damage property is only used
Expand Down Expand Up @@ -895,6 +918,8 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
getCellRenderer,
minimumCellWidth,
resizeIndicator,
columnSelectionGridLines,
rowSelectionGridLines,
]);

const lastDrawRef = React.useRef(draw);
Expand Down Expand Up @@ -951,15 +976,15 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
const cursor = isDragging
? "grabbing"
: canDrag || isResizing
? "col-resize"
: overFill || isFilling
? "crosshair"
: cursorOverride !== undefined
? cursorOverride
: headerHovered || clickableInnerCellHovered || editableBoolHovered || groupHeaderHovered
? "pointer"
: "default";
? "col-resize"
: overFill || isFilling
? "crosshair"
: cursorOverride !== undefined
? cursorOverride
: headerHovered || clickableInnerCellHovered || editableBoolHovered || groupHeaderHovered
? "pointer"
: "default";

const style = React.useMemo(
() => ({
// width,
Expand Down Expand Up @@ -1716,7 +1741,7 @@ const DataGrid: React.ForwardRefRenderFunction<DataGridRef, DataGridProps> = (p,
}

return getMouseArgsForPosition(canvasRef.current, posX, posY, ev);
}
},
}),
[canvasRef, damage, getBoundsForItem]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ export function drawGridLines(
freezeTrailingRows: number,
rows: number,
theme: FullTheme,
verticalOnly: boolean = false
verticalOnly: boolean = false,
selectedColumns?: CompactSelection,
selectedRows?: CompactSelection
) {
if (spans !== undefined) {
ctx.beginPath();
Expand All @@ -307,6 +309,8 @@ export function drawGridLines(
}
const hColor = theme.horizontalBorderColor ?? theme.borderColor;
const vColor = theme.borderColor;
const selectedVColor = theme.accentColor;
const selectedHColor = theme.accentColor;

const { minX, maxX, minY, maxY } = getMinMaxXY(drawRegions, width, height);

Expand All @@ -322,12 +326,20 @@ export function drawGridLines(
x += c.width;
const tx = c.sticky ? x : x + translateX;
if (tx >= minX && tx <= maxX && verticalBorder(index + 1)) {
const leftSelected = selectedColumns?.hasIndex(c.sourceIndex) ?? false;
const rightSelected =
index < effectiveCols.length - 1
? selectedColumns?.hasIndex(effectiveCols[index + 1].sourceIndex) ?? false
: false;
const color = leftSelected !== rightSelected
? selectedVColor
: vColor;
toDraw.push({
x1: tx,
y1: Math.max(groupHeaderHeight, minY),
x2: tx,
y2: Math.min(height, maxY),
color: vColor,
color,
});
}
}
Expand All @@ -348,12 +360,25 @@ export function drawGridLines(
const ty = y + translateY;
if (ty >= minY && ty <= maxY - 1) {
const rowTheme = getRowThemeOverride?.(row);

let color = rowTheme?.horizontalBorderColor ?? rowTheme?.borderColor ?? hColor;

if (selectedRows !== undefined) {
const currentSelected = selectedRows.hasIndex(row);
const prevSelected = row > 0 ? selectedRows.hasIndex(row - 1) : false;

// Accent the line if it is a boundary between selected and unselected rows.
if (currentSelected !== prevSelected && (currentSelected || prevSelected)) {
color = selectedHColor;
}
}

toDraw.push({
x1: minX,
y1: ty,
x2: maxX,
y2: ty,
color: rowTheme?.horizontalBorderColor ?? rowTheme?.borderColor ?? hColor,
color,
});
}

Expand Down
29 changes: 18 additions & 11 deletions packages/core/src/internal/data-grid/render/data-grid-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ export function drawGrid(arg: DrawGridArg, lastArg: DrawGridArg | undefined) {
}
}
const drawHeaderTexture = () => {

// Draw the bottom border of the header.
overlayCtx.beginPath();
overlayCtx.moveTo(0, overlayHeight - 0.5);
overlayCtx.lineTo(width, overlayHeight - 0.5);
overlayCtx.strokeStyle = blend(
theme.headerBottomBorderColor ?? theme.horizontalBorderColor ?? theme.borderColor,
theme.bgHeader
);
overlayCtx.stroke();

drawGridHeaders(
overlayCtx,
effectiveCols,
Expand Down Expand Up @@ -308,17 +319,10 @@ export function drawGrid(arg: DrawGridArg, lastArg: DrawGridArg | undefined) {
freezeTrailingRows,
rows,
theme,
true
);

overlayCtx.beginPath();
overlayCtx.moveTo(0, overlayHeight - 0.5);
overlayCtx.lineTo(width, overlayHeight - 0.5);
overlayCtx.strokeStyle = blend(
theme.headerBottomBorderColor ?? theme.horizontalBorderColor ?? theme.borderColor,
theme.bgHeader
true,
arg.columnSelectionGridLines ? selection.columns : undefined,
arg.rowSelectionGridLines ? selection.rows : undefined
);
overlayCtx.stroke();

if (mustDrawHighlightRingsOnHeader) {
drawHighlightRings(
Expand Down Expand Up @@ -716,7 +720,10 @@ export function drawGrid(arg: DrawGridArg, lastArg: DrawGridArg | undefined) {
verticalBorder,
freezeTrailingRows,
rows,
theme
theme,
false,
arg.columnSelectionGridLines ? selection.columns : undefined,
arg.rowSelectionGridLines ? selection.rows : undefined
);

highlightRedraw?.();
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/internal/data-grid/render/draw-grid-arg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ export interface DrawGridArg {
readonly getCellRenderer: GetCellRendererCallback;
readonly minimumCellWidth: number;
readonly resizeIndicator: "full" | "header" | "none";
readonly columnSelectionGridLines: boolean;
readonly rowSelectionGridLines: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ const GridScroller: React.FunctionComponent<ScrollingDataGridProps> = p => {
smoothScrollX={p.smoothScrollX}
smoothScrollY={p.smoothScrollY}
resizeIndicator={p.resizeIndicator}
columnSelectionGridLines={p.columnSelectionGridLines}
rowSelectionGridLines={p.rowSelectionGridLines}
/>
</InfiniteScroller>
);
Expand Down