-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add window example for react, update docs (#614)
- Loading branch information
Showing
14 changed files
with
639 additions
and
67 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
.DS_Store | ||
dist | ||
dist-ssr | ||
*.local |
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,6 @@ | ||
# Example | ||
|
||
To run this example: | ||
|
||
- `npm install` or `yarn` | ||
- `npm run start` or `yarn start` |
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,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
</html> |
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,23 @@ | ||
{ | ||
"name": "window", | ||
"private": true, | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"serve": "vite preview" | ||
}, | ||
"dependencies": { | ||
"@tanstack/react-virtual": "3.0.0-beta.66", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-replace": "^5.0.2", | ||
"@types/node": "18.x", | ||
"@types/react": "^18.0.27", | ||
"@types/react-dom": "^18.0.10", | ||
"@vitejs/plugin-react": "^3.0.1", | ||
"typescript": "^4.9.5", | ||
"vite": "^4.0.4" | ||
} | ||
} |
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,28 @@ | ||
html { | ||
font-family: sans-serif; | ||
font-size: 14px; | ||
} | ||
|
||
body { | ||
padding: 1rem; | ||
} | ||
|
||
.List { | ||
border: 1px solid #e6e4dc; | ||
max-width: 100%; | ||
} | ||
|
||
.ListItemEven, | ||
.ListItemOdd { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.ListItemEven { | ||
background-color: #e6e4dc; | ||
} | ||
|
||
button { | ||
border: 1px solid gray; | ||
} |
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,82 @@ | ||
import * as React from 'react' | ||
import * as ReactDOM from 'react-dom/client' | ||
|
||
import './index.css' | ||
|
||
import { useWindowVirtualizer } from '@tanstack/react-virtual' | ||
|
||
function Example() { | ||
const listRef = React.useRef<HTMLDivElement | null>(null) | ||
|
||
const virtualizer = useWindowVirtualizer({ | ||
count: 10000, | ||
estimateSize: () => 35, | ||
overscan: 5, | ||
scrollMargin: listRef.current?.offsetTop ?? 0, | ||
}) | ||
|
||
return ( | ||
<> | ||
<div ref={listRef} className="List"> | ||
<div | ||
style={{ | ||
height: `${virtualizer.getTotalSize()}px`, | ||
width: '100%', | ||
position: 'relative', | ||
}} | ||
> | ||
{virtualizer.getVirtualItems().map((item) => ( | ||
<div | ||
key={item.key} | ||
className={item.index % 2 ? 'ListItemOdd' : 'ListItemEven'} | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
height: `${item.size}px`, | ||
transform: `translateY(${ | ||
item.start - virtualizer.options.scrollMargin | ||
}px)`, | ||
}} | ||
> | ||
Row {item.index} | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
function App() { | ||
return ( | ||
<div> | ||
<p> | ||
In many cases, when implementing a virtualizer with a window as the | ||
scrolling element, developers often find the need to specify a | ||
"scrollMargin." The scroll margin is a crucial setting that defines the | ||
space or gap between the start of the page and the edges of the list. | ||
</p> | ||
<br /> | ||
<br /> | ||
<h3>Window scroller</h3> | ||
<Example /> | ||
<br /> | ||
<br /> | ||
{process.env.NODE_ENV === 'development' ? ( | ||
<p> | ||
<strong>Notice:</strong> You are currently running React in | ||
development mode. Rendering performance will be slightly degraded | ||
until this application is build for production. | ||
</p> | ||
) : null} | ||
</div> | ||
) | ||
} | ||
|
||
ReactDOM.createRoot(document.getElementById('root')!).render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
) |
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,12 @@ | ||
{ | ||
"composite": true, | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "./build/types" | ||
}, | ||
"files": ["src/main.tsx"], | ||
"include": [ | ||
"src" | ||
// "__tests__/**/*.test.*" | ||
] | ||
} |
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,7 @@ | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
}) |
Oops, something went wrong.