react-native-live-widgets 1.2.0 → 1.2.2

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 (87) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +129 -50
  3. package/android/src/main/AndroidManifest.xml +12 -0
  4. package/android/src/main/assets/fonts/MaterialIcons-Regular.ttf +0 -0
  5. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +61 -1
  6. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +153 -17
  7. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +1 -1
  8. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +529 -59
  9. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +53 -0
  10. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +684 -83
  11. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +129 -57
  12. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicQuickSettingsTileService.kt +120 -0
  13. package/android/src/main/res/layout/rn_live_widget_hit_zone.xml +16 -0
  14. package/android/src/main/res/layout/rn_live_widget_host.xml +16 -87
  15. package/app.plugin.js +36 -0
  16. package/assets/MaterialIcons.json +2236 -0
  17. package/cli/codegen.js +789 -0
  18. package/cli/setup.js +323 -63
  19. package/docs/EXPO-GUIDE-EN.md +21 -3
  20. package/docs/EXPO-GUIDE-VI.md +21 -3
  21. package/docs/GUIDE-EN.md +444 -21
  22. package/docs/GUIDE-VI.md +473 -22
  23. package/docs/index.html +2263 -265
  24. package/docs/llms-full.txt +41 -0
  25. package/docs/llms.txt +24 -7
  26. package/ios/Bridge/LiveActivityModule.swift +333 -44
  27. package/ios/Intents/DynamicWidgetActionIntent.swift +57 -2
  28. package/ios/Models/DynamicLayoutModels.swift +120 -5
  29. package/ios/Renderer/DynamicNodeView.swift +184 -20
  30. package/ios/Storage/DynamicWidgetStateStore.swift +649 -38
  31. package/ios/Widgets/DynamicControlWidget.swift +63 -0
  32. package/ios/Widgets/DynamicHomeScreenWidget.swift +104 -26
  33. package/ios/Widgets/DynamicLiveActivityWidget.swift +25 -4
  34. package/lib/builder.d.ts +50 -7
  35. package/lib/builder.d.ts.map +1 -1
  36. package/lib/builder.js +391 -29
  37. package/lib/builder.js.map +1 -1
  38. package/lib/dispatcher.js +16 -10
  39. package/lib/dispatcher.js.map +1 -1
  40. package/lib/hooks.d.ts +13 -9
  41. package/lib/hooks.d.ts.map +1 -1
  42. package/lib/hooks.js +105 -42
  43. package/lib/hooks.js.map +1 -1
  44. package/lib/index.d.ts +96 -8
  45. package/lib/index.d.ts.map +1 -1
  46. package/lib/index.js +415 -97
  47. package/lib/index.js.map +1 -1
  48. package/lib/ir.d.ts +15 -0
  49. package/lib/ir.d.ts.map +1 -0
  50. package/lib/ir.js +386 -0
  51. package/lib/ir.js.map +1 -0
  52. package/lib/jsx.d.ts +19 -7
  53. package/lib/jsx.d.ts.map +1 -1
  54. package/lib/jsx.js +179 -95
  55. package/lib/jsx.js.map +1 -1
  56. package/lib/material-icons.generated.d.ts +8 -0
  57. package/lib/material-icons.generated.d.ts.map +1 -0
  58. package/lib/material-icons.generated.js +9 -0
  59. package/lib/material-icons.generated.js.map +1 -0
  60. package/lib/preview.d.ts +11 -27
  61. package/lib/preview.d.ts.map +1 -1
  62. package/lib/preview.js +456 -119
  63. package/lib/preview.js.map +1 -1
  64. package/lib/sf-symbols.generated.d.ts +8 -0
  65. package/lib/sf-symbols.generated.d.ts.map +1 -0
  66. package/lib/sf-symbols.generated.js +9 -0
  67. package/lib/sf-symbols.generated.js.map +1 -0
  68. package/lib/types.d.ts +506 -30
  69. package/lib/types.d.ts.map +1 -1
  70. package/lib/types.js +2 -1
  71. package/lib/types.js.map +1 -1
  72. package/package.json +7 -2
  73. package/scripts/build.js +98 -0
  74. package/scripts/generate-material-icons.js +93 -0
  75. package/scripts/generate-sf-symbols.js +95 -0
  76. package/scripts/release.js +115 -0
  77. package/scripts/test-ir-semantic.js +314 -0
  78. package/src/builder.ts +439 -26
  79. package/src/hooks.ts +88 -12
  80. package/src/index.ts +385 -57
  81. package/src/ir.ts +436 -0
  82. package/src/jsx.tsx +99 -21
  83. package/src/material-icons.generated.ts +3844 -0
  84. package/src/preview.tsx +533 -55
  85. package/src/react-native-env.d.ts +1 -0
  86. package/src/sf-symbols.generated.ts +9533 -0
  87. package/src/types.ts +601 -31
