Browse Source

fix: button wave memory leak

pull/7839/head
tangjinzhou 2 months ago
parent
commit
bb443a05e2
  1. 6
      components/_util/wave/WaveEffect.tsx
  2. 3
      components/_util/wave/index.tsx
  3. 14
      components/_util/wave/useWave.ts

6
components/_util/wave/WaveEffect.tsx

@ -159,6 +159,12 @@ function showWaveEffect(node: HTMLElement, className: string) {
node?.insertBefore(holder, node?.firstChild);
render(<WaveEffect target={node} className={className} />, holder);
return () => {
render(null, holder);
if (holder.parentElement) {
holder.parentElement.removeChild(holder);
}
};
}
export default showWaveEffect;

3
components/_util/wave/index.tsx

@ -33,13 +33,12 @@ export default defineComponent({
// =============================== Wave ===============================
const showWave = useWave(
instance,
computed(() => classNames(prefixCls.value, hashId.value)),
wave,
);
let onClick: (e: MouseEvent) => void;
const clear = () => {
const node = findDOMNode(instance);
const node = findDOMNode(instance) as HTMLElement;
node.removeEventListener('click', onClick, true);
};
onMounted(() => {

14
components/_util/wave/useWave.ts

@ -1,21 +1,25 @@
import type { ComponentInternalInstance, ComputedRef, Ref } from 'vue';
import type { ComputedRef, Ref } from 'vue';
import { onBeforeUnmount, getCurrentInstance } from 'vue';
import { findDOMNode } from '../props-util';
import showWaveEffect from './WaveEffect';
export default function useWave(
instance: ComponentInternalInstance | null,
className: Ref<string>,
wave?: ComputedRef<{ disabled?: boolean }>,
): VoidFunction {
const instance = getCurrentInstance();
let stopWave: () => void;
function showWave() {
const node = findDOMNode(instance);
stopWave?.();
if (wave?.value?.disabled || !node) {
return;
}
showWaveEffect(node, className.value);
stopWave = showWaveEffect(node, className.value);
}
onBeforeUnmount(() => {
stopWave?.();
});
return showWave;
}

Loading…
Cancel
Save