Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/privatenumber/vue-pseudo-window

πŸ–Ό Declaratively interface window/document/body in your Vue template
https://github.com/privatenumber/vue-pseudo-window

declarative document event-handling event-listeners html vue window

Last synced: 19 days ago
JSON representation

πŸ–Ό Declaratively interface window/document/body in your Vue template

Awesome Lists containing this project

README

        

# πŸ–Ό Pseudo Window

Vue component to bind event-handlers or classes to `window`/`document`/`body`!

Insert pseudo-window anywhere in your template:
```html

```

πŸ‘‰ Try out a [demo in this CodePen](https://codepen.io/privatenumber/pen/qBqEOGa)!

## πŸš€ Install
```sh
npm i vue-pseudo-window
```
## πŸ™‹β€β™‚οΈ Why?

- ✨ **Cleaner code** No longer pollute your component with `.addEventListener()` & `.removeEventListener()`
- ♻️ **Template API** Use Vue's `@event` syntax to bind listeners to the window as like you would to any other element
- πŸ’ͺ **Robust** Supports all event modifiers `capture`, `passive`, and `once`. SSR friendly.
- πŸ₯ **Tiny** 819 B Gzipped!

### Before
```html

...

export default {

// Your component would be polluted with event binding logic
mounted() {
window.addEventListener('resize', this.handleResize, { passive: true })
},

beforeDestroy() {
window.removeEventListener('resize', this.handleResize)
},

methods: {
handleResize() {
...
}
}
}

```

### After ✨

```html


...



export default {

// Much cleaner!
methods: {
handleResize() {
...
}
}
}

```

## πŸ‘¨β€πŸ« Demos [![JSFiddle Demo](https://flat.badgen.net/badge/JSFiddle/Open%20Demo/blue)](https://jsfiddle.net/hirokiosame/p5Lz419s/)

Adding listeners to window

```html



Window width: {{ winWidth }}


@resize.passive="onResize"
/>

import PseudoWindow from 'vue-pseudo-window';

export default {
components: {
PseudoWindow
},

data() {
return {
winWidth: 0
}
},

methods: {
onResize() {
this.winWidth = window.innerWidth;
}
}
}

```

Adding class & listeners to document <html>

```html


-->
:class="$style.lockScroll"


@click="onClick"
/>

import PseudoWindow from 'vue-pseudo-window';

export default {
components: {
PseudoWindow
},

methods: {
onClick() {
console.log('Document click!')
}
}
}

.lockScroll {
overflow: hidden;
}

```

Adding class & listeners to body <body>

```html


-->
:class="$style.lockScroll"


@click="onClick"
/>

import PseudoWindow from 'vue-pseudo-window';

export default {
components: {
PseudoWindow
},

methods: {
onClick() {
console.log('Body click!')
}
}
}

.lockScroll {
overflow: hidden;
}

```

Only want one root element?



PseudoWindow is a functional component that returns exactly what's passed into it. By using it as the root component, its contents will pass-through.

```html





import PseudoWindow from 'vue-pseudo-window';

export default {
components: {
PseudoWindow
},

methods: {
resume() {
this.$el.play()
},
pause() {
this.$el.pause()
}
}
}

```

## πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ Related
- [vue-subslot](https://github.com/privatenumber/vue-subslot) - πŸ’ Pick 'n choose what you want from a slot passed into your Vue component
- [vue-proxi](https://github.com/privatenumber/vue-proxi) - πŸ’  Tiny proxy component for Vue.js
- [vue-vnode-syringe](https://github.com/privatenumber/vue-vnode-syringe) - 🧬Mutate your vNodes with vNode Syringe πŸ’‰