Last active
October 18, 2018 11:24
-
-
Save leevigraham/0bfc1a5740f9cd528cb1fcfb97635f4b to your computer and use it in GitHub Desktop.
Revisions
-
leevigraham revised this gist
Apr 16, 2018 . 2 changed files with 19 additions and 32 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,16 @@ <flyout v-cloak> <div slot-scope="props"> <button v-on:click="props.toggle()" v-bind:class="'border-2 p-1 ' + (props.isActive ? 'bg-green': 'bg-red')" data-reference >…</button> <div v-show="props.isActive" class="list-reset bg-white border p-4 shadow z-10" data-popper > Dropdown Content </div> </div> </flyout> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,11 @@ <script> import Popper from 'popper.js' export default { name: 'flyout', data: function () { return { isActive: false, popperOptions: { placement: 'bottom-start', @@ -29,36 +22,26 @@ } } }, render () { return this.$scopedSlots.default({ isActive: this.isActive, toggle: this.toggle }) }, created () {}, destroyed () { this.destroyPopper() }, mounted () { this.referenceElement = this.$el.querySelectorAll('[data-reference]')[0] this.popperElement = this.$el.querySelectorAll('[data-popper]')[0] this.popper = null }, watch: { isActive: function (isActive) { (isActive) ? this.createPopper() : this.destroyPopper() } }, methods: { toggle () { this.isActive = !this.isActive -
leevigraham created this gist
Apr 16, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ <flyout v-cloak> <button slot="reference" slot-scope="props" v-on:click="props.toggle()" v-bind:class="(props.isActive ? 'bg-green': 'bg-red')">…</button> <div slot-scope="props" v-show="props.isActive" class="list-reset bg-white border p-4 shadow z-10" > Drop Down Content </ol> </flyout> This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ <script> # Renderless Flyout component # See: https://adamwathan.me/renderless-components-in-vuejs/ import Popper from 'popper.js' export default { name: 'flyout', data: function () { return { isActive: false, referenceElement: null, popperElement: null, popper: null, popperOptions: { placement: 'bottom-start', modifiers: { arrow: {enabled: false}, preventOverflow: { boundariesElement: 'viewport' }, flip: { behavior: ['bottom-start', 'bottom-end', 'top-start', 'top-end'], boundariesElement: 'viewport' } } } } }, render (createElement) { this.referenceVnode = this.$scopedSlots.reference({ isActive: this.isActive, toggle: this.toggle, }) this.popperVnode = this.$scopedSlots.default({ isActive: this.isActive, toggle: this.toggle }) return createElement('div', [this.referenceVnode, this.popperVnode]) }, created () { this.referenceVnode = null this.popperVnode = null }, destroyed () { this.destroyPopper() }, mounted () { this.referenceElement = this.referenceVnode.elm this.popperElement = this.popperVnode.elm }, watch: { isActive: function (isActive) { (isActive) ? this.createPopper() : this.destroyPopper() } }, computed: {}, methods: { toggle () { this.isActive = !this.isActive }, createPopper () { if (this.popper) { return } this.popper = new Popper( this.referenceElement, this.popperElement, this.popperOptions ) document.addEventListener('click', this.handleDocumentClick) }, destroyPopper () { if (!this.popper) { return } this.popper.destroy() this.popper = null document.removeEventListener('click', this.handleDocumentClick) }, handleDocumentClick (e) { let target = e.target if ( this.referenceElement === target || this.popperElement === target || this.referenceElement.contains(target) || this.popperElement.contains(target) ) { return } this.isActive = false } } } </script>