From bdfd7436e27bc4dfb2b4827c04fd094a6de53187 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 21 Oct 2026 22:18:59 -0400 Subject: [PATCH] chore: Upgrade react-window to 2 (#14196) --- .../DocumentExplorer/DocumentExplorer.tsx | 110 ++++++------------ .../IconPicker/components/EmojiPanel.tsx | 5 +- app/components/IconPicker/components/Grid.tsx | 61 +++++----- .../IconPicker/components/GridTemplate.tsx | 5 +- .../IconPicker/components/IconPanel.tsx | 5 +- package.json | 4 +- yarn.lock | 47 ++------ 7 files changed, 80 insertions(+), 157 deletions(-) diff --git a/app/components/DocumentExplorer/DocumentExplorer.tsx b/app/components/DocumentExplorer/DocumentExplorer.tsx index 6ba53ec952..44d3cbc0c8 100644 --- a/app/components/DocumentExplorer/DocumentExplorer.tsx +++ b/app/components/DocumentExplorer/DocumentExplorer.tsx @@ -3,7 +3,6 @@ import { concat, difference, fill, - filter, flatten, includes, map, @@ -13,8 +12,8 @@ import { observer } from "mobx-react"; import { StarredIcon, DocumentIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; -import AutoSizer from "react-virtualized-auto-sizer"; -import { FixedSizeList as List } from "react-window"; +import type { ListImperativeAPI, RowComponentProps } from "react-window"; +import { List } from "react-window"; import styled, { useTheme } from "styled-components"; import { breakpoint } from "@shared/styles/breakpoint"; import Icon from "@shared/components/Icon"; @@ -51,25 +50,12 @@ const expandableDescendantIds = (node: NavigationNode) => .filter((des) => des.children.length > 0) .map((des) => des.id); -function innerElementType({ - style, - ref, - ...rest -}: React.HTMLAttributes & { - ref?: React.Ref; -}) { - return ( -
- ); +interface RowProps { + data: NavigationNode[]; } +const rowKey = (index: number, { data }: RowProps) => data[index].id; + function DocumentExplorer({ onSubmit, onSelect, @@ -121,7 +107,7 @@ function DocumentExplorer({ const inputSearchRef = React.useRef( null ); - const listRef = React.useRef>(null); + const listRef = React.useRef(null); const searchIndex = React.useMemo( () => @@ -171,7 +157,10 @@ function DocumentExplorer({ if (defaultValue && selectedNode && listRef) { const index = nodes.findIndex((node) => node.id === selectedNode.id); if (index > 0) { - setTimeout(() => listRef.current?.scrollToItem(index, "center"), 50); + setTimeout( + () => listRef.current?.scrollToRow({ index, align: "center" }), + 50 + ); } } // oxlint-disable-next-line react-hooks/exhaustive-deps @@ -184,7 +173,7 @@ function DocumentExplorer({ (baseDepth === Infinity ? 0 : baseDepth) + (showDocuments ? 0 : 1); const scrollNodeIntoView = React.useCallback((node: number) => { - listRef.current?.scrollToItem(node, "smart"); + listRef.current?.scrollToRow({ index: node, align: "smart" }); }, []); const handleSearch = (ev: React.ChangeEvent) => { @@ -193,18 +182,6 @@ function DocumentExplorer({ const isExpanded = (node: number) => includes(expandedNodes, nodes[node].id); - const preserveScrollOffset = (itemCount: number) => { - if (listRef.current) { - const { height, itemSize } = listRef.current.props; - const { scrollOffset } = listRef.current.state as { - scrollOffset: number; - }; - const itemsHeight = itemCount * itemSize; - const offset = itemsHeight < Number(height) ? 0 : scrollOffset; - setTimeout(() => listRef.current?.scrollTo(offset), 0); - } - }; - const collapse = (node: number) => { const descendantIds = descendants(nodes[node]).map((des) => des.id); const ancestorIds = ancestors(nodes[node]).map((anc) => anc.id); @@ -218,10 +195,6 @@ function DocumentExplorer({ nodes[node].id, ]) ); - - // remove children - const newNodes = filter(nodes, (n) => !includes(descendantIds, n.id)); - preserveScrollOffset(newNodes.length); }; const expand = (node: number, deep = false) => { @@ -236,11 +209,6 @@ function DocumentExplorer({ } else { setExpandedNodes(concat(expandedNodes, nodes[node].id)); } - - // add children - const newNodes = nodes.slice(); - newNodes.splice(node + 1, 0, ...descendants(nodes[node], deep ? 0 : 1)); - preserveScrollOffset(newNodes.length); }; // Expand descendants of alt-clicked nodes that load after the click. @@ -309,15 +277,7 @@ function DocumentExplorer({ }; const ListItem = observer( - ({ - index, - data, - style, - }: { - index: number; - data: NavigationNode[]; - style: React.CSSProperties; - }) => { + ({ index, data, style }: RowComponentProps) => { const node = data[index]; const isCollection = node.type === "collection"; let renderedIcon, @@ -359,9 +319,9 @@ function DocumentExplorer({ active={activeNode === index} style={{ ...style, - top: (style.top as number) + VERTICAL_PADDING, - left: (style.left as number) + HORIZONTAL_PADDING, - width: `calc(${style.width} - ${HORIZONTAL_PADDING * 2}px)`, + top: VERTICAL_PADDING, + left: HORIZONTAL_PADDING, + width: `calc(100% - ${HORIZONTAL_PADDING * 2}px)`, }} onPointerMove={() => setActiveNode(index)} onClick={() => selectNode(nodes[index])} @@ -374,9 +334,9 @@ function DocumentExplorer({ setActiveNode(index)} onClick={() => selectNode(nodes[index])} @@ -465,24 +425,15 @@ function DocumentExplorer({ /> {nodes.length ? ( - - {({ width, height }: { width: number; height: number }) => ( - - results[index].id} - > - {ListItem} - - - )} - + ) : ( @@ -509,6 +460,11 @@ const ListSearch = styled(InputSearch).attrs({ round: true })` padding-right: 24px; `; +// Rows are offset by the top padding, so the bottom needs room for both. +const StyledList = styled(List)` + padding-bottom: ${VERTICAL_PADDING * 2}px; +`; + const ListContainer = styled.div` height: 65vh; diff --git a/app/components/IconPicker/components/EmojiPanel.tsx b/app/components/IconPicker/components/EmojiPanel.tsx index 800d826db5..f66ec2f19f 100644 --- a/app/components/IconPicker/components/EmojiPanel.tsx +++ b/app/components/IconPicker/components/EmojiPanel.tsx @@ -1,6 +1,7 @@ import { concat } from "es-toolkit/compat"; import { PlusIcon } from "outline-icons"; import * as React from "react"; +import type { ListImperativeAPI } from "react-window"; import { useTranslation } from "react-i18next"; import type { EmojiSkinTone } from "@shared/types"; import { EmojiCategory, IconType } from "@shared/types"; @@ -48,7 +49,7 @@ const EmojiPanel = ({ const team = useCurrentTeam(); const can = usePolicy(team); const searchRef = React.useRef(null); - const scrollableRef = React.useRef(null); + const scrollableRef = React.useRef(null); const customEmojis = useComputed( () => emojis.orderedData.map(toIcon), [emojis.orderedData] @@ -162,7 +163,7 @@ const EmojiPanel = ({ if (!panelActive) { return; } - scrollableRef.current?.scroll({ top: 0 }); + scrollableRef.current?.element?.scroll({ top: 0 }); requestAnimationFrame(() => searchRef.current?.focus()); }, [panelActive]); diff --git a/app/components/IconPicker/components/Grid.tsx b/app/components/IconPicker/components/Grid.tsx index b82ac48bfc..3f8f8d8763 100644 --- a/app/components/IconPicker/components/Grid.tsx +++ b/app/components/IconPicker/components/Grid.tsx @@ -1,6 +1,6 @@ import React from "react"; -import type { ListChildComponentProps } from "react-window"; -import { FixedSizeList } from "react-window"; +import type { ListImperativeAPI, RowComponentProps } from "react-window"; +import { List } from "react-window"; import styled from "styled-components"; type Props = { @@ -10,7 +10,7 @@ type Props = { columns: number; itemWidth: number; onOverflowChange?: (hasMoreBelow: boolean) => void; - ref?: React.Ref; + ref?: React.Ref; }; function Grid({ @@ -33,25 +33,26 @@ function Grid({ React.useEffect(notify, [notify]); const handleScroll = React.useCallback( - ({ scrollOffset }: { scrollOffset: number }) => { - offsetRef.current = scrollOffset; + (event: React.UIEvent) => { + offsetRef.current = event.currentTarget.scrollTop; notify(); }, [notify] ); + const style = React.useMemo(() => ({ width, height }), [width, height]); + const rowProps = React.useMemo(() => ({ data, columns }), [data, columns]); + return ( - {Row} - + /> ); } @@ -60,32 +61,28 @@ type RowProps = { columns: number; }; -const Row = ({ index, style, data }: ListChildComponentProps) => { - const { data: rows, columns } = data; - const row = rows[index]; +const Row = ({ + index, + style, + ariaAttributes, + data, + columns, +}: RowComponentProps) => ( + + {data[index]} + +); - return ( - - {row} - - ); -}; - -const Container = styled(FixedSizeList)` - padding: 0px 12px; +const Container = styled(List)` overflow-x: hidden !important; - - // Needed for the absolutely positioned children - // to respect the VirtualList's padding - & > div { - position: relative; - } `; +// Rows are absolutely positioned, so the horizontal padding lives here. const RowContainer = styled.div<{ columns: number }>` display: grid; grid-template-columns: ${({ columns }) => `repeat(${columns}, 1fr)`}; align-content: center; + padding: 0 12px; `; export default Grid; diff --git a/app/components/IconPicker/components/GridTemplate.tsx b/app/components/IconPicker/components/GridTemplate.tsx index 486e41ca05..4b22274756 100644 --- a/app/components/IconPicker/components/GridTemplate.tsx +++ b/app/components/IconPicker/components/GridTemplate.tsx @@ -1,5 +1,6 @@ import { chunk, compact } from "es-toolkit/compat"; import * as React from "react"; +import type { ListImperativeAPI } from "react-window"; import styled from "styled-components"; import { IconType } from "@shared/types"; import { IconLibrary } from "@shared/utils/IconLibrary"; @@ -60,8 +61,8 @@ type Props = { onIconActive?: (icon: IconNode) => void; /** Callback when whether the grid has content below the fold changes */ onOverflowChange?: (hasMoreBelow: boolean) => void; - /** Ref to the scrolling container */ - ref?: React.Ref; + /** Ref to the virtualized list */ + ref?: React.Ref; }; function GridTemplate({ diff --git a/app/components/IconPicker/components/IconPanel.tsx b/app/components/IconPicker/components/IconPanel.tsx index f9eb4f78b6..e824892451 100644 --- a/app/components/IconPicker/components/IconPanel.tsx +++ b/app/components/IconPicker/components/IconPanel.tsx @@ -1,4 +1,5 @@ import * as React from "react"; +import type { ListImperativeAPI } from "react-window"; import { useTranslation } from "react-i18next"; import styled from "styled-components"; import { IconType } from "@shared/types"; @@ -47,7 +48,7 @@ const IconPanel = ({ const { t } = useTranslation(); const searchRef = React.useRef(null); - const scrollableRef = React.useRef(null); + const scrollableRef = React.useRef(null); const { incrementIconCount, frequentIcons } = useIconState(IconType.SVG); @@ -131,7 +132,7 @@ const IconPanel = ({ if (!panelActive) { return; } - scrollableRef.current?.scroll({ top: 0 }); + scrollableRef.current?.element?.scroll({ top: 0 }); requestAnimationFrame(() => searchRef.current?.focus()); }, [panelActive]); diff --git a/package.json b/package.json index 89003ccebd..4472165c1d 100644 --- a/package.json +++ b/package.json @@ -229,9 +229,8 @@ "react-remove-scroll": "^2.7.2", "react-router-dom": "^5.3.4", "react-use-measure": "^2.1.7", - "react-virtualized-auto-sizer": "^1.0.26", "react-waypoint": "^10.3.0", - "react-window": "^1.8.11", + "react-window": "^2.3.4", "react-zoom-pan-pinch": "^3.7.0", "redlock": "^5.0.0-beta2", "reflect-metadata": "^0.2.2", @@ -311,7 +310,6 @@ "@types/react": "19.3.0", "@types/react-dom": "^19.3.0", "@types/react-router-dom": "^5.3.3", - "@types/react-window": "^1.8.8", "@types/readable-stream": "^4.0.24", "@types/redis-info": "^3.0.3", "@types/refractor": "^3.4.1", diff --git a/yarn.lock b/yarn.lock index d84fc2f136..e7bb221f02 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1438,7 +1438,7 @@ __metadata: languageName: node linkType: hard -"@babel/runtime@npm:^7.0.0, @babel/runtime@npm:^7.1.2, @babel/runtime@npm:^7.11.2, @babel/runtime@npm:^7.12.13, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.17.8, @babel/runtime@npm:^7.20.6, @babel/runtime@npm:^7.21.0, @babel/runtime@npm:^7.23.2, @babel/runtime@npm:^7.25.0": +"@babel/runtime@npm:^7.1.2, @babel/runtime@npm:^7.11.2, @babel/runtime@npm:^7.12.13, @babel/runtime@npm:^7.12.5, @babel/runtime@npm:^7.17.8, @babel/runtime@npm:^7.20.6, @babel/runtime@npm:^7.21.0, @babel/runtime@npm:^7.23.2, @babel/runtime@npm:^7.25.0": version: 7.29.7 resolution: "@babel/runtime@npm:7.29.7" checksum: 10c0/ca11572f7146b21e0bde6a9ed4bb6a89eafbee5f0944c7eb54d0d8a2dac962c33638a1d611e14faa71dfbb92b4b5f9236232208568a6b7d5c6f3f39ddb91771e @@ -6627,15 +6627,6 @@ __metadata: languageName: node linkType: hard -"@types/react-window@npm:^1.8.8": - version: 1.8.8 - resolution: "@types/react-window@npm:1.8.8" - dependencies: - "@types/react": "npm:*" - checksum: 10c0/2170a3957752603e8b994840c5d31b72ddf94c427c0f42b0175b343cc54f50fe66161d8871e11786ec7a59906bd33861945579a3a8f745455a3744268ec1069f - languageName: node - linkType: hard - "@types/react@npm:19.3.0": version: 19.3.0 resolution: "@types/react@npm:19.3.0" @@ -13267,13 +13258,6 @@ __metadata: languageName: node linkType: hard -"memoize-one@npm:>=3.1.1 <6": - version: 5.2.1 - resolution: "memoize-one@npm:5.2.1" - checksum: 10c0/fd22dbe9a978a2b4f30d6a491fc02fb90792432ad0dab840dc96c1734d2bd7c9cdeb6a26130ec60507eb43230559523615873168bcbe8fafab221c30b11d54c1 - languageName: node - linkType: hard - "merge-descriptors@npm:^2.0.0": version: 2.0.0 resolution: "merge-descriptors@npm:2.0.0" @@ -14278,7 +14262,6 @@ __metadata: "@types/react": "npm:19.3.0" "@types/react-dom": "npm:^19.3.0" "@types/react-router-dom": "npm:^5.3.3" - "@types/react-window": "npm:^1.8.8" "@types/readable-stream": "npm:^4.0.24" "@types/redis-info": "npm:^3.0.3" "@types/refractor": "npm:^3.4.1" @@ -14419,9 +14402,8 @@ __metadata: react-remove-scroll: "npm:^2.7.2" react-router-dom: "npm:^5.3.4" react-use-measure: "npm:^2.1.7" - react-virtualized-auto-sizer: "npm:^1.0.26" react-waypoint: "npm:^10.3.0" - react-window: "npm:^1.8.11" + react-window: "npm:^2.3.4" react-zoom-pan-pinch: "npm:^3.7.0" redlock: "npm:^5.0.0-beta2" reflect-metadata: "npm:^0.2.2" @@ -16070,16 +16052,6 @@ __metadata: languageName: node linkType: hard -"react-virtualized-auto-sizer@npm:^1.0.26": - version: 1.0.26 - resolution: "react-virtualized-auto-sizer@npm:1.0.26" - peerDependencies: - react: ^15.3.0 || ^16.0.0-alpha || ^17.0.0 || ^18.0.0 || ^19.0.0 - react-dom: ^15.3.0 || ^16.0.0-alpha || ^17.0.0 || ^18.0.0 || ^19.0.0 - checksum: 10c0/788b438c9cb55f94a0561ef07e6bb6e5051ad3d5ececd9b2131014324ffe773b507ac7060f965e44c84bd8d6aa85c686754ac944384878c97f7304c0473a7754 - languageName: node - linkType: hard - "react-waypoint@npm:^10.3.0": version: 10.3.0 resolution: "react-waypoint@npm:10.3.0" @@ -16094,16 +16066,13 @@ __metadata: languageName: node linkType: hard -"react-window@npm:^1.8.11": - version: 1.8.11 - resolution: "react-window@npm:1.8.11" - dependencies: - "@babel/runtime": "npm:^7.0.0" - memoize-one: "npm:>=3.1.1 <6" +"react-window@npm:^2.3.4": + version: 2.3.4 + resolution: "react-window@npm:2.3.4" peerDependencies: - react: ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 - react-dom: ^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 - checksum: 10c0/5ae8da1bc5c47d8f0a428b28a600256e2db511975573e52cb65a9b27ed1a0e5b9f7b3bee5a54fb0da93956d782c24010be434be451072f46ba5a89159d2b3944 + react: ^18.0.0 || ^19.0.0 + react-dom: ^18.0.0 || ^19.0.0 + checksum: 10c0/6990fe8e2933c621407a5eaa5be928c1c414b19184c6f5054e5f2be510aa5f54a44d9addf76f64381919cde0839e788163a6dec3ef67824eb8ec0c5859c4fb60 languageName: node linkType: hard