|
| 1 | +import React from 'react'; |
| 2 | +import { HappyProvider } from '@ant-design/happy-work-theme'; |
| 3 | +import { Button, ConfigProvider, Flex } from 'antd'; |
| 4 | +import type { ConfigProviderProps, GetProp } from 'antd'; |
| 5 | + |
| 6 | +type WaveConfig = GetProp<ConfigProviderProps, 'wave'>; |
| 7 | + |
| 8 | +// Prepare effect holder |
| 9 | +const createHolder = (node: HTMLElement) => { |
| 10 | + const { borderWidth } = getComputedStyle(node); |
| 11 | + const borderWidthNum = Number.parseInt(borderWidth, 10); |
| 12 | + |
| 13 | + const div = document.createElement('div'); |
| 14 | + div.style.position = 'absolute'; |
| 15 | + div.style.inset = `-${borderWidthNum}px`; |
| 16 | + div.style.borderRadius = 'inherit'; |
| 17 | + div.style.background = 'transparent'; |
| 18 | + div.style.zIndex = '999'; |
| 19 | + div.style.pointerEvents = 'none'; |
| 20 | + div.style.overflow = 'hidden'; |
| 21 | + node.appendChild(div); |
| 22 | + |
| 23 | + return div; |
| 24 | +}; |
| 25 | + |
| 26 | +const createDot = (holder: HTMLElement, color: string, left: number, top: number, size = 0) => { |
| 27 | + const dot = document.createElement('div'); |
| 28 | + dot.style.position = 'absolute'; |
| 29 | + dot.style.left = `${left}px`; |
| 30 | + dot.style.top = `${top}px`; |
| 31 | + dot.style.width = `${size}px`; |
| 32 | + dot.style.height = `${size}px`; |
| 33 | + dot.style.borderRadius = '50%'; |
| 34 | + dot.style.background = color; |
| 35 | + dot.style.transform = 'translate3d(-50%, -50%, 0)'; |
| 36 | + dot.style.transition = 'all 1s ease-out'; |
| 37 | + holder.appendChild(dot); |
| 38 | + return dot; |
| 39 | +}; |
| 40 | + |
| 41 | +// Inset Effect |
| 42 | +const showInsetEffect: WaveConfig['showEffect'] = (node, { event, component }) => { |
| 43 | + if (component !== 'Button') { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const holder = createHolder(node); |
| 48 | + |
| 49 | + const rect = holder.getBoundingClientRect(); |
| 50 | + |
| 51 | + const left = event.clientX - rect.left; |
| 52 | + const top = event.clientY - rect.top; |
| 53 | + |
| 54 | + const dot = createDot(holder, 'rgba(255, 255, 255, 0.65)', left, top); |
| 55 | + |
| 56 | + // Motion |
| 57 | + requestAnimationFrame(() => { |
| 58 | + dot.ontransitionend = () => { |
| 59 | + holder.remove(); |
| 60 | + }; |
| 61 | + |
| 62 | + dot.style.width = '200px'; |
| 63 | + dot.style.height = '200px'; |
| 64 | + dot.style.opacity = '0'; |
| 65 | + }); |
| 66 | +}; |
| 67 | + |
| 68 | +// Shake Effect |
| 69 | +const showShakeEffect: WaveConfig['showEffect'] = (node, { component }) => { |
| 70 | + if (component !== 'Button') { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const seq = [0, -15, 15, -5, 5, 0]; |
| 75 | + const itv = 10; |
| 76 | + |
| 77 | + let steps = 0; |
| 78 | + |
| 79 | + const loop = () => { |
| 80 | + cancelAnimationFrame((node as any).effectTimeout); |
| 81 | + |
| 82 | + (node as any).effectTimeout = requestAnimationFrame(() => { |
| 83 | + const currentStep = Math.floor(steps / itv); |
| 84 | + const current = seq[currentStep]; |
| 85 | + const next = seq[currentStep + 1]; |
| 86 | + |
| 87 | + if (next === undefined || next === null) { |
| 88 | + node.style.transform = ''; |
| 89 | + node.style.transition = ''; |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // Trans from current to next by itv |
| 94 | + const angle = current + ((next - current) / itv) * (steps % itv); |
| 95 | + |
| 96 | + node.style.transform = `rotate(${angle}deg)`; |
| 97 | + node.style.transition = 'none'; |
| 98 | + |
| 99 | + steps += 1; |
| 100 | + loop(); |
| 101 | + }); |
| 102 | + }; |
| 103 | + |
| 104 | + loop(); |
| 105 | +}; |
| 106 | + |
| 107 | +// Component |
| 108 | +const Wrapper: React.FC<WaveConfig & { name: string }> = ({ name, ...wave }) => ( |
| 109 | + <ConfigProvider wave={wave}> |
| 110 | + <Button type="primary">{name}</Button> |
| 111 | + </ConfigProvider> |
| 112 | +); |
| 113 | + |
| 114 | +const Demo: React.FC = () => ( |
| 115 | + <Flex gap="large" wrap> |
| 116 | + <Wrapper name="Disabled" disabled /> |
| 117 | + <Wrapper name="Default" /> |
| 118 | + <Wrapper name="Inset" showEffect={showInsetEffect} /> |
| 119 | + <Wrapper name="Shake" showEffect={showShakeEffect} /> |
| 120 | + <HappyProvider> |
| 121 | + <Button type="primary">Happy Work</Button> |
| 122 | + </HappyProvider> |
| 123 | + </Flex> |
| 124 | +); |
| 125 | + |
| 126 | +export default Demo; |
0 commit comments