Skip to content
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
5 changes: 3 additions & 2 deletions src/internal/components/dropdown/context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useContext } from 'react';
import React, { useContext, useMemo } from 'react';

type Position = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';

Expand All @@ -14,7 +14,8 @@ export interface DropdownContextProviderProps {
}

export function DropdownContextProvider({ children, position = 'bottom-right' }: DropdownContextProviderProps) {
return <DropdownContext.Provider value={{ position }}>{children}</DropdownContext.Provider>;
const value = useMemo(() => ({ position }), [position]);
return <DropdownContext.Provider value={value}>{children}</DropdownContext.Provider>;
}

export function useDropdownContext() {
Expand Down
114 changes: 75 additions & 39 deletions src/table/use-column-widths.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { createContext, useContext, useEffect, useRef, useState } from 'react';
import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';

import { useResizeObserver, useStableCallback } from '@cloudscape-design/component-toolkit/internal';
import { getLogicalBoundingClientRect } from '@cloudscape-design/component-toolkit/internal';
Expand Down Expand Up @@ -35,12 +35,12 @@
}

function updateWidths(
visibleColumns: readonly ColumnWidthDefinition[],
columnById: Map<PropertyKey, ColumnWidthDefinition>,
oldWidths: Map<PropertyKey, number>,
newWidth: number,
columnId: PropertyKey
) {
const column = visibleColumns.find(column => column.id === columnId);
const column = columnById.get(columnId);
let minWidth = DEFAULT_COLUMN_WIDTH;
if (typeof column?.width === 'number' && column.width < DEFAULT_COLUMN_WIDTH) {
minWidth = column?.width;
Expand Down Expand Up @@ -83,61 +83,84 @@
const containerWidthRef = useRef(0);
const [columnWidths, setColumnWidths] = useState<null | Map<PropertyKey, number>>(null);

// Pre-build a Map for column lookups
const columnById = useMemo(

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / build (React 18) / build

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / build / build

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 1/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (6/6)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components unit tests

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 18, shard 2/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (2/6)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Build components

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 18, shard 1/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (3/6)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components motion tests (React 16)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 3/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components motion tests (React 18)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 4/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (5/6)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (4/6)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Build components (React 18)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 2/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`

Check failure on line 87 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 18, shard 4/4)

Replace `⏎····()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),⏎····[visibleColumns]⏎··` with `()·=>·new·Map(visibleColumns.map(column·=>·[column.id,·column])),·[visibleColumns]`
() => new Map(visibleColumns.map(column => [column.id, column])),
[visibleColumns]
);

const cellsRef = useRef(new Map<PropertyKey, HTMLElement>());
const stickyCellsRef = useRef(new Map<PropertyKey, HTMLElement>());
// Cache measured widths from real cells to avoid getBoundingClientRect() during render
const measuredWidthsRef = useRef(new Map<PropertyKey, number>());
const getCell = (columnId: PropertyKey): null | HTMLElement => cellsRef.current.get(columnId) ?? null;
const setCell = (sticky: boolean, columnId: PropertyKey, node: null | HTMLElement) => {
const setCell = useCallback((sticky: boolean, columnId: PropertyKey, node: null | HTMLElement) => {
const ref = sticky ? stickyCellsRef : cellsRef;
if (node) {
ref.current.set(columnId, node);
} else {
ref.current.delete(columnId);
}
};
}, []);

const getColumnStyles = (sticky: boolean, columnId: PropertyKey): ColumnWidthStyle => {
const column = visibleColumns.find(column => column.id === columnId);
if (!column) {
return {};
}
const getColumnStyles = useCallback(
(sticky: boolean, columnId: PropertyKey): ColumnWidthStyle => {
const column = columnById.get(columnId);
if (!column) {
return {};
}

if (sticky) {
return {
width:
cellsRef.current.get(column.id)?.getBoundingClientRect().width ||
(columnWidths?.get(column.id) ?? column.width),
};
}
if (sticky) {
// Use cached measured width to avoid getBoundingClientRect() during render.
Copy link
Member

Choose a reason for hiding this comment

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

What is the cost of calling getBoundingClientRect() during render exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Calling getBoundingClientRect() forces the browser to recompute layout. Calling it multiple times in a row is fine, since (if nothing in the DOM has changed) the layout computations are cached. But calling it in a React render is unpredictable: React may or may not batch different component renders together, so it's possible for the DOM state to change in between different getBoundingClientRect() calls, causing layout thrashing. It's also considered generally bad practice to perform expensive operations that block React updates.

The key change here is that all of the getBoundingClientRect() calls for the table are batched together to minimize potential overhead, and performed outside of render so they don't block React.

// The cache is populated by updateColumnWidths() which runs outside the render path.
return {
width: measuredWidthsRef.current.get(column.id) || columnWidths?.get(column.id) || column.width,
};
}

if (resizableColumns && columnWidths) {
const isLastColumn = column.id === visibleColumns[visibleColumns.length - 1]?.id;
const totalWidth = visibleColumns.reduce(
(sum, { id }) => sum + (columnWidths.get(id) || DEFAULT_COLUMN_WIDTH),
0
);
if (isLastColumn && containerWidthRef.current > totalWidth) {
return { width: 'auto', minWidth: column?.minWidth };
} else {
return { width: columnWidths.get(column.id), minWidth: column?.minWidth };
if (resizableColumns && columnWidths) {
const isLastColumn = column.id === visibleColumns[visibleColumns.length - 1]?.id;
const totalWidth = visibleColumns.reduce(
(sum, { id }) => sum + (columnWidths.get(id) || DEFAULT_COLUMN_WIDTH),
0
);
if (isLastColumn && containerWidthRef.current > totalWidth) {
return { width: 'auto', minWidth: column?.minWidth };
} else {
return { width: columnWidths.get(column.id), minWidth: column?.minWidth };
}
}
}
return {
width: column.width,
minWidth: column.minWidth,
maxWidth: !resizableColumns ? column.maxWidth : undefined,
};
};
return {
width: column.width,
minWidth: column.minWidth,
maxWidth: !resizableColumns ? column.maxWidth : undefined,
};
},
[columnById, columnWidths, resizableColumns, visibleColumns]
);

// Imperatively sets width style for a cell avoiding React state.
// This allows setting the style as soon container's size change is observed.
const updateColumnWidths = useStableCallback(() => {
// First pass: set widths on real cells
for (const { id } of visibleColumns) {
const element = cellsRef.current.get(id);
if (element) {
setElementWidths(element, getColumnStyles(false, id));
}
}
// Sticky column widths must be synchronized once all real column widths are assigned.
// Second pass: measure real cell widths and cache them for sticky columns.
// This avoids calling getBoundingClientRect() during render.
for (const { id } of visibleColumns) {
const element = cellsRef.current.get(id);
if (element) {
const width = element.getBoundingClientRect().width;
if (width > 0) {
measuredWidthsRef.current.set(id, width);
}
}
}
// Third pass: set sticky column widths using cached measurements
for (const { id } of visibleColumns) {
const element = stickyCellsRef.current.get(id);
if (element) {
Expand Down Expand Up @@ -189,12 +212,25 @@
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

function updateColumn(columnId: PropertyKey, newWidth: number) {
setColumnWidths(columnWidths => updateWidths(visibleColumns, columnWidths ?? new Map(), newWidth, columnId));
}
const updateColumn = useCallback(
(columnId: PropertyKey, newWidth: number) => {
setColumnWidths(columnWidths => updateWidths(columnById, columnWidths ?? new Map(), newWidth, columnId));
},
[columnById]
);

const contextValue = useMemo(
() => ({
getColumnStyles,
columnWidths: columnWidths ?? new Map<PropertyKey, number>(),
updateColumn,
setCell,
}),
[getColumnStyles, columnWidths, updateColumn, setCell]
);

return (

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / build (React 18) / build

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / build / build

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 1/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (6/6)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components unit tests

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 18, shard 2/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (2/6)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Build components

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 18, shard 1/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (3/6)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components motion tests (React 16)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 3/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components motion tests (React 18)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 4/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (5/6)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components accessibility tests shards (4/6)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Build components (React 18)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 16, shard 2/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`

Check failure on line 232 in src/table/use-column-widths.tsx

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (React 18, shard 4/4)

Replace `(⏎····<WidthsContext.Provider·value={contextValue}>⏎······{children}⏎····</WidthsContext.Provider>⏎··)` with `<WidthsContext.Provider·value={contextValue}>{children}</WidthsContext.Provider>`
<WidthsContext.Provider value={{ getColumnStyles, columnWidths: columnWidths ?? new Map(), updateColumn, setCell }}>
<WidthsContext.Provider value={contextValue}>
{children}
</WidthsContext.Provider>
);
Expand Down
5 changes: 3 additions & 2 deletions src/top-navigation/parts/overflow-menu/router.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { createContext, Dispatch, SetStateAction, useContext, useState } from 'react';
import React, { createContext, Dispatch, SetStateAction, useContext, useMemo, useState } from 'react';

type View = 'utilities' | 'dropdown-menu';

Expand Down Expand Up @@ -60,7 +60,8 @@ interface RouterProps {

const Router = ({ children }: RouterProps) => {
const [state, setState] = useState<RouteState>({ view: 'utilities', data: null });
return <ViewContext.Provider value={{ state, setState }}>{children}</ViewContext.Provider>;
const value = useMemo(() => ({ state, setState }), [state]);
return <ViewContext.Provider value={value}>{children}</ViewContext.Provider>;
};

export default Router;
Loading