Skip to content

Commit a802d00

Browse files
committed
Initial commit! 🚀
0 parents  commit a802d00

10 files changed

+429
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,.*rc,*.yml}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.eslintrc

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"parser": "babel-eslint",
3+
"extends": "eslint:recommended",
4+
"env": {
5+
"browser": true
6+
},
7+
"plugins": [
8+
"react"
9+
],
10+
"settings": {
11+
"react": {
12+
"pragma": "h"
13+
}
14+
},
15+
"parserOptions": {
16+
"ecmaVersion": 6,
17+
"sourceType": "module",
18+
"ecmaFeatures": {
19+
"modules": true,
20+
"jsx": true
21+
}
22+
},
23+
"rules": {
24+
"react/jsx-uses-react": 2,
25+
"react/jsx-uses-vars": 2,
26+
"no-unused-vars": [1, { "varsIgnorePattern": "^h$" }],
27+
"no-cond-assign": 1,
28+
"semi": 2,
29+
"camelcase": 0,
30+
"comma-style": 2,
31+
"comma-dangle": [2, "never"],
32+
"indent": [2, "tab", {"SwitchCase": 1}],
33+
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
34+
"no-trailing-spaces": [2, { "skipBlankLines": true }],
35+
"max-nested-callbacks": [2, 3],
36+
"no-eval": 2,
37+
"no-implied-eval": 2,
38+
"no-new-func": 2,
39+
"guard-for-in": 2,
40+
"eqeqeq": 2,
41+
"no-else-return": 2,
42+
"no-redeclare": 2,
43+
"no-dupe-keys": 2,
44+
"radix": 2,
45+
"strict": [2, "never"],
46+
"no-shadow": 0,
47+
"callback-return": [1, ["callback", "cb", "next", "done"]],
48+
"no-delete-var": 2,
49+
"no-undef-init": 2,
50+
"no-shadow-restricted-names": 2,
51+
"handle-callback-err": 0,
52+
"no-lonely-if": 2,
53+
"keyword-spacing": 2,
54+
"constructor-super": 2,
55+
"no-this-before-super": 2,
56+
"no-dupe-class-members": 2,
57+
"no-const-assign": 2,
58+
"prefer-spread": 2,
59+
"no-useless-concat": 2,
60+
"no-var": 2,
61+
"object-shorthand": 2,
62+
"prefer-arrow-callback": 2
63+
}
64+
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/dist
2+
/node_modules
3+
/npm-debug.log
4+
.DS_Store

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- stable

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Jason Miller
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# `<ScrollViewport />` <sub>_for [Preact]_</sub>
2+
3+
[![NPM](https://img.shields.io/npm/v/preact-scroll-viewport.svg)](https://www.npmjs.com/package/preact-scroll-viewport)
4+
[![Travis](https://travis-ci.org/developit/preact-scroll-viewport.svg?branch=master)](https://travis-ci.org/developit/preact-scroll-viewport)
5+
6+
A compositional component that renders its children based on the current viewport.
7+
8+
Useful for those super important business applications where one must show _all_ million rows.
9+
10+
#### [Demo](https://jsfiddle.net/developit/t6qqnwn9/)
11+
12+
<a href="https://jsfiddle.net/developit/t6qqnwn9/">
13+
<img alt="preview" src="https://i.gyazo.com/38f75b5db9615b0a08304d6cca209e47.gif" width="420">
14+
</a>
15+
16+
---
17+
18+
19+
## Usage Example
20+
21+
Simply wrap a large collection of children in this component, and they will be rendered based on the viewport.
22+
You can define a default row height (`defaultRowHeight`) to use prior to dimensions being available, or a static row height (`rowHeight`) to avoid style recalculation entirely. If `rowHeight` is not provided, the height of the first row will be calculated and extrapolated.
23+
24+
```js
25+
// create 100,000 children:
26+
let children = [];
27+
for (let i=1; i<100000; i++) {
28+
children.push(<div class="row">{i}</div>);
29+
}
30+
31+
// ...but only render what is in-viewport:
32+
render(
33+
<ScrollViewport rowHeight={22}>
34+
{children}
35+
</ScrollViewport>
36+
);
37+
```
38+
39+
40+
---
41+
42+
43+
## Props
44+
45+
| Prop | Type | Description |
46+
|-----------------------|------------|---------------------|
47+
| **`rowHeight`** | _Number_ | Static height of a row (prevents style recalc)
48+
| **`defaultRowHeight`** | _Number_ | Initial height of a row prior to dimensions being available
49+
| **`overscan`** | _Number_ | Number of extra rows to render above and below visible list. Defaults to 10. \*
50+
| **`sync`** | _Boolean_ | If `true`, forces synchronous rendering \*\*
51+
52+
_**\* Why overscan?** Rendering normalized blocks of rows reduces the number of DOM interactions by grouping all row renders into a single atomic update._
53+
54+
_**\*\* About synchronous rendering:** It's best to try without `sync` enabled first. If the normal async rendering behavior is fine, leave sync turned off. If you see flickering, enabling sync will ensure every update gets out to the screen without dropping renders, but does so at the expense of actual framerate._
55+
56+
57+
| _Without_ Overscan | _With_ Overscan |
58+
|--------------------|-----------------|
59+
| <img src="https://i.gyazo.com/e192bf1ca835fbe6ad803f7b6270e424.gif" height="150"> | <img src="https://i.gyazo.com/478440d1f06fe543e69fff8b88ce7963.gif" height="150"> |
60+
61+
62+
---
63+
64+
## Simple Example
65+
66+
[**View this example on JSFiddle**](https://jsfiddle.net/developit/t6qqnwn9/)
67+
68+
```js
69+
import ScrollViewport from 'preact-scroll-viewport';
70+
71+
class Demo extends Component {
72+
// 30px tall rows
73+
rowHeight = 30;
74+
75+
render() {
76+
// Generate 100,000 rows of data
77+
let rows = [];
78+
for (let x=1e5; x--; ) rows[x] = `Item #${x+1}`;
79+
80+
return (
81+
<ScrollViewport class="list" rowHeight={this.rowHeight}>
82+
{ rows.map( row => (
83+
<div class="row">{row}</div>
84+
)) }
85+
</ScrollViewport>
86+
);
87+
}
88+
}
89+
90+
render(Demo, document.body);
91+
```
92+
93+
94+
---
95+
96+
97+
### License
98+
99+
[MIT]
100+
101+
102+
[Preact]: https://github.com/developit/preact
103+
[MIT]: http://choosealicense.com/licenses/mit/

package.json

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"name": "preact-scroll-viewport",
3+
"amdName": "ScrollViewport",
4+
"version": "0.1.0",
5+
"description": "Only renders items visible in the current viewport.",
6+
"main": "dist/preact-scroll-viewport.js",
7+
"minified:main": "dist/preact-scroll-viewport.min.js",
8+
"jsnext:main": "src/index.js",
9+
"scripts": {
10+
"build": "npm-run-all transpile optimize minify",
11+
"transpile": "rollup -c rollup.config.js -m ${npm_package_main}.map -f umd -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main",
12+
"optimize": "uglifyjs $npm_package_main -bc dead_code --pure-funcs _possibleConstructorReturn _classCallCheck -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map",
13+
"minify": "uglifyjs $npm_package_main -mc collapse_vars,evaluate,screw_ie8,unsafe,loops=false,keep_fargs=false,pure_getters,unused,dead_code --pure-funcs _possibleConstructorReturn _classCallCheck -o $npm_package_minified_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_minified_main}.map",
14+
"test": "eslint {src,test} && mocha --compilers js:babel-register test/**/*.js",
15+
"prepublish": "npm run build",
16+
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
17+
},
18+
"babel": {
19+
"presets": [
20+
"es2015",
21+
"stage-0"
22+
],
23+
"plugins": [
24+
"transform-class-properties",
25+
[
26+
"transform-react-jsx",
27+
{
28+
"pragma": "h"
29+
}
30+
]
31+
]
32+
},
33+
"keywords": [
34+
"preact",
35+
"virtual",
36+
"viewport",
37+
"virtualized",
38+
"scroll"
39+
],
40+
"files": [
41+
"src",
42+
"dist"
43+
],
44+
"author": "Jason Miller <[email protected]>",
45+
"license": "MIT",
46+
"repository": {
47+
"type": "git",
48+
"url": "git+https://github.com/developit/preact-scroll-viewport.git"
49+
},
50+
"bugs": {
51+
"url": "https://github.com/developit/preact-scroll-viewport/issues"
52+
},
53+
"homepage": "https://github.com/developit/preact-scroll-viewport",
54+
"devDependencies": {
55+
"babel": "^6.5.2",
56+
"babel-eslint": "^7.1.0",
57+
"babel-plugin-transform-class-properties": "^6.18.0",
58+
"babel-plugin-transform-es2015-classes-simple": "^1.0.0",
59+
"babel-plugin-transform-react-jsx": "^6.8.0",
60+
"babel-preset-es2015": "^6.18.0",
61+
"babel-preset-stage-0": "^6.16.0",
62+
"babel-register": "^6.18.0",
63+
"chai": "^3.5.0",
64+
"eslint": "^3.10.2",
65+
"eslint-plugin-react": "^6.7.1",
66+
"mocha": "^3.1.2",
67+
"npm-run-all": "^3.1.1",
68+
"rollup": "^0.36.3",
69+
"rollup-plugin-babel": "^2.6.1",
70+
"rollup-plugin-es3": "^1.0.3",
71+
"rollup-plugin-post-replace": "^1.0.0",
72+
"uglify-js": "^2.7.4"
73+
},
74+
"dependencies": {
75+
"preact": "^6.4.0"
76+
}
77+
}

rollup.config.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import babel from 'rollup-plugin-babel';
2+
import es3 from 'rollup-plugin-es3';
3+
import replace from 'rollup-plugin-post-replace';
4+
5+
export default {
6+
useStrict: false,
7+
plugins: [
8+
babel({
9+
babelrc: false,
10+
sourceMap: true,
11+
exclude: 'node_modules/**',
12+
presets: [
13+
['es2015', { modules:false, loose:true }],
14+
'stage-0'
15+
],
16+
plugins: [
17+
'transform-class-properties',
18+
['transform-react-jsx', { pragma:'h' }]
19+
]
20+
}),
21+
22+
// strip Object.freeze()
23+
es3(),
24+
25+
// remove Babel helpers
26+
replace({
27+
'throw ': 'return; throw '
28+
})
29+
]
30+
};

0 commit comments

Comments
 (0)