package/src/preview.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState } from 'react';
1
+ import React, { useEffect, useState } from 'react';
2
2
  import {
3
3
  View,
4
4
  Text as RNText,
@@ -9,6 +9,7 @@ import {
9
9
  TextStyle as RNTextStyle,
10
10
  } from 'react-native';
11
11
  import {
12
+ ControlWidgetConfig,
12
13
  LayoutNode,
13
14
  WidgetSinglePageConfig,
14
15
  VStackNode,
@@ -21,8 +22,18 @@ import {
21
22
  ImageNode,
22
23
  } from './types';
23
24
  import { renderToWidget, extractPropsAndStyle } from './jsx';
25
+ import { actionHandlers, globalActionHandlers } from './dispatcher';
26
+ import { flattenDataObject, resolveTemplateString } from './builder';
24
27
 
25
- export type WidgetPreviewSize = 'small' | 'medium' | 'large' | 'lockScreen' | 'dynamicIsland';
28
+ export type WidgetPreviewSize =
29
+ | 'small'
30
+ | 'medium'
31
+ | 'large'
32
+ | 'extraLarge'
33
+ | 'standBy'
34
+ | 'lockScreen'
35
+ | 'dynamicIsland'
36
+ | 'control';
26
37
 
27
38
  export interface WidgetPreviewProps {
28
39
  /**
@@ -30,13 +41,20 @@ export interface WidgetPreviewProps {
30
41
  * - 'small' (158 × 158 dp)
31
42
  * - 'medium' (338 × 158 dp)
32
43
  * - 'large' (338 × 354 dp)
44
+ * - 'extraLarge' (338 × 420 dp)
45
+ * - 'standBy' (310 × 158 dp)
33
46
  * - 'lockScreen' (350 × 80 dp)
34
47
  * - 'dynamicIsland' (350 × 160 dp)
35
- * Mặc định là 'medium'.
48
+ * - 'control' (170 × 76 dp — iOS 18 Control Center / Android Quick Settings Tile)
49
+ * Mặc định là 'medium' (hoặc 'control' nếu truyền controlConfig).
36
50
  */
37
51
  size?: WidgetPreviewSize;
38
- /** Cây LayoutNode hoặc đối tượng chứa { small, medium, large } */
52
+ /** Cây LayoutNode hoặc đối tượng chứa { small, medium, large, extraLarge, standBy } */
39
53
  layout?: LayoutNode | WidgetSinglePageConfig;
54
+ /** Dữ liệu JSON mẫu dùng để nội suy các biểu thức Data Binding (`{{user.name}}`, `data.*`) khi xem trước */
55
+ data?: Record<string, any>;
56
+ /** Cấu hình Control Widget dùng để xem trước Control Center / Quick Settings Tile */
57
+ controlConfig?: ControlWidgetConfig;
40
58
  /** Cú pháp JSX trực tiếp truyền vào bên trong thẻ `<WidgetPreview>` */
41
59
  children?: React.ReactNode;
42
60
  /** Bắt sự kiện khi người dùng bấm vào các nút tương tác trên Widget xem trước */
@@ -53,17 +71,40 @@ const SIZE_DIMENSIONS: Record<WidgetPreviewSize, { width: number; height: number
53
71
  small: { width: 158, height: 158, label: 'Small • 158×158' },
54
72
  medium: { width: 338, height: 158, label: 'Medium • 338×158' },
55
73
  large: { width: 338, height: 354, label: 'Large • 338×354' },
74
+ extraLarge: { width: 338, height: 420, label: 'Extra Large • 338×420' },
75
+ standBy: { width: 310, height: 158, label: 'StandBy • 310×158' },
56
76
  lockScreen: { width: 350, height: 80, label: 'Lock Screen • 350×80' },
57
77
  dynamicIsland: { width: 350, height: 160, label: 'Dynamic Island' },
78
+ control: { width: 170, height: 76, label: 'Control • iOS 18 / QS Tile' },
58
79
  };
59
80
 
60
- function parseDynamicColor(c: any): string | undefined {
81
+ function parseDynamicColor(c: any, mode: 'light' | 'dark' | 'tinted' = 'dark'): string | undefined {
61
82
  if (!c) return undefined;
62
83
  if (typeof c === 'string') return c;
63
- if (typeof c === 'object') return c.dark || c.light;
84
+ if (typeof c === 'object') {
85
+ if (mode === 'tinted' && c.tinted) return c.tinted;
86
+ if (mode === 'light' && c.light) return c.light;
87
+ return c.dark || c.light || c.tinted;
88
+ }
64
89
  return undefined;
65
90
  }
66
91
 
92
+ function extractShadowStyle(p: any): ViewStyle {
93
+ if (!p) return {};
94
+ const shadowColor = parseDynamicColor(p.shadowColor);
95
+ if (!shadowColor) return {};
96
+ const rad = p.shadowRadius ?? 4;
97
+ return {
98
+ shadowColor,
99
+ shadowOffset: p.shadowOffset
100
+ ? { width: p.shadowOffset.x ?? 0, height: p.shadowOffset.y ?? 2 }
101
+ : { width: 0, height: 2 },
102
+ shadowOpacity: p.shadowOpacity ?? 0.35,
103
+ shadowRadius: rad,
104
+ elevation: Math.min(8, Math.max(1, Math.round(rad))),
105
+ };
106
+ }
107
+
67
108
  /**
68
109
  * 📱 Bộ Component xem trước tiện ích trực tiếp trong ứng dụng React Native (In-App Widget Preview).
69
110
  * Cho phép lập trình viên và người dùng trải nghiệm giao diện & bấm thử tương tác của Widget
@@ -87,20 +128,61 @@ function parseDynamicColor(c: any): string | undefined {
87
128
  * </WidgetPreview>
88
129
  * ```
89
130
  */
131
+ type PreviewSyncVars = {
132
+ _lastSyncTime: string;
133
+ id: string;
134
+ title: string;
135
+ };
136
+
90
137
  export const WidgetPreview: React.FC<WidgetPreviewProps> = ({
91
- size: initialSize = 'medium',
138
+ size: initialSizeProp,
92
139
  layout,
140
+ data,
141
+ controlConfig,
93
142
  children,
94
143
  onAction,
95
144
  showBadge = true,
96
145
  interactiveSizeSwitch = false,
97
146
  style,
98
147
  }: WidgetPreviewProps) => {
148
+ const initialSize: WidgetPreviewSize = initialSizeProp ?? (controlConfig ? 'control' : 'medium');
99
149
  const [activeSize, setActiveSize] = useState<WidgetPreviewSize>(initialSize);
100
150
 
151
+ const pageConfigObj =
152
+ !children && layout && typeof layout === 'object' && (layout as any).pages
153
+ ? (layout as any)
154
+ : null;
155
+ const pageOrder: string[] = pageConfigObj
156
+ ? Array.isArray(pageConfigObj.pageOrder) && pageConfigObj.pageOrder.length > 0
157
+ ? pageConfigObj.pageOrder
158
+ : Object.keys(pageConfigObj.pages)
159
+ : [];
160
+ const defaultPage: string = pageConfigObj
161
+ ? pageConfigObj.initialPage || pageOrder[0] || ''
162
+ : '';
163
+
164
+ const [activePage, setActivePage] = useState<string>(defaultPage);
165
+ const [syncVars, setSyncVars] = useState<PreviewSyncVars>(() => ({
166
+ _lastSyncTime: new Date().toLocaleTimeString('en-GB', { hour12: false }),
167
+ id: '2026',
168
+ title: 'Hoàn thành kế hoạch quý 4',
169
+ }));
170
+
171
+ useEffect(() => {
172
+ if (defaultPage) {
173
+ setActivePage((prev) => (pageConfigObj?.pages?.[prev] ? prev : defaultPage));
174
+ }
175
+ }, [defaultPage, pageConfigObj]);
176
+
101
177
  // Trích xuất LayoutNode từ props hoặc JSX children
102
178
  let rootNode: LayoutNode | null = null;
103
179
  let rawTarget: any = null;
180
+ const currentPageKey =
181
+ pageConfigObj && pageConfigObj.pages
182
+ ? pageConfigObj.pages[activePage]
183
+ ? activePage
184
+ : defaultPage
185
+ : '';
104
186
 
105
187
  if (children) {
106
188
  rawTarget = children;
@@ -110,17 +192,63 @@ export const WidgetPreview: React.FC<WidgetPreviewProps> = ({
110
192
  } else if ((layout as any).type && typeof (layout as any).type === 'string' && (layout as any).props !== undefined) {
111
193
  rawTarget = layout;
112
194
  } else {
113
- const pageConfig = layout as WidgetSinglePageConfig;
114
- rawTarget =
115
- (pageConfig as any)[activeSize] ||
116
- pageConfig.medium ||
117
- pageConfig.large ||
118
- pageConfig.small ||
119
- pageConfig.accessoryRectangular ||
120
- null;
195
+ const pageConfig =
196
+ pageConfigObj && currentPageKey
197
+ ? pageConfigObj.pages[currentPageKey] || {}
198
+ : (layout as any);
199
+ const primary = pageConfig?.[activeSize];
200
+ if (primary) {
201
+ rawTarget = primary;
202
+ } else if (activeSize === 'extraLarge') {
203
+ rawTarget = pageConfig?.large || pageConfig?.medium || pageConfig?.small || null;
204
+ } else if (activeSize === 'standBy') {
205
+ rawTarget = pageConfig?.small || pageConfig?.medium || pageConfig?.large || null;
206
+ } else {
207
+ rawTarget =
208
+ pageConfig?.medium ||
209
+ pageConfig?.large ||
210
+ pageConfig?.small ||
211
+ pageConfig?.accessoryRectangular ||
212
+ null;
213
+ }
121
214
  }
122
215
  }
123
216
 
217
+ const handlePreviewAction = (actionId: string) => {
218
+ if (actionId.startsWith('page:') && pageConfigObj && pageOrder.length > 0) {
219
+ const target = actionId.slice('page:'.length);
220
+ if (target === 'next') {
221
+ const idx = pageOrder.indexOf(currentPageKey);
222
+ const nextPage = idx >= 0 ? pageOrder[(idx + 1) % pageOrder.length] : pageOrder[0];
223
+ setActivePage(nextPage);
224
+ } else if (target === 'prev') {
225
+ const idx = pageOrder.indexOf(currentPageKey);
226
+ const prevPage =
227
+ idx >= 0 ? pageOrder[(idx - 1 + pageOrder.length) % pageOrder.length] : pageOrder[0];
228
+ setActivePage(prevPage);
229
+ } else if (pageConfigObj.pages[target]) {
230
+ setActivePage(target);
231
+ }
232
+ } else if (actionId === 'refresh' || actionId.startsWith('refresh:')) {
233
+ const nowTime = new Date().toLocaleTimeString('en-GB', { hour12: false });
234
+ const sampleNotes = [
235
+ 'Đồng bộ báo cáo tiến độ OKRs',
236
+ 'Kiểm tra chấm công ca hành chính',
237
+ 'Rà soát task Sprint tuần này',
238
+ 'Cập nhật số liệu Realtime API',
239
+ ];
240
+ const nextId = String(Math.floor(Math.random() * 90) + 10);
241
+ setSyncVars({
242
+ _lastSyncTime: nowTime,
243
+ id: nextId,
244
+ title: sampleNotes[Number(nextId) % sampleNotes.length],
245
+ });
246
+ }
247
+ if (onAction) {
248
+ onAction(actionId);
249
+ }
250
+ };
251
+
124
252
  if (rawTarget) {
125
253
  if (React.isValidElement(rawTarget) || (rawTarget as any)?.$$typeof || typeof (rawTarget as any)?.type === 'function') {
126
254
  rootNode = renderToWidget(rawTarget);
@@ -132,18 +260,50 @@ export const WidgetPreview: React.FC<WidgetPreviewProps> = ({
132
260
  const dim =
133
261
  SIZE_DIMENSIONS[(activeSize as WidgetPreviewSize) || 'medium'] || SIZE_DIMENSIONS.medium;
134
262
 
263
+ const anyLayout = layout as any;
264
+ const effectiveControlConfig: ControlWidgetConfig | null =
265
+ controlConfig ||
266
+ (anyLayout?.control as ControlWidgetConfig) ||
267
+ (activeSize === 'control' && anyLayout && !anyLayout.type && typeof anyLayout.title === 'string' && typeof anyLayout.actionId === 'string'
268
+ ? (anyLayout as ControlWidgetConfig)
269
+ : null);
270
+
271
+ const switchSizes: WidgetPreviewSize[] = ['small', 'medium', 'large'];
272
+ if (
273
+ initialSize === 'extraLarge' ||
274
+ anyLayout?.extraLarge ||
275
+ (currentPageKey && pageConfigObj?.pages?.[currentPageKey]?.extraLarge)
276
+ ) {
277
+ switchSizes.push('extraLarge');
278
+ }
279
+ if (
280
+ initialSize === 'standBy' ||
281
+ anyLayout?.standBy ||
282
+ (currentPageKey && pageConfigObj?.pages?.[currentPageKey]?.standBy)
283
+ ) {
284
+ switchSizes.push('standBy');
285
+ }
286
+ if (initialSize === 'control' || effectiveControlConfig) {
287
+ switchSizes.push('control');
288
+ }
289
+
290
+ const effectiveSyncVars: Record<string, string> = {
291
+ ...syncVars,
292
+ ...(data ? flattenDataObject(data) : {}),
293
+ };
294
+
135
295
  return (
136
296
  <View style={[styles.container, style]}>
137
297
  {interactiveSizeSwitch && (
138
298
  <View style={styles.tabBar}>
139
- {(['small', 'medium', 'large'] as WidgetPreviewSize[]).map((s) => (
299
+ {switchSizes.map((s) => (
140
300
  <TouchableOpacity
141
301
  key={s}
142
302
  onPress={() => setActiveSize(s)}
143
303
  style={[styles.tabItem, activeSize === s && styles.tabItemActive]}
144
304
  >
145
305
  <RNText style={[styles.tabText, activeSize === s && styles.tabTextActive]}>
146
- {s.toUpperCase()}
306
+ {s === 'extraLarge' ? 'XLARGE' : s === 'control' ? 'CONTROL' : s.toUpperCase()}
147
307
  </RNText>
148
308
  </TouchableOpacity>
149
309
  ))}
@@ -168,8 +328,20 @@ export const WidgetPreview: React.FC<WidgetPreviewProps> = ({
168
328
  },
169
329
  ]}
170
330
  >
171
- {rootNode ? (
172
- renderPreviewNode(rootNode, onAction, true, 'column')
331
+ {activeSize === 'control' ? (
332
+ <PreviewControlTile
333
+ config={
334
+ effectiveControlConfig || {
335
+ type: 'button',
336
+ title: 'Live Control',
337
+ subtitle: 'Nhấn để kích hoạt',
338
+ actionId: 'control_action_main',
339
+ }
340
+ }
341
+ onAction={handlePreviewAction}
342
+ />
343
+ ) : rootNode ? (
344
+ renderPreviewNode(rootNode, handlePreviewAction, true, 'column', effectiveSyncVars)
173
345
  ) : (
174
346
  <View style={styles.emptyContainer}>
175
347
  <RNText style={styles.emptyText}>Chưa có cấu hình layout cho kích thước {activeSize}</RNText>
@@ -185,38 +357,299 @@ export const VoltraWidgetPreview = WidgetPreview;
185
357
 
186
358
  // ─── RECURSIVE NODE RENDERER ─────────────────────────────────────────────────
187
359
 
360
+ function dispatchPreviewAction(
361
+ actionId: string | undefined,
362
+ onAction?: (actionId: string) => void,
363
+ extraPayload?: Record<string, any>
364
+ ) {
365
+ if (!actionId) return;
366
+ if (onAction) {
367
+ onAction(actionId);
368
+ }
369
+ const payload = { actionId, ...extraPayload };
370
+ const registered = actionHandlers.get(actionId);
371
+ if (registered) {
372
+ try {
373
+ registered(payload);
374
+ } catch (e) {
375
+ console.warn(`[WidgetPreview] Action '${actionId}' error:`, e);
376
+ }
377
+ }
378
+ for (const globalHandler of globalActionHandlers) {
379
+ try {
380
+ globalHandler(actionId, extraPayload);
381
+ } catch (e) {
382
+ console.warn(`[WidgetPreview] Global action handler error:`, e);
383
+ }
384
+ }
385
+ }
386
+
387
+ const PreviewControlTile: React.FC<{
388
+ config: ControlWidgetConfig;
389
+ onAction?: (actionId: string) => void;
390
+ }> = ({ config, onAction }) => {
391
+ const [isOn, setIsOn] = useState<boolean>(Boolean(config.checked));
392
+
393
+ useEffect(() => {
394
+ setIsOn(Boolean(config.checked));
395
+ }, [config.checked]);
396
+
397
+ const isToggle = config.type === 'toggle' || Boolean(config.actionId?.startsWith('toggle:'));
398
+ const active = isToggle && isOn;
399
+ const tint = config.tintColor || '#2563EB';
400
+ const title = active ? config.checkedTitle || config.title : config.title;
401
+ const subtitle = active
402
+ ? config.checkedSubtitle ?? config.subtitle ?? 'Đang bật'
403
+ : config.subtitle ?? (isToggle ? 'Tạm tắt' : config.systemName || 'Control');
404
+
405
+ return (
406
+ <TouchableOpacity
407
+ accessible
408
+ accessibilityRole={isToggle ? 'switch' : 'button'}
409
+ accessibilityLabel={title}
410
+ accessibilityValue={isToggle ? { text: active ? 'Bật' : 'Tắt' } : undefined}
411
+ activeOpacity={0.75}
412
+ onPress={() => {
413
+ const nextVal = isToggle ? !isOn : undefined;
414
+ if (isToggle) {
415
+ setIsOn(Boolean(nextVal));
416
+ }
417
+ dispatchPreviewAction(
418
+ config.actionId,
419
+ onAction,
420
+ isToggle ? { isChecked: nextVal } : undefined
421
+ );
422
+ }}
423
+ style={{
424
+ width: '100%',
425
+ height: '100%',
426
+ flexDirection: 'row',
427
+ alignItems: 'center',
428
+ paddingHorizontal: 12,
429
+ gap: 10,
430
+ backgroundColor: active ? '#1E293B' : '#0F172A',
431
+ borderWidth: active ? 1.5 : 1,
432
+ borderColor: active ? tint : 'rgba(148, 163, 184, 0.2)',
433
+ borderRadius: 20,
434
+ }}
435
+ >
436
+ <View
437
+ style={{
438
+ width: 34,
439
+ height: 34,
440
+ borderRadius: 17,
441
+ backgroundColor: active ? tint : 'rgba(255, 255, 255, 0.1)',
442
+ alignItems: 'center',
443
+ justifyContent: 'center',
444
+ }}
445
+ >
446
+ <RNText style={{ color: '#FFFFFF', fontSize: 14, fontWeight: 'bold' }}>
447
+ {active ? '✓' : '⚡'}
448
+ </RNText>
449
+ </View>
450
+ <View style={{ flex: 1 }}>
451
+ <RNText
452
+ numberOfLines={1}
453
+ style={{ color: '#F8FAFC', fontSize: 12, fontWeight: '700' }}
454
+ >
455
+ {title}
456
+ </RNText>
457
+ <RNText
458
+ numberOfLines={1}
459
+ style={{ color: active ? '#93C5FD' : '#94A3B8', fontSize: 10, marginTop: 1 }}
460
+ >
461
+ {subtitle}
462
+ </RNText>
463
+ </View>
464
+ </TouchableOpacity>
465
+ );
466
+ };
467
+
468
+ const PreviewToggleItem: React.FC<{
469
+ node: any;
470
+ onAction?: (actionId: string) => void;
471
+ }> = ({ node, onAction }) => {
472
+ const p = node.props || {};
473
+ const [isChecked, setIsChecked] = useState<boolean>(!!p.checked || !!p.isChecked);
474
+ const tint = parseDynamicColor(p.tintColor) || '#38BDF8';
475
+ const isSwitch = p.style === 'switch';
476
+
477
+ const handlePress = () => {
478
+ const nextVal = !isChecked;
479
+ setIsChecked(nextVal);
480
+ dispatchPreviewAction(p.actionId, onAction, { isChecked: nextVal });
481
+ };
482
+
483
+ return (
484
+ <TouchableOpacity
485
+ accessible={p.accessible !== false}
486
+ accessibilityLabel={p.accessibilityLabel || p.label}
487
+ accessibilityHint={p.accessibilityHint}
488
+ accessibilityRole="switch"
489
+ accessibilityValue={{ text: isChecked ? 'Đã chọn' : 'Chưa chọn' }}
490
+ activeOpacity={0.7}
491
+ onPress={handlePress}
492
+ style={{
493
+ flexDirection: 'row',
494
+ alignItems: 'center',
495
+ gap: 8,
496
+ paddingVertical: 6,
497
+ paddingHorizontal: 2,
498
+ width: '100%',
499
+ alignSelf: 'stretch',
500
+ }}
501
+ >
502
+ {isSwitch ? (
503
+ <View
504
+ style={{
505
+ width: 34,
506
+ height: 20,
507
+ borderRadius: 10,
508
+ backgroundColor: isChecked ? tint : '#475569',
509
+ justifyContent: 'center',
510
+ paddingHorizontal: 3,
511
+ }}
512
+ >
513
+ <View
514
+ style={{
515
+ width: 14,
516
+ height: 14,
517
+ borderRadius: 7,
518
+ backgroundColor: '#FFFFFF',
519
+ alignSelf: isChecked ? 'flex-end' : 'flex-start',
520
+ }}
521
+ />
522
+ </View>
523
+ ) : (
524
+ <View
525
+ style={{
526
+ width: 18,
527
+ height: 18,
528
+ borderRadius: 4,
529
+ borderWidth: 1.5,
530
+ borderColor: isChecked ? tint : '#64748B',
531
+ backgroundColor: isChecked ? tint : 'transparent',
532
+ alignItems: 'center',
533
+ justifyContent: 'center',
534
+ }}
535
+ >
536
+ {isChecked && (
537
+ <RNText style={{ color: '#FFFFFF', fontSize: 11, fontWeight: 'bold' }}>✓</RNText>
538
+ )}
539
+ </View>
540
+ )}
541
+ <RNText
542
+ style={{
543
+ flex: 1,
544
+ color:
545
+ isChecked && p.strikethroughOnCheck
546
+ ? '#64748B'
547
+ : parseDynamicColor(p.textColor) || '#F8FAFC',
548
+ fontSize: p.fontSize ?? 13,
549
+ textDecorationLine:
550
+ isChecked && p.strikethroughOnCheck ? 'line-through' : 'none',
551
+ }}
552
+ >
553
+ {p.label}
554
+ </RNText>
555
+ </TouchableOpacity>
556
+ );
557
+ };
558
+
559
+ const PreviewTimerText: React.FC<{
560
+ nodeProps: any;
561
+ parentDirection: 'row' | 'column';
562
+ }> = ({ nodeProps: p, parentDirection }) => {
563
+ const formatRemaining = () => {
564
+ const target = Number(p.targetTimestamp ?? 0);
565
+ if (!target) return '00:00';
566
+ const diff = Math.max(0, Math.round(Math.abs(target - Date.now() / 1000)));
567
+ const hrs = Math.floor(diff / 3600);
568
+ const mins = Math.floor((diff % 3600) / 60);
569
+ const secs = diff % 60;
570
+ const pad = (n: number) => (n < 10 ? `0${n}` : `${n}`);
571
+ return hrs > 0 ? `${hrs}:${pad(mins)}:${pad(secs)}` : `${pad(mins)}:${pad(secs)}`;
572
+ };
573
+
574
+ const [display, setDisplay] = useState<string>(formatRemaining);
575
+
576
+ useEffect(() => {
577
+ setDisplay(formatRemaining());
578
+ const id = setInterval(() => {
579
+ setDisplay(formatRemaining());
580
+ }, 1000);
581
+ return () => clearInterval(id);
582
+ }, [p.targetTimestamp, p.mode]);
583
+
584
+ return (
585
+ <RNText
586
+ style={[
587
+ { color: parseDynamicColor(p.color) || '#2563EB', fontSize: p.fontSize ?? 14, fontWeight: 'bold' },
588
+ parentDirection === 'row' ? { flexShrink: 0 } : undefined,
589
+ ]}
590
+ >
591
+ {p.prefix || ''}{display}{p.suffix || ''}
592
+ </RNText>
593
+ );
594
+ };
595
+
596
+ function resolvePreviewText(raw: any, syncVars?: Record<string, string>): string {
597
+ const str = String(raw ?? '');
598
+ return syncVars ? resolveTemplateString(str, syncVars) : str;
599
+ }
600
+
601
+ function resolvePreviewNumber(
602
+ raw: any,
603
+ fallback: number,
604
+ syncVars?: Record<string, string>
605
+ ): number {
606
+ if (typeof raw === 'number' && !Number.isNaN(raw)) return raw;
607
+ if (typeof raw === 'string') {
608
+ const resolved = syncVars ? resolveTemplateString(raw, syncVars) : raw;
609
+ const num = parseFloat(resolved);
610
+ return Number.isNaN(num) ? fallback : num;
611
+ }
612
+ return fallback;
613
+ }
614
+
188
615
  function renderPreviewNode(
189
616
  node: any,
190
617
  onAction?: (actionId: string) => void,
191
618
  isRoot: boolean = false,
192
- parentDirection: 'row' | 'column' = 'column'
619
+ parentDirection: 'row' | 'column' = 'column',
620
+ syncVars?: Record<string, string>
193
621
  ): React.ReactNode {
194
622
  if (!node) return null;
195
623
 
196
624
  if (React.isValidElement(node) || (node as any)?.$$typeof || typeof (node as any)?.type === 'function') {
197
625
  const converted = renderToWidget(node);
198
626
  if (!converted) return null;
199
- return renderPreviewNode(converted, onAction, isRoot, parentDirection);
627
+ return renderPreviewNode(converted, onAction, isRoot, parentDirection, syncVars);
200
628
  }
201
629
 
202
630
  switch (node.type) {
203
631
  case 'VStack': {
204
632
  const vnode = node as VStackNode;
205
633
  const p = extractPropsAndStyle(vnode.props);
634
+ const bg = p.gradient?.colors?.[0]
635
+ ? parseDynamicColor(p.gradient.colors[0])
636
+ : parseDynamicColor(p.backgroundColor) || 'transparent';
637
+
206
638
  const containerStyle: ViewStyle = {
207
639
  flexDirection: 'column',
208
640
  padding: p.padding ?? (p.paddingHorizontal || p.paddingVertical ? undefined : 0),
209
641
  paddingHorizontal: p.paddingHorizontal,
210
642
  paddingVertical: p.paddingVertical,
211
- backgroundColor: parseDynamicColor(p.backgroundColor) || 'transparent',
643
+ backgroundColor: bg,
212
644
  borderRadius: p.borderRadius ?? p.cornerRadius ?? 0,
213
645
  borderWidth: p.borderWidth,
214
646
  borderColor: parseDynamicColor(p.borderColor),
647
+ borderStyle: p.borderStyle || 'solid',
215
648
  rowGap: p.spacing ?? 0,
216
649
  alignItems:
217
650
  p.alignment === 'center'
218
651
  ? 'center'
219
- : p.alignment === 'trailing'
652
+ : p.alignment === 'trailing' || p.alignment === 'end'
220
653
  ? 'flex-end'
221
654
  : 'flex-start',
222
655
  justifyContent:
@@ -226,9 +659,12 @@ function renderPreviewNode(
226
659
  ? 'space-between'
227
660
  : p.justifyContent === 'spaceAround'
228
661
  ? 'space-around'
662
+ : p.justifyContent === 'spaceEvenly'
663
+ ? 'space-evenly'
229
664
  : p.justifyContent === 'flexEnd'
230
665
  ? 'flex-end'
231
666
  : 'flex-start',
667
+ ...extractShadowStyle(p),
232
668
  };
233
669
 
234
670
  if (isRoot) {
@@ -247,7 +683,7 @@ function renderPreviewNode(
247
683
  <View style={containerStyle}>
248
684
  {vnode.children?.map((child, idx) => (
249
685
  <React.Fragment key={idx}>
250
- {renderPreviewNode(child, onAction, false, 'column')}
686
+ {renderPreviewNode(child, onAction, false, 'column', syncVars)}
251
687
  </React.Fragment>
252
688
  ))}
253
689
  </View>
@@ -257,20 +693,26 @@ function renderPreviewNode(
257
693
  case 'HStack': {
258
694
  const hnode = node as HStackNode;
259
695
  const p = extractPropsAndStyle(hnode.props);
696
+ const isRtl = p.direction === 'rtl' || p.isRTL;
697
+ const bg = p.gradient?.colors?.[0]
698
+ ? parseDynamicColor(p.gradient.colors[0])
699
+ : parseDynamicColor(p.backgroundColor) || 'transparent';
700
+
260
701
  const containerStyle: ViewStyle = {
261
- flexDirection: 'row',
702
+ flexDirection: isRtl ? 'row-reverse' : 'row',
262
703
  padding: p.padding,
263
704
  paddingHorizontal: p.paddingHorizontal,
264
705
  paddingVertical: p.paddingVertical,
265
- backgroundColor: parseDynamicColor(p.backgroundColor) || 'transparent',
706
+ backgroundColor: bg,
266
707
  borderRadius: p.borderRadius ?? p.cornerRadius ?? 0,
267
708
  borderWidth: p.borderWidth,
268
709
  borderColor: parseDynamicColor(p.borderColor),
710
+ borderStyle: p.borderStyle || 'solid',
269
711
  columnGap: p.spacing ?? 0,
270
712
  alignItems:
271
713
  p.alignment === 'center'
272
714
  ? 'center'
273
- : p.alignment === 'trailing'
715
+ : p.alignment === 'trailing' || p.alignment === 'bottom'
274
716
  ? 'flex-end'
275
717
  : p.alignment === 'top'
276
718
  ? 'flex-start'
@@ -282,9 +724,12 @@ function renderPreviewNode(
282
724
  ? 'space-between'
283
725
  : p.justifyContent === 'spaceAround'
284
726
  ? 'space-around'
727
+ : p.justifyContent === 'spaceEvenly'
728
+ ? 'space-evenly'
285
729
  : p.justifyContent === 'flexEnd'
286
730
  ? 'flex-end'
287
731
  : 'flex-start',
732
+ ...extractShadowStyle(p),
288
733
  };
289
734
 
290
735
  if (isRoot) {
@@ -300,7 +745,7 @@ function renderPreviewNode(
300
745
  <View style={containerStyle}>
301
746
  {hnode.children?.map((child, idx) => (
302
747
  <React.Fragment key={idx}>
303
- {renderPreviewNode(child, onAction, false, 'row')}
748
+ {renderPreviewNode(child, onAction, false, 'row', syncVars)}
304
749
  </React.Fragment>
305
750
  ))}
306
751
  </View>
@@ -332,7 +777,7 @@ function renderPreviewNode(
332
777
  : { ...StyleSheet.absoluteFillObject, alignItems: 'center', justifyContent: 'center' }
333
778
  }
334
779
  >
335
- {renderPreviewNode(child, onAction, false, parentDirection)}
780
+ {renderPreviewNode(child, onAction, false, parentDirection, syncVars)}
336
781
  </View>
337
782
  ))}
338
783
  </View>
@@ -355,7 +800,7 @@ function renderPreviewNode(
355
800
  >
356
801
  {gnode.children?.map((child: any, idx: number) => (
357
802
  <View key={idx} style={{ flexBasis: `${Math.floor(100 / cols) - 3}%` }}>
358
- {renderPreviewNode(child, onAction, false, 'column')}
803
+ {renderPreviewNode(child, onAction, false, 'column', syncVars)}
359
804
  </View>
360
805
  ))}
361
806
  </View>
@@ -365,6 +810,12 @@ function renderPreviewNode(
365
810
  case 'Text': {
366
811
  const tnode = node as TextNode;
367
812
  const p = extractPropsAndStyle(tnode.props);
813
+ const isRtl = p.direction === 'rtl' || p.isRTL;
814
+ const shadowColor = parseDynamicColor(p.shadowColor);
815
+
816
+ const resolvedText = resolvePreviewText(p.text, syncVars);
817
+ const isRedacted = Boolean(p.redacted);
818
+
368
819
  const textStyle: RNTextStyle = {
369
820
  color: parseDynamicColor(p.color) || '#FFFFFF',
370
821
  fontSize: p.fontSize ?? 14,
@@ -376,17 +827,30 @@ function renderPreviewNode(
376
827
  : p.fontWeight === 'medium'
377
828
  ? '500'
378
829
  : 'normal',
379
- textAlign: p.textAlign ?? 'left',
830
+ fontFamily: p.fontDesign === 'monospaced' ? 'monospace' : undefined,
831
+ textAlign: p.textAlign ?? (isRtl ? 'right' : 'left'),
832
+ lineHeight: p.lineHeight,
833
+ letterSpacing: p.letterSpacing,
834
+ opacity: isRedacted ? 0.45 : undefined,
835
+ textShadowColor: shadowColor,
836
+ textShadowOffset: p.shadowOffset
837
+ ? { width: p.shadowOffset.x ?? 0, height: p.shadowOffset.y ?? 1 }
838
+ : undefined,
839
+ textShadowRadius: p.shadowRadius,
380
840
  };
381
841
  return (
382
842
  <RNText
843
+ accessible={p.accessible !== false}
844
+ accessibilityLabel={p.accessibilityLabel || resolvedText}
845
+ accessibilityHint={p.accessibilityHint}
846
+ accessibilityRole={p.accessibilityRole as any}
383
847
  numberOfLines={p.numberOfLines}
384
848
  style={[
385
849
  textStyle,
386
850
  parentDirection === 'row' ? { flexShrink: 1 } : undefined,
387
851
  ]}
388
852
  >
389
- {p.text}
853
+ {isRedacted ? '••••••••' : resolvedText}
390
854
  </RNText>
391
855
  );
392
856
  }
@@ -394,34 +858,45 @@ function renderPreviewNode(
394
858
  case 'Button': {
395
859
  const bnode = node as ButtonNode;
396
860
  const p = bnode.props;
861
+ const bg = p.gradient?.colors?.[0]
862
+ ? parseDynamicColor(p.gradient.colors[0])
863
+ : parseDynamicColor(p.backgroundColor) || '#2563EB';
864
+
397
865
  const btnStyle: ViewStyle = {
398
- backgroundColor: parseDynamicColor(p.backgroundColor) || '#2563EB',
866
+ backgroundColor: bg,
399
867
  borderRadius: p.borderRadius ?? p.cornerRadius ?? 8,
868
+ borderWidth: p.borderWidth,
869
+ borderColor: parseDynamicColor(p.borderColor),
870
+ borderStyle: p.borderStyle || 'solid',
400
871
  paddingHorizontal: p.paddingHorizontal ?? p.padding ?? 12,
401
872
  paddingVertical: p.paddingVertical ?? p.padding ?? 6,
402
873
  alignItems: 'center',
403
874
  justifyContent: 'center',
404
875
  alignSelf: p.fullWidth ? 'stretch' : 'auto',
405
- width: p.fullWidth ? '100%' : undefined,
876
+ ...(p.fullWidth ? (parentDirection === 'row' ? { flex: 1 } : { width: '100%' }) : {}),
877
+ ...extractShadowStyle(p),
406
878
  };
407
879
  const textStyle: RNTextStyle = {
408
880
  color: parseDynamicColor(p.textColor) || '#FFFFFF',
409
881
  fontSize: p.fontSize ?? 12,
410
882
  fontWeight: (p.fontWeight as any) ?? '600',
883
+ fontFamily: p.fontDesign === 'monospaced' ? 'monospace' : undefined,
411
884
  };
412
885
 
886
+ const btnTitle = resolvePreviewText(p.title, syncVars);
887
+
413
888
  return (
414
889
  <TouchableOpacity
890
+ accessible={p.accessible !== false}
891
+ accessibilityLabel={p.accessibilityLabel || btnTitle}
892
+ accessibilityHint={p.accessibilityHint}
893
+ accessibilityRole="button"
415
894
  activeOpacity={0.7}
416
- onPress={() => {
417
- if (p.actionId && onAction) {
418
- onAction(p.actionId);
419
- }
420
- }}
895
+ onPress={() => dispatchPreviewAction(p.actionId, onAction)}
421
896
  style={btnStyle}
422
897
  >
423
898
  <RNText numberOfLines={1} style={textStyle}>
424
- {p.title}
899
+ {btnTitle}
425
900
  </RNText>
426
901
  </TouchableOpacity>
427
902
  );
@@ -449,7 +924,7 @@ function renderPreviewNode(
449
924
  fontWeight: '600',
450
925
  }}
451
926
  >
452
- {p.text}
927
+ {resolvePreviewText(p.text, syncVars)}
453
928
  </RNText>
454
929
  </View>
455
930
  );
@@ -458,7 +933,7 @@ function renderPreviewNode(
458
933
  case 'Progress': {
459
934
  const pnode = node as ProgressNode;
460
935
  const p = pnode.props || {};
461
- const val = Math.max(0, Math.min(1, p.value ?? 0.5));
936
+ const val = Math.max(0, Math.min(1, resolvePreviewNumber(p.value, 0.5, syncVars)));
462
937
  const height = p.height ?? 6;
463
938
  const tint = parseDynamicColor(p.tintColor) || '#2563EB';
464
939
  const track = parseDynamicColor(p.trackColor) || '#334155';
@@ -500,16 +975,7 @@ function renderPreviewNode(
500
975
  case 'Timer': {
501
976
  const tnode = node as TimerNode;
502
977
  const p = tnode.props || {};
503
- return (
504
- <RNText
505
- style={[
506
- { color: parseDynamicColor(p.color) || '#2563EB', fontSize: p.fontSize ?? 14, fontWeight: 'bold' },
507
- parentDirection === 'row' ? { flexShrink: 0 } : undefined,
508
- ]}
509
- >
510
- {p.prefix || ''}1:29:55{p.suffix || ''}
511
- </RNText>
512
- );
978
+ return <PreviewTimerText nodeProps={p} parentDirection={parentDirection} />;
513
979
  }
514
980
 
515
981
  case 'Image': {
@@ -589,7 +1055,7 @@ function renderPreviewNode(
589
1055
  >
590
1056
  {lnode.children?.map((child: any, idx: number) => (
591
1057
  <React.Fragment key={idx}>
592
- {renderPreviewNode(child, onAction, false, parentDirection)}
1058
+ {renderPreviewNode(child, onAction, false, parentDirection, syncVars)}
593
1059
  </React.Fragment>
594
1060
  ))}
595
1061
  </TouchableOpacity>
@@ -612,11 +1078,13 @@ function renderPreviewNode(
612
1078
  case 'Gauge': {
613
1079
  const gnode = node as any;
614
1080
  const p = gnode.props || {};
615
- const val = Math.round((p.value ?? 0.75) * 100);
1081
+ const val = Math.round(resolvePreviewNumber(p.value, 0.75, syncVars) * 100);
616
1082
  const tint = parseDynamicColor(p.tintColor) || '#10B981';
617
1083
  return (
618
1084
  <View style={{ alignItems: 'center', justifyContent: 'center' }}>
619
- <RNText style={{ color: tint, fontSize: 13, fontWeight: 'bold' }}>{val}%</RNText>
1085
+ <RNText style={{ color: tint, fontSize: 13, fontWeight: 'bold' }}>
1086
+ {p.valueText ? resolvePreviewText(p.valueText, syncVars) : `${val}%`}
1087
+ </RNText>
620
1088
  </View>
621
1089
  );
622
1090
  }
@@ -644,6 +1112,16 @@ function renderPreviewNode(
644
1112
  );
645
1113
  }
646
1114
 
1115
+ case 'Toggle': {
1116
+ return (
1117
+ <PreviewToggleItem
1118
+ key={`toggle-${(node as any).id || (node as any).props?.actionId || Math.random()}`}
1119
+ node={node}
1120
+ onAction={onAction}
1121
+ />
1122
+ );
1123
+ }
1124
+
647
1125
  default:
648
1126
  return null;
649
1127
  }