-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Sticky columns utils (#1400)
- Loading branch information
Showing
7 changed files
with
116 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { | ||
useStickyColumns, | ||
useStickyCellStyles, | ||
StickyColumnsModel, | ||
StickyColumnsCellState, | ||
} from './use-sticky-columns'; | ||
export { StickyColumnsCellState } from './interfaces'; | ||
export { useStickyColumns, useStickyCellStyles, StickyColumnsModel } from './use-sticky-columns'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export interface StickyColumnsProps { | ||
visibleColumns: readonly PropertyKey[]; | ||
stickyColumnsFirst: number; | ||
stickyColumnsLast: number; | ||
} | ||
|
||
export interface StickyColumnsState { | ||
cellState: Record<PropertyKey, null | StickyColumnsCellState>; | ||
wrapperState: StickyColumnsWrapperState; | ||
} | ||
|
||
// Cell state is used to apply respective styles and offsets to sticky cells. | ||
export interface StickyColumnsCellState { | ||
padLeft: boolean; | ||
lastLeft: boolean; | ||
lastRight: boolean; | ||
offset: { left?: number; right?: number }; | ||
} | ||
|
||
// Scroll padding is applied to table's wrapper so that the table scrolls when focus goes behind sticky column. | ||
export interface StickyColumnsWrapperState { | ||
scrollPaddingLeft: number; | ||
scrollPaddingRight: number; | ||
} | ||
|
||
export interface CellOffsets { | ||
offsets: Map<PropertyKey, { first: number; last: number }>; | ||
stickyWidthLeft: number; | ||
stickyWidthRight: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { CellOffsets, StickyColumnsCellState, StickyColumnsProps, StickyColumnsWrapperState } from './interfaces'; | ||
|
||
export function isCellStatesEqual(s1: null | StickyColumnsCellState, s2: null | StickyColumnsCellState): boolean { | ||
if (s1 && s2) { | ||
return ( | ||
s1.padLeft === s2.padLeft && | ||
s1.lastLeft === s2.lastLeft && | ||
s1.lastRight === s2.lastRight && | ||
s1.offset.left === s2.offset.left && | ||
s1.offset.right === s2.offset.right | ||
); | ||
} | ||
return s1 === s2; | ||
} | ||
|
||
export function isWrapperStatesEqual(s1: StickyColumnsWrapperState, s2: StickyColumnsWrapperState): boolean { | ||
return s1.scrollPaddingLeft === s2.scrollPaddingLeft && s1.scrollPaddingRight === s2.scrollPaddingRight; | ||
} | ||
|
||
export function updateCellOffsets(cells: Record<PropertyKey, HTMLElement>, props: StickyColumnsProps): CellOffsets { | ||
const totalColumns = props.visibleColumns.length; | ||
|
||
const firstColumnsWidths: number[] = []; | ||
for (let i = 0; i < Math.min(totalColumns, props.stickyColumnsFirst); i++) { | ||
const element = cells[props.visibleColumns[i]]; | ||
const cellWidth = element.getBoundingClientRect().width ?? 0; | ||
firstColumnsWidths[i] = (firstColumnsWidths[i - 1] ?? 0) + cellWidth; | ||
} | ||
|
||
const lastColumnsWidths: number[] = []; | ||
for (let i = 0; i < Math.min(totalColumns, props.stickyColumnsLast); i++) { | ||
const element = cells[props.visibleColumns[totalColumns - 1 - i]]; | ||
const cellWidth = element.getBoundingClientRect().width ?? 0; | ||
lastColumnsWidths[i] = (lastColumnsWidths[i - 1] ?? 0) + cellWidth; | ||
} | ||
|
||
const stickyWidthLeft = firstColumnsWidths[props.stickyColumnsFirst - 1] ?? 0; | ||
const stickyWidthRight = lastColumnsWidths[props.stickyColumnsLast - 1] ?? 0; | ||
const offsets = props.visibleColumns.reduce( | ||
(map, columnId, columnIndex) => | ||
map.set(columnId, { | ||
first: firstColumnsWidths[columnIndex - 1] ?? 0, | ||
last: lastColumnsWidths[totalColumns - 1 - columnIndex - 1] ?? 0, | ||
}), | ||
new Map() | ||
); | ||
|
||
return { offsets, stickyWidthLeft, stickyWidthRight }; | ||
} |