@tbox.cn/app-module-mall-core 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +12 -3
  2. package/dist/HomeActivityDetailDialog-2GJFYSMZ.less +178 -0
  3. package/dist/MallHome-PABCNS6D.less +623 -0
  4. package/dist/chunk-B7ZAK6VM.js +724 -0
  5. package/dist/chunk-HMQKNJEO.js +1722 -0
  6. package/dist/client/index.d.ts +22 -0
  7. package/dist/client/index.js +8 -0
  8. package/dist/home-slot-WBZ6ONDP.less +4 -0
  9. package/dist/index-ELJ55M66.less +256 -0
  10. package/dist/index-U5YRUY7P.less +352 -0
  11. package/dist/index.d.ts +6 -1
  12. package/dist/index.js +7 -1
  13. package/dist/member-service-icon-SIGSRKE3.svg +1 -0
  14. package/dist/parking-service-icon-3A2TGUUM.svg +1 -0
  15. package/dist/server/index.d.ts +338 -6
  16. package/dist/server/index.js +3 -1
  17. package/dist/welcome-sparkle-transparent-v07-75RL4QUU.png +0 -0
  18. package/package.json +24 -6
  19. package/src/client/HomeActivityDetailDialog.less +178 -0
  20. package/src/client/HomeActivityDetailDialog.tsx +256 -0
  21. package/src/client/MallHome.less +623 -0
  22. package/src/client/MallHome.tsx +667 -0
  23. package/src/client/assets/home/member-service-icon.svg +1 -0
  24. package/src/client/assets/home/parking-service-icon.svg +1 -0
  25. package/src/client/assets/home/welcome-sparkle-transparent-v07.png +0 -0
  26. package/src/client/assets.d.ts +13 -0
  27. package/src/client/cards/mall-home/index.tsx +32 -0
  28. package/src/client/cards/overrides/BumpInActivityChatCard.tsx +81 -0
  29. package/src/client/components/ActivityDetailPresentation/index.less +352 -0
  30. package/src/client/components/ActivityDetailPresentation/index.tsx +31 -0
  31. package/src/client/components/MemberGateSheet/index.less +256 -0
  32. package/src/client/components/MemberGateSheet/index.tsx +211 -0
  33. package/src/client/components/MemberGateSheet/model.ts +42 -0
  34. package/src/client/home-runtime.ts +493 -0
  35. package/src/client/home-slot.less +4 -0
  36. package/src/client/home-slot.tsx +345 -0
  37. package/src/client/index.ts +32 -0
  38. package/src/client/member-gate.ts +337 -0
  39. package/src/client/mock-direct-gate.ts +45 -0
  40. package/src/index.ts +2 -2
  41. package/src/server/assembly.ts +19 -7
  42. package/src/server/cards/mall-home/meta.ts +61 -0
  43. package/src/server/cards/mall-home/schema.ts +73 -0
  44. package/src/server/home-config.ts +36 -0
  45. package/src/server/index.ts +45 -4
  46. package/src/server/linked-panel-member.ts +65 -0
  47. package/src/server/linked-panel-parking.ts +124 -0
  48. package/src/server/module-ports.ts +67 -0
  49. package/src/server/routes.ts +286 -0
  50. package/src/vite-env.d.ts +1 -0
  51. package/tbox.module.json +15 -5
  52. package/tests/BumpInActivityChatCard.test.tsx +214 -0
  53. package/tests/MallHome.test.tsx +481 -0
  54. package/tests/MemberGateSheet-model.test.ts +65 -0
  55. package/tests/MemberGateSheet.test.tsx +173 -0
  56. package/tests/home-config.test.ts +43 -0
  57. package/tests/home-runtime.test.ts +545 -0
  58. package/tests/home-slot.test.tsx +718 -0
  59. package/tests/linked-panel-member.test.ts +78 -0
  60. package/tests/linked-panel-parking-presence.test.ts +117 -0
  61. package/tests/linked-panel-privacy.test.ts +79 -0
  62. package/tests/linked-panel-scope.test.ts +116 -0
  63. package/tests/mall-home-preview.test.tsx +71 -0
  64. package/tests/member-gate.test.ts +384 -0
  65. package/tests/mock-direct-gate.test.ts +136 -0
  66. package/tests/module-ports.test.ts +18 -0
  67. package/tests/route-paths.test.ts +70 -0
  68. package/tests/usePullDownHistory.test.tsx +191 -0
  69. package/tsconfig.json +1 -0
  70. package/tsup.config.ts +22 -1
  71. package/dist/chunk-WSJ2HA5F.js +0 -267
