'use client' import { useRef, useState } from 'react' import type { ClassName } from 'rntwsc/tw/class-name' import { clsx } from 'rntwsc/tw/clsx' import { isClickDOM, useParentDOM } from 'rntwsc/utils/dom' import { ulid } from 'rntwsc/libs/ulidx' export type RippleProps = { className?: ClassName } export type RippleData = { id: string x: number y: number size: number } export const Ripple = ({ className }: RippleProps) => { const timeoutsRef = useRef([]) const [rippleData, setRippleData] = useState([]) const mousedownAnchor = useParentDOM(dom => { const listener = (e: MouseEvent) => { if (!isClickDOM(e, dom)) { return } const rect = dom.getBoundingClientRect() const x = e.clientX - rect.left const y = e.clientY - rect.top const dx = Math.max(x, rect.width - x) const dy = Math.max(y, rect.height - y) const size = Math.ceil(Math.sqrt(dx * dx + dy * dy) * 2) const id = ulid() setRippleData(prev => [ ...prev, { id, x, y, size, }, ]) const t = window.setTimeout(() => { setRippleData(prev => prev.filter(r => r.id !== id)) }, 1000 + 17) timeoutsRef.current.push(t) } dom.addEventListener('mousedown', listener) return () => { dom.removeEventListener('mousedown', listener) for (const t of timeoutsRef.current) { window.clearTimeout(t) } timeoutsRef.current = [] } }) const classNameString = clsx('ripple', className) as string const ripples = rippleData.map(r => ( )) return ( <> {mousedownAnchor} {ripples} ) }