react-native-live-widgets 1.1.1 → 1.2.0

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 (46) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +73 -9
  3. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +5 -1
  4. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +5 -1
  5. package/docs/GUIDE-EN.md +91 -7
  6. package/docs/GUIDE-VI.md +91 -7
  7. package/docs/index.html +37 -2
  8. package/docs/llms-full.txt +20 -6
  9. package/docs/llms.txt +29 -7
  10. package/ios/Models/DynamicLayoutModels.swift +5 -0
  11. package/ios/Widgets/DynamicHomeScreenWidget.swift +2 -3
  12. package/lib/builder.d.ts.map +1 -1
  13. package/lib/builder.js +29 -19
  14. package/lib/builder.js.map +1 -1
  15. package/lib/dispatcher.d.ts +7 -0
  16. package/lib/dispatcher.d.ts.map +1 -1
  17. package/lib/dispatcher.js +11 -0
  18. package/lib/dispatcher.js.map +1 -1
  19. package/lib/hooks.d.ts +66 -0
  20. package/lib/hooks.d.ts.map +1 -0
  21. package/lib/hooks.js +166 -0
  22. package/lib/hooks.js.map +1 -0
  23. package/lib/index.d.ts +32 -7
  24. package/lib/index.d.ts.map +1 -1
  25. package/lib/index.js +115 -8
  26. package/lib/index.js.map +1 -1
  27. package/lib/jsx.d.ts +79 -0
  28. package/lib/jsx.d.ts.map +1 -0
  29. package/lib/jsx.js +196 -0
  30. package/lib/jsx.js.map +1 -0
  31. package/lib/preview.d.ts +54 -0
  32. package/lib/preview.d.ts.map +1 -0
  33. package/lib/preview.js +466 -0
  34. package/lib/preview.js.map +1 -0
  35. package/lib/types.d.ts +8 -0
  36. package/lib/types.d.ts.map +1 -1
  37. package/package.json +2 -1
  38. package/src/builder.ts +29 -19
  39. package/src/dispatcher.ts +17 -0
  40. package/src/hooks.ts +223 -0
  41. package/src/index.ts +137 -13
  42. package/src/jsx.tsx +296 -0
  43. package/src/preview.tsx +719 -0
  44. package/src/react-ambient.d.ts +10 -0
  45. package/src/react-native-env.d.ts +43 -0
  46. package/src/types.ts +8 -0
