Skip to content

Commit d325285

Browse files
committed
perf: WIP on implementing react-virtualized
1 parent 6fb14d0 commit d325285

File tree

6 files changed

+102
-15
lines changed

6 files changed

+102
-15
lines changed

package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@
5050
"react-highlight": "^0.12.0",
5151
"react-markdown": "^4.1.0",
5252
"react-perfect-scrollbar": "^1.5.3",
53+
"react-virtualized": "^9.21.1",
5354
"sharp": "^0.22.1",
5455
"styled-components": "^4.3.2"
5556
},
5657
"devDependencies": {
5758
"@types/react-helmet": "^5.0.8",
5859
"@types/react-highlight": "^0.12.1",
60+
"@types/react-virtualized": "^9.21.2",
5961
"@types/styled-components": "^4.1.16",
6062
"prettier": "^1.18.2",
6163
"tslint": "^5.18.0",

src/components/DocsContent/index.tsx

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { navigate } from "gatsby"
22
import React, { memo } from "react"
3+
import {
4+
AutoSizer,
5+
CellMeasurer,
6+
CellMeasurerCache,
7+
List,
8+
ListRowProps,
9+
} from "react-virtualized"
310
import { Method as MethodInterface } from "../../types"
411
import Header from "../Header"
512
import Method from "../Method"
@@ -18,12 +25,57 @@ const methodFromPath = (props: any) => {
1825
const DocsContent = (props: DocsContentProps): JSX.Element => {
1926
const currentMethod = methodFromPath(props)
2027
const { methods } = props
28+
const cache = new CellMeasurerCache({ defaultHeight: 700, fixedWidth: true })
2129

2230
const SingleMethod = ({ name }) => {
23-
const method = methods.find(({ node: method }) => method.name === name)
31+
const method = methods.find(
32+
({ node: method }) => method.name === name
33+
) as MethodInterface
2434
return <Method method={method.node} />
2535
}
2636

37+
function rowRenderer({
38+
index, // Index of row
39+
// isScrolling, // The List is currently being scrolled
40+
// isVisible, // This row is visible within the List (eg it is not an overscanned row)
41+
key, // Unique key within array of rendered rows
42+
parent, // Reference to the parent List (instance)
43+
style, // Style object to be applied to row (to position it);
44+
}: ListRowProps): JSX.Element {
45+
const { node: method } = methods[index]
46+
47+
// If row content is complex, consider rendering a light-weight placeholder while scrolling.
48+
const content = (
49+
<Method
50+
key={`${method.category}-${method.name}-${index}`}
51+
method={method}
52+
/>
53+
)
54+
55+
// Style is required since it specifies how the row is to be sized and positioned.
56+
// React Virtualized depends on this sizing/positioning for proper scrolling behavior.
57+
// By default, the List component provides following style properties:
58+
// position
59+
// left
60+
// top
61+
// height
62+
// width
63+
// You can add additional class names or style properties as you would like.
64+
// Key is also required by React to more efficiently manage the array of rows.
65+
return (
66+
<CellMeasurer
67+
cache={cache}
68+
columnIndex={0}
69+
key={key}
70+
parent={parent}
71+
rowIndex={index}
72+
// width={this._mostRecentWidth}
73+
>
74+
<div style={style}>{content}</div>
75+
</CellMeasurer>
76+
)
77+
}
78+
2779
return (
2880
<SC.DocsContentWrapper>
2981
<Header />
@@ -34,20 +86,20 @@ const DocsContent = (props: DocsContentProps): JSX.Element => {
3486
</SC.SeeAll>
3587
)}
3688

37-
{/* TODO: optimize performance */}
3889
{currentMethod ? (
3990
<SingleMethod name={currentMethod} />
4091
) : (
41-
/* TODO: get rid of i, currently a dirty fix because Seq-chain is not unique */
42-
methods.map((methodNode, i) => {
43-
const { node: method } = methodNode
44-
return (
45-
<Method
46-
key={`${method.category}-${method.name}-${i}`}
47-
method={method}
92+
<AutoSizer>
93+
{({ width, height }) => (
94+
<List
95+
rowRenderer={rowRenderer}
96+
width={width}
97+
height={height}
98+
rowHeight={cache.rowHeight}
99+
rowCount={methods.length}
48100
/>
49-
)
50-
})
101+
)}
102+
</AutoSizer>
51103
)}
52104
</SC.Content>
53105
</SC.DocsContentWrapper>

src/components/Header/styles.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const HeaderWrapper = styled.header<{ scrolled: boolean }>`
1111
top: 0;
1212
left: 320px;
1313
right: 0;
14+
z-index: 10;
1415
1516
&:after {
1617
content: "";

src/components/Method/styles.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ export const MethodWrapper = styled.div`
99
flex-direction: column;
1010
background: #293845;
1111
padding: 11px 11px 24px;
12-
13-
& + & {
14-
margin-top: 75px;
15-
}
12+
margin-bottom: 75px;
1613
`
1714

1815
export const Name = styled.h2`

src/pages/docs.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { graphql, useStaticQuery } from "gatsby"
22
import React from "react"
3+
import "react-virtualized/styles.css"
34
import styled from "styled-components"
45

56
import DocsContent from "../components/DocsContent"

0 commit comments

Comments
 (0)