Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ec4c239
chore: create BVHoverSwap utility helper component
tmorehouse Feb 13, 2020
a9c13cc
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
d56bde4
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
f361034
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
e2e1190
Create bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
bccad09
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
598690a
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
81f51f5
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
7c8069c
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
56d6f6f
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
9f9dd54
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
8f7b552
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
00a5e0d
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
8f1233b
Add missing `@vue/component` comments
jacobmllr95 Feb 13, 2020
f28295e
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
a444d86
Update bv-hover-swap.js
jacobmllr95 Feb 13, 2020
aa1ca05
Merge branch 'hover-swap' of https://github.com/bootstrap-vue/bootstr…
jacobmllr95 Feb 13, 2020
f586926
Merge branch 'hover-swap' of https://github.com/bootstrap-vue/bootstr…
jacobmllr95 Feb 13, 2020
680ead7
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
bb449a1
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
a48e21f
Update bv-hover-swap.js
jacobmllr95 Feb 13, 2020
a18b8e1
Merge branch 'hover-swap' of https://github.com/bootstrap-vue/bootstr…
jacobmllr95 Feb 13, 2020
a265459
Update bv-hover-swap.js
tmorehouse Feb 13, 2020
fdb7f4a
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
b7216db
Update bv-hover-swap.spec.js
tmorehouse Feb 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/utils/bv-hover-swap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Vue from './vue'
import { eventOn, eventOff } from './dom'

// --- Constants ---

const EVENT_OPTIONS = { passive: true }

// @vue/component
export const BVHoverSwap = /*#__PURE__*/ Vue.extend({
name: 'BVHoverSwap',
props: {
tag: {
type: String,
default: 'div'
},
parent: {
type: Boolean,
default: false
}
},
data() {
return {
isHovered: false
}
},
watch: {
parent() {
this.listen(true)
}
},
created() {
// Create non-reactive property
this.$_hoverEl = null
},
mounted() {
this.$nextTick(() => this.listen(true))
},
updated() /* istanbul ignore next */ {
this.$nextTick(() => this.listen(true))
},
beforeDestroy() {
this.listen(false)
this.$_hoverEl = null
},
methods: {
listen(on) {
const el = this.parent ? this.$el.parentElement || this.$el : this.$el
if (on && this.$_hoverEl !== el) {
this.listen(false)
this.$_hoverEl = el
}
const method = on ? eventOn : eventOff
method(this.$_hoverEl, 'mouseenter', this.handleHover, EVENT_OPTIONS)
method(this.$_hoverEl, 'mouseleave', this.handleHover, EVENT_OPTIONS)
},
handleHover(evt) {
this.isHovered = evt.type === 'mouseenter'
}
},
render(h) {
const $scoped = this.$scopedSlots
const $default = $scoped.default || (() => h())
const $hovered = $scoped.hovered || $default
return h(this.tag, [this.isHovered ? $hovered() : $default()])
}
})
99 changes: 99 additions & 0 deletions src/utils/bv-hover-swap.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { mount } from '@vue/test-utils'
import { waitNT } from '../../tests/utils'
import { BVHoverSwap } from './bv-hover-swap'

describe('utils/bv-hoverswap', () => {
it('works', async () => {
const wrapper = mount(BVHoverSwap, {
slots: {
default: '<span>FOO</span>',
hovered: '<span>BAR</span>'
}
})

expect(wrapper.isVueInstance()).toBe(true)
await waitNT(wrapper.vm)
expect(wrapper.is('div')).toBe(true)
expect(wrapper.text()).toBe('FOO')

wrapper.trigger('mouseenter')
await waitNT(wrapper.vm)

expect(wrapper.is('div')).toBe(true)
expect(wrapper.text()).toBe('BAR')

wrapper.trigger('mouseleave')
await waitNT(wrapper.vm)

expect(wrapper.is('div')).toBe(true)
expect(wrapper.text()).toBe('FOO')

wrapper.destroy()
})

it('works when `parent` is true ', async () => {
const app = {
props: {
parent: {
type: Boolean,
defaut: false
}
},
methods: {
foo() {
return this.$createElement('span', {}, 'FOO')
},
bar() {
return this.$createElement('span', {}, 'BAR')
}
},
render(h) {
const $content = h(BVHoverSwap, {
props: { parent: this.parent },
scopedSlots: { default: this.foo, hovered: this.bar }
})
return h('div', {}, [$content])
}
}
const wrapper = mount(app, {
propsData: {
parent: true
}
})

expect(wrapper.isVueInstance()).toBe(true)
await waitNT(wrapper.vm)
expect(wrapper.is('div')).toBe(true)
expect(wrapper.find('div > div').exists()).toBe(true)
expect(wrapper.find('div > div').is(BVHoverSwap)).toBe(true)
expect(wrapper.text()).toBe('FOO')

wrapper.trigger('mouseenter')
await waitNT(wrapper.vm)

expect(wrapper.is('div')).toBe(true)
expect(wrapper.text()).toBe('BAR')

wrapper.trigger('mouseleave')
await waitNT(wrapper.vm)

expect(wrapper.text()).toBe('FOO')

wrapper.setProps({
parent: false
})
await waitNT(wrapper.vm)

wrapper.trigger('mouseenter')
await waitNT(wrapper.vm)

expect(wrapper.text()).toBe('FOO')

wrapper.find('div > div').trigger('mouseenter')
await waitNT(wrapper.vm)

expect(wrapper.text()).toBe('BAR')

wrapper.destroy()
})
})
1 change: 1 addition & 0 deletions src/utils/bv-transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const FADE_PROPS = {
leaveActiveClass: 'fade'
}

// @vue/component
export const BVTransition = /*#__PURE__*/ Vue.extend({
name: 'BVTransition',
functional: true,
Expand Down