Skip to content

fix(collapse/toggle): persist toggle state on element and prevent multiple state emits (closes #2923) #2924

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 5 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions src/components/collapse/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ export default {
this.setWindowEvents(true)
}
},
updated() {
this.$root.$emit(EVENT_STATE, this.id, this.show)
},
beforeDestroy() /* istanbul ignore next */ {
// Trigger state emit if needed
this.show = false
if (this.isNav && inBrowser) {
this.setWindowEvents(false)
}
Expand Down
38 changes: 33 additions & 5 deletions src/directives/toggle/toggle.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import target from '../../utils/target'
import { setAttr, addClass, removeClass } from '../../utils/dom'

// Are we client side?
const inBrowser = typeof window !== 'undefined'
import { setAttr, removeAttr, addClass, removeClass } from '../../utils/dom'
import { inBrowser } from '../../utils/env'

// target listen types
const listenTypes = { click: true }

// Property key for handler storage
const BVT = '__BV_toggle__'
const BVT_STATE = '__BV_toggle_STATE__'
const BVT_CONTROLS = '__BV_toggle_CONTROLS__'

// Emitted Control Event for collapse (emitted to collapse)
const EVENT_TOGGLE = 'bv::toggle::collapse'

// Listen to Event for toggle state update (Emited by collapse)
const EVENT_STATE = 'bv::collapse::state'

/* istanbul ignore next */
const handleUpdate = (el, binding, vnode) => {
// Ensure the collapse class and aria-* attributes persist
// after element is updated (eitehr by parent re-rendering
// or changes to this element or it's contents.
if (inBrowser) {
if (el[BVT_STATE] === true) {
addClass(el, 'collapsed')
setAttr(el, 'aria-expanded', 'true')
} else if (el[BVT_STATE] === false) {
removeClass(el, 'collapsed')
setAttr(el, 'aria-expanded', 'false')
}
setAttr(el, 'aria-controls', el[BVT_CONTROLS])
}
}

export default {
bind(el, binding, vnode) {
const targets = target(vnode, binding, listenTypes, ({ targets, vnode }) => {
Expand All @@ -26,7 +43,10 @@ export default {

if (inBrowser && vnode.context && targets.length > 0) {
// Add aria attributes to element
setAttr(el, 'aria-controls', targets.join(' '))
el[BVT_CONTROLS] = targets.join(' ')
// state is initialy collapsed until we receive a state event
el[BVT_STATE] = false
setAttr(el, 'aria-controls', el[BVT_CONTROLS])
setAttr(el, 'aria-expanded', 'false')
if (el.tagName !== 'BUTTON') {
// If element is not a button, we add `role="button"` for accessibility
Expand All @@ -39,6 +59,7 @@ export default {
// Set aria-expanded state
setAttr(el, 'aria-expanded', state ? 'true' : 'false')
// Set/Clear 'collapsed' class state
el[BVT_STATE] = state
if (state) {
removeClass(el, 'collapsed')
} else {
Expand All @@ -51,11 +72,18 @@ export default {
vnode.context.$root.$on(EVENT_STATE, el[BVT])
}
},
componentUpdated: handleUpdate,
updated: handleUpdate,
unbind(el, binding, vnode) /* istanbul ignore next */ {
if (el[BVT]) {
// Remove our $root listener
vnode.context.$root.$off(EVENT_STATE, el[BVT])
el[BVT] = null
el[BVT_STATE] = null
el[BVT_CONTROLS] = null
removeClass(el, 'collapsed')
removeAttr(el, 'aria-expanded')
removeAttr(el, 'aria-controls')
}
}
}