@solidtv/solid 1.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 (237) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +7 -0
  3. package/README.md +95 -0
  4. package/dist/jsx-runtime.d.ts +14 -0
  5. package/dist/src/activeElement.d.ts +2 -0
  6. package/dist/src/activeElement.js +3 -0
  7. package/dist/src/activeElement.js.map +1 -0
  8. package/dist/src/core/config.d.ts +51 -0
  9. package/dist/src/core/config.js +37 -0
  10. package/dist/src/core/config.js.map +1 -0
  11. package/dist/src/core/dom-renderer/domRenderer.d.ts +138 -0
  12. package/dist/src/core/dom-renderer/domRenderer.js +1844 -0
  13. package/dist/src/core/dom-renderer/domRenderer.js.map +1 -0
  14. package/dist/src/core/dom-renderer/domRendererTypes.d.ts +110 -0
  15. package/dist/src/core/dom-renderer/domRendererTypes.js +2 -0
  16. package/dist/src/core/dom-renderer/domRendererTypes.js.map +1 -0
  17. package/dist/src/core/dom-renderer/domRendererUtils.d.ts +23 -0
  18. package/dist/src/core/dom-renderer/domRendererUtils.js +232 -0
  19. package/dist/src/core/dom-renderer/domRendererUtils.js.map +1 -0
  20. package/dist/src/core/elementNode.d.ts +508 -0
  21. package/dist/src/core/elementNode.js +928 -0
  22. package/dist/src/core/elementNode.js.map +1 -0
  23. package/dist/src/core/flex.d.ts +2 -0
  24. package/dist/src/core/flex.js +284 -0
  25. package/dist/src/core/flex.js.map +1 -0
  26. package/dist/src/core/flexLayout.d.ts +2 -0
  27. package/dist/src/core/flexLayout.js +360 -0
  28. package/dist/src/core/flexLayout.js.map +1 -0
  29. package/dist/src/core/focusKeyTypes.d.ts +42 -0
  30. package/dist/src/core/focusKeyTypes.js +2 -0
  31. package/dist/src/core/focusKeyTypes.js.map +1 -0
  32. package/dist/src/core/focusManager.d.ts +13 -0
  33. package/dist/src/core/focusManager.js +270 -0
  34. package/dist/src/core/focusManager.js.map +1 -0
  35. package/dist/src/core/index.d.ts +14 -0
  36. package/dist/src/core/index.js +11 -0
  37. package/dist/src/core/index.js.map +1 -0
  38. package/dist/src/core/intrinsicTypes.d.ts +106 -0
  39. package/dist/src/core/intrinsicTypes.js +2 -0
  40. package/dist/src/core/intrinsicTypes.js.map +1 -0
  41. package/dist/src/core/lightningInit.d.ts +9 -0
  42. package/dist/src/core/lightningInit.js +34 -0
  43. package/dist/src/core/lightningInit.js.map +1 -0
  44. package/dist/src/core/nodeTypes.d.ts +6 -0
  45. package/dist/src/core/nodeTypes.js +6 -0
  46. package/dist/src/core/nodeTypes.js.map +1 -0
  47. package/dist/src/core/shaders.d.ts +52 -0
  48. package/dist/src/core/shaders.js +86 -0
  49. package/dist/src/core/shaders.js.map +1 -0
  50. package/dist/src/core/states.d.ts +12 -0
  51. package/dist/src/core/states.js +84 -0
  52. package/dist/src/core/states.js.map +1 -0
  53. package/dist/src/core/utils.d.ts +39 -0
  54. package/dist/src/core/utils.js +164 -0
  55. package/dist/src/core/utils.js.map +1 -0
  56. package/dist/src/devtools/index.d.ts +6 -0
  57. package/dist/src/devtools/index.js +65 -0
  58. package/dist/src/devtools/index.js.map +1 -0
  59. package/dist/src/index.d.ts +9 -0
  60. package/dist/src/index.js +7 -0
  61. package/dist/src/index.js.map +1 -0
  62. package/dist/src/primitives/Column.d.ts +3 -0
  63. package/dist/src/primitives/Column.jsx +25 -0
  64. package/dist/src/primitives/Column.jsx.map +1 -0
  65. package/dist/src/primitives/FPSCounter.d.ts +4 -0
  66. package/dist/src/primitives/FPSCounter.jsx +188 -0
  67. package/dist/src/primitives/FPSCounter.jsx.map +1 -0
  68. package/dist/src/primitives/FadeInOut.d.ts +18 -0
  69. package/dist/src/primitives/FadeInOut.jsx +39 -0
  70. package/dist/src/primitives/FadeInOut.jsx.map +1 -0
  71. package/dist/src/primitives/Grid.d.ts +23 -0
  72. package/dist/src/primitives/Grid.jsx +100 -0
  73. package/dist/src/primitives/Grid.jsx.map +1 -0
  74. package/dist/src/primitives/Image.d.ts +8 -0
  75. package/dist/src/primitives/Image.jsx +42 -0
  76. package/dist/src/primitives/Image.jsx.map +1 -0
  77. package/dist/src/primitives/KeepAlive.d.ts +42 -0
  78. package/dist/src/primitives/KeepAlive.jsx +160 -0
  79. package/dist/src/primitives/KeepAlive.jsx.map +1 -0
  80. package/dist/src/primitives/Lazy.d.ts +15 -0
  81. package/dist/src/primitives/Lazy.jsx +95 -0
  82. package/dist/src/primitives/Lazy.jsx.map +1 -0
  83. package/dist/src/primitives/LazyImport.d.ts +8 -0
  84. package/dist/src/primitives/LazyImport.js +40 -0
  85. package/dist/src/primitives/LazyImport.js.map +1 -0
  86. package/dist/src/primitives/Marquee.d.ts +64 -0
  87. package/dist/src/primitives/Marquee.jsx +86 -0
  88. package/dist/src/primitives/Marquee.jsx.map +1 -0
  89. package/dist/src/primitives/Preserve.d.ts +4 -0
  90. package/dist/src/primitives/Preserve.jsx +11 -0
  91. package/dist/src/primitives/Preserve.jsx.map +1 -0
  92. package/dist/src/primitives/Row.d.ts +3 -0
  93. package/dist/src/primitives/Row.jsx +24 -0
  94. package/dist/src/primitives/Row.jsx.map +1 -0
  95. package/dist/src/primitives/Suspense.d.ts +22 -0
  96. package/dist/src/primitives/Suspense.jsx +33 -0
  97. package/dist/src/primitives/Suspense.jsx.map +1 -0
  98. package/dist/src/primitives/Virtual.d.ts +18 -0
  99. package/dist/src/primitives/Virtual.jsx +495 -0
  100. package/dist/src/primitives/Virtual.jsx.map +1 -0
  101. package/dist/src/primitives/VirtualGrid.d.ts +13 -0
  102. package/dist/src/primitives/VirtualGrid.jsx +160 -0
  103. package/dist/src/primitives/VirtualGrid.jsx.map +1 -0
  104. package/dist/src/primitives/Visible.d.ts +6 -0
  105. package/dist/src/primitives/Visible.jsx +37 -0
  106. package/dist/src/primitives/Visible.jsx.map +1 -0
  107. package/dist/src/primitives/announcer/announcer.d.ts +40 -0
  108. package/dist/src/primitives/announcer/announcer.js +125 -0
  109. package/dist/src/primitives/announcer/announcer.js.map +1 -0
  110. package/dist/src/primitives/announcer/index.d.ts +6 -0
  111. package/dist/src/primitives/announcer/index.js +15 -0
  112. package/dist/src/primitives/announcer/index.js.map +1 -0
  113. package/dist/src/primitives/announcer/speech.d.ts +10 -0
  114. package/dist/src/primitives/announcer/speech.js +281 -0
  115. package/dist/src/primitives/announcer/speech.js.map +1 -0
  116. package/dist/src/primitives/borderBox.d.ts +8 -0
  117. package/dist/src/primitives/borderBox.jsx +47 -0
  118. package/dist/src/primitives/borderBox.jsx.map +1 -0
  119. package/dist/src/primitives/createFocusStack.d.ts +31 -0
  120. package/dist/src/primitives/createFocusStack.jsx +62 -0
  121. package/dist/src/primitives/createFocusStack.jsx.map +1 -0
  122. package/dist/src/primitives/createInfiniteItems.d.ts +27 -0
  123. package/dist/src/primitives/createInfiniteItems.js +46 -0
  124. package/dist/src/primitives/createInfiniteItems.js.map +1 -0
  125. package/dist/src/primitives/createTag.d.ts +8 -0
  126. package/dist/src/primitives/createTag.jsx +20 -0
  127. package/dist/src/primitives/createTag.jsx.map +1 -0
  128. package/dist/src/primitives/index.d.ts +32 -0
  129. package/dist/src/primitives/index.js +31 -0
  130. package/dist/src/primitives/index.js.map +1 -0
  131. package/dist/src/primitives/portal.d.ts +5 -0
  132. package/dist/src/primitives/portal.jsx +21 -0
  133. package/dist/src/primitives/portal.jsx.map +1 -0
  134. package/dist/src/primitives/router.d.ts +20 -0
  135. package/dist/src/primitives/router.js +90 -0
  136. package/dist/src/primitives/router.js.map +1 -0
  137. package/dist/src/primitives/types.d.ts +79 -0
  138. package/dist/src/primitives/types.js +2 -0
  139. package/dist/src/primitives/types.js.map +1 -0
  140. package/dist/src/primitives/useFocusManager.d.ts +5 -0
  141. package/dist/src/primitives/useFocusManager.js +21 -0
  142. package/dist/src/primitives/useFocusManager.js.map +1 -0
  143. package/dist/src/primitives/useHold.d.ts +27 -0
  144. package/dist/src/primitives/useHold.js +54 -0
  145. package/dist/src/primitives/useHold.js.map +1 -0
  146. package/dist/src/primitives/useMouse.d.ts +18 -0
  147. package/dist/src/primitives/useMouse.js +240 -0
  148. package/dist/src/primitives/useMouse.js.map +1 -0
  149. package/dist/src/primitives/utils/chainFunctions.d.ts +30 -0
  150. package/dist/src/primitives/utils/chainFunctions.js +32 -0
  151. package/dist/src/primitives/utils/chainFunctions.js.map +1 -0
  152. package/dist/src/primitives/utils/createBlurredImage.d.ts +56 -0
  153. package/dist/src/primitives/utils/createBlurredImage.js +223 -0
  154. package/dist/src/primitives/utils/createBlurredImage.js.map +1 -0
  155. package/dist/src/primitives/utils/createSpriteMap.d.ts +9 -0
  156. package/dist/src/primitives/utils/createSpriteMap.js +18 -0
  157. package/dist/src/primitives/utils/createSpriteMap.js.map +1 -0
  158. package/dist/src/primitives/utils/handleNavigation.d.ts +103 -0
  159. package/dist/src/primitives/utils/handleNavigation.js +300 -0
  160. package/dist/src/primitives/utils/handleNavigation.js.map +1 -0
  161. package/dist/src/primitives/utils/withScrolling.d.ts +22 -0
  162. package/dist/src/primitives/utils/withScrolling.js +186 -0
  163. package/dist/src/primitives/utils/withScrolling.js.map +1 -0
  164. package/dist/src/render.d.ts +28 -0
  165. package/dist/src/render.js +107 -0
  166. package/dist/src/render.js.map +1 -0
  167. package/dist/src/solidOpts.d.ts +15 -0
  168. package/dist/src/solidOpts.js +93 -0
  169. package/dist/src/solidOpts.js.map +1 -0
  170. package/dist/src/types.d.ts +11 -0
  171. package/dist/src/types.js +2 -0
  172. package/dist/src/types.js.map +1 -0
  173. package/dist/src/utils.d.ts +19 -0
  174. package/dist/src/utils.js +94 -0
  175. package/dist/src/utils.js.map +1 -0
  176. package/dist/tsconfig.tsbuildinfo +1 -0
  177. package/docs/AI_CODING_GUIDELINES.md +108 -0
  178. package/docs/README.md +35 -0
  179. package/jsx-runtime.d.ts +14 -0
  180. package/package.json +125 -0
  181. package/src/activeElement.ts +5 -0
  182. package/src/core/config.ts +101 -0
  183. package/src/core/dom-renderer/domRenderer.ts +2066 -0
  184. package/src/core/dom-renderer/domRendererTypes.ts +148 -0
  185. package/src/core/dom-renderer/domRendererUtils.ts +292 -0
  186. package/src/core/elementNode.ts +1623 -0
  187. package/src/core/flex.ts +331 -0
  188. package/src/core/flexLayout.ts +414 -0
  189. package/src/core/focusKeyTypes.ts +90 -0
  190. package/src/core/focusManager.ts +374 -0
  191. package/src/core/index.ts +15 -0
  192. package/src/core/intrinsicTypes.ts +216 -0
  193. package/src/core/lightningInit.ts +45 -0
  194. package/src/core/nodeTypes.ts +6 -0
  195. package/src/core/shaders.ts +187 -0
  196. package/src/core/states.ts +91 -0
  197. package/src/core/utils.ts +222 -0
  198. package/src/devtools/index.ts +77 -0
  199. package/src/index.ts +18 -0
  200. package/src/primitives/Column.tsx +59 -0
  201. package/src/primitives/FPSCounter.tsx +209 -0
  202. package/src/primitives/FadeInOut.tsx +58 -0
  203. package/src/primitives/Grid.tsx +151 -0
  204. package/src/primitives/Image.tsx +56 -0
  205. package/src/primitives/KeepAlive.tsx +268 -0
  206. package/src/primitives/Lazy.tsx +128 -0
  207. package/src/primitives/LazyImport.ts +53 -0
  208. package/src/primitives/Marquee.tsx +149 -0
  209. package/src/primitives/Preserve.tsx +18 -0
  210. package/src/primitives/Row.tsx +58 -0
  211. package/src/primitives/Suspense.tsx +39 -0
  212. package/src/primitives/Virtual.tsx +602 -0
  213. package/src/primitives/VirtualGrid.tsx +220 -0
  214. package/src/primitives/Visible.tsx +59 -0
  215. package/src/primitives/announcer/announcer.ts +198 -0
  216. package/src/primitives/announcer/index.ts +20 -0
  217. package/src/primitives/announcer/speech.ts +334 -0
  218. package/src/primitives/borderBox.tsx +69 -0
  219. package/src/primitives/createFocusStack.tsx +79 -0
  220. package/src/primitives/createInfiniteItems.ts +66 -0
  221. package/src/primitives/createTag.tsx +33 -0
  222. package/src/primitives/index.ts +37 -0
  223. package/src/primitives/portal.tsx +25 -0
  224. package/src/primitives/router.ts +147 -0
  225. package/src/primitives/types.ts +109 -0
  226. package/src/primitives/useFocusManager.ts +47 -0
  227. package/src/primitives/useHold.ts +69 -0
  228. package/src/primitives/useMouse.ts +417 -0
  229. package/src/primitives/utils/chainFunctions.ts +60 -0
  230. package/src/primitives/utils/createBlurredImage.ts +366 -0
  231. package/src/primitives/utils/createSpriteMap.ts +32 -0
  232. package/src/primitives/utils/handleNavigation.ts +373 -0
  233. package/src/primitives/utils/withScrolling.ts +238 -0
  234. package/src/render.ts +164 -0
  235. package/src/solidOpts.ts +107 -0
  236. package/src/types.ts +22 -0
  237. package/src/utils.ts +129 -0
