-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useScroll, extract useElementScroll, useWindowScroll (#219)
* feat: add useScroll, extract useElementScroll, useWindowScroll * fix: include list postion in window scroll mode * docs: update api * fix: update types * test: add simple test using windowRef, custom useScroll hook * test: pin @testing-library/react-hooks to 5.1.3 for node10 support
- Loading branch information
Showing
13 changed files
with
1,236 additions
and
82 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
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,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.jsx"></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,20 @@ | ||
{ | ||
"name": "window-scroll", | ||
"private": true, | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "vite build", | ||
"serve": "vite preview", | ||
"start": "vite" | ||
}, | ||
"dependencies": { | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"react-virtual": "2.9.0" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-replace": "^3.0.0", | ||
"@vitejs/plugin-react": "^1.0.8", | ||
"vite": "^2.6.14" | ||
} | ||
} |
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,21 @@ | ||
html, body { | ||
margin: 0px; | ||
padding: 0px; | ||
font-family: sans-serif; | ||
font-size: 14px; | ||
} | ||
|
||
.List { | ||
max-width: 100%; | ||
} | ||
|
||
.ListItemEven, | ||
.ListItemOdd { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.ListItemEven { | ||
background-color: #e6e4dc; | ||
} |
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,64 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import './index.css' | ||
|
||
import { useVirtual } from 'react-virtual' | ||
|
||
const rows = new Array(10000) | ||
.fill(true) | ||
.map(() => 25 + Math.round(Math.random() * 100)) | ||
|
||
function App() { | ||
const parentRef = React.useRef(null) | ||
|
||
const rowVirtualizer = useVirtual({ | ||
size: rows.length, | ||
parentRef, | ||
windowRef: React.useRef(window), | ||
}) | ||
|
||
return ( | ||
<div> | ||
<h1>Window scroll - demo</h1> | ||
<div> | ||
<div ref={parentRef} style={{ width: `100%` }}> | ||
<div | ||
style={{ | ||
height: `${rowVirtualizer.totalSize}px`, | ||
width: '100%', | ||
position: 'relative', | ||
}} | ||
> | ||
{rowVirtualizer.virtualItems.map(virtualRow => ( | ||
<div | ||
key={virtualRow.key} | ||
ref={virtualRow.measureRef} | ||
className={ | ||
virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven' | ||
} | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
transform: `translateY(${virtualRow.start}px)`, | ||
}} | ||
> | ||
<div style={{ height: `${rows[virtualRow.index]}px` }}> | ||
Row {virtualRow.index} | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
) |
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,25 @@ | ||
import * as path from 'path' | ||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
import rollupReplace from '@rollup/plugin-replace' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [ | ||
rollupReplace({ | ||
preventAssignment: true, | ||
values: { | ||
__DEV__: JSON.stringify(true), | ||
'process.env.NODE_ENV': JSON.stringify('development'), | ||
}, | ||
}), | ||
react(), | ||
], | ||
resolve: process.env.USE_SOURCE | ||
? { | ||
alias: { | ||
'react-virtual': path.resolve(__dirname, '../../src/index.js'), | ||
}, | ||
} | ||
: {}, | ||
}) |
Oops, something went wrong.
aa8dc9c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: