Skip to content

Set drag column image #3783

New issue

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

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

Already on GitHub? # to your account

Merged
merged 5 commits into from
May 16, 2025
Merged
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
78 changes: 47 additions & 31 deletions src/HeaderCell.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from 'react';
import { useId, useRef, useState } from 'react';
import { css } from '@linaria/core';

import { useRovingTabIndex } from './hooks';
Expand Down Expand Up @@ -43,17 +43,29 @@ export const resizeHandleClassname = css`
const cellDraggableClassname = 'rdg-cell-draggable';

const cellDragging = css`
opacity: 0.5;
@layer rdg.HeaderCell {
background-color: var(--rdg-header-draggable-background-color);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Kept the same style as cell over for now. We cannot use opacity as we are using a custom drag image

}
`;

const cellDraggingClassname = `rdg-cell-dragging ${cellDragging}`;

const cellOver = css`
background-color: var(--rdg-header-draggable-background-color);
@layer rdg.HeaderCell {
background-color: var(--rdg-header-draggable-background-color);
}
`;

const cellOverClassname = `rdg-cell-drag-over ${cellOver}`;

const dragImageClassname = css`
@layer rdg.HeaderCell {
border-radius: 4px;
width: fit-content;
outline: 2px solid hsl(207, 100%, 50%);
}
`;

type SharedHeaderRowProps<R, SR> = Pick<
HeaderRowProps<R, SR, React.Key>,
| 'sortColumns'
Expand All @@ -70,7 +82,8 @@ export interface HeaderCellProps<R, SR> extends SharedHeaderRowProps<R, SR> {
colSpan: number | undefined;
rowIdx: number;
isCellSelected: boolean;
dragDropKey: string;
draggedColumnKey: string | undefined;
setDraggedColumnKey: (draggedColumnKey: string | undefined) => void;
}

export default function HeaderCell<R, SR>({
Expand All @@ -85,10 +98,11 @@ export default function HeaderCell<R, SR>({
onSortColumnsChange,
selectCell,
direction,
dragDropKey
draggedColumnKey,
setDraggedColumnKey
}: HeaderCellProps<R, SR>) {
const [isDragging, setIsDragging] = useState(false);
const [isOver, setIsOver] = useState(false);
const isDragging = draggedColumnKey === column.key;
const rowSpan = getHeaderCellRowSpan(column, rowIdx);
const { tabIndex, childTabIndex, onFocus } = useRovingTabIndex(isCellSelected);
const sortIndex = sortColumns?.findIndex((sort) => sort.columnKey === column.key);
Expand All @@ -99,6 +113,7 @@ export default function HeaderCell<R, SR>({
const ariaSort =
sortDirection && !priority ? (sortDirection === 'ASC' ? 'ascending' : 'descending') : undefined;
const { sortable, resizable, draggable } = column;
const dragImageId = useId();

const className = getCellClassname(column, column.headerCellClass, {
[cellSortableClassname]: sortable,
Expand Down Expand Up @@ -180,13 +195,18 @@ export default function HeaderCell<R, SR>({
}

function onDragStart(event: React.DragEvent<HTMLDivElement>) {
event.dataTransfer.setData(dragDropKey, column.key);
const dragImage = event.currentTarget.cloneNode(true) as HTMLDivElement;
dragImage.classList.add(dragImageClassname);
dragImage.id = dragImageId;
event.currentTarget.parentElement!.insertBefore(dragImage, event.currentTarget);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This way the element is behind the column

event.dataTransfer.setDragImage(dragImage, 0, 0);
event.dataTransfer.dropEffect = 'move';
setIsDragging(true);
setDraggedColumnKey(column.key);
}

function onDragEnd() {
setIsDragging(false);
setDraggedColumnKey(undefined);
document.getElementById(dragImageId)?.remove();
}

function onDragOver(event: React.DragEvent<HTMLDivElement>) {
Expand All @@ -197,18 +217,9 @@ export default function HeaderCell<R, SR>({

function onDrop(event: React.DragEvent<HTMLDivElement>) {
setIsOver(false);
// The dragDropKey is derived from the useId() hook, which can sometimes generate keys with uppercase letters.
// When setting data using event.dataTransfer.setData(), the key is automatically converted to lowercase in some browsers.
// To ensure consistent comparison, we normalize the dragDropKey to lowercase before checking its presence in the event's dataTransfer types.
// https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface
if (event.dataTransfer.types.includes(dragDropKey.toLowerCase())) {
const sourceKey = event.dataTransfer.getData(dragDropKey.toLowerCase());
if (sourceKey !== column.key) {
// prevent the browser from redirecting in some cases
event.preventDefault();
onColumnsReorder?.(sourceKey, column.key);
}
}
// prevent the browser from redirecting in some cases
event.preventDefault();
onColumnsReorder?.(draggedColumnKey!, column.key);
}

function onDragEnter(event: React.DragEvent<HTMLDivElement>) {
Expand All @@ -223,19 +234,23 @@ export default function HeaderCell<R, SR>({
}
}

let draggableProps: React.ComponentProps<'div'> | undefined;
let dragTargetProps: React.ComponentProps<'div'> | undefined;
let dropTargetProps: React.ComponentProps<'div'> | undefined;
if (draggable) {
draggableProps = {
dragTargetProps = {
draggable: true,
/* events fired on the draggable target */
onDragStart,
onDragEnd,
/* events fired on the drop targets */
onDragOver,
onDragEnter,
onDragLeave,
onDrop
onDragEnd
};

if (draggedColumnKey !== undefined && draggedColumnKey !== column.key) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now we won't fire these events if external item is dragged over

dropTargetProps = {
onDragOver,
onDragEnter,
onDragLeave,
onDrop
};
}
}

return (
Expand All @@ -256,7 +271,8 @@ export default function HeaderCell<R, SR>({
onFocus={onFocus}
onClick={onClick}
onKeyDown={onKeyDown}
{...draggableProps}
{...dragTargetProps}
{...dropTargetProps}
>
{column.renderHeaderCell({
column,
Expand Down
7 changes: 4 additions & 3 deletions src/HeaderRow.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memo, useId } from 'react';
import { memo, useState } from 'react';
import { css } from '@linaria/core';
import clsx from 'clsx';

Expand Down Expand Up @@ -60,7 +60,7 @@ function HeaderRow<R, SR, K extends React.Key>({
selectCell,
direction
}: HeaderRowProps<R, SR, K>) {
const dragDropKey = useId();
const [draggedColumnKey, setDraggedColumnKey] = useState<string>();
Copy link
Contributor Author

@amanmahajan7 amanmahajan7 May 15, 2025

Choose a reason for hiding this comment

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

We can move this to the parent component and reorder columns internally as user is dragging. I will check


const cells = [];
for (let index = 0; index < columns.length; index++) {
Expand All @@ -84,7 +84,8 @@ function HeaderRow<R, SR, K extends React.Key>({
sortColumns={sortColumns}
selectCell={selectCell}
direction={direction}
dragDropKey={dragDropKey}
draggedColumnKey={draggedColumnKey}
setDraggedColumnKey={setDraggedColumnKey}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/style/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const root = css`

