Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

v-ripple on router-link: mdc classes get lost #591

@meibegger

Description

@meibegger

Describe the bug
When adding v-ripple to router-link, the ripple only works once, because the mdc- classes vanish on route-change

Desktop:

  • OS: Mac
  • Browser Current Firefox & Chrome

Possible fix
I fixed this by using a MutationObserver and re-adding the previous classes.

interface MDCRippledElement extends HTMLElement {
  mdcRipple: MDCRipple
}

const mutationConfig: MutationObserverInit = {
  attributeFilter: ['class'],
  attributes: true,
  attributeOldValue: true,
}
const observer = new MutationObserver(mutationCallback)

function mdcClasses(className: string | null) {
  return className ? className.match(/mdc-[a-z-]+/g) || [] : []
}

function mutationCallback(mutationList: MutationRecord[]) {
  mutationList.forEach(({ target: el, oldValue }) => {
    const target = el as MDCRippledElement
    // re-add the lost mdc-classes
    const currentMDCClasses = mdcClasses(target.className)
    if (!currentMDCClasses.length) {
      const oldMDCClasses = mdcClasses(oldValue)
      target.classList.add(...oldMDCClasses)
    }
  })
}
...
bind() {
  observer.observe(el, mutationConfig)
}

(this will not work, if the customized modifier is used))

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions