@lynx-js/lynx-ui-common 0.0.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 (215) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +28 -0
  3. package/dist/const/BaseScrollEventsMapping.d.ts +2 -0
  4. package/dist/const/BaseScrollEventsMapping.d.ts.map +1 -0
  5. package/dist/const/BaseScrollEventsMapping.jsx +13 -0
  6. package/dist/const/BaseScrollEventsMapping.jsx.map +1 -0
  7. package/dist/const/BaseUIEventsMapping.d.ts +4 -0
  8. package/dist/const/BaseUIEventsMapping.d.ts.map +1 -0
  9. package/dist/const/BaseUIEventsMapping.jsx +24 -0
  10. package/dist/const/BaseUIEventsMapping.jsx.map +1 -0
  11. package/dist/const/index.d.ts +3 -0
  12. package/dist/const/index.d.ts.map +1 -0
  13. package/dist/const/index.jsx +2 -0
  14. package/dist/hooks/docs/useBounce.jsx +0 -0
  15. package/dist/hooks/docs/useRefresh.jsx +0 -0
  16. package/dist/hooks/index.d.ts +19 -0
  17. package/dist/hooks/index.d.ts.map +1 -0
  18. package/dist/hooks/index.jsx +73 -0
  19. package/dist/hooks/index.jsx.map +1 -0
  20. package/dist/hooks/useBounce.d.ts +26 -0
  21. package/dist/hooks/useBounce.d.ts.map +1 -0
  22. package/dist/hooks/useBounce.jsx +383 -0
  23. package/dist/hooks/useBounce.jsx.map +1 -0
  24. package/dist/hooks/useGCCollector.d.ts +18 -0
  25. package/dist/hooks/useGCCollector.d.ts.map +1 -0
  26. package/dist/hooks/useGCCollector.jsx +48 -0
  27. package/dist/hooks/useGCCollector.jsx.map +1 -0
  28. package/dist/hooks/useGlobalEventListener.d.ts +9 -0
  29. package/dist/hooks/useGlobalEventListener.d.ts.map +1 -0
  30. package/dist/hooks/useGlobalEventListener.jsx +19 -0
  31. package/dist/hooks/useGlobalEventListener.jsx.map +1 -0
  32. package/dist/hooks/useLatest.d.ts +6 -0
  33. package/dist/hooks/useLatest.d.ts.map +1 -0
  34. package/dist/hooks/useLatest.jsx +9 -0
  35. package/dist/hooks/useLatest.jsx.map +1 -0
  36. package/dist/hooks/useMemoizedFn.d.ts +8 -0
  37. package/dist/hooks/useMemoizedFn.d.ts.map +1 -0
  38. package/dist/hooks/useMemoizedFn.jsx +15 -0
  39. package/dist/hooks/useMemoizedFn.jsx.map +1 -0
  40. package/dist/hooks/usePrevious.d.ts +16 -0
  41. package/dist/hooks/usePrevious.d.ts.map +1 -0
  42. package/dist/hooks/usePrevious.jsx +11 -0
  43. package/dist/hooks/usePrevious.jsx.map +1 -0
  44. package/dist/hooks/useRefresh.d.ts +24 -0
  45. package/dist/hooks/useRefresh.d.ts.map +1 -0
  46. package/dist/hooks/useRefresh.jsx +554 -0
  47. package/dist/hooks/useRefresh.jsx.map +1 -0
  48. package/dist/hooks/useRegisteredEvents.d.ts +19 -0
  49. package/dist/hooks/useRegisteredEvents.d.ts.map +1 -0
  50. package/dist/hooks/useRegisteredEvents.jsx +24 -0
  51. package/dist/hooks/useRegisteredEvents.jsx.map +1 -0
  52. package/dist/hooks/useUnmount.d.ts +2 -0
  53. package/dist/hooks/useUnmount.d.ts.map +1 -0
  54. package/dist/hooks/useUnmount.jsx +11 -0
  55. package/dist/hooks/useUnmount.jsx.map +1 -0
  56. package/dist/index.d.ts +8 -0
  57. package/dist/index.d.ts.map +1 -0
  58. package/dist/index.jsx +6 -0
  59. package/dist/reactive/hooks/useReactiveValue.d.ts +22 -0
  60. package/dist/reactive/hooks/useReactiveValue.d.ts.map +1 -0
  61. package/dist/reactive/hooks/useReactiveValue.jsx +75 -0
  62. package/dist/reactive/hooks/useReactiveValue.jsx.map +1 -0
  63. package/dist/reactive/hooks/useReactiveValueChange.d.ts +4 -0
  64. package/dist/reactive/hooks/useReactiveValueChange.d.ts.map +1 -0
  65. package/dist/reactive/hooks/useReactiveValueChange.jsx +7 -0
  66. package/dist/reactive/hooks/useReactiveValueChange.jsx.map +1 -0
  67. package/dist/reactive/hooks/useReactiveValueEvent.d.ts +5 -0
  68. package/dist/reactive/hooks/useReactiveValueEvent.d.ts.map +1 -0
  69. package/dist/reactive/hooks/useReactiveValueEvent.jsx +15 -0
  70. package/dist/reactive/hooks/useReactiveValueEvent.jsx.map +1 -0
  71. package/dist/reactive/index.d.ts +6 -0
  72. package/dist/reactive/index.d.ts.map +1 -0
  73. package/dist/reactive/index.jsx +5 -0
  74. package/dist/reactive/types.d.ts +17 -0
  75. package/dist/reactive/types.d.ts.map +1 -0
  76. package/dist/reactive/types.jsx +0 -0
  77. package/dist/reactive/updateReactive.d.ts +4 -0
  78. package/dist/reactive/updateReactive.d.ts.map +1 -0
  79. package/dist/reactive/updateReactive.jsx +8 -0
  80. package/dist/reactive/updateReactive.jsx.map +1 -0
  81. package/dist/types/index.d.ts +2 -0
  82. package/dist/types/index.d.ts.map +1 -0
  83. package/dist/types/index.jsx +0 -0
  84. package/dist/types/interfaces/BaseScrollEvents.d.ts +40 -0
  85. package/dist/types/interfaces/BaseScrollEvents.d.ts.map +1 -0
  86. package/dist/types/interfaces/BaseScrollEvents.jsx +0 -0
  87. package/dist/types/interfaces/BaseUIEvents.d.ts +132 -0
  88. package/dist/types/interfaces/BaseUIEvents.d.ts.map +1 -0
  89. package/dist/types/interfaces/BaseUIEvents.jsx +0 -0
  90. package/dist/types/interfaces/BaseUIProps.d.ts +19 -0
  91. package/dist/types/interfaces/BaseUIProps.d.ts.map +1 -0
  92. package/dist/types/interfaces/BaseUIProps.jsx +0 -0
  93. package/dist/types/interfaces/BounceableInterface.d.ts +120 -0
  94. package/dist/types/interfaces/BounceableInterface.d.ts.map +1 -0
  95. package/dist/types/interfaces/BounceableInterface.jsx +0 -0
  96. package/dist/types/interfaces/EventMTSInterface.d.ts +1 -0
  97. package/dist/types/interfaces/EventMTSInterface.d.ts.map +1 -0
  98. package/dist/types/interfaces/EventMTSInterface.jsx +0 -0
  99. package/dist/types/interfaces/LazyInterface.d.ts +72 -0
  100. package/dist/types/interfaces/LazyInterface.d.ts.map +1 -0
  101. package/dist/types/interfaces/LazyInterface.jsx +0 -0
  102. package/dist/types/interfaces/RefreshInterface.d.ts +98 -0
  103. package/dist/types/interfaces/RefreshInterface.d.ts.map +1 -0
  104. package/dist/types/interfaces/RefreshInterface.jsx +10 -0
  105. package/dist/types/interfaces/RefreshInterface.jsx.map +1 -0
  106. package/dist/types/interfaces/index.d.ts +36 -0
  107. package/dist/types/interfaces/index.d.ts.map +1 -0
  108. package/dist/types/interfaces/index.jsx +0 -0
  109. package/dist/utils/common.d.ts +15 -0
  110. package/dist/utils/common.d.ts.map +1 -0
  111. package/dist/utils/common.jsx +12 -0
  112. package/dist/utils/common.jsx.map +1 -0
  113. package/dist/utils/common.test.jsx +55 -0
  114. package/dist/utils/common.test.jsx.map +1 -0
  115. package/dist/utils/convertToPx.d.ts +4 -0
  116. package/dist/utils/convertToPx.d.ts.map +1 -0
  117. package/dist/utils/convertToPx.jsx +20 -0
  118. package/dist/utils/convertToPx.jsx.map +1 -0
  119. package/dist/utils/delayFrames.d.ts +2 -0
  120. package/dist/utils/delayFrames.d.ts.map +1 -0
  121. package/dist/utils/delayFrames.jsx +12 -0
  122. package/dist/utils/delayFrames.jsx.map +1 -0
  123. package/dist/utils/getEventDetail.d.ts +23 -0
  124. package/dist/utils/getEventDetail.d.ts.map +1 -0
  125. package/dist/utils/getEventDetail.jsx +7 -0
  126. package/dist/utils/getEventDetail.jsx.map +1 -0
  127. package/dist/utils/index.d.ts +13 -0
  128. package/dist/utils/index.d.ts.map +1 -0
  129. package/dist/utils/index.jsx +12 -0
  130. package/dist/utils/interpolation.d.ts +40 -0
  131. package/dist/utils/interpolation.d.ts.map +1 -0
  132. package/dist/utils/interpolation.jsx +86 -0
  133. package/dist/utils/interpolation.jsx.map +1 -0
  134. package/dist/utils/interpolationJS.d.ts +39 -0
  135. package/dist/utils/interpolationJS.d.ts.map +1 -0
  136. package/dist/utils/interpolationJS.jsx +80 -0
  137. package/dist/utils/interpolationJS.jsx.map +1 -0
  138. package/dist/utils/log.d.ts +3 -0
  139. package/dist/utils/log.d.ts.map +1 -0
  140. package/dist/utils/log.jsx +10 -0
  141. package/dist/utils/log.jsx.map +1 -0
  142. package/dist/utils/mainThreadify.d.ts +2 -0
  143. package/dist/utils/mainThreadify.d.ts.map +1 -0
  144. package/dist/utils/mainThreadify.jsx +11 -0
  145. package/dist/utils/mainThreadify.jsx.map +1 -0
  146. package/dist/utils/popoverUtils.d.ts +3 -0
  147. package/dist/utils/popoverUtils.d.ts.map +1 -0
  148. package/dist/utils/popoverUtils.jsx +12 -0
  149. package/dist/utils/popoverUtils.jsx.map +1 -0
  150. package/dist/utils/renderContentWithExtraProps.d.ts +5 -0
  151. package/dist/utils/renderContentWithExtraProps.d.ts.map +1 -0
  152. package/dist/utils/renderContentWithExtraProps.jsx +10 -0
  153. package/dist/utils/renderContentWithExtraProps.jsx.map +1 -0
  154. package/dist/utils/selector.d.ts +36 -0
  155. package/dist/utils/selector.d.ts.map +1 -0
  156. package/dist/utils/selector.jsx +85 -0
  157. package/dist/utils/selector.jsx.map +1 -0
  158. package/dist/utils/selector.test.jsx +215 -0
  159. package/dist/utils/selector.test.jsx.map +1 -0
  160. package/dist/utils/version.d.ts +6 -0
  161. package/dist/utils/version.d.ts.map +1 -0
  162. package/dist/utils/version.jsx +28 -0
  163. package/dist/utils/version.jsx.map +1 -0
  164. package/dist/utils/version.test.jsx +77 -0
  165. package/dist/utils/version.test.jsx.map +1 -0
  166. package/package.json +52 -0
  167. package/src/const/BaseScrollEventsMapping.ts +14 -0
  168. package/src/const/BaseUIEventsMapping.ts +25 -0
  169. package/src/const/index.ts +6 -0
  170. package/src/hooks/docs/useBounce.mdx +2 -0
  171. package/src/hooks/docs/useRefresh.mdx +2 -0
  172. package/src/hooks/index.ts +96 -0
  173. package/src/hooks/useBounce.tsx +814 -0
  174. package/src/hooks/useGCCollector.tsx +76 -0
  175. package/src/hooks/useGlobalEventListener.ts +50 -0
  176. package/src/hooks/useLatest.ts +16 -0
  177. package/src/hooks/useMemoizedFn.ts +35 -0
  178. package/src/hooks/usePrevious.ts +28 -0
  179. package/src/hooks/useRefresh.ts +1054 -0
  180. package/src/hooks/useRegisteredEvents.ts +69 -0
  181. package/src/hooks/useUnmount.ts +18 -0
  182. package/src/index.tsx +25 -0
  183. package/src/reactive/hooks/useReactiveValue.ts +132 -0
  184. package/src/reactive/hooks/useReactiveValueChange.ts +15 -0
  185. package/src/reactive/hooks/useReactiveValueEvent.ts +26 -0
  186. package/src/reactive/index.ts +20 -0
  187. package/src/reactive/types.ts +29 -0
  188. package/src/reactive/updateReactive.ts +16 -0
  189. package/src/types/index.ts +5 -0
  190. package/src/types/interfaces/BaseScrollEvents.ts +47 -0
  191. package/src/types/interfaces/BaseUIEvents.ts +142 -0
  192. package/src/types/interfaces/BaseUIProps.ts +22 -0
  193. package/src/types/interfaces/BounceableInterface.ts +125 -0
  194. package/src/types/interfaces/EventMTSInterface.ts +35 -0
  195. package/src/types/interfaces/LazyInterface.tsx +78 -0
  196. package/src/types/interfaces/RefreshInterface.ts +105 -0
  197. package/src/types/interfaces/index.ts +44 -0
  198. package/src/utils/common.test.ts +64 -0
  199. package/src/utils/common.ts +35 -0
  200. package/src/utils/convertToPx.ts +32 -0
  201. package/src/utils/delayFrames.ts +24 -0
  202. package/src/utils/getEventDetail.ts +36 -0
  203. package/src/utils/index.ts +16 -0
  204. package/src/utils/interpolation.ts +222 -0
  205. package/src/utils/interpolationJS.ts +212 -0
  206. package/src/utils/log.ts +16 -0
  207. package/src/utils/mainThreadify.ts +17 -0
  208. package/src/utils/popoverUtils.ts +25 -0
  209. package/src/utils/renderContentWithExtraProps.ts +20 -0
  210. package/src/utils/selector.test.ts +237 -0
  211. package/src/utils/selector.ts +222 -0
  212. package/src/utils/version.test.ts +98 -0
  213. package/src/utils/version.ts +50 -0
  214. package/tsconfig.docs.json +7 -0
  215. package/tsconfig.json +12 -0
