Skip to content

Commit d3f4a9d

Browse files
committed
add directive
1 parent a01eaad commit d3f4a9d

7 files changed

+5954
-1
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
example
3+
node_modules

README.md

+72-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,72 @@
1-
# vue-screen-resize
1+
# vue-not-visible
2+
3+
Vue directive for removing from dom (like v-if) element on screen smaller then breakpoints;
4+
5+
## Install
6+
7+
```bash
8+
$ npm install --save vue-not-visible
9+
```
10+
11+
```bash
12+
$ yarn add vue-not-visible
13+
```
14+
15+
16+
## Use default
17+
18+
```js
19+
import Vue from 'vue'
20+
import vueNotVisible from 'vue-not-visible'
21+
22+
/* const BREAKPOINTS = {
23+
mobile: 425,
24+
tablet: 768,
25+
tablet_landscape: 1024,
26+
desktop: 1200,
27+
desktop_large: 1440,
28+
hd: 1920,
29+
}
30+
*/
31+
Vue.use(vueNotVisible) // this is default
32+
Vue.use(vueNotVisible, {ipad: 1280}) // this is custom
33+
34+
```
35+
36+
```html
37+
<template>
38+
<div id="test">
39+
{{ message }} {{ count }}
40+
<div v-not-visible="'tablet'">
41+
<div v-on:click="count = count + 1">Not visible on table(screen < 768)</div>
42+
</div>
43+
<div v-not-visible="'mobile'">
44+
<div v-on:click="count = count + 1">Not visible on mobile(screen < 425)</div>
45+
</div>
46+
</div>
47+
</template>
48+
```
49+
50+
## Use custom breakpoints
51+
52+
```js
53+
import Vue from 'vue'
54+
import vueNotVisible from 'vue-not-visible'
55+
56+
Vue.use(vueNotVisible, {ipad: 1280}) // this is custom
57+
58+
```
59+
60+
```html
61+
<template>
62+
<div id="test">
63+
{{ message }} {{ count }}
64+
<div v-not-visible="'ipad'">
65+
<div v-on:click="count = count + 1">Not visible on ipad(screen < 1280)</div>
66+
</div>
67+
</div>
68+
</template>
69+
```
70+
71+
## License
72+
[MIT License](https://github.com/PxyUp/vue-not-visible/blob/master/LICENSE)

dist/vue-not-visible.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "vue-not-visible",
3+
"version": "1.0.0",
4+
"description": "Vue directive for removing from dom (like v-if) element on screen smaller then breakpoints;",
5+
"main": "/src/index.js",
6+
"repository": "[email protected]:PxyUp/vue-not-visible.git",
7+
"author": "Yura Panarin <[email protected]>",
8+
"license": "MIT",
9+
"scripts": {
10+
"build": "webpack",
11+
"precommit": "yarn build && git add --all"
12+
},
13+
"devDependencies": {
14+
"clean-webpack-plugin": "^0.1.19",
15+
"git-pre-commit": "^2.1.4",
16+
"vue": "^2.5.17",
17+
"webpack": "^4.16.5",
18+
"webpack-bundle-analyzer": "^2.13.1",
19+
"webpack-cli": "^3.1.0"
20+
},
21+
"engines": {
22+
"node": ">=6"
23+
}
24+
}

src/index.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
const BREAKPOINTS = {
3+
mobile: 425,
4+
tablet: 768,
5+
tablet_landscape: 1024,
6+
desktop: 1200,
7+
desktop_large: 1440,
8+
hd: 1920,
9+
}
10+
11+
const plugin = {
12+
install(Vue, options) {
13+
const _breakpoints = options || BREAKPOINTS
14+
const eventBus$ = new Vue();
15+
window.addEventListener('resize', () => {
16+
eventBus$.$emit('$vueNotVisible', window.innerWidth)
17+
})
18+
Vue.directive('not-visible', {
19+
bind(el, binding) {
20+
let removeChild = document.createComment(' ');
21+
if (!_breakpoints[binding.value]) {
22+
throw new Error(`Missing breakpoint for ${binding.value}`)
23+
}
24+
eventBus$.$on('$vueNotVisible', value => {
25+
if (_breakpoints[binding.value] > value) {
26+
if (el.parentNode) {
27+
el.parentNode.replaceChild(removeChild, el)
28+
}
29+
} else {
30+
if (removeChild.parentNode) {
31+
removeChild.parentNode.replaceChild(el, removeChild)
32+
}
33+
}
34+
})
35+
},
36+
inserted() {
37+
window.dispatchEvent(new Event('resize'));
38+
}
39+
})
40+
}
41+
}
42+
43+
export default plugin

webpack.config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const CleanWebpackPlugin = require('clean-webpack-plugin');
2+
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
3+
4+
module.exports = {
5+
entry: "./src/index.js",
6+
output: {
7+
libraryTarget: "umd",
8+
library: "vueNotVisible",
9+
filename: "vue-not-visible.js"
10+
},
11+
resolve: {
12+
extensions: [".js"],
13+
},
14+
target: 'web',
15+
mode: "production",
16+
plugins: [
17+
new CleanWebpackPlugin(['./dist'])
18+
],
19+
};

0 commit comments

Comments
 (0)