Skip to content

Commit

Permalink
fi(v-b-toggle): don't check for evt.defaultPrevened (closes #5391) (#…
Browse files Browse the repository at this point in the history
…5396)

Co-authored-by: Jacob Müller
  • Loading branch information
tmorehouse committed May 18, 2020
1 parent b701d73 commit a1543b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
11 changes: 2 additions & 9 deletions src/directives/toggle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,8 @@ trigger element when the target component is closed, and removed when open. As o
## Preventing the target from opening or closing

To prevent the trigger element from toggling the target, set the `disabled` prop on `<button>`,
`<b-button>`, or `<b-link>` and the toggle event will _not_ dispatched to the target(s).

`v-b-toggle` also checks if the `click` event (and `keydown` event for non-button/links) was
canceled (i.e. via `event.preventDefault()` or `@click.prevent`), and if so, it will _not_ dispatch
the toggle event to the target(s).

Because of this, avoid placing `v-b-toggle` on a `<b-button>` or `<b-link>` that has the `href` prop
set to `'#'`, as these components (or components based on them) call `event.preventDefault()` to
stop the browser from scrolling to the top of the page.
`<b-button>`, or `<b-link>` (or components based on from `<b-link>`) and the toggle event will _not_
dispatched to the target(s).

## Accessibility

Expand Down
10 changes: 6 additions & 4 deletions src/directives/toggle/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const EVENT_STATE_SYNC = 'bv::collapse::sync::state'
// Private event we send to collapse to request state update sync event
export const EVENT_STATE_REQUEST = 'bv::request::collapse::state'

const keyDownEvents = [ENTER, SPACE]
const KEYDOWN_KEY_CODES = [ENTER, SPACE]

const RX_SPLIT_SEPARATOR = /\s+/

Expand Down Expand Up @@ -86,9 +86,11 @@ const addClickListener = (el, vnode) => {
removeClickListener(el)
if (vnode.context) {
const handler = evt => {
const targets = el[BV_TOGGLE_TARGETS] || []
const ignore = evt.type === 'keydown' && !arrayIncludes(keyDownEvents, evt.keyCode)
if (!evt.defaultPrevented && !ignore && !isDisabled(el)) {
if (
!(evt.type === 'keydown' && !arrayIncludes(KEYDOWN_KEY_CODES, evt.keyCode)) &&
!isDisabled(el)
) {
const targets = el[BV_TOGGLE_TARGETS] || []
targets.forEach(target => {
vnode.context.$root.$emit(EVENT_TOGGLE, target)
})
Expand Down

0 comments on commit a1543b2

Please sign in to comment.