@@ -0,0 +1,256 @@
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useId,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ type ReactNode,
9
+ type RefObject,
10
+ } from 'react';
11
+ import { createPortal } from 'react-dom';
12
+ import { X } from '@phosphor-icons/react';
13
+ import type { TboxCardPayload } from '@tbox.cn/app-contracts';
14
+ import { ActivityDetailPresentation } from './components/ActivityDetailPresentation';
15
+ import './HomeActivityDetailDialog.less';
16
+
17
+ const FOCUSABLE_SELECTOR = [
18
+ 'button:not([disabled])',
19
+ '[href]',
20
+ 'input:not([disabled])',
21
+ 'select:not([disabled])',
22
+ 'textarea:not([disabled])',
23
+ '[tabindex]:not([tabindex="-1"])',
24
+ ].join(',');
25
+ const MORPH_DURATION_MS = 300;
26
+
27
+ type MorphPhase = 'opening' | 'open' | 'closing';
28
+
29
+ export interface SurfaceRect {
30
+ readonly top: number;
31
+ readonly left: number;
32
+ readonly width: number;
33
+ readonly height: number;
34
+ }
35
+
36
+ type ActivityDetailDialogCard = Pick<TboxCardPayload, 'id' | 'data'>;
37
+ type ActivityDetailMotion = 'morph' | 'fade';
38
+
39
+ export function getHomeActivityDialogId(cardId: string): string {
40
+ return `home-activity-detail-${cardId.replace(/[^a-zA-Z0-9_-]/g, '-')}`;
41
+ }
42
+
43
+ function rectOf(value: DOMRect | DOMRectReadOnly): SurfaceRect {
44
+ return { top: value.top, left: value.left, width: value.width, height: value.height };
45
+ }
46
+
47
+ function targetRect(): SurfaceRect {
48
+ const viewportWidth = window.innerWidth;
49
+ const viewportHeight = window.innerHeight;
50
+ if (viewportWidth <= 600) return { top: 0, left: 0, width: viewportWidth, height: viewportHeight };
51
+ const width = Math.min(620, viewportWidth - 32);
52
+ const height = Math.min(820, viewportHeight - 24);
53
+ return {
54
+ top: (viewportHeight - height) / 2,
55
+ left: (viewportWidth - width) / 2,
56
+ width,
57
+ height,
58
+ };
59
+ }
60
+
61
+ function isVisibleOrigin(rect: SurfaceRect): boolean {
62
+ return rect.width > 0
63
+ && rect.height > 0
64
+ && rect.top < window.innerHeight
65
+ && rect.left < window.innerWidth
66
+ && rect.top + rect.height > 0
67
+ && rect.left + rect.width > 0;
68
+ }
69
+
70
+ function prefersReducedMotion(): boolean {
71
+ return window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
72
+ }
73
+
74
+ export function HomeActivityDetailDialog({
75
+ card,
76
+ origin,
77
+ triggerRef,
78
+ transitionVisual,
79
+ motion = 'morph',
80
+ children,
81
+ onClosed,
82
+ }: {
83
+ readonly card: ActivityDetailDialogCard;
84
+ readonly origin: SurfaceRect;
85
+ readonly triggerRef: RefObject<HTMLElement | null>;
86
+ readonly transitionVisual?: ReactNode;
87
+ readonly motion?: ActivityDetailMotion;
88
+ readonly children: ReactNode;
89
+ readonly onClosed: () => void;
90
+ }) {
91
+ const [phase, setPhase] = useState<MorphPhase>('opening');
92
+ const [destination, setDestination] = useState<SurfaceRect>(() => targetRect());
93
+ const [closingOrigin, setClosingOrigin] = useState<SurfaceRect>(origin);
94
+ const [returnToOrigin, setReturnToOrigin] = useState(true);
95
+ const closeRef = useRef<HTMLButtonElement>(null);
96
+ const panelRef = useRef<HTMLElement>(null);
97
+ const closeTimerRef = useRef<number | null>(null);
98
+ const phaseRef = useRef<MorphPhase>('opening');
99
+ const onClosedRef = useRef(onClosed);
100
+ const titleId = useId();
101
+ const rawTitle = typeof card.data['mainTitle'] === 'string'
102
+ ? card.data['mainTitle']
103
+ : typeof card.data['title'] === 'string' ? card.data['title'] : '';
104
+ const title = rawTitle.trim() || '活动详情';
105
+ onClosedRef.current = onClosed;
106
+
107
+ const finishClose = useCallback(() => {
108
+ if (closeTimerRef.current !== null) window.clearTimeout(closeTimerRef.current);
109
+ closeTimerRef.current = null;
110
+ onClosedRef.current();
111
+ }, []);
112
+
113
+ const requestClose = useCallback(() => {
114
+ if (phaseRef.current === 'closing') return;
115
+ const triggerRect = triggerRef.current?.getBoundingClientRect();
116
+ const nextOrigin = triggerRect ? rectOf(triggerRect) : origin;
117
+ const canReturnToOrigin = motion === 'morph' && isVisibleOrigin(nextOrigin);
118
+ if (canReturnToOrigin) setClosingOrigin(nextOrigin);
119
+ setReturnToOrigin(canReturnToOrigin);
120
+ phaseRef.current = 'closing';
121
+ setPhase('closing');
122
+ closeTimerRef.current = window.setTimeout(
123
+ finishClose,
124
+ prefersReducedMotion() ? 0 : MORPH_DURATION_MS,
125
+ );
126
+ }, [finishClose, motion, origin, triggerRef]);
127
+
128
+ useEffect(() => {
129
+ const frame = window.requestAnimationFrame(() => {
130
+ if (phaseRef.current !== 'opening') return;
131
+ phaseRef.current = 'open';
132
+ setPhase('open');
133
+ });
134
+ return () => window.cancelAnimationFrame(frame);
135
+ }, []);
136
+
137
+ useEffect(() => {
138
+ const updateDestination = () => setDestination(targetRect());
139
+ window.addEventListener('resize', updateDestination);
140
+ return () => window.removeEventListener('resize', updateDestination);
141
+ }, []);
142
+
143
+ useEffect(() => {
144
+ const previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
145
+ const previousBodyOverflow = document.body.style.overflow;
146
+ const appRoot = document.getElementById('root');
147
+ const previousAriaHidden = appRoot?.getAttribute('aria-hidden') ?? null;
148
+ const previousInert = appRoot?.inert ?? false;
149
+ const scrollHost = triggerRef.current?.closest<HTMLElement>('.chat-page__body') ?? null;
150
+ const previousScrollOverflow = scrollHost?.style.overflow ?? '';
151
+ const previousOverscroll = scrollHost?.style.overscrollBehavior ?? '';
152
+
153
+ document.body.style.overflow = 'hidden';
154
+ if (scrollHost) {
155
+ scrollHost.style.overflow = 'hidden';
156
+ scrollHost.style.overscrollBehavior = 'none';
157
+ }
158
+ if (appRoot) {
159
+ appRoot.setAttribute('aria-hidden', 'true');
160
+ appRoot.inert = true;
161
+ }
162
+ closeRef.current?.focus();
163
+
164
+ const handleKeyDown = (event: KeyboardEvent) => {
165
+ if (event.key === 'Escape') {
166
+ event.preventDefault();
167
+ requestClose();
168
+ return;
169
+ }
170
+ if (event.key !== 'Tab') return;
171
+ const focusable = Array.from(
172
+ panelRef.current?.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR) ?? [],
173
+ );
174
+ if (focusable.length === 0) {
175
+ event.preventDefault();
176
+ panelRef.current?.focus();
177
+ return;
178
+ }
179
+ const first = focusable[0];
180
+ const last = focusable[focusable.length - 1];
181
+ if (event.shiftKey && document.activeElement === first) {
182
+ event.preventDefault();
183
+ last.focus();
184
+ } else if (!event.shiftKey && document.activeElement === last) {
185
+ event.preventDefault();
186
+ first.focus();
187
+ }
188
+ };
189
+ document.addEventListener('keydown', handleKeyDown);
190
+
191
+ return () => {
192
+ document.removeEventListener('keydown', handleKeyDown);
193
+ if (closeTimerRef.current !== null) window.clearTimeout(closeTimerRef.current);
194
+ document.body.style.overflow = previousBodyOverflow;
195
+ if (scrollHost) {
196
+ scrollHost.style.overflow = previousScrollOverflow;
197
+ scrollHost.style.overscrollBehavior = previousOverscroll;
198
+ }
199
+ if (appRoot) {
200
+ appRoot.inert = previousInert;
201
+ if (previousAriaHidden === null) appRoot.removeAttribute('aria-hidden');
202
+ else appRoot.setAttribute('aria-hidden', previousAriaHidden);
203
+ }
204
+ (triggerRef.current ?? previousFocus)?.focus({ preventScroll: true });
205
+ };
206
+ }, [requestClose, triggerRef]);
207
+
208
+ const surfaceRect = motion === 'fade'
209
+ ? destination
210
+ : phase === 'opening'
211
+ ? origin
212
+ : phase === 'closing'
213
+ ? returnToOrigin ? closingOrigin : destination
214
+ : destination;
215
+ const surfaceStyle = useMemo(() => ({
216
+ top: `${surfaceRect.top}px`,
217
+ left: `${surfaceRect.left}px`,
218
+ width: `${surfaceRect.width}px`,
219
+ height: `${surfaceRect.height}px`,
220
+ }), [surfaceRect]);
221
+
222
+ return createPortal(
223
+ <div
224
+ className="home-activity-detail"
225
+ data-motion={motion}
226
+ data-phase={phase}
227
+ data-return-origin={returnToOrigin ? 'true' : 'false'}
228
+ >
229
+ <button type="button" className="home-activity-detail__mask" aria-label="关闭活动详情" onClick={requestClose} />
230
+ <section
231
+ ref={panelRef}
232
+ id={getHomeActivityDialogId(card.id)}
233
+ className="home-activity-detail__surface"
234
+ style={surfaceStyle}
235
+ role="dialog"
236
+ aria-modal="true"
237
+ aria-labelledby={titleId}
238
+ tabIndex={-1}
239
+ >
240
+ <h2 id={titleId} className="home-activity-detail__sr-title">{title}</h2>
241
+ {transitionVisual && (
242
+ <div className="home-activity-detail__transition-visual" aria-hidden="true">{transitionVisual}</div>
243
+ )}
244
+ <div className="home-activity-detail__content">
245
+ <ActivityDetailPresentation variant="page">
246
+ {children}
247
+ </ActivityDetailPresentation>
248
+ </div>
249
+ <button ref={closeRef} type="button" className="home-activity-detail__close" aria-label="关闭活动详情,返回首页" onClick={requestClose}>
250
+ <X size={21} weight="bold" aria-hidden="true" />
251
+ </button>
252
+ </section>
253
+ </div>,
254
+ document.body,
255
+ );
256
+ }