-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Dell
committed
Nov 14, 2020
1 parent
a95469d
commit 7b248d8
Showing
11 changed files
with
12,921 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
current node | ||
last 2 versions and > 2% | ||
ie > 10 |
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,72 @@ | ||
# nuxt-render-on-scroll | ||
`nuxt-render-on-scroll` - A Vue/Nuxt component that allows you to lazily render components only when they are scrolled into the viewport. | ||
|
||
Based on the idea from [vue-render-on-scroll | ||
](https://github.com/petr-nazarov/vue-render-on-scroll) and adopted for SSR usage in Nuxt.js: | ||
Adding `v-if="false"` to the content of the component as long as the component is not in the current viewport or while in SSR mode. | ||
|
||
Nevertheless it should still be usable independent of Nuxt via stock Vue. | ||
|
||
## install | ||
```sh | ||
yarn add nuxt-render-on-scroll | ||
# OR | ||
npm i nuxt-render-on-scroll | ||
``` | ||
```js | ||
import RenderOnScroll from 'nuxt-render-on-scroll'; | ||
export default { | ||
components: { | ||
RenderOnScroll, | ||
// ... | ||
}, | ||
}; | ||
``` | ||
|
||
|
||
## Simple Usage | ||
```html | ||
<render-on-scroll> | ||
<div>This content will be loaded only when it enters viewport</div> | ||
</render-on-scroll> | ||
``` | ||
|
||
## Advanced Usage | ||
```html | ||
<render-on-scroll :offset-y="100"> | ||
<div>This content will already be rendered when scrolled 100px near it</div> | ||
</render-on-scroll> | ||
``` | ||
```html | ||
<render-on-scroll height="180px" :offset-y="-40"> | ||
<div>This content will only be rendered when at least 40px scrolled into the 180px high placeholder space</div> | ||
</render-on-scroll> | ||
``` | ||
## Configuration Options | ||
| prop | type | default | comments | | ||
| ------------|:--------------:| --------:| ---------| | ||
| height | String | '' | CSS string for height property of the wrapping div. Useful to preserve the space while to content is not rendered yet. Can be used in combination with a negative value `offsetY` render only when scrolled into the content area, e.g. for large charts. | | ||
| offsetY | Number | 0 | Additional amount of pixel to be added on the content's bounds. E.g. value of 100 will render the content when scrolled within 100px _near_ it. | | ||
|
||
## Development | ||
This project is based on [vue-sfc-rollup](https://github.com/team-innovation/vue-sfc-rollup) | ||
### Clone this repo | ||
```sh | ||
git clone git@github.com:Michi-4G/nuxt-render-on-scroll.git | ||
cd nuxt-render-on-scroll | ||
``` | ||
|
||
### Install dependencies | ||
```sh | ||
npm install | ||
``` | ||
|
||
### Run development server | ||
```sh | ||
npm run serve | ||
``` | ||
|
||
### Build for production | ||
```sh | ||
npm run build | ||
``` |
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 @@ | ||
const devPresets = ['@vue/babel-preset-app']; | ||
const buildPresets = ['@babel/preset-env']; | ||
module.exports = { | ||
presets: (process.env.NODE_ENV === 'development' ? devPresets : buildPresets), | ||
}; |
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,159 @@ | ||
// rollup.config.js | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import vue from 'rollup-plugin-vue'; | ||
import alias from '@rollup/plugin-alias'; | ||
import commonjs from '@rollup/plugin-commonjs'; | ||
import replace from '@rollup/plugin-replace'; | ||
import babel from 'rollup-plugin-babel'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
import minimist from 'minimist'; | ||
|
||
// Get browserslist config and remove ie from es build targets | ||
const esbrowserslist = fs.readFileSync('./.browserslistrc') | ||
.toString() | ||
.split('\n') | ||
.filter((entry) => entry && entry.substring(0, 2) !== 'ie'); | ||
|
||
const argv = minimist(process.argv.slice(2)); | ||
|
||
const projectRoot = path.resolve(__dirname, '..'); | ||
|
||
const baseConfig = { | ||
input: 'src/entry.js', | ||
plugins: { | ||
preVue: [ | ||
alias({ | ||
resolve: ['.js', '.jsx', '.ts', '.tsx', '.vue'], | ||
entries: { | ||
'@': path.resolve(projectRoot, 'src'), | ||
}, | ||
}), | ||
], | ||
replace: { | ||
'process.env.NODE_ENV': JSON.stringify('production'), | ||
'process.env.ES_BUILD': JSON.stringify('false'), | ||
}, | ||
vue: { | ||
css: true, | ||
template: { | ||
isProduction: true, | ||
}, | ||
}, | ||
babel: { | ||
exclude: 'node_modules/**', | ||
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'], | ||
}, | ||
}, | ||
}; | ||
|
||
// ESM/UMD/IIFE shared settings: externals | ||
// Refer to https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency | ||
const external = [ | ||
// list external dependencies, exactly the way it is written in the import statement. | ||
// eg. 'jquery' | ||
'vue', | ||
]; | ||
|
||
// UMD/IIFE shared settings: output.globals | ||
// Refer to https://rollupjs.org/guide/en#output-globals for details | ||
const globals = { | ||
// Provide global variable names to replace your external imports | ||
// eg. jquery: '$' | ||
vue: 'Vue', | ||
}; | ||
|
||
// Customize configs for individual targets | ||
const buildFormats = []; | ||
if (!argv.format || argv.format === 'es') { | ||
const esConfig = { | ||
...baseConfig, | ||
external, | ||
output: { | ||
file: 'dist/render-on-scroll.esm.js', | ||
format: 'esm', | ||
exports: 'named', | ||
}, | ||
plugins: [ | ||
replace({ | ||
...baseConfig.plugins.replace, | ||
'process.env.ES_BUILD': JSON.stringify('true'), | ||
}), | ||
...baseConfig.plugins.preVue, | ||
vue(baseConfig.plugins.vue), | ||
babel({ | ||
...baseConfig.plugins.babel, | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: esbrowserslist, | ||
}, | ||
], | ||
], | ||
}), | ||
commonjs(), | ||
], | ||
}; | ||
buildFormats.push(esConfig); | ||
} | ||
|
||
if (!argv.format || argv.format === 'cjs') { | ||
const umdConfig = { | ||
...baseConfig, | ||
external, | ||
output: { | ||
compact: true, | ||
file: 'dist/render-on-scroll.ssr.js', | ||
format: 'cjs', | ||
name: 'RenderOnScroll', | ||
exports: 'named', | ||
globals, | ||
}, | ||
plugins: [ | ||
replace(baseConfig.plugins.replace), | ||
...baseConfig.plugins.preVue, | ||
vue({ | ||
...baseConfig.plugins.vue, | ||
template: { | ||
...baseConfig.plugins.vue.template, | ||
optimizeSSR: true, | ||
}, | ||
}), | ||
babel(baseConfig.plugins.babel), | ||
commonjs(), | ||
], | ||
}; | ||
buildFormats.push(umdConfig); | ||
} | ||
|
||
if (!argv.format || argv.format === 'iife') { | ||
const unpkgConfig = { | ||
...baseConfig, | ||
external, | ||
output: { | ||
compact: true, | ||
file: 'dist/render-on-scroll.min.js', | ||
format: 'iife', | ||
name: 'RenderOnScroll', | ||
exports: 'named', | ||
globals, | ||
}, | ||
plugins: [ | ||
replace(baseConfig.plugins.replace), | ||
...baseConfig.plugins.preVue, | ||
vue(baseConfig.plugins.vue), | ||
babel(baseConfig.plugins.babel), | ||
commonjs(), | ||
terser({ | ||
output: { | ||
ecma: 5, | ||
}, | ||
}), | ||
], | ||
}; | ||
buildFormats.push(unpkgConfig); | ||
} | ||
|
||
// Export config | ||
export default buildFormats; |
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,8 @@ | ||
import Vue from 'vue'; | ||
import Dev from './serve.vue'; | ||
|
||
Vue.config.productionTip = false; | ||
|
||
new Vue({ | ||
render: (h) => h(Dev), | ||
}).$mount('#app'); |
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,45 @@ | ||
<script> | ||
import Vue from "vue"; | ||
import RenderOnScroll from "@/render-on-scroll.vue"; | ||
export default Vue.extend({ | ||
name: "ServeDev", | ||
components: { | ||
RenderOnScroll, | ||
}, | ||
data: function() { | ||
return { | ||
contents: [...Array(50).keys()].map((i) => ({ | ||
text: "sample content #" + i, | ||
})), | ||
}; | ||
}, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div id="app"> | ||
<render-on-scroll height="100px" v-for="item in contents" :key="item.text"> | ||
<div class="sampleContent"> | ||
{{ item.text }} | ||
</div> | ||
</render-on-scroll> | ||
</div> | ||
</template> | ||
|
||
<style> | ||
.sampleContent { | ||
height: 100px; | ||
animation: fadein 3s; | ||
text-align: center; | ||
} | ||
@keyframes fadein { | ||
0% { | ||
opacity: 0; | ||
} | ||
100% { | ||
opacity: 1; | ||
} | ||
} | ||
</style> |
Oops, something went wrong.