MethodologySection.tsx
8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { useEffect, useRef, useState } from 'react';
import type { TouchEvent } from 'react';
import type { Methodology, MethodologyStep } from '../types/home';
export interface MethodologySectionProps {
methodology: Methodology;
currentStep: number;
onStepChange: (index: number) => void;
}
export function MethodologySection({
methodology,
currentStep,
onStepChange,
}: MethodologySectionProps) {
const touchStartXRef = useRef<number | null>(null);
const previewTouchStartXRef = useRef<number | null>(null);
const blockPreviewClickUntilRef = useRef(0);
const [isImagePreviewOpen, setIsImagePreviewOpen] = useState(false);
useEffect(() => {
if (!isImagePreviewOpen) {
return;
}
const previousBodyOverflow = document.body.style.overflow;
const previousHtmlOverflow = document.documentElement.style.overflow;
document.body.style.overflow = 'hidden';
document.documentElement.style.overflow = 'hidden';
return () => {
document.body.style.overflow = previousBodyOverflow;
document.documentElement.style.overflow = previousHtmlOverflow;
};
}, [isImagePreviewOpen]);
if (!methodology.display_status || methodology.steps.length === 0) {
return null;
}
const currentStepData: MethodologyStep | undefined = methodology.steps[currentStep];
if (!currentStepData) {
return null;
}
const lastStepIndex = methodology.steps.length - 1;
const getLoopedStepIndex = (direction: 'next' | 'previous') => {
if (direction === 'next') {
return currentStep === lastStepIndex ? 0 : currentStep + 1;
}
return currentStep === 0 ? lastStepIndex : currentStep - 1;
};
const handleTouchStart = (event: TouchEvent<HTMLDivElement>) => {
touchStartXRef.current = event.touches[0]?.clientX ?? null;
};
const handleTouchEnd = (event: TouchEvent<HTMLDivElement>) => {
const startX = touchStartXRef.current;
const endX = event.changedTouches[0]?.clientX;
touchStartXRef.current = null;
if (startX === null || typeof endX !== 'number') {
return;
}
const deltaX = startX - endX;
if (Math.abs(deltaX) < 40) {
return;
}
blockPreviewClickUntilRef.current = Date.now() + 350;
if (deltaX > 0) {
onStepChange(getLoopedStepIndex('next'));
}
if (deltaX < 0) {
onStepChange(getLoopedStepIndex('previous'));
}
};
const handleTabClick = (index: number) => {
onStepChange(index);
};
const handleImageClick = () => {
if (Date.now() < blockPreviewClickUntilRef.current) {
return;
}
setIsImagePreviewOpen(true);
};
const handlePreviewTouchStart = (event: TouchEvent<HTMLDivElement>) => {
previewTouchStartXRef.current = event.touches[0]?.clientX ?? null;
};
const handlePreviewTouchEnd = (event: TouchEvent<HTMLDivElement>) => {
const startX = previewTouchStartXRef.current;
const endX = event.changedTouches[0]?.clientX;
previewTouchStartXRef.current = null;
if (startX === null || typeof endX !== 'number') {
return;
}
const deltaX = startX - endX;
if (Math.abs(deltaX) < 40) {
return;
}
blockPreviewClickUntilRef.current = Date.now() + 350;
if (deltaX > 0) {
onStepChange(getLoopedStepIndex('next'));
}
if (deltaX < 0) {
onStepChange(getLoopedStepIndex('previous'));
}
};
return (
<section className="relative overflow-hidden bg-surface-container-lowest py-20 tablet:py-24" data-testid="methodology-section">
<div className="max-w-container-max mx-auto px-margin-mobile tablet:px-margin-desktop relative z-10">
<div className="mb-12 tablet:mb-14 text-center">
<h2 className="text-headline-md tablet:text-display-sm mb-3">{methodology.main_title}</h2>
<p className="text-body-md tablet:text-body-lg text-on-surface-variant">{methodology.sub_title}</p>
</div>
<div className="flex flex-col">
<div
className="order-1 tablet:order-2 mt-0 mb-10 tablet:mt-8 tablet:mb-0"
data-testid="methodology-tabs-wrapper"
>
<div
data-testid="methodology-tabs"
role="tablist"
aria-label="方法论步骤"
className="flex flex-nowrap tablet:flex-wrap justify-start tablet:justify-center gap-3 overflow-x-auto tablet:overflow-x-visible pb-1"
>
{methodology.steps.map((step, index) => {
const isActive = index === currentStep;
return (
<button
key={step.step_name}
type="button"
role="tab"
aria-selected={isActive}
data-testid={`step-${index}`}
onClick={() => handleTabClick(index)}
className={`inline-flex items-center gap-3 rounded-full px-5 py-3 text-label-lg font-semibold transition-all duration-200 shrink-0 ${
isActive
? 'bg-primary text-white shadow-[0_12px_28px_rgba(18,87,255,0.2)]'
: 'bg-white text-primary border border-outline-variant/30 hover:border-primary/30 hover:bg-primary/5'
}`}
>
<span
className={`inline-flex h-8 w-8 items-center justify-center overflow-hidden rounded-full ${
isActive ? 'bg-white' : 'bg-primary/10'
}`}
>
{step.step_icon ? (
<img
src={step.step_icon}
alt=""
aria-hidden="true"
data-testid={`step-icon-${index}`}
className="h-full w-full rounded-full object-cover"
/>
) : (
<span
data-testid={`step-icon-${index}`}
className="text-sm font-bold leading-none text-primary"
>
{index + 1}
</span>
)}
</span>
<span>{step.step_name}</span>
</button>
);
})}
</div>
</div>
<div
className="order-2 tablet:order-1 mx-auto w-full max-w-[1080px]"
data-testid="methodology-carousel"
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
>
<div className="aspect-video overflow-hidden rounded-[28px] border border-outline-variant/20 bg-[#F4F7FC] shadow-[0_18px_40px_rgba(22,38,74,0.08)]">
<button
type="button"
data-testid="methodology-image-trigger"
onClick={handleImageClick}
className="block h-full w-full tablet:hidden"
aria-label={`放大查看${currentStepData.step_name}`}
>
<img
src={currentStepData.step_image}
alt={currentStepData.step_name}
className="h-full w-full object-contain"
draggable={false}
/>
</button>
<img
src={currentStepData.step_image}
alt={currentStepData.step_name}
className="hidden h-full w-full object-contain tablet:block"
draggable={false}
/>
</div>
</div>
</div>
</div>
{isImagePreviewOpen ? (
<div
data-testid="methodology-image-preview"
className="fixed inset-0 z-50 flex items-center justify-center overflow-hidden bg-black/85 py-8 tablet:hidden"
onClick={() => setIsImagePreviewOpen(false)}
onTouchStart={handlePreviewTouchStart}
onTouchEnd={handlePreviewTouchEnd}
>
<div
className="w-screen max-w-none focus:outline-none"
onClick={(event) => event.stopPropagation()}
>
<img
src={currentStepData.step_image}
alt={`${currentStepData.step_name}大图`}
className="h-auto w-full rounded-[24px] object-contain"
draggable={false}
/>
</div>
</div>
) : null}
</section>
);
}