Skip to content
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

Enable table column resizing on touch-based devices #2435

Merged
merged 19 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
"type": "patch",
"comment": "Enable table column resizing on touch-based devices",
"packageName": "@ni/nimble-components",
"email": "7282195+m-akinc@users.noreply.github.com",
"dependentChangeType": "patch"
}
12 changes: 6 additions & 6 deletions packages/nimble-components/src/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,11 @@ export class Table<
}

/** @internal */
public onRightDividerMouseDown(
event: MouseEvent,
public onRightDividerPointerDown(
event: PointerEvent,
columnIndex: number
): void {
if (event.button === 0) {
if (event.pointerType !== 'mouse' || event.button === 0) {
this.layoutManager.beginColumnInteractiveSize(
event.clientX,
this.getRightDividerIndex(columnIndex)
Expand All @@ -558,11 +558,11 @@ export class Table<
}

/** @internal */
public onLeftDividerMouseDown(
event: MouseEvent,
public onLeftDividerPointerDown(
event: PointerEvent,
columnIndex: number
): void {
if (event.button === 0) {
if (event.pointerType !== 'mouse' || event.button === 0) {
this.layoutManager.beginColumnInteractiveSize(
event.clientX,
this.getLeftDividerIndex(columnIndex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class TableLayoutManager<TData extends TableRecord> {
this.initialTableScrollableMinWidth = this.table.tableScrollableMinWidth;
this.initialColumnTotalWidth = this.getTotalColumnFixedWidth();
this.isColumnBeingSized = true;
document.addEventListener('mousemove', this.onDividerMouseMove);
document.addEventListener('mouseup', this.onDividerMouseUp);
document.addEventListener('pointermove', this.onDividerPointerMove);
document.addEventListener('pointerup', this.onDividerPointerUp);
}

/**
Expand All @@ -95,13 +95,12 @@ export class TableLayoutManager<TData extends TableRecord> {
return this.getFirstRightResizableColumnIndex(columnIndex) !== -1;
}

private readonly onDividerMouseMove = (event: Event): void => {
const mouseEvent = event as MouseEvent;
private readonly onDividerPointerMove = (event: PointerEvent): void => {
for (let i = 0; i < this.visibleColumns.length; i++) {
this.visibleColumns[i]!.columnInternals.currentPixelWidth = this.initialColumnWidths[i]?.initialPixelWidth;
}
this.currentTotalDelta = this.getAllowedSizeDelta(
mouseEvent.clientX - this.dragStart
event.clientX - this.dragStart
);
this.performCascadeSizeLeft(
this.leftColumnIndex!,
Expand All @@ -120,9 +119,9 @@ export class TableLayoutManager<TData extends TableRecord> {
}
};

private readonly onDividerMouseUp = (): void => {
document.removeEventListener('mousemove', this.onDividerMouseMove);
document.removeEventListener('mouseup', this.onDividerMouseUp);
private readonly onDividerPointerUp = (): void => {
document.removeEventListener('pointermove', this.onDividerPointerMove);
document.removeEventListener('pointerup', this.onDividerPointerUp);
this.resetGridSizedColumns();
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
this.isColumnBeingSized = false;
this.activeColumnIndex = undefined;
Expand Down
1 change: 1 addition & 0 deletions packages/nimble-components/src/table/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export const styles = css`
cursor: col-resize;
position: absolute;
z-index: ${ZIndexLevels.zIndex1};
touch-action: pan-y;
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
}

.column-divider:hover,
Expand Down
4 changes: 2 additions & 2 deletions packages/nimble-components/src/table/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const template = html<Table>`
${(_, c) => `${c.parent.layoutManager.activeColumnDivider === c.parent.getLeftDividerIndex(c.index) ? 'divider-active' : ''}`}
${(_, c) => `${c.parent.layoutManager.hasResizableColumnToLeft(c.index - 1) ? 'draggable' : ''}`}
"
@mousedown="${(_, c) => c.parent.onLeftDividerMouseDown(c.event as MouseEvent, c.index)}">
@pointerdown="${(_, c) => c.parent.onLeftDividerPointerDown(c.event as PointerEvent, c.index)}">
</div>
`)}
<${tableHeaderTag}
Expand All @@ -128,7 +128,7 @@ export const template = html<Table>`
${(_, c) => `${c.parent.layoutManager.activeColumnDivider === c.parent.getRightDividerIndex(c.index) ? 'divider-active' : ''}`}
${(_, c) => `${c.parent.layoutManager.hasResizableColumnToLeft(c.index) ? 'draggable' : ''}`}
"
@mousedown="${(_, c) => c.parent.onRightDividerMouseDown(c.event as MouseEvent, c.index)}">
@pointerdown="${(_, c) => c.parent.onRightDividerPointerDown(c.event as PointerEvent, c.index)}">
</div>
`)}
</div>
Expand Down
64 changes: 27 additions & 37 deletions packages/nimble-components/src/table/testing/table.pageobject.ts
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -541,24 +541,7 @@ export class TablePageObject<T extends TableRecord> {
'The provided column index has no right divider associated with it.'
);
}
const dividerRect = divider.getBoundingClientRect();
let currentMouseX = (dividerRect.x + dividerRect.width) / 2;
const mouseDownEvent = new MouseEvent('mousedown', {
clientX: currentMouseX,
clientY: (dividerRect.y + dividerRect.height) / 2
});
divider.dispatchEvent(mouseDownEvent);

for (const delta of deltas) {
currentMouseX += delta;
const mouseMoveEvent = new MouseEvent('mousemove', {
clientX: currentMouseX
});
document.dispatchEvent(mouseMoveEvent);
}

const mouseUpEvent = new MouseEvent('mouseup');
document.dispatchEvent(mouseUpEvent);
this.dragSizeColumnByDivider(divider, deltas);
}

/**
Expand All @@ -576,24 +559,7 @@ export class TablePageObject<T extends TableRecord> {
'The provided column index has no left divider associated with it.'
);
}
const dividerRect = divider.getBoundingClientRect();
let currentMouseX = (dividerRect.x + dividerRect.width) / 2;
const mouseDownEvent = new MouseEvent('mousedown', {
clientX: currentMouseX,
clientY: (dividerRect.y + dividerRect.height) / 2
});
divider.dispatchEvent(mouseDownEvent);

for (const delta of deltas) {
currentMouseX += delta;
const mouseMoveEvent = new MouseEvent('mousemove', {
clientX: currentMouseX
});
document.dispatchEvent(mouseMoveEvent);
}

const mouseUpEvent = new MouseEvent('mouseup');
document.dispatchEvent(mouseUpEvent);
this.dragSizeColumnByDivider(divider, deltas);
}

public getColumnRightDivider(index: number): HTMLElement | null {
Expand Down Expand Up @@ -855,9 +821,33 @@ export class TablePageObject<T extends TableRecord> {
return spinnerOrIcon;
}

private dragSizeColumnByDivider(
divider: HTMLElement,
deltas: readonly number[]
): void {
const dividerRect = divider.getBoundingClientRect();
let currentPointerX = (dividerRect.x + dividerRect.width) / 2;
const mouseDownEvent = new PointerEvent('pointerdown', {
clientX: currentPointerX,
clientY: (dividerRect.y + dividerRect.height) / 2
});
divider.dispatchEvent(mouseDownEvent);

for (const delta of deltas) {
currentPointerX += delta;
const pointerMoveEvent = new PointerEvent('pointermove', {
clientX: currentPointerX
});
document.dispatchEvent(pointerMoveEvent);
}

const pointerUpEvent = new PointerEvent('pointerup');
document.dispatchEvent(pointerUpEvent);
}

private readonly isSlotElement = (
element: Node | undefined
): element is HTMLSlotElement => {
return element?.nodeName === 'SLOT' ?? false;
return element?.nodeName === 'SLOT';
};
}
Loading