@@ -0,0 +1,373 @@
1
+ import * as s from 'solid-js';
2
+ import * as lng from '../../index.js';
3
+ import * as lngp from '../index.js';
4
+
5
+ export const defaultTransitionBack = {
6
+ x: {
7
+ duration: 180,
8
+ easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
9
+ },
10
+ };
11
+
12
+ export const defaultTransitionForward = {
13
+ x: {
14
+ duration: 180,
15
+ easing: 'cubic-bezier(0.2, 0, 0, 1)',
16
+ },
17
+ };
18
+
19
+ export const defaultTransitionDown = {
20
+ y: {
21
+ duration: 300,
22
+ easing: 'cubic-bezier(0.2, 1, 0.8, 1)',
23
+ },
24
+ };
25
+
26
+ export const defaultTransitionUp = {
27
+ y: {
28
+ duration: 300,
29
+ easing: 'cubic-bezier(0.3, 0, 0.2, 1)',
30
+ },
31
+ };
32
+
33
+ function idxInArray(idx: number, arr: readonly any[]): boolean {
34
+ return idx >= 0 && idx < arr.length;
35
+ }
36
+
37
+ function findFirstFocusableChildIdx(
38
+ el: lngp.NavigableElement,
39
+ from = 0,
40
+ delta = 1,
41
+ ): number {
42
+ for (let i = from; ; i += delta) {
43
+ if (!idxInArray(i, el.children)) {
44
+ if (el.wrap) {
45
+ i = (i + el.children.length) % el.children.length;
46
+ } else break;
47
+ }
48
+ if (!el.children[i]?.skipFocus) {
49
+ return i;
50
+ }
51
+ }
52
+ return -1;
53
+ }
54
+
55
+ function selectChild(el: lngp.NavigableElement, index: number): boolean {
56
+ const child = el.children[index];
57
+
58
+ if (child == null || child.skipFocus) {
59
+ el.selected = -1;
60
+ return false;
61
+ }
62
+
63
+ const lastSelected = el.selected;
64
+ el.selected = index;
65
+
66
+ if (!lng.isFocused(child)) {
67
+ child.setFocus();
68
+ }
69
+
70
+ // Always call onSelectedChanged on first focus for clients
71
+ el.onSelectedChanged?.(index, el, child as lng.ElementNode, lastSelected);
72
+
73
+ return true;
74
+ }
75
+
76
+ /** @deprecated Use {@link navigableForwardFocus} instead */
77
+ export function onGridFocus(
78
+ _?: lngp.OnSelectedChanged,
79
+ ): lng.ForwardFocusHandler {
80
+ return function () {
81
+ return navigableForwardFocus.call(this, this);
82
+ };
83
+ }
84
+
85
+ /**
86
+ * Forwards focus to the first focusable child of a {@link lngp.NavigableElement} and
87
+ * selects it.
88
+ *
89
+ * @example
90
+ * ```tsx
91
+ * <view
92
+ * selected={0}
93
+ * forwardFocus={navigableForwardFocus}
94
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
95
+ * >
96
+ * ```
97
+ */
98
+ export const navigableForwardFocus: lng.ForwardFocusHandler = function () {
99
+ const navigable = this as lngp.NavigableElement;
100
+
101
+ let selected = Math.max(navigable.selected, 0);
102
+
103
+ if (this.children.length === 0) {
104
+ return false;
105
+ }
106
+
107
+ if (selected !== 0) {
108
+ selected = lng.clamp(selected, 0, Math.max(0, this.children.length - 1));
109
+ while (!idxInArray(selected, this.children)) {
110
+ selected--;
111
+ }
112
+ }
113
+
114
+ selected = findFirstFocusableChildIdx(navigable, selected);
115
+ // update selected as firstfocusable maybe different if first element has skipFocus
116
+ navigable.selected = selected;
117
+ return selectChild(navigable, selected);
118
+ };
119
+
120
+ export function handleNavigation(
121
+ direction: 'up' | 'right' | 'down' | 'left',
122
+ ): lng.KeyHandler {
123
+ return function () {
124
+ const el = this as lngp.NavigableElement;
125
+ const transition =
126
+ direction === 'up'
127
+ ? el.transitionUp
128
+ : direction === 'down'
129
+ ? el.transitionDown
130
+ : direction === 'left'
131
+ ? el.transitionLeft
132
+ : direction === 'right'
133
+ ? el.transitionRight
134
+ : undefined;
135
+
136
+ if (transition) {
137
+ const currentTransition =
138
+ typeof el.transition === 'object' && el.transition !== null
139
+ ? el.transition
140
+ : {};
141
+
142
+ el.transition = {
143
+ ...currentTransition,
144
+ ...(transition as object),
145
+ };
146
+ }
147
+
148
+ return moveSelection(
149
+ this as lngp.NavigableElement,
150
+ direction === 'up' || direction === 'left' ? -1 : 1,
151
+ );
152
+ };
153
+ }
154
+
155
+ /**
156
+ * Handles navigation key events for navigable elements, \
157
+ * such as {@link lngp.Row} and {@link lngp.Column}.
158
+ *
159
+ * Uses {@link moveSelection} to select the next or previous child based on the key pressed.
160
+ *
161
+ * @example
162
+ * ```tsx
163
+ * <view
164
+ * selected={0}
165
+ * onUp={navigableHandleNavigation}
166
+ * onDown={navigableHandleNavigation}
167
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
168
+ * >
169
+ * ```
170
+ */
171
+ export const navigableHandleNavigation: lng.KeyHandler = function (e) {
172
+ return moveSelection(
173
+ this as lngp.NavigableElement,
174
+ e.key === 'ArrowUp' || e.key === 'ArrowLeft' ? -1 : 1,
175
+ );
176
+ };
177
+
178
+ /**
179
+ * Moves the selection within a {@link lngp.NavigableElement}.
180
+ */
181
+ export function moveSelection(
182
+ el: lngp.NavigableElement,
183
+ delta: number,
184
+ ): boolean {
185
+ let selected = findFirstFocusableChildIdx(el, el.selected + delta, delta);
186
+
187
+ if (selected === -1) {
188
+ if (
189
+ !idxInArray(el.selected, el.children) ||
190
+ el.children[el.selected]?.skipFocus ||
191
+ lng.isFocused(el.children[el.selected]!)
192
+ ) {
193
+ return false;
194
+ }
195
+ selected = el.selected;
196
+ }
197
+
198
+ const active = el.children[selected]!;
199
+
200
+ if (el.plinko) {
201
+ // Set the next item to have the same selected index
202
+ // so we move up / down directly
203
+ const lastSelectedChild = el.children[el.selected];
204
+ lng.assertTruthy(lastSelectedChild instanceof lng.ElementNode);
205
+
206
+ const num = lastSelectedChild.selected || 0;
207
+ active.selected =
208
+ num < active.children.length ? num : active.children.length - 1;
209
+ }
210
+
211
+ return selectChild(el, selected);
212
+ }
213
+
214
+ function distanceBetweenRectCenters(a: lng.Rect, b: lng.Rect): number {
215
+ const dx = Math.abs(a.x + a.width / 2 - (b.x + b.width / 2)) / 2;
216
+ const dy = Math.abs(a.y + a.height / 2 - (b.y + b.height / 2)) / 2;
217
+ return Math.sqrt(dx * dx + dy * dy);
218
+ }
219
+
220
+ function findClosestFocusableChildIdx(
221
+ el: lng.ElementNode,
222
+ prevEl: lng.ElementNode,
223
+ ): number {
224
+ // select child closest to the previous active element
225
+ const prevRect = lng.getElementScreenRect(prevEl);
226
+ const elRect = lng.getElementScreenRect(el);
227
+ const childRect: lng.Rect = { x: 0, y: 0, width: 0, height: 0 };
228
+
229
+ let closestIdx = -1;
230
+ let closestDist = Infinity;
231
+
232
+ for (const [idx, child] of el.children.entries()) {
233
+ if (!child.skipFocus) {
234
+ lng.getElementScreenRect(child, el, childRect);
235
+ childRect.x += elRect.x;
236
+ childRect.y += elRect.y;
237
+ const distance = distanceBetweenRectCenters(prevRect, childRect);
238
+ if (distance < closestDist) {
239
+ closestDist = distance;
240
+ closestIdx = idx;
241
+ }
242
+ }
243
+ }
244
+
245
+ return closestIdx;
246
+ }
247
+
248
+ /**
249
+ * Forwards focus to the closest or first focusable child of a {@link lngp.NavigableElement} and
250
+ * selects it.
251
+ *
252
+ * To determine the closest child, it uses the distance between the center of the previous focused element
253
+ * and the center of each child element.
254
+ *
255
+ * @example
256
+ * ```tsx
257
+ * <view
258
+ * selected={0}
259
+ * forwardFocus={spatialForwardFocus}
260
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
261
+ * >
262
+ * ```
263
+ */
264
+ export const spatialForwardFocus: lng.ForwardFocusHandler = function () {
265
+ const prevEl = s.untrack(lng.activeElement);
266
+ if (prevEl) {
267
+ const idx = findClosestFocusableChildIdx(this, prevEl);
268
+ const selected = selectChild(this as lngp.NavigableElement, idx);
269
+ if (selected) return true;
270
+ }
271
+ const idx = findFirstFocusableChildIdx(this as lngp.NavigableElement);
272
+ return selectChild(this as lngp.NavigableElement, idx);
273
+ };
274
+
275
+ /**
276
+ * Handles spatial navigation within a {@link lngp.NavigableElement} by moving focus
277
+ * based on the arrow keys pressed.
278
+ *
279
+ * This function allows for navigation in a grid-like manner for flex-wrap containers, \
280
+ * where pressing the arrow keys will either:
281
+ * - move focus to the next/prev child in the same row/column
282
+ * - or find the closest child in the next/prev row/column.
283
+ *
284
+ * @example
285
+ * ```tsx
286
+ * <view
287
+ * selected={0}
288
+ * display="flex"
289
+ * flexWrap="wrap"
290
+ * onUp={spatialHandleNavigation}
291
+ * onDown={spatialHandleNavigation}
292
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
293
+ * >
294
+ * ```
295
+ */
296
+ export const spatialHandleNavigation: lng.KeyHandler = function (e) {
297
+ let selected = this.selected;
298
+
299
+ if (typeof selected !== 'number' || !idxInArray(selected, this.children)) {
300
+ selected = findFirstFocusableChildIdx(this as lngp.NavigableElement);
301
+ return selectChild(this as lngp.NavigableElement, selected);
302
+ }
303
+
304
+ const prevChild = this.children[selected]!;
305
+
306
+ const move = { x: 0, y: 0 };
307
+ switch (e.key) {
308
+ case 'ArrowLeft':
309
+ move.x = -1;
310
+ break;
311
+ case 'ArrowRight':
312
+ move.x = 1;
313
+ break;
314
+ case 'ArrowUp':
315
+ move.y = -1;
316
+ break;
317
+ case 'ArrowDown':
318
+ move.y = 1;
319
+ break;
320
+ default:
321
+ return false;
322
+ }
323
+
324
+ const flexDir = this.flexDirection === 'column' ? 'y' : 'x';
325
+ const crossDir = flexDir === 'x' ? 'y' : 'x';
326
+ const flexDelta = move[flexDir];
327
+ const crossDelta = move[crossDir];
328
+
329
+ // Select next/prev child in the current column/row
330
+ if (flexDelta !== 0) {
331
+ for (
332
+ let i = selected + flexDelta;
333
+ idxInArray(i, this.children);
334
+ i += flexDelta
335
+ ) {
336
+ const child = this.children[i]!;
337
+ if (child.skipFocus) continue;
338
+
339
+ // Different column/row
340
+ if (child[crossDir] !== prevChild[crossDir]) break;
341
+
342
+ return selectChild(this as lngp.NavigableElement, i);
343
+ }
344
+ }
345
+ // Find child in next/prev column/row
346
+ else {
347
+ let closestIdx = -1;
348
+ let closestDist = Infinity;
349
+
350
+ for (
351
+ let i = selected + crossDelta;
352
+ idxInArray(i, this.children);
353
+ i += crossDelta
354
+ ) {
355
+ const child = this.children[i]!;
356
+ if (child.skipFocus) continue;
357
+
358
+ // Same column/row, skip
359
+ if (child[crossDir] === prevChild[crossDir]) continue;
360
+
361
+ // Different column/row, check distance
362
+ const distance = Math.abs(child[flexDir] - prevChild[flexDir]);
363
+ if (distance >= closestDist) break; // getting further away
364
+
365
+ closestDist = distance;
366
+ closestIdx = i;
367
+ }
368
+
369
+ return selectChild(this as lngp.NavigableElement, closestIdx);
370
+ }
371
+
372
+ return false;
373
+ };
@@ -0,0 +1,238 @@
1
+ import type {
2
+ ElementNode,
3
+ ElementText,
4
+ INode,
5
+ Styles,
6
+ } from '../../core/index.js';
7
+
8
+ export type Scroller = (
9
+ selected: number | ElementNode,
10
+ component?: ElementNode,
11
+ selectedElement?: ElementNode | ElementText,
12
+ lastSelected?: number,
13
+ ) => void;
14
+
15
+ // Adds properties expected by withScrolling
16
+ export interface ScrollableElement extends ElementNode {
17
+ scrollIndex?: number;
18
+ scroll?: 'always' | 'none' | 'edge' | 'auto' | 'center' | 'bounded';
19
+ selected: number;
20
+ offset?: number;
21
+ endOffset?: number;
22
+ upCount?: number;
23
+ onScrolled?: (
24
+ elm: ScrollableElement,
25
+ offset: number,
26
+ isInitial: boolean,
27
+ ) => void;
28
+ _targetPosition?: number;
29
+ _screenOffset?: number;
30
+ _initialPosition?: number;
31
+ }
32
+
33
+ // From the renderer, not exported
34
+ const InViewPort = 8;
35
+ const isNotShown = (node: ElementNode | ElementText) => {
36
+ return node.lng.renderState !== InViewPort;
37
+ };
38
+ /*
39
+ Auto Scrolling starts scrolling right away until the last item is shown. Keeping a full view of the list.
40
+ Edge starts scrolling when it reaches the edge of the viewport.
41
+ Always scroll moves the list every time
42
+ */
43
+
44
+ /**
45
+ * Checks if the selected index is in the non-scrollable zone (last upCount items).
46
+ */
47
+ export function checkIsInNonScrollableZone(
48
+ componentRef: ScrollableElement,
49
+ ): boolean {
50
+ const totalItems = componentRef.children.length;
51
+ const upCount = componentRef.upCount || 6;
52
+ const selected = componentRef.selected || 0;
53
+ const nonScrollableZoneStart = Math.max(0, totalItems - upCount);
54
+ return selected >= nonScrollableZoneStart;
55
+ }
56
+
57
+ /** @deprecated Use {@link scrollRow} or {@link scrollColumn} */
58
+ export function withScrolling(isRow: boolean): Scroller {
59
+ const dimension = isRow ? 'width' : 'height';
60
+ const axis = isRow ? 'x' : 'y';
61
+
62
+ return (selected, component, selectedElement, lastSelected) => {
63
+ let componentRef = component as ScrollableElement;
64
+ if (typeof selected !== 'number') {
65
+ componentRef = selected as ScrollableElement;
66
+ selected = componentRef.selected || 0;
67
+ }
68
+ if (
69
+ !componentRef ||
70
+ componentRef.scroll === 'none' ||
71
+ selected === lastSelected ||
72
+ !componentRef.children.length
73
+ )
74
+ return;
75
+
76
+ if (componentRef._initialPosition === undefined) {
77
+ componentRef._initialPosition = componentRef[axis];
78
+ }
79
+
80
+ const lng = componentRef.lng as unknown as INode;
81
+ const screenSize = isRow ? lng.stage.root.w : lng.stage.root.h;
82
+ // Determine if movement is incremental or decremental
83
+ const isIncrementing =
84
+ lastSelected === undefined || lastSelected - 1 !== selected;
85
+
86
+ if (componentRef._screenOffset === undefined) {
87
+ if (componentRef.parent!.clipping) {
88
+ const p = componentRef.parent!;
89
+ componentRef.endOffset =
90
+ componentRef.endOffset ??
91
+ screenSize - ((isRow ? p.absX : p.absY) || 0) - p[dimension];
92
+ }
93
+
94
+ componentRef._screenOffset =
95
+ componentRef.offset ??
96
+ (isRow ? lng.absX : lng.absY) - componentRef[axis];
97
+ }
98
+
99
+ const screenOffset = componentRef._screenOffset;
100
+ const gap = componentRef.gap || 0;
101
+ // when creating we set scroll to always so we setup the right location for selected and scrollIndex
102
+ const scroll =
103
+ componentRef.scroll ||
104
+ (lastSelected === undefined
105
+ ? componentRef.scrollIndex
106
+ ? 'center'
107
+ : 'always'
108
+ : 'auto');
109
+
110
+ // Allows manual position control
111
+ const targetPosition = componentRef._targetPosition ?? componentRef[axis];
112
+ const rootPosition = isIncrementing
113
+ ? Math.min(targetPosition, componentRef[axis])
114
+ : Math.max(targetPosition, componentRef[axis]);
115
+ componentRef.offset = componentRef.offset ?? rootPosition;
116
+ const offset = componentRef.offset;
117
+ selectedElement =
118
+ selectedElement ||
119
+ (componentRef.children[selected] as ElementNode | undefined);
120
+
121
+ if (!selectedElement) {
122
+ return;
123
+ }
124
+ const selectedPosition = selectedElement[axis] ?? 0;
125
+ const selectedSize = selectedElement[dimension] ?? 0;
126
+ const selectedScale =
127
+ selectedElement.scale ??
128
+ (selectedElement.style?.focus as Styles)?.scale ??
129
+ 1;
130
+ const selectedSizeScaled = selectedSize * selectedScale;
131
+ const containerSize = componentRef[dimension] ?? 0;
132
+ const maxOffset = Math.min(
133
+ screenSize -
134
+ containerSize -
135
+ screenOffset -
136
+ (componentRef.endOffset ?? 2 * gap),
137
+ offset,
138
+ );
139
+
140
+ // Determine the next element based on whether incrementing or decrementing
141
+ const nextIndex = isIncrementing ? selected + 1 : selected - 1;
142
+ const nextElement = componentRef.children[nextIndex] || null;
143
+
144
+ // Default nextPosition to align with the selected position and offset
145
+ let nextPosition = rootPosition;
146
+
147
+ // Update nextPosition based on scroll type and specific conditions
148
+ if (selectedElement.centerScroll) {
149
+ nextPosition = -selectedPosition + (screenSize - selectedSizeScaled) / 2;
150
+ } else if (scroll === 'always') {
151
+ nextPosition = -selectedPosition + offset;
152
+ } else if (scroll === 'bounded') {
153
+ const totalItems = componentRef.children.length;
154
+ const upCount = componentRef.upCount || 6;
155
+ const nonScrollableZoneStart = Math.max(0, totalItems - upCount);
156
+ const isInNonScrollableZone = selected >= nonScrollableZoneStart;
157
+ const isFirstOfNonScrollableZone = selected === nonScrollableZoneStart;
158
+ const isEnteringZone =
159
+ isFirstOfNonScrollableZone &&
160
+ lastSelected !== undefined &&
161
+ lastSelected < nonScrollableZoneStart;
162
+
163
+ if (!isInNonScrollableZone) {
164
+ nextPosition = -selectedPosition + offset;
165
+ } else if (isIncrementing) {
166
+ if (isEnteringZone) {
167
+ const firstOfZoneElement =
168
+ componentRef.children[nonScrollableZoneStart];
169
+ const firstOfZonePosition = firstOfZoneElement?.[axis] ?? 0;
170
+ nextPosition = firstOfZoneElement
171
+ ? -firstOfZonePosition + offset
172
+ : rootPosition;
173
+ } else {
174
+ nextPosition = rootPosition;
175
+ }
176
+ } else if (isFirstOfNonScrollableZone) {
177
+ nextPosition = -selectedPosition + offset;
178
+ } else {
179
+ nextPosition = rootPosition;
180
+ }
181
+ } else if (scroll === 'center') {
182
+ const centerPosition =
183
+ -selectedPosition +
184
+ (screenSize - selectedSizeScaled) / 2 -
185
+ screenOffset;
186
+ // clamp position to avoid going beyond bounds
187
+ nextPosition = Math.min(Math.max(centerPosition, maxOffset), offset);
188
+ } else if (!nextElement) {
189
+ // If at the last element, align to end
190
+ nextPosition = isIncrementing ? maxOffset : offset;
191
+ } else if (scroll === 'auto') {
192
+ if (componentRef.scrollIndex && componentRef.scrollIndex > 0) {
193
+ // Prevent scrolling if the selected item is within the last scrollIndex items
194
+ const totalItems = componentRef.children.length;
195
+ const nearEndIndex = totalItems - componentRef.scrollIndex;
196
+
197
+ if (
198
+ isIncrementing &&
199
+ componentRef.selected >= componentRef.scrollIndex
200
+ ) {
201
+ nextPosition = rootPosition - selectedSize - gap;
202
+ } else if (!isIncrementing && componentRef.selected < nearEndIndex) {
203
+ nextPosition = rootPosition + selectedSize + gap;
204
+ }
205
+ } else if (isIncrementing) {
206
+ nextPosition = rootPosition - selectedSize - gap;
207
+ } else {
208
+ nextPosition = rootPosition + selectedSize + gap;
209
+ }
210
+ } // Handle Edge scrolling
211
+ else if (isIncrementing && isNotShown(nextElement)) {
212
+ nextPosition = rootPosition - selectedSize - gap;
213
+ } else if (isNotShown(nextElement)) {
214
+ nextPosition = -selectedPosition + offset;
215
+ }
216
+
217
+ // Prevent container from moving beyond bounds
218
+ nextPosition =
219
+ isIncrementing && scroll !== 'always' && scroll !== 'bounded'
220
+ ? Math.max(nextPosition, maxOffset)
221
+ : Math.min(nextPosition, offset);
222
+
223
+ // Update position if it has changed
224
+ if (componentRef[axis] !== nextPosition) {
225
+ if (componentRef.onScrolled) {
226
+ const isInitial = nextPosition === componentRef._initialPosition;
227
+ componentRef.onScrolled(componentRef, nextPosition, isInitial);
228
+ }
229
+
230
+ componentRef[axis] = nextPosition;
231
+ // Store the new position to keep track during animations
232
+ componentRef._targetPosition = nextPosition;
233
+ }
234
+ };
235
+ }
236
+
237
+ export const scrollRow = /* @__PURE__ */ withScrolling(true);
238
+ export const scrollColumn = /* @__PURE__ */ withScrolling(false);