package/src/jsx.tsx ADDED
@@ -0,0 +1,296 @@
1
+ import React from 'react';
2
+ import {
3
+ LayoutNode,
4
+ VStackNode,
5
+ HStackNode,
6
+ ZStackNode,
7
+ GridNode,
8
+ TextNode,
9
+ ButtonNode,
10
+ BadgeNode,
11
+ ProgressNode,
12
+ TimerNode,
13
+ EqualizerNode,
14
+ ImageNode,
15
+ SpacerNode,
16
+ DividerNode,
17
+ LinkNode,
18
+ GaugeNode,
19
+ ChartNode,
20
+ ContainerProps,
21
+ TextStyle,
22
+ ZStackProps,
23
+ GridProps,
24
+ LinkProps,
25
+ EqualizerProps,
26
+ StyleProp,
27
+ } from './types';
28
+ import { UI, flattenStyle } from './builder';
29
+
30
+ // ─── 1. JSX COMPONENT DEFINITIONS ────────────────────────────────────────────
31
+
32
+ export function extractPropsAndStyle<T extends object>(props: any): any {
33
+ if (!props) return {};
34
+ const { style, children, ...rest } = props;
35
+ if (!style) return rest;
36
+ const flattened = flattenStyle(style);
37
+ return { ...flattened, ...rest };
38
+ }
39
+
40
+ export interface VStackProps extends ContainerProps {
41
+ children?: React.ReactNode;
42
+ style?: StyleProp<ContainerProps>;
43
+ }
44
+ export const VStack: React.FC<VStackProps> = () => null;
45
+ VStack.displayName = 'VStack';
46
+
47
+ export interface HStackProps extends ContainerProps {
48
+ children?: React.ReactNode;
49
+ style?: StyleProp<ContainerProps>;
50
+ }
51
+ export const HStack: React.FC<HStackProps> = () => null;
52
+ HStack.displayName = 'HStack';
53
+
54
+ export interface ZStackJSXProps extends ZStackProps {
55
+ children?: React.ReactNode;
56
+ style?: StyleProp<ZStackProps>;
57
+ }
58
+ export const ZStack: React.FC<ZStackJSXProps> = () => null;
59
+ ZStack.displayName = 'ZStack';
60
+
61
+ export interface GridJSXProps extends GridProps {
62
+ children?: React.ReactNode;
63
+ style?: StyleProp<GridProps>;
64
+ }
65
+ export const Grid: React.FC<GridJSXProps> = () => null;
66
+ Grid.displayName = 'Grid';
67
+
68
+ export interface TextJSXProps extends TextStyle {
69
+ children?: React.ReactNode;
70
+ text?: string;
71
+ style?: StyleProp<TextStyle>;
72
+ }
73
+ export const Text: React.FC<TextJSXProps> = () => null;
74
+ Text.displayName = 'Text';
75
+
76
+ export type ButtonJSXProps = ButtonNode['props'] & {
77
+ children?: React.ReactNode;
78
+ style?: StyleProp<ButtonNode['props']>;
79
+ };
80
+ export const Button: React.FC<ButtonJSXProps> = () => null;
81
+ Button.displayName = 'Button';
82
+
83
+ export type BadgeJSXProps = BadgeNode['props'] & {
84
+ children?: React.ReactNode;
85
+ style?: StyleProp<BadgeNode['props']>;
86
+ };
87
+ export const Badge: React.FC<BadgeJSXProps> = () => null;
88
+ Badge.displayName = 'Badge';
89
+
90
+ export type ProgressJSXProps = ProgressNode['props'] & {
91
+ style?: StyleProp<ProgressNode['props']>;
92
+ };
93
+ export const Progress: React.FC<ProgressJSXProps> = () => null;
94
+ Progress.displayName = 'Progress';
95
+
96
+ export type TimerJSXProps = TimerNode['props'] & {
97
+ style?: StyleProp<TimerNode['props']>;
98
+ };
99
+ export const Timer: React.FC<TimerJSXProps> = () => null;
100
+ Timer.displayName = 'Timer';
101
+
102
+ export type ImageJSXProps = ImageNode['props'] & {
103
+ style?: StyleProp<ImageNode['props']>;
104
+ };
105
+ export const Image: React.FC<ImageJSXProps> = () => null;
106
+ Image.displayName = 'Image';
107
+
108
+ export const Spacer: React.FC<{ minLength?: number }> = () => null;
109
+ Spacer.displayName = 'Spacer';
110
+
111
+ export const Divider: React.FC<DividerNode['props']> = () => null;
112
+ Divider.displayName = 'Divider';
113
+
114
+ export interface LinkJSXProps extends LinkProps {
115
+ children?: React.ReactNode;
116
+ style?: StyleProp<LinkProps>;
117
+ }
118
+ export const Link: React.FC<LinkJSXProps> = () => null;
119
+ Link.displayName = 'Link';
120
+
121
+ export const Equalizer: React.FC<EqualizerProps> = () => null;
122
+ Equalizer.displayName = 'Equalizer';
123
+
124
+ export const Gauge: React.FC<GaugeNode['props']> = () => null;
125
+ Gauge.displayName = 'Gauge';
126
+
127
+ export const Chart: React.FC<ChartNode['props']> = () => null;
128
+ Chart.displayName = 'Chart';
129
+
130
+ // ─── 2. RECURSIVE JSX -> LAYOUTNODE CONVERTER ────────────────────────────────
131
+
132
+ /**
133
+ * Chuyển đổi một cây JSX/TSX thành cấu trúc cây LayoutNode chuẩn của thư viện.
134
+ *
135
+ * ```tsx
136
+ * const layout = renderToWidget(
137
+ * <VStack padding={16} backgroundColor="#0F172A" borderRadius={16}>
138
+ * <HStack justifyContent="spaceBetween">
139
+ * <Text fontSize={14} fontWeight="bold" color="#FFFFFF">Vào ca sáng</Text>
140
+ * <Badge text="07:54 (Hợp lệ)" backgroundColor="#064E3B" textColor="#6EE7B7" />
141
+ * </HStack>
142
+ * <Button title="Làm mới" actionId="refresh" />
143
+ * </VStack>
144
+ * );
145
+ * ```
146
+ */
147
+ export function renderToWidget(element: any): LayoutNode | null {
148
+ if (element == null || typeof element === 'boolean') {
149
+ return null;
150
+ }
151
+ if (typeof element === 'string' || typeof element === 'number') {
152
+ return UI.Text(String(element));
153
+ }
154
+ if (Array.isArray(element)) {
155
+ const children = element.map(renderToWidget).filter(Boolean) as LayoutNode[];
156
+ return UI.VStack({}, ...children);
157
+ }
158
+
159
+ // Nếu bản thân đối tượng đã là một LayoutNode của builder (đã có type string)
160
+ if (element && typeof element.type === 'string' && (element as any).props !== undefined && !React.isValidElement(element)) {
161
+ return element as LayoutNode;
162
+ }
163
+
164
+ if (!React.isValidElement(element)) {
165
+ return null;
166
+ }
167
+
168
+ const { type, props } = element as React.ReactElement<any>;
169
+ const restProps = extractPropsAndStyle(props);
170
+ const children = props?.children;
171
+
172
+ const parsedChildren: LayoutNode[] = [];
173
+ if (children !== undefined && children !== null) {
174
+ React.Children.forEach(children, (child: any) => {
175
+ const parsed = renderToWidget(child);
176
+ if (parsed) {
177
+ parsedChildren.push(parsed);
178
+ }
179
+ });
180
+ }
181
+
182
+ // Trường hợp Component là Function
183
+ if (typeof type === 'function') {
184
+ const compName = (type as any).displayName || type.name;
185
+ const isComp = (target: any, name: string) => type === target || compName === name;
186
+
187
+ if (isComp(VStack, 'VStack')) {
188
+ return UI.VStack(restProps, ...parsedChildren);
189
+ }
190
+ if (isComp(HStack, 'HStack')) {
191
+ return UI.HStack(restProps, ...parsedChildren);
192
+ }
193
+ if (isComp(ZStack, 'ZStack')) {
194
+ return UI.ZStack(restProps, ...parsedChildren);
195
+ }
196
+ if (isComp(Grid, 'Grid')) {
197
+ return UI.Grid(restProps, ...parsedChildren);
198
+ }
199
+ if (isComp(Text, 'Text')) {
200
+ const textContent =
201
+ typeof children === 'string' || typeof children === 'number'
202
+ ? String(children)
203
+ : restProps.text || '';
204
+ return UI.Text(textContent, restProps);
205
+ }
206
+ if (isComp(Button, 'Button')) {
207
+ const title =
208
+ restProps.title || (typeof children === 'string' ? children : '');
209
+ return UI.Button({ ...restProps, title });
210
+ }
211
+ if (isComp(Badge, 'Badge')) {
212
+ const text =
213
+ restProps.text || (typeof children === 'string' ? children : '');
214
+ return UI.Badge({ ...restProps, text });
215
+ }
216
+ if (isComp(Progress, 'Progress')) {
217
+ return UI.Progress(restProps.value ?? restProps.totalDuration ?? 0, restProps);
218
+ }
219
+ if (isComp(Timer, 'Timer')) {
220
+ return UI.Timer(restProps.targetTimestamp ?? Date.now(), restProps);
221
+ }
222
+ if (isComp(Image, 'Image')) {
223
+ return UI.Image(
224
+ restProps.url || restProps.base64 || restProps.systemName || restProps.source || '',
225
+ restProps
226
+ );
227
+ }
228
+ if (isComp(Spacer, 'Spacer')) {
229
+ return UI.Spacer(restProps.minLength);
230
+ }
231
+ if (isComp(Divider, 'Divider')) {
232
+ return UI.Divider(restProps);
233
+ }
234
+ if (isComp(Equalizer, 'Equalizer')) {
235
+ return UI.Equalizer(restProps);
236
+ }
237
+ if (isComp(Link, 'Link')) {
238
+ return UI.Link(restProps, ...parsedChildren);
239
+ }
240
+ if (isComp(Gauge, 'Gauge')) {
241
+ return UI.Gauge(restProps.value ?? 0, restProps);
242
+ }
243
+ if (isComp(Chart, 'Chart')) {
244
+ return UI.Chart(restProps.values ?? [], restProps);
245
+ }
246
+
247
+ // Component do người dùng tự viết: gọi hàm render để đệ quy phân giải
248
+ try {
249
+ const rendered = (type as any)(props);
250
+ return renderToWidget(rendered);
251
+ } catch {
252
+ return null;
253
+ }
254
+ }
255
+
256
+ // Trường hợp Component được truyền dạng string type
257
+ if (typeof type === 'string') {
258
+ switch (type) {
259
+ case 'VStack':
260
+ return UI.VStack(restProps, ...parsedChildren);
261
+ case 'HStack':
262
+ return UI.HStack(restProps, ...parsedChildren);
263
+ case 'ZStack':
264
+ return UI.ZStack(restProps, ...parsedChildren);
265
+ case 'Text': {
266
+ const textContent =
267
+ typeof children === 'string' || typeof children === 'number'
268
+ ? String(children)
269
+ : restProps.text || '';
270
+ return UI.Text(textContent, restProps);
271
+ }
272
+ case 'Button':
273
+ return UI.Button({
274
+ ...restProps,
275
+ title: restProps.title || (typeof children === 'string' ? children : ''),
276
+ });
277
+ case 'Badge':
278
+ return UI.Badge({
279
+ ...restProps,
280
+ text: restProps.text || (typeof children === 'string' ? children : ''),
281
+ });
282
+ case 'Progress':
283
+ return UI.Progress(restProps.value ?? 0, restProps);
284
+ case 'Image':
285
+ return UI.Image(restProps.url || restProps.base64 || restProps.systemName || '', restProps);
286
+ case 'Spacer':
287
+ return UI.Spacer(restProps.minLength);
288
+ case 'Divider':
289
+ return UI.Divider(restProps);
290
+ default:
291
+ return null;
292
+ }
293
+ }
294
+
295
+ return null;
296
+ }