Skip to content

Commit

Permalink
fix(v-b-modal): only unbind/rebind if trigger element or modal ID cha…
Browse files Browse the repository at this point in the history
…nges (closes #4669) (#4672)
  • Loading branch information
tmorehouse authored Jan 24, 2020
1 parent 3a3ee1d commit e53a05d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/directives/modal/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { keys } from '../../utils/object'
const EVENT_SHOW = 'bv::show::modal'

// Prop name we use to store info on root element
const HANDLER = '__bv_modal_directive__'
const PROPERTY = '__bv_modal_directive__'

const EVENT_OPTS = { passive: true }

Expand Down Expand Up @@ -64,7 +64,7 @@ const bind = (el, binding, vnode) => {
}
}
}
el[HANDLER] = handler
el[PROPERTY] = { handler, target, trigger }
// If element is not a button, we add `role="button"` for accessibility
setRole(trigger)
// Listen for click events
Expand All @@ -78,19 +78,30 @@ const bind = (el, binding, vnode) => {
}

const unbind = el => {
const trigger = getTriggerElement(el)
const handler = el ? el[HANDLER] : null
const oldProp = el[PROPERTY] || {}
const trigger = oldProp.trigger
const handler = oldProp.handler
if (trigger && handler) {
eventOff(trigger, 'click', handler, EVENT_OPTS)
eventOff(trigger, 'keydown', handler, EVENT_OPTS)
eventOff(el, 'click', handler, EVENT_OPTS)
eventOff(el, 'keydown', handler, EVENT_OPTS)
}
delete el[HANDLER]
delete el[PROPERTY]
}

const componentUpdated = (el, binding, vnode) => {
// We bind and rebind just in case target changes
unbind(el, binding, vnode)
bind(el, binding, vnode)
const oldProp = el[PROPERTY] || {}
const target = getTarget(binding)
const trigger = getTriggerElement(el)
if (target !== oldProp.target || trigger !== oldProp.trigger) {
// We bind and rebind if the target or trigger changes
unbind(el, binding, vnode)
bind(el, binding, vnode)
}
// If trigger element is not a button, ensure `role="button"`
// is still set for accessibility
setRole(trigger)
}

const updated = () => {}
Expand Down

0 comments on commit e53a05d

Please sign in to comment.