@layer rdg.Root {
${lightTheme}
--rdg-selection-color: #66afe9;
--rdg-selection-color: hsl(207, 75%, 66%);
--rdg-font-size: 14px;
--rdg-cell-frozen-box-shadow: 2px 0 5px -2px rgba(136, 136, 136, 0.3);

Expand Down
33 changes: 21 additions & 12 deletions website/routes/ColumnsReordering.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,27 @@ function ColumnsReordering() {
}, [rows, sortColumns]);

function onColumnsReorder(sourceKey: string, targetKey: string) {
setColumnsOrder((columnsOrder) => {
const sourceColumnOrderIndex = columnsOrder.findIndex(
(index) => columns[index].key === sourceKey
);
const targetColumnOrderIndex = columnsOrder.findIndex(
(index) => columns[index].key === targetKey
);
const sourceColumnOrder = columnsOrder[sourceColumnOrderIndex];
const newColumnsOrder = columnsOrder.toSpliced(sourceColumnOrderIndex, 1);
newColumnsOrder.splice(targetColumnOrderIndex, 0, sourceColumnOrder);
return newColumnsOrder;
});
function reorderColumns() {
setColumnsOrder((columnsOrder) => {
const sourceColumnOrderIndex = columnsOrder.findIndex(
(index) => columns[index].key === sourceKey
);
const targetColumnOrderIndex = columnsOrder.findIndex(
(index) => columns[index].key === targetKey
);
const sourceColumnOrder = columnsOrder[sourceColumnOrderIndex];
const newColumnsOrder = columnsOrder.toSpliced(sourceColumnOrderIndex, 1);
newColumnsOrder.splice(targetColumnOrderIndex, 0, sourceColumnOrder);
return newColumnsOrder;
});
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (document.startViewTransition) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FF does not support this

document.startViewTransition(reorderColumns);
} else {
reorderColumns();
}
}

function resetOrderAndWidths() {
Expand Down