Skip to content

Commit 3bf218b

Browse files
First commit
0 parents  commit 3bf218b

File tree

9 files changed

+312
-0
lines changed

9 files changed

+312
-0
lines changed

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extends: "@gianlucaguarini/eslint-config"
2+
env:
3+
mocha: true

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# nyc test coverage
18+
.nyc_output
19+
20+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
21+
.grunt
22+
23+
# node-waf configuration
24+
.lock-wscript
25+
26+
# Compiled binary addons (http://nodejs.org/api/addons.html)
27+
build/Release
28+
29+
# Dependency directories
30+
node_modules
31+
jspm_packages
32+
33+
# Optional npm cache directory
34+
.npm
35+
36+
# Optional REPL history
37+
.node_repl_history
38+
39+
# generated files
40+
index.js

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: node_js
2+
node_js:
3+
- "6.*"
4+
5+
branches:
6+
only:
7+
- master
8+
9+
before_install:
10+
11+
12+
- npm i @gianlucaguarini/[email protected]
13+
14+
15+
before_script:
16+
- npm run build
17+
18+
notifications:
19+
email: false
20+
21+
sudo: false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Gianluca Guarini
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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# bianco.viewport
2+
3+
[![Build Status][travis-image]][travis-url]
4+
5+
[![NPM version][npm-version-image]][npm-url]
6+
[![NPM downloads][npm-downloads-image]][npm-url]
7+
[![MIT License][license-image]][license-url]
8+
9+
10+
11+
## Usage
12+
13+
```js
14+
import $ from 'bianco.viewport'
15+
16+
```
17+
18+
[travis-image]:https://img.shields.io/travis/biancojs/viewport.svg?style=flat-square
19+
[travis-url]:https://travis-ci.org/biancojs/viewport
20+
21+
[license-image]:http://img.shields.io/badge/license-MIT-000000.svg?style=flat-square
22+
[license-url]:LICENSE.txt
23+
24+
[npm-version-image]:http://img.shields.io/npm/v/bianco.viewport.svg?style=flat-square
25+
[npm-downloads-image]:http://img.shields.io/npm/dm/bianco.viewport.svg?style=flat-square
26+
[npm-url]:https://npmjs.org/package/bianco.viewport
27+
28+
## API
29+

index.next.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const doc = document
2+
const win = window
3+
const body = doc.body
4+
const html = doc.documentElement
5+
const ZERO = 0
6+
7+
/**
8+
* Simple function convert in boolean any value and return true if its value was truthy
9+
* @param {*} v - anything
10+
* @returns { Boolean } true if truthy
11+
*/
12+
const isTruthy = v => !!v
13+
14+
/**
15+
* Get the max value from a list of arguments filtering the falsy values
16+
* @param {...Number} args - list of numbers
17+
* @returns { Number } the highest value
18+
*/
19+
const max = (...args) => Math.max(ZERO, ...args.filter(isTruthy), ZERO)
20+
21+
/**
22+
* Return the size of the scrollbar that depends on the browser or device used on the client
23+
* @returns { Number } - the browser scrollbar width
24+
*/
25+
export function scrollbarWidth() {
26+
// Create the measurement node
27+
const div = doc.createElement('div')
28+
Object.assign(div.style, {
29+
width: '100px',
30+
height: '100px',
31+
overflow: 'scroll',
32+
position: 'fixed',
33+
opacity: '0'
34+
})
35+
36+
doc.body.appendChild(div)
37+
// Read values
38+
const { offsetWidth, clientWidth } = div
39+
// Delete helper element
40+
doc.body.removeChild(div)
41+
42+
return max(offsetWidth - clientWidth)
43+
}
44+
45+
/**
46+
* Get the height of the whole page
47+
* @returns { Number } height in px of the document
48+
*/
49+
export function documentHeight() {
50+
return max(
51+
body.scrollHeight,
52+
body.offsetHeight,
53+
html.clientHeight,
54+
html.scrollHeight,
55+
html.offsetHeight
56+
)
57+
}
58+
59+
/**
60+
* Get the width of the whole page
61+
* @returns { Number } width in px of the document
62+
*/
63+
export function documentWidth() {
64+
return max(
65+
body.scrollWidth,
66+
body.offsetWidth,
67+
html.clientWidth,
68+
html.scrollWidth,
69+
html.offsetWidth
70+
)
71+
}
72+
73+
/**
74+
* Return amount of px scrolled from the top of the document
75+
* @returns { Number } scroll top value in px
76+
*/
77+
export function scrollTop() {
78+
return max(
79+
win.scrollY,
80+
win.pageYOffset,
81+
doc.documentElement.scrollTop
82+
)
83+
}
84+
85+
/**
86+
* Return amount of px scrolled from the top of the document
87+
* @returns { Number } scroll top value in px
88+
*/
89+
export function scrollLeft() {
90+
return max(
91+
win.scrollX,
92+
win.pageXOffset,
93+
doc.documentElement.scrollLeft
94+
)
95+
}
96+
97+
98+
/**
99+
* Get the offset top of any DOM element
100+
* @param { HTMLElement } el - the element we need to check
101+
* @returns { Number } the element y position in px
102+
*/
103+
export function elementOffsetTop(el) {
104+
return max(scrollTop() + el.getBoundingClientRect().top)
105+
}

