import {onBeforeUnmount, watch} from '@peryl/react-compose'; import './click-wave.scss'; import {delay} from "@peryl/utils/delay"; import {ThemeSize} from "../../uses/useStyle"; export interface ClickWaveOptions { disabled?: boolean, size?: () => typeof ThemeSize.TYPE, } function createClickWaveManager(el: HTMLElement, o?: ClickWaveOptions | string) { let option: ClickWaveOptions; let wavingTimer = null as null | number; const onClick = () => { // console.log(this.option,this.option.disabled === true) if (option.disabled) return; if (!!wavingTimer) return; el.setAttribute('plain-click-node-waving', 'active'); wavingTimer = setTimeout(() => { el.removeAttribute('plain-click-node-waving'); wavingTimer = null; }, 500) as any as number; }; const updateOption = (o?: ClickWaveOptions | string) => { o = o || {}; if (typeof o === "string") { o = { size: o as any }; } option = o; }; updateOption(o); el.setAttribute(`plain-click-node-${option!.size?.() || 'normal'}`, 'active'); el.addEventListener('mousedown', onClick, true); return { updateOption, destroy: () => {el.removeEventListener('click', onClick, true);}, }; } type WaveManager = ReturnType /** * 代替 plain-ui for Vue3.0 中 v-click-wave 指令 * @author 韦胜健 * @date 2021/3/16 11:03 */ export function useClickWave( { elGetter, optionsGetter, }: { elGetter: () => HTMLElement | undefined | null, optionsGetter?: () => string | ClickWaveOptions, }) { const state = { waveManager: null as null | WaveManager, }; watch(elGetter, async el => { await delay(0); if (!!state.waveManager) {state.waveManager.destroy();} state.waveManager = null; !!el && (state.waveManager = createClickWaveManager(el, !!optionsGetter ? optionsGetter() : undefined)); }, { immediate: true }); !!optionsGetter && watch(optionsGetter, opt => { !!state.waveManager && state.waveManager.updateOption(opt); }); onBeforeUnmount(() => { !!state.waveManager && state.waveManager.destroy(); }); }