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 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
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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add DOM.Iterable to the build",
"packageName": "@ni/spright-components",
"email": "7282195+m-akinc@users.noreply.github.com",
"dependentChangeType": "patch"
}
16 changes: 10 additions & 6 deletions packages/nimble-components/src/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,25 +545,29 @@ 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.target as HTMLElement,
event.pointerId,
event.clientX,
this.getRightDividerIndex(columnIndex)
);
}
}

/** @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.target as HTMLElement,
event.pointerId,
event.clientX,
this.getLeftDividerIndex(columnIndex)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class TableLayoutManager<TData extends TableRecord> {
private initialTableScrollableWidth?: number;
private initialTableScrollableMinWidth?: number;
private initialColumnTotalWidth?: number;
private activeColumnDividerElement?: HTMLElement;
private currentTotalDelta = 0;
private dragStart = 0;
private leftColumnIndex?: number;
Expand Down Expand Up @@ -56,14 +57,19 @@ export class TableLayoutManager<TData extends TableRecord> {

/**
* Sets up state related to interactively sizing a column.
* @param activeColumnDividerElement The divider element that was clicked on
* @param pointerId The pointerId of the pointer that started the drag
* @param dragStart The x-position from which a column size was started
* @param activeColumnDivider The divider that was clicked on
* @param activeColumnDivider The 1-based index of the divider that was clicked on
*/
public beginColumnInteractiveSize(
activeColumnDividerElement: HTMLElement,
pointerId: number,
dragStart: number,
activeColumnDivider: number
): void {
this.activeColumnDivider = activeColumnDivider;
this.activeColumnDividerElement = activeColumnDividerElement;
this.leftColumnIndex = this.getLeftColumnIndexFromDivider(
this.activeColumnDivider
);
Expand All @@ -77,8 +83,18 @@ 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);
// pointerId of -1 indicates source was synthetic PointerEvent: https://w3c.github.io/pointerevents/#dom-pointerevent-pointerid
if (pointerId !== -1) {
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
activeColumnDividerElement.setPointerCapture(pointerId);
}
activeColumnDividerElement.addEventListener(
'pointermove',
this.onDividerPointerMove
);
activeColumnDividerElement.addEventListener(
'pointerup',
this.onDividerPointerUp
);
}

/**
Expand All @@ -95,13 +111,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,13 +135,20 @@ export class TableLayoutManager<TData extends TableRecord> {
}
};

private readonly onDividerMouseUp = (): void => {
document.removeEventListener('mousemove', this.onDividerMouseMove);
document.removeEventListener('mouseup', this.onDividerMouseUp);
private readonly onDividerPointerUp = (): void => {
this.activeColumnDividerElement!.removeEventListener(
'pointermove',
this.onDividerPointerMove
);
this.activeColumnDividerElement!.removeEventListener(
'pointerup',
this.onDividerPointerUp
);
this.resetGridSizedColumns();
m-akinc marked this conversation as resolved.
Show resolved Hide resolved
this.isColumnBeingSized = false;
this.activeColumnIndex = undefined;
this.activeColumnDivider = undefined;
this.activeColumnDividerElement = undefined;
this.visibleColumns = [];
};

Expand Down
8 changes: 1 addition & 7 deletions packages/nimble-components/src/table/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ export const styles = css`
cursor: var(--ni-private-table-cursor-override);
}

.glass-overlay {
width: 100%;
height: 100%;
display: contents;
pointer-events: var(--ni-private-glass-overlay-pointer-events);
}

.header-row-container {
position: sticky;
top: 0;
Expand Down Expand Up @@ -150,6 +143,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
Loading