mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
chore: Upgrade react-window to 2 (#14196)
This commit is contained in:
1 parent
9e602f8e18
commit
bdfd7436e2
7 files changed
+80
-157
No files matched your search
@@ -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<HTMLDivElement> & {
|
||||
ref?: React.Ref<HTMLDivElement>;
|
||||
}) {
|
||||
return (
|
||||
<div
|
||||
ref={ref}
|
||||
style={{
|
||||
...style,
|
||||
height: `${parseFloat(style?.height + "") + VERTICAL_PADDING * 2}px`,
|
||||
}}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
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<HTMLInputElement | HTMLTextAreaElement>(
|
||||
null
|
||||
);
|
||||
const listRef = React.useRef<List<NavigationNode[]>>(null);
|
||||
const listRef = React.useRef<ListImperativeAPI>(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<HTMLInputElement>) => {
|
||||
@@ -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<RowProps>) => {
|
||||
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({
|
||||
<DocumentExplorerNode
|
||||
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])}
|
||||
@@ -465,24 +425,15 @@ function DocumentExplorer({
|
||||
/>
|
||||
<ListContainer>
|
||||
{nodes.length ? (
|
||||
<AutoSizer>
|
||||
{({ width, height }: { width: number; height: number }) => (
|
||||
<Flex role="listbox" column>
|
||||
<List
|
||||
ref={listRef}
|
||||
width={width}
|
||||
height={height}
|
||||
itemData={nodes}
|
||||
itemCount={nodes.length}
|
||||
itemSize={isMobile ? 48 : 32}
|
||||
innerElementType={innerElementType}
|
||||
itemKey={(index, results) => results[index].id}
|
||||
>
|
||||
{ListItem}
|
||||
</List>
|
||||
</Flex>
|
||||
)}
|
||||
</AutoSizer>
|
||||
<StyledList
|
||||
role="listbox"
|
||||
listRef={listRef}
|
||||
rowCount={nodes.length}
|
||||
rowHeight={isMobile ? 48 : 32}
|
||||
rowComponent={ListItem}
|
||||
rowProps={{ data: nodes }}
|
||||
rowKey={rowKey}
|
||||
/>
|
||||
) : (
|
||||
<FlexContainer>
|
||||
<Text as="p" type="secondary">
|
||||
@@ -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<RowProps>)`
|
||||
padding-bottom: ${VERTICAL_PADDING * 2}px;
|
||||
`;
|
||||
|
||||
const ListContainer = styled.div`
|
||||
height: 65vh;
|
||||
|
||||
|
||||
@@ -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<HTMLInputElement | null>(null);
|
||||
const scrollableRef = React.useRef<HTMLDivElement | null>(null);
|
||||
const scrollableRef = React.useRef<ListImperativeAPI>(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]);
|
||||
|
||||
|
||||
@@ -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<HTMLDivElement>;
|
||||
ref?: React.Ref<ListImperativeAPI>;
|
||||
};
|
||||
|
||||
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<HTMLDivElement>) => {
|
||||
offsetRef.current = event.currentTarget.scrollTop;
|
||||
notify();
|
||||
},
|
||||
[notify]
|
||||
);
|
||||
|
||||
const style = React.useMemo(() => ({ width, height }), [width, height]);
|
||||
const rowProps = React.useMemo(() => ({ data, columns }), [data, columns]);
|
||||
|
||||
return (
|
||||
<Container
|
||||
outerRef={ref}
|
||||
width={width}
|
||||
height={height}
|
||||
itemCount={data.length}
|
||||
itemSize={itemWidth}
|
||||
itemData={{ data, columns }}
|
||||
listRef={ref}
|
||||
style={style}
|
||||
rowCount={data.length}
|
||||
rowHeight={itemWidth}
|
||||
rowComponent={Row}
|
||||
rowProps={rowProps}
|
||||
onScroll={handleScroll}
|
||||
>
|
||||
{Row}
|
||||
</Container>
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,32 +61,28 @@ type RowProps = {
|
||||
columns: number;
|
||||
};
|
||||
|
||||
const Row = ({ index, style, data }: ListChildComponentProps<RowProps>) => {
|
||||
const { data: rows, columns } = data;
|
||||
const row = rows[index];
|
||||
const Row = ({
|
||||
index,
|
||||
style,
|
||||
ariaAttributes,
|
||||
data,
|
||||
columns,
|
||||
}: RowComponentProps<RowProps>) => (
|
||||
<RowContainer style={style} columns={columns} {...ariaAttributes}>
|
||||
{data[index]}
|
||||
</RowContainer>
|
||||
);
|
||||
|
||||
return (
|
||||
<RowContainer style={style} columns={columns}>
|
||||
{row}
|
||||
</RowContainer>
|
||||
);
|
||||
};
|
||||
|
||||
const Container = styled(FixedSizeList<RowProps>)`
|
||||
padding: 0px 12px;
|
||||
const Container = styled(List<RowProps>)`
|
||||
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;
|
||||
@@ -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<HTMLDivElement>;
|
||||
/** Ref to the virtualized list */
|
||||
ref?: React.Ref<ListImperativeAPI>;
|
||||
};
|
||||
|
||||
function GridTemplate({
|
||||
|
||||
@@ -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<HTMLInputElement | null>(null);
|
||||
const scrollableRef = React.useRef<HTMLDivElement | null>(null);
|
||||
const scrollableRef = React.useRef<ListImperativeAPI>(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]);
|
||||
|
||||
|
||||
+1
-3
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user