Skip to content

Commit a1cc627

Browse files
authored
demo: migrate wave demo from ConfigProvider to Button (ant-design#56303)
* demo: migrate wave demo from ConfigProvider to Button * update * update
1 parent b0a52d7 commit a1cc627

7 files changed

Lines changed: 241 additions & 11 deletions

File tree

components/button/__tests__/__snapshots__/demo-extend.test.ts.snap

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4411,3 +4411,52 @@ exports[`renders components/button/demo/style-class.tsx extend context correctly
44114411
`;
44124412

44134413
exports[`renders components/button/demo/style-class.tsx extend context correctly 2`] = `[]`;
4414+
4415+
exports[`renders components/button/demo/wave.tsx extend context correctly 1`] = `
4416+
<div
4417+
class="ant-flex css-var-test-id ant-flex-wrap-wrap ant-flex-gap-large"
4418+
>
4419+
<button
4420+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
4421+
type="button"
4422+
>
4423+
<span>
4424+
Disabled
4425+
</span>
4426+
</button>
4427+
<button
4428+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
4429+
type="button"
4430+
>
4431+
<span>
4432+
Default
4433+
</span>
4434+
</button>
4435+
<button
4436+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
4437+
type="button"
4438+
>
4439+
<span>
4440+
Inset
4441+
</span>
4442+
</button>
4443+
<button
4444+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
4445+
type="button"
4446+
>
4447+
<span>
4448+
Shake
4449+
</span>
4450+
</button>
4451+
<button
4452+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
4453+
type="button"
4454+
>
4455+
<span>
4456+
Happy Work
4457+
</span>
4458+
</button>
4459+
</div>
4460+
`;
4461+
4462+
exports[`renders components/button/demo/wave.tsx extend context correctly 2`] = `[]`;

components/button/__tests__/__snapshots__/demo.test.ts.snap

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3880,3 +3880,50 @@ exports[`renders components/button/demo/style-class.tsx correctly 1`] = `
38803880
</button>
38813881
</div>
38823882
`;
3883+
3884+
exports[`renders components/button/demo/wave.tsx correctly 1`] = `
3885+
<div
3886+
class="ant-flex css-var-test-id ant-flex-wrap-wrap ant-flex-gap-large"
3887+
>
3888+
<button
3889+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
3890+
type="button"
3891+
>
3892+
<span>
3893+
Disabled
3894+
</span>
3895+
</button>
3896+
<button
3897+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
3898+
type="button"
3899+
>
3900+
<span>
3901+
Default
3902+
</span>
3903+
</button>
3904+
<button
3905+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
3906+
type="button"
3907+
>
3908+
<span>
3909+
Inset
3910+
</span>
3911+
</button>
3912+
<button
3913+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
3914+
type="button"
3915+
>
3916+
<span>
3917+
Shake
3918+
</span>
3919+
</button>
3920+
<button
3921+
class="ant-btn css-var-test-id ant-btn-primary ant-btn-color-primary ant-btn-variant-solid"
3922+
type="button"
3923+
>
3924+
<span>
3925+
Happy Work
3926+
</span>
3927+
</button>
3928+
</div>
3929+
`;

components/button/demo/wave.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## zh-CN
2+
3+
波纹效果带来了灵动性,你也可以使用 [`@ant-design/happy-work-theme`](https://github.com/ant-design/happy-work-theme) 提供的 HappyProvider 实现动态波纹效果。
4+
5+
## en-US
6+
7+
Wave effect brings dynamic. You can also use HappyProvider from [`@ant-design/happy-work-theme`](https://github.com/ant-design/happy-work-theme) to implement dynamic wave effect.

components/button/demo/wave.tsx

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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;

components/button/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ And 4 other properties additionally.
5252
<code src="./demo/chinese-chars-loading.tsx" debug>Loading style bug</code>
5353
<code src="./demo/component-token.tsx" debug>Component Token</code>
5454
<code src="./demo/linear-gradient.tsx">Gradient Button</code>
55+
<code src="./demo/wave.tsx">Custom Wave</code>
5556
<code src="./demo/custom-disabled-bg.tsx">Custom disabled backgroundColor</code>
5657
<code src="./demo/style-class.tsx" version="6.0.0">Custom semantic dom styling</code>
5758

components/button/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ group:
5353
<code src="./demo/chinese-chars-loading.tsx" debug>加载中状态 bug 还原</code>
5454
<code src="./demo/component-token.tsx" debug>组件 Token</code>
5555
<code src="./demo/linear-gradient.tsx">渐变按钮</code>
56+
<code src="./demo/wave.tsx">自定义按钮波纹</code>
5657
<code src="./demo/chinese-space.tsx" version="5.17.0">移除两个汉字之间的空格</code>
5758
<code src="./demo/custom-disabled-bg.tsx">自定义禁用样式背景</code>
5859
<code src="./demo/style-class.tsx" version="6.0.0">自定义语义结构的样式和类</code>

components/config-provider/demo/wave.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { HappyProvider } from '@ant-design/happy-work-theme';
3-
import { Button, ConfigProvider, Space } from 'antd';
3+
import { Button, ConfigProvider, Flex } from 'antd';
44
import type { ConfigProviderProps, GetProp } from 'antd';
55

66
type WaveConfig = GetProp<ConfigProviderProps, 'wave'>;
@@ -32,10 +32,9 @@ const createDot = (holder: HTMLElement, color: string, left: number, top: number
3232
dot.style.height = `${size}px`;
3333
dot.style.borderRadius = '50%';
3434
dot.style.background = color;
35-
dot.style.transform = 'translate(-50%, -50%)';
35+
dot.style.transform = 'translate3d(-50%, -50%, 0)';
3636
dot.style.transition = 'all 1s ease-out';
3737
holder.appendChild(dot);
38-
3938
return dot;
4039
};
4140

@@ -77,15 +76,15 @@ const showShakeEffect: WaveConfig['showEffect'] = (node, { component }) => {
7776

7877
let steps = 0;
7978

80-
function loop() {
79+
const loop = () => {
8180
cancelAnimationFrame((node as any).effectTimeout);
8281

8382
(node as any).effectTimeout = requestAnimationFrame(() => {
8483
const currentStep = Math.floor(steps / itv);
8584
const current = seq[currentStep];
8685
const next = seq[currentStep + 1];
8786

88-
if (!next) {
87+
if (next === undefined || next === null) {
8988
node.style.transform = '';
9089
node.style.transition = '';
9190
return;
@@ -100,28 +99,28 @@ const showShakeEffect: WaveConfig['showEffect'] = (node, { component }) => {
10099
steps += 1;
101100
loop();
102101
});
103-
}
102+
};
104103

105104
loop();
106105
};
107106

108107
// Component
109-
const Wrapper = ({ name, ...wave }: WaveConfig & { name: string }) => (
108+
const Wrapper: React.FC<WaveConfig & { name: string }> = ({ name, ...wave }) => (
110109
<ConfigProvider wave={wave}>
111110
<Button type="primary">{name}</Button>
112111
</ConfigProvider>
113112
);
114113

115-
const App = () => (
116-
<Space style={{ padding: 24 }} size="large">
114+
const Demo: React.FC = () => (
115+
<Flex gap="large" wrap>
117116
<Wrapper name="Disabled" disabled />
118117
<Wrapper name="Default" />
119118
<Wrapper name="Inset" showEffect={showInsetEffect} />
120119
<Wrapper name="Shake" showEffect={showShakeEffect} />
121120
<HappyProvider>
122121
<Button type="primary">Happy Work</Button>
123122
</HappyProvider>
124-
</Space>
123+
</Flex>
125124
);
126125

127-
export default App;
126+
export default Demo;

0 commit comments

Comments
 (0)