@@ -0,0 +1,237 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { describe, it, expect, vi, beforeEach } from 'vitest'
6
+ import {
7
+ InvokeRejectError,
8
+ setNativePropsByRef,
9
+ setNativePropsById,
10
+ setNativeProps,
11
+ invokeByRef,
12
+ invokeById,
13
+ getRectByRef,
14
+ getRectById,
15
+ getRect,
16
+ } from './selector'
17
+
18
+ // Mock the global lynx object
19
+ const mockExec = vi.fn()
20
+ const mockSelect = vi.fn().mockReturnThis()
21
+ const mockSetNativeProps = vi.fn().mockReturnThis()
22
+ const mockInvoke = vi.fn().mockReturnThis()
23
+
24
+ global.lynx = {
25
+ // @ts-expect-error expected
26
+ createSelectorQuery: () => ({
27
+ select: mockSelect,
28
+ setNativeProps: mockSetNativeProps,
29
+ invoke: mockInvoke,
30
+ exec: mockExec,
31
+ }),
32
+ }
33
+
34
+ describe('selector utils', () => {
35
+ beforeEach(() => {
36
+ vi.clearAllMocks()
37
+ })
38
+
39
+ describe('InvokeRejectError', () => {
40
+ it('should create error with code and message', () => {
41
+ const error = new InvokeRejectError(404, 'Not Found')
42
+ expect(error.errorCode).toBe(404)
43
+ expect(error.message).toBe('Not Found')
44
+ })
45
+ })
46
+
47
+ describe('setNativeProps functions', () => {
48
+ const testProps = { style: { color: 'red' } }
49
+
50
+ describe('setNativePropsByRef', () => {
51
+ it('should call setNativeProps with ref', () => {
52
+ const mockRef = {
53
+ current: {
54
+ setNativeProps: vi.fn().mockReturnThis(),
55
+ exec: vi.fn(),
56
+ },
57
+ }
58
+ // @ts-expect-error expected
59
+ setNativePropsByRef(mockRef, testProps)
60
+ expect(mockRef.current.setNativeProps).toHaveBeenCalledWith(testProps)
61
+ expect(mockRef.current.exec).toHaveBeenCalled()
62
+ })
63
+ })
64
+
65
+ describe('setNativePropsById', () => {
66
+ it('should call setNativeProps with id', () => {
67
+ setNativePropsById('test-id', testProps)
68
+ expect(mockSelect).toHaveBeenCalledWith('#test-id')
69
+ expect(mockSetNativeProps).toHaveBeenCalledWith(testProps)
70
+ expect(mockExec).toHaveBeenCalled()
71
+ })
72
+ })
73
+
74
+ describe('setNativeProps', () => {
75
+ it('should use id when available', () => {
76
+ setNativeProps({ id: 'test-id' }, testProps)
77
+ expect(mockSelect).toHaveBeenCalledWith('#test-id')
78
+ })
79
+
80
+ it('should use ref when id is not available', () => {
81
+ const mockRef = {
82
+ current: {
83
+ setNativeProps: vi.fn().mockReturnThis(),
84
+ exec: vi.fn(),
85
+ },
86
+ }
87
+ // @ts-expect-error expected
88
+ setNativeProps({ ref: mockRef }, testProps)
89
+ expect(mockRef.current.setNativeProps).toHaveBeenCalledWith(testProps)
90
+ })
91
+ })
92
+ })
93
+
94
+ describe('invoke functions', () => {
95
+ const method = 'testMethod'
96
+ const params = { test: true }
97
+
98
+ describe('invokeByRef', () => {
99
+ it('should resolve successfully', async () => {
100
+ const successResponse = { data: 'success' }
101
+ const mockRef = {
102
+ current: {
103
+ invoke: vi.fn().mockImplementation(({ success }) => ({
104
+ exec: () => success(successResponse),
105
+ })),
106
+ },
107
+ }
108
+
109
+ // @ts-expect-error expected
110
+ const result = await invokeByRef(mockRef, method, params)
111
+ expect(result).toBe(successResponse)
112
+ })
113
+
114
+ it('should reject when no ref current', async () => {
115
+ // @ts-expect-error expected
116
+ await expect(invokeByRef({}, method, params)).rejects.toThrow(
117
+ 'no node found for the ref',
118
+ )
119
+ })
120
+
121
+ it('should reject on failure', async () => {
122
+ const mockRef = {
123
+ current: {
124
+ invoke: vi.fn().mockImplementation(({ fail }) => ({
125
+ exec: () => fail({ code: 500, data: 'error' }),
126
+ })),
127
+ },
128
+ }
129
+
130
+ // @ts-expect-error expected
131
+ await expect(invokeByRef(mockRef, method, params)).rejects.toThrow()
132
+ })
133
+ })
134
+
135
+ describe('invokeById', () => {
136
+ it('should resolve successfully', async () => {
137
+ const successResponse = { data: 'success' }
138
+ mockInvoke.mockImplementation(({ success }) => {
139
+ success(successResponse)
140
+ return { exec: mockExec }
141
+ })
142
+
143
+ const result = await invokeById('test-id', method, params)
144
+ expect(result).toBe(successResponse)
145
+ })
146
+
147
+ it('should reject on failure', async () => {
148
+ mockInvoke.mockImplementation(({ fail }) => {
149
+ fail({ code: 500, data: 'error' })
150
+ return { exec: mockExec }
151
+ })
152
+
153
+ await expect(invokeById('test-id', method, params)).rejects.toThrow()
154
+ })
155
+ })
156
+ })
157
+
158
+ describe('getRect functions', () => {
159
+ const mockRect = {
160
+ left: 0,
161
+ top: 0,
162
+ width: 100,
163
+ height: 100,
164
+ }
165
+
166
+ describe('getRectByRef', () => {
167
+ it('should get rect relative to viewport', async () => {
168
+ const mockRef = {
169
+ current: {
170
+ invoke: vi.fn().mockImplementation(({ success }) => ({
171
+ exec: () => success(mockRect),
172
+ })),
173
+ },
174
+ }
175
+
176
+ // @ts-expect-error expected
177
+ const result = await getRectByRef(mockRef)
178
+ expect(result).toEqual(mockRect)
179
+ })
180
+
181
+ it('should get rect relative to screen', async () => {
182
+ const mockRef = {
183
+ current: {
184
+ invoke: vi.fn().mockImplementation(({ success, params }) => ({
185
+ exec: () => {
186
+ expect(params.relativeTo).toBe('screen')
187
+ success(mockRect)
188
+ },
189
+ })),
190
+ },
191
+ }
192
+
193
+ // @ts-expect-error expected
194
+ const result = await getRectByRef(mockRef, true)
195
+ expect(result).toEqual(mockRect)
196
+ })
197
+ })
198
+
199
+ describe('getRectById', () => {
200
+ it('should get rect by id', async () => {
201
+ mockInvoke.mockImplementation(({ success }) => {
202
+ success(mockRect)
203
+ return { exec: mockExec }
204
+ })
205
+
206
+ const result = await getRectById('test-id')
207
+ expect(result).toEqual(mockRect)
208
+ })
209
+ })
210
+
211
+ describe('getRect', () => {
212
+ it('should use id when available', async () => {
213
+ mockInvoke.mockImplementation(({ success }) => {
214
+ success(mockRect)
215
+ return { exec: mockExec }
216
+ })
217
+
218
+ const result = await getRect({ id: 'test-id' })
219
+ expect(result).toEqual(mockRect)
220
+ })
221
+
222
+ it('should use ref when id is not available', async () => {
223
+ const mockRef = {
224
+ current: {
225
+ invoke: vi.fn().mockImplementation(({ success }) => ({
226
+ exec: () => success(mockRect),
227
+ })),
228
+ },
229
+ }
230
+
231
+ // @ts-expect-error expected
232
+ const result = await getRect({ ref: mockRef })
233
+ expect(result).toEqual(mockRect)
234
+ })
235
+ })
236
+ })
237
+ })
@@ -0,0 +1,222 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import type { RefObject } from '@lynx-js/react'
6
+
7
+ import type { AnyObject, MainThread, NodesRef } from '@lynx-js/types'
8
+
9
+ export class InvokeRejectError extends Error {
10
+ errorCode: number
11
+ detail?: object | string
12
+ constructor(errorCode: number, errorMsg?: string) {
13
+ super(typeof errorMsg === 'string' ? errorMsg : 'unknown error')
14
+ this.errorCode = errorCode
15
+ this.detail = errorMsg
16
+ }
17
+ }
18
+
19
+ export const setNativePropsByRef = (
20
+ ref: RefObject<NodesRef> | undefined,
21
+ // biome-ignore lint/suspicious/noExplicitAny: expected
22
+ props: Record<string, any>,
23
+ ) => {
24
+ ref?.current?.setNativeProps(props).exec()
25
+ }
26
+
27
+ export const setNativePropsById = (
28
+ id: string,
29
+ // biome-ignore lint/suspicious/noExplicitAny: expected
30
+ props: Record<string, any>,
31
+ ) => {
32
+ lynx.createSelectorQuery().select(`#${id}`).setNativeProps(props).exec()
33
+ }
34
+
35
+ export const setNativeProps = (
36
+ target: {
37
+ ref?: RefObject<NodesRef>
38
+ id?: string
39
+ },
40
+ // biome-ignore lint/suspicious/noExplicitAny: expected
41
+ props: Record<string, any>,
42
+ ) =>
43
+ target.id
44
+ ? setNativePropsById(target.id, props)
45
+ : setNativePropsByRef(target.ref, props)
46
+
47
+ export const invokeByRef = (
48
+ ref: RefObject<NodesRef> | undefined,
49
+ method: string,
50
+ params?: AnyObject,
51
+ ): Promise<unknown> =>
52
+ new Promise((resolve, reject) => {
53
+ if (!ref?.current) {
54
+ reject(new InvokeRejectError(2, 'no node found for the ref'))
55
+ return
56
+ }
57
+ ref?.current
58
+ ?.invoke({
59
+ method,
60
+ params,
61
+ success: (res) => {
62
+ resolve(res)
63
+ },
64
+ // biome-ignore lint/suspicious/noExplicitAny:expected
65
+ fail: (res: { code: number, data?: any }) => {
66
+ reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))
67
+ },
68
+ })
69
+ .exec()
70
+ })
71
+
72
+ export const invokeById = (
73
+ id: string,
74
+ method: string,
75
+ params?: AnyObject,
76
+ ): Promise<unknown> =>
77
+ new Promise((resolve, reject) => {
78
+ lynx
79
+ .createSelectorQuery()
80
+ .select(`#${id}`)
81
+ .invoke({
82
+ method,
83
+ params,
84
+ success: (res) => {
85
+ resolve(res)
86
+ },
87
+ // biome-ignore lint/suspicious/noExplicitAny:expected
88
+ fail: (res: { code: number, data?: any }) => {
89
+ reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))
90
+ },
91
+ })
92
+ .exec()
93
+ })
94
+
95
+ export const invoke = (
96
+ target: {
97
+ ref?: RefObject<NodesRef>
98
+ id?: string
99
+ },
100
+ method: string,
101
+ params?: AnyObject,
102
+ ): Promise<unknown> =>
103
+ target.id
104
+ ? invokeById(target.id, method, params)
105
+ : invokeByRef(target.ref, method, params)
106
+
107
+ export interface GetRectPromise {
108
+ left: number
109
+ top: number
110
+ width: number
111
+ height: number
112
+ bottom: number
113
+ right: number
114
+ }
115
+
116
+ export const getRectByRef = (
117
+ ref?: RefObject<NodesRef>,
118
+ relativeToScreen = false,
119
+ relativeTo = '',
120
+ ): Promise<GetRectPromise> =>
121
+ new Promise((resolve, reject) => {
122
+ invokeByRef(ref, 'boundingClientRect', {
123
+ relativeTo: relativeToScreen ? 'screen' : relativeTo,
124
+ androidEnableTransformProps: true,
125
+ })
126
+ .then((res) => {
127
+ resolve(
128
+ res as {
129
+ left: number
130
+ top: number
131
+ width: number
132
+ height: number
133
+ bottom: number
134
+ right: number
135
+ },
136
+ )
137
+ })
138
+ .catch((error: InvokeRejectError) => {
139
+ reject(error)
140
+ })
141
+ })
142
+
143
+ export const getRootRect = (
144
+ relativeToScreen = false,
145
+ relativeTo = '',
146
+ ): Promise<GetRectPromise> =>
147
+ new Promise((resolve, reject) => {
148
+ lynx
149
+ .createSelectorQuery()
150
+ .selectRoot()
151
+ .invoke({
152
+ method: 'boundingClientRect',
153
+ params: {
154
+ relativeTo: relativeToScreen ? 'screen' : relativeTo,
155
+ androidEnableTransformProps: true,
156
+ },
157
+ success: (res) => {
158
+ resolve(
159
+ res as {
160
+ left: number
161
+ top: number
162
+ width: number
163
+ height: number
164
+ bottom: number
165
+ right: number
166
+ },
167
+ )
168
+ },
169
+ fail: (res: { code: number, data?: object }) => {
170
+ reject(new InvokeRejectError(res.code, JSON.stringify(res.data)))
171
+ },
172
+ })
173
+ .exec()
174
+ })
175
+
176
+ export const getRectById = (
177
+ id: string,
178
+ relativeToScreen = false,
179
+ relativeTo = '',
180
+ ): Promise<GetRectPromise> =>
181
+ new Promise((resolve, reject) => {
182
+ invokeById(id, 'boundingClientRect', {
183
+ relativeTo: relativeToScreen ? 'screen' : relativeTo,
184
+ androidEnableTransformProps: true,
185
+ })
186
+ .then((res) => {
187
+ resolve(
188
+ res as {
189
+ left: number
190
+ top: number
191
+ width: number
192
+ height: number
193
+ bottom: number
194
+ right: number
195
+ },
196
+ )
197
+ })
198
+ .catch((error: InvokeRejectError) => {
199
+ reject(error)
200
+ })
201
+ })
202
+
203
+ export const getRect = (
204
+ target: {
205
+ ref?: RefObject<NodesRef>
206
+ id?: string
207
+ },
208
+ relativeToScreen = false,
209
+ ): Promise<GetRectPromise> =>
210
+ target.id
211
+ ? getRectById(target.id, relativeToScreen)
212
+ : getRectByRef(target.ref, relativeToScreen)
213
+
214
+ export const selectorMT: (selector: string) => MainThread.Element | null = (
215
+ selector: string,
216
+ ) => {
217
+ 'main thread'
218
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
219
+ // @ts-ignore
220
+
221
+ return lynx.querySelector(`#${selector}`)
222
+ }
@@ -0,0 +1,98 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { describe, it, expect, beforeEach } from 'vitest'
6
+ import {
7
+ lynxSDKVersionStringToNumber,
8
+ nativeLynxSDKVersionGreaterThan,
9
+ nativeLynxSDKVersionLessThan,
10
+ } from './version'
11
+
12
+ function createMockSystemInfo(lynxSdkVersion: string): typeof SystemInfo {
13
+ return {
14
+ lynxSdkVersion,
15
+ osVersion: '1.0',
16
+ pixelHeight: 100,
17
+ pixelWidth: 100,
18
+ pixelRatio: 1,
19
+ platform: 'Android',
20
+ runtimeType: 'quickjs',
21
+ }
22
+ }
23
+
24
+ describe('version utils', () => {
25
+ describe('lynxSDKVersionStringToNumber', () => {
26
+ it('should convert version string to number correctly', () => {
27
+ expect(lynxSDKVersionStringToNumber('1.2.3')).toBe(10203)
28
+ expect(lynxSDKVersionStringToNumber('2.0.0')).toBe(20000)
29
+ expect(lynxSDKVersionStringToNumber('0.1.0')).toBe(100)
30
+ })
31
+
32
+ it('should handle missing version parts', () => {
33
+ expect(lynxSDKVersionStringToNumber('1')).toBe(10000)
34
+ expect(lynxSDKVersionStringToNumber('1.2')).toBe(10200)
35
+ expect(lynxSDKVersionStringToNumber('')).toBe(0)
36
+ })
37
+
38
+ it('should handle version strings with leading zeros', () => {
39
+ expect(lynxSDKVersionStringToNumber('01.02.03')).toBe(10203)
40
+ })
41
+
42
+ it('should handle large version numbers', () => {
43
+ expect(lynxSDKVersionStringToNumber('99.99.99')).toBe(999999)
44
+ })
45
+ })
46
+
47
+ describe('nativeLynxSDKVersionGreaterThan', () => {
48
+ beforeEach(() => {
49
+ global.SystemInfo = createMockSystemInfo('1.2.3')
50
+ })
51
+
52
+ it('should return true when native version is greater', () => {
53
+ global.SystemInfo = createMockSystemInfo('1.2.4')
54
+ expect(nativeLynxSDKVersionGreaterThan('1.2.3')).toBe(true)
55
+ })
56
+
57
+ it('should return false when native version is equal', () => {
58
+ global.SystemInfo = createMockSystemInfo('1.2.3')
59
+ expect(nativeLynxSDKVersionGreaterThan('1.2.3')).toBe(false)
60
+ })
61
+
62
+ it('should return false when native version is less', () => {
63
+ global.SystemInfo = createMockSystemInfo('1.2.2')
64
+ expect(nativeLynxSDKVersionGreaterThan('1.2.3')).toBe(false)
65
+ })
66
+ })
67
+
68
+ describe('nativeLynxSDKVersionLessThan', () => {
69
+ beforeEach(() => {
70
+ global.SystemInfo = createMockSystemInfo('1.2.3')
71
+ })
72
+
73
+ it('should return true when native version is less', () => {
74
+ global.SystemInfo = createMockSystemInfo('1.2.2')
75
+ expect(nativeLynxSDKVersionLessThan('1.2.3')).toBe(true)
76
+ })
77
+
78
+ it('should return false when native version is equal', () => {
79
+ global.SystemInfo = createMockSystemInfo('1.2.3')
80
+ expect(nativeLynxSDKVersionLessThan('1.2.3')).toBe(false)
81
+ })
82
+
83
+ it('should return false when native version is greater', () => {
84
+ global.SystemInfo = createMockSystemInfo('1.2.4')
85
+ expect(nativeLynxSDKVersionLessThan('1.2.3')).toBe(false)
86
+ })
87
+
88
+ it('should handle major version differences', () => {
89
+ global.SystemInfo = createMockSystemInfo('1.0.0')
90
+ expect(nativeLynxSDKVersionLessThan('2.0.0')).toBe(true)
91
+ })
92
+
93
+ it('should handle minor version differences', () => {
94
+ global.SystemInfo = createMockSystemInfo('1.1.0')
95
+ expect(nativeLynxSDKVersionLessThan('1.2.0')).toBe(true)
96
+ })
97
+ })
98
+ })
@@ -0,0 +1,50 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ export function lynxSDKVersionStringToNumber(lynxSDKVersion: string): number {
6
+ const ver: string[] = lynxSDKVersion.split('.')
7
+ const major: number = Number(ver[0] ? ver[0] : 0) * 10000
8
+ const minor: number = Number(ver[1] ? ver[1] : 0) * 100
9
+ const patch = Number(ver[2] ? ver[2] : 0)
10
+ return major + minor + patch
11
+ }
12
+
13
+ export function mtsLynxSDKVersionStringToNumber(
14
+ lynxSDKVersion: string,
15
+ ): number {
16
+ 'main thread'
17
+ const ver: string[] = lynxSDKVersion.split('.')
18
+ const major: number = Number(ver[0] ? ver[0] : 0) * 10000
19
+ const minor: number = Number(ver[1] ? ver[1] : 0) * 100
20
+ const patch = Number(ver[2] ? ver[2] : 0)
21
+ return major + minor + patch
22
+ }
23
+
24
+ export function nativeLynxSDKVersionGreaterThan(
25
+ lynxSDKVersion: string,
26
+ ): boolean {
27
+ return (
28
+ lynxSDKVersionStringToNumber(SystemInfo.lynxSdkVersion)
29
+ > lynxSDKVersionStringToNumber(lynxSDKVersion)
30
+ )
31
+ }
32
+
33
+ export function nativeLynxSDKVersionLessThan(lynxSDKVersion: string): boolean {
34
+ return (
35
+ lynxSDKVersionStringToNumber(SystemInfo.lynxSdkVersion)
36
+ < lynxSDKVersionStringToNumber(lynxSDKVersion)
37
+ )
38
+ }
39
+
40
+ export function mtsNativeLynxSDKVersionLessThan(
41
+ lynxSDKVersion: string,
42
+ ): boolean {
43
+ 'main thread'
44
+ return (
45
+ mtsLynxSDKVersionStringToNumber(
46
+ SystemInfo.engineVersion ?? SystemInfo.lynxSdkVersion,
47
+ )
48
+ < mtsLynxSDKVersionStringToNumber(lynxSDKVersion)
49
+ )
50
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tools/configs/tsconfig.docs.json",
3
+ "include": [
4
+ "src",
5
+ "dist/types/index.docs.d.ts"
6
+ ]
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../../tools/configs/tsconfig.lib.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./src",
5
+ },
6
+ "include": [
7
+ "src",
8
+ ],
9
+ "exclude": [
10
+ "**/*.test.ts",
11
+ ],
12
+ }