package.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "bianco.viewport",
3+
"version": "0.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"jsnext:main": "index.next.js",
7+
"module": "index.next.js",
8+
"scripts": {
9+
"prepare": "npm run build && npm test",
10+
"lint": "eslint index.next.js test.js rollup.config.js",
11+
"build": "rollup -c",
12+
"doc": "documentation readme index.next.js -s API",
13+
"test": "npm run lint && mocha test.js"
14+
},
15+
"files": [
16+
"index.js",
17+
"index.next.js"
18+
],
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/biancojs/viewport.git"
22+
},
23+
"keywords": [
24+
"es6",
25+
"es2015"
26+
],
27+
"author": "Gianluca Guarini <[email protected]> (http://gianlucaguarini.com)",
28+
"license": "MIT",
29+
"bugs": {
30+
"url": "https://github.com/biancojs/viewport/issues"
31+
},
32+
"homepage": "https://github.com/biancojs/viewport#readme",
33+
"devDependencies": {
34+
"jsdom": "9.5.0",
35+
"jsdom-global": "2.1.0",
36+
"rollup-plugin-node-resolve": "^2.0.0"
37+
}
38+
}

rollup.config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import resolve from 'rollup-plugin-node-resolve'
2+
3+
export default {
4+
entry: 'index.next.js',
5+
plugins: [
6+
resolve({
7+
jsnext: true
8+
})
9+
],
10+
targets: [
11+
{
12+
dest: 'index.js',
13+
format: 'cjs'
14+
}
15+
]
16+
}

test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('jsdom-global')()
2+
const assert = require('assert')
3+
const viewport = require('./')
4+
const body = document.body
5+
6+
const notNaN = v => !isNaN(v)
7+
8+
describe('Bianco viewport', function() {
9+
it('scrollbarWidth is a number', function() {
10+
assert.equal(typeof viewport.scrollbarWidth(), 'number')
11+
assert.ok(notNaN(viewport.scrollbarWidth()))
12+
})
13+
14+
it('documentHeight is a number', function() {
15+
assert.equal(typeof viewport.documentHeight(), 'number')
16+
assert.ok(notNaN(viewport.documentHeight()))
17+
})
18+
19+
it('documentWidth is a number', function() {
20+
assert.equal(typeof viewport.documentWidth(), 'number')
21+
assert.ok(notNaN(viewport.documentWidth()))
22+
})
23+
24+
it('scrollTop is a number', function() {
25+
assert.equal(typeof viewport.scrollTop(), 'number')
26+
assert.ok(notNaN(viewport.scrollTop()))
27+
})
28+
29+
it('scrollLeft is a number', function() {
30+
assert.equal(typeof viewport.scrollLeft(), 'number')
31+
assert.ok(notNaN(viewport.scrollLeft()))
32+
})
33+
34+
it('elementOffsetTop is a number', function() {
35+
const div = document.createElement('div')
36+
assert.equal(typeof viewport.elementOffsetTop(div), 'number')
37+
assert.ok(notNaN(viewport.elementOffsetTop(div)))
38+
})
39+
})

0 commit comments

Comments
 (0)