Skip to content

fix(v-b-modal): only unbind/rebind if trigger element or modal ID changes (closes #4669) #4672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Changes from all commits
Commits
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
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