@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,220 @@
1
+ import * as s from 'solid-js';
2
+ import * as lng from '@solidtv/solid';
3
+ import * as lngp from '@solidtv/solid/primitives';
4
+ import { List } from '@solid-primitives/list';
5
+ import * as utils from '../utils.js';
6
+
7
+ const columnScroll = lngp.withScrolling(false);
8
+
9
+ const rowStyles: lng.NodeStyles = {
10
+ display: 'flex',
11
+ flexWrap: 'wrap',
12
+ transition: {
13
+ y: true,
14
+ },
15
+ };
16
+
17
+ export type VirtualGridProps<T> = lng.NewOmit<lngp.RowProps, 'children'> & {
18
+ each: readonly T[] | undefined | null | false;
19
+ columns: number; // items per row
20
+ rows?: number; // number of visible rows (default: 1)
21
+ buffer?: number;
22
+ onEndReached?: () => void;
23
+ onEndReachedThreshold?: number;
24
+ children: (item: s.Accessor<T>, index: s.Accessor<number>) => s.JSX.Element;
25
+ };
26
+
27
+ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
28
+ const bufferSize = () => props.buffer ?? 2;
29
+ const [ cursor, setCursor ] = s.createSignal(props.selected ?? 0);
30
+ const items = s.createMemo(() => props.each || []);
31
+ const itemCount = () => items().length;
32
+ const itemsPerRow = () => props.columns;
33
+ const numberOfRows = () => props.rows ?? 1;
34
+ const totalVisibleItems = () => itemsPerRow() * numberOfRows();
35
+
36
+ const start = s.createMemo(() => {
37
+ const perRow = itemsPerRow();
38
+ const newRowIndex = Math.floor(cursor() / perRow);
39
+ const rawStart = newRowIndex * perRow - bufferSize() * perRow;
40
+ return Math.max(0, rawStart);
41
+ });
42
+
43
+ const end = s.createMemo(() => {
44
+ const perRow = itemsPerRow();
45
+ const newRowIndex = Math.floor(cursor() / perRow);
46
+ const rawEnd = (newRowIndex + bufferSize()) * perRow + totalVisibleItems();
47
+ return Math.min(items().length, rawEnd);
48
+ });
49
+
50
+ const [slice, setSlice] = s.createSignal(items().slice(start(), end()));
51
+
52
+ let viewRef!: lngp.NavigableElement;
53
+
54
+ function onVerticalNav(dir: -1 | 1): lngp.KeyHandler {
55
+ return function () {
56
+ const perRow = itemsPerRow();
57
+ const currentRowIndex = Math.floor(cursor() / perRow);
58
+ const maxRows = Math.floor(items().length / perRow);
59
+
60
+ if (
61
+ currentRowIndex === 0 && dir === -1
62
+ || currentRowIndex === maxRows && dir === 1
63
+ ) return;
64
+
65
+ const selected = this.selected || 0;
66
+ const offset = dir * perRow;
67
+ const newIndex = utils.clamp(selected + offset, 0, items().length - 1);
68
+ const lastIdx = selected;
69
+ this.selected = newIndex;
70
+ const active = this.children[this.selected];
71
+
72
+ if (active instanceof lng.ElementNode) {
73
+ active.setFocus();
74
+ chainedOnSelectedChanged.call(
75
+ this as lngp.NavigableElement,
76
+ this.selected,
77
+ this as lngp.NavigableElement,
78
+ active,
79
+ lastIdx
80
+ );
81
+ return true;
82
+ }
83
+ };
84
+ }
85
+
86
+ const onUp = onVerticalNav(-1);
87
+ const onDown = onVerticalNav(1);
88
+
89
+ const onSelectedChanged: lngp.OnSelectedChanged = function (_idx, elm, active, _lastIdx,) {
90
+ let idx = _idx;
91
+ let lastIdx = _lastIdx;
92
+ const perRow = itemsPerRow();
93
+ const newRowIndex = Math.floor(idx / perRow);
94
+ const prevRowIndex = Math.floor((lastIdx || 0) / perRow);
95
+ const prevStart = start();
96
+
97
+ setCursor(prevStart + idx);
98
+ if (newRowIndex === prevRowIndex) return;
99
+
100
+ setSlice(items().slice(start(), end()));
101
+
102
+ // this.selected is relative to the slice
103
+ // and it doesn't get corrected automatically after children change
104
+ const idxCorrection = prevStart - start();
105
+ if (lastIdx) lastIdx += idxCorrection;
106
+ idx += idxCorrection;
107
+ this.selected += idxCorrection;
108
+
109
+ if (props.onEndReachedThreshold !== undefined && cursor() >= items().length - props.onEndReachedThreshold) {
110
+ props.onEndReached?.();
111
+ }
112
+
113
+ queueMicrotask(() => {
114
+ const prevRowY = this.y + active.y;
115
+ this.updateLayout();
116
+ this.lng.y = prevRowY - active.y;
117
+ columnScroll(idx, elm, active, lastIdx);
118
+ });
119
+ };
120
+
121
+ const chainedOnSelectedChanged = lngp.chainFunctions(props.onSelectedChanged, onSelectedChanged)!;
122
+
123
+ let cachedSelected: number | undefined;
124
+ const updateSelected = ([selected, _items]: [number?, any?]) => {
125
+ if (!viewRef || selected == null) return;
126
+
127
+ if (cachedSelected !== undefined) {
128
+ selected = cachedSelected;
129
+ cachedSelected = undefined;
130
+ }
131
+
132
+ if (selected >= items().length && props.onEndReached) {
133
+ props.onEndReached?.();
134
+ cachedSelected = selected;
135
+ return;
136
+ }
137
+
138
+ const item = items()[selected];
139
+ let active = viewRef.children.find(x => x.item === item);
140
+ const lastSelected = viewRef.selected;
141
+
142
+ if (active instanceof lng.ElementNode) {
143
+ viewRef.selected = viewRef.children.indexOf(active);
144
+ if (lng.hasFocus(viewRef)) {
145
+ // force focus as scrollToIndex is manually called
146
+ active.setFocus();
147
+ }
148
+ chainedOnSelectedChanged.call(viewRef, viewRef.selected, viewRef, active, lastSelected);
149
+ } else {
150
+ setCursor(selected);
151
+ setSlice(items().slice(start(), end()));
152
+
153
+ queueMicrotask(() => {
154
+ viewRef.updateLayout();
155
+ active = viewRef.children.find(x => x.item === item);
156
+ if (active instanceof lng.ElementNode) {
157
+ viewRef.selected = viewRef.children.indexOf(active);
158
+ if (lng.hasFocus(viewRef)) {
159
+ active.setFocus();
160
+ }
161
+ chainedOnSelectedChanged.call(viewRef, viewRef.selected, viewRef, active, lastSelected);
162
+ }
163
+ });
164
+ }
165
+ };
166
+
167
+ const scrollToIndex = (index: number) => {
168
+ s.untrack(() => updateSelected([index]));
169
+ }
170
+
171
+ s.createEffect(s.on([() => props.selected, items], updateSelected));
172
+
173
+ s.createEffect(
174
+ s.on(items, (gridItems, _prevGridItems, prevSize) => {
175
+ if (!viewRef) return;
176
+
177
+ if (cachedSelected !== undefined) {
178
+ // This occurs when VG is reloaded and user wants to select a paginated item
179
+ updateSelected([cachedSelected]);
180
+ return gridItems.length;
181
+ }
182
+
183
+ if (gridItems.length === 0) {
184
+ setCursor(0);
185
+ cachedSelected = undefined;
186
+ setSlice([]);
187
+ } else if (cursor() >= itemCount()) {
188
+ updateSelected([Math.max(0, itemCount() - 1)]);
189
+ } else if (prevSize === 0) {
190
+ updateSelected([0]);
191
+ } else {
192
+ setSlice(items().slice(start(), end()));
193
+ }
194
+
195
+ return gridItems.length;
196
+ }, { defer: true })
197
+ );
198
+
199
+
200
+ return (
201
+ <view
202
+ {...props}
203
+ scroll={props.scroll || 'always'}
204
+ ref={lngp.chainRefs(el => { viewRef = el as lngp.NavigableElement; }, props.ref)}
205
+ selected={props.selected || 0}
206
+ cursor={cursor()}
207
+ onLeft={/* @once */ lngp.chainFunctions(props.onLeft, lngp.navigableHandleNavigation)}
208
+ onRight={/* @once */ lngp.chainFunctions(props.onRight, lngp.navigableHandleNavigation)}
209
+ onUp={/* @once */ lngp.chainFunctions(props.onUp, onUp)}
210
+ onDown={/* @once */ lngp.chainFunctions(props.onDown, onDown)}
211
+ forwardFocus={/* @once */ lngp.navigableForwardFocus}
212
+ onCreate={/* @once */ props.selected ? lngp.chainFunctions(props.onCreate, columnScroll) : props.onCreate}
213
+ scrollToIndex={/* @once */ scrollToIndex}
214
+ onSelectedChanged={/* @once */ chainedOnSelectedChanged}
215
+ style={/* @once */ lng.combineStyles(props.style, rowStyles)}
216
+ >
217
+ <List each={slice()}>{props.children}</List>
218
+ </view>
219
+ );
220
+ }
@@ -0,0 +1,59 @@
1
+ import {
2
+ DEV,
3
+ children,
4
+ createMemo,
5
+ ChildrenReturn,
6
+ JSX,
7
+ untrack,
8
+ onCleanup,
9
+ createRoot,
10
+ } from 'solid-js';
11
+ import { ElementNode } from '@solidtv/solid';
12
+
13
+ export function Visible<T>(props: {
14
+ when: T | undefined | null | false;
15
+ keyed?: boolean;
16
+ children: JSX.Element;
17
+ }): JSX.Element {
18
+ let child: ChildrenReturn | undefined;
19
+ let disposer: VoidFunction | undefined;
20
+ const keyed = props.keyed;
21
+ const condition = createMemo<T | undefined | null | boolean>(
22
+ () => props.when,
23
+ undefined,
24
+ DEV
25
+ ? {
26
+ equals: (a, b) => (keyed ? a === b : !a === !b),
27
+ name: "condition"
28
+ }
29
+ : { equals: (a, b) => (keyed ? a === b : !a === !b) }
30
+ );
31
+
32
+ onCleanup(() => disposer?.());
33
+
34
+
35
+ return createMemo(() => {
36
+ const c = condition();
37
+ const isKeyed = untrack(() => !!keyed);
38
+ if (isKeyed){
39
+ disposer?.();
40
+ child = undefined;
41
+ }
42
+
43
+ if (c && !child) {
44
+ disposer = createRoot((dispose) => {
45
+ child = children(() => props.children);
46
+ return dispose;
47
+ })
48
+ }
49
+
50
+ const isHidden = !c;
51
+ child?.toArray().forEach((childNode) => {
52
+ if (childNode instanceof ElementNode) {
53
+ childNode.hidden = isHidden;
54
+ }
55
+ });
56
+
57
+ return c || child ? child : null;
58
+ }) as unknown as JSX.Element;
59
+ };
@@ -0,0 +1,198 @@
1
+ import type { ElementNode } from '@solidtv/solid';
2
+ import { untrack } from 'solid-js';
3
+ import SpeechEngine, { type SeriesResult, type SpeechType } from './speech.js';
4
+ import { debounce } from '@solid-primitives/scheduled';
5
+ import { focusPath } from '../useFocusManager.js';
6
+
7
+ type DebounceWithFlushFunction<T> = {
8
+ (newValue: T): void;
9
+ flush(): void;
10
+ clear: VoidFunction;
11
+ };
12
+
13
+ declare module '@solidtv/solid' {
14
+ /**
15
+ * Augment the existing ElementNode interface with our own
16
+ * Announcer-specific properties.
17
+ */
18
+ interface ElementNode {
19
+ announce?: SpeechType;
20
+ announceContext?: SpeechType;
21
+ title?: SpeechType;
22
+ loading?: boolean;
23
+ }
24
+ }
25
+
26
+ let resetFocusPathTimer: DebounceWithFlushFunction<void>;
27
+ let prevFocusPath: ElementNode[] = [];
28
+ let currentlySpeaking: SeriesResult | undefined;
29
+ let voiceOutDisabled = false;
30
+ const fiveMinutes = 300000;
31
+
32
+ function debounceWithFlush<T>(
33
+ callback: (newValue: T) => void,
34
+ time?: number,
35
+ ): DebounceWithFlushFunction<T> {
36
+ const trigger = debounce(callback, time);
37
+ let scopedValue: T;
38
+
39
+ const debounced = (newValue: T) => {
40
+ scopedValue = newValue;
41
+ trigger(newValue);
42
+ };
43
+
44
+ debounced.flush = () => {
45
+ trigger.clear();
46
+ callback(scopedValue);
47
+ };
48
+
49
+ debounced.clear = trigger.clear;
50
+
51
+ return debounced;
52
+ }
53
+
54
+ function getElmName(elm: ElementNode): string {
55
+ return (elm.id || elm.name) as string;
56
+ }
57
+
58
+ function onFocusChangeCore(focusPath: ElementNode[] = []) {
59
+ if (!Announcer.onFocusChange || !Announcer.enabled) {
60
+ return;
61
+ }
62
+
63
+ const loaded = focusPath.every((elm) => !elm.loading);
64
+ const focusDiff = focusPath.filter((elm) => !prevFocusPath.includes(elm));
65
+
66
+ resetFocusPathTimer();
67
+
68
+ if (!loaded && Announcer.onFocusChange) {
69
+ Announcer.onFocusChange([]);
70
+ return;
71
+ }
72
+
73
+ prevFocusPath = focusPath.slice(0);
74
+
75
+ const toAnnounceText: SpeechType[] = [];
76
+ const toAnnounce = focusDiff
77
+ .reverse()
78
+ .reduce((acc: [string, string, SpeechType][], elm) => {
79
+ if (elm.announce) {
80
+ acc.push([getElmName(elm), 'Announce', elm.announce]);
81
+ toAnnounceText.push(elm.announce);
82
+ } else if (elm.title) {
83
+ acc.push([getElmName(elm), 'Title', elm.title]);
84
+ toAnnounceText.push(elm.title);
85
+ } else {
86
+ acc.push([getElmName(elm), 'No Announce', '']);
87
+ }
88
+ return acc;
89
+ }, []);
90
+
91
+ focusDiff.reverse().reduce((acc, elm) => {
92
+ if (elm.announceContext) {
93
+ acc.push([getElmName(elm), 'Context', elm.announceContext]);
94
+ toAnnounceText.push(elm.announceContext);
95
+ } else {
96
+ acc.push([getElmName(elm), 'No Context', '']);
97
+ }
98
+ return acc;
99
+ }, toAnnounce);
100
+
101
+ if (Announcer.debug) {
102
+ console.table(toAnnounce);
103
+ }
104
+
105
+ if (toAnnounceText.length) {
106
+ return Announcer.speak(
107
+ toAnnounceText.reduce((acc: SpeechType[], val) => acc.concat(val), []),
108
+ );
109
+ }
110
+ }
111
+
112
+ function textToSpeech(
113
+ toSpeak: SpeechType,
114
+ aria: boolean,
115
+ lang: string,
116
+ voice?: string,
117
+ ) {
118
+ if (voiceOutDisabled) {
119
+ return;
120
+ }
121
+
122
+ return (currentlySpeaking = SpeechEngine(toSpeak, aria, lang, voice));
123
+ }
124
+
125
+ export interface Announcer {
126
+ debug: boolean;
127
+ enabled: boolean;
128
+ lang: string;
129
+ aria: boolean;
130
+ voice?: string;
131
+ cancel: VoidFunction;
132
+ clearPrevFocus: (depth?: number) => void;
133
+ speak: (
134
+ text: SpeechType,
135
+ options?: { append?: boolean; notification?: boolean },
136
+ ) => SeriesResult;
137
+ setupTimers: (options?: {
138
+ focusDebounce?: number;
139
+ focusChangeTimeout?: number;
140
+ }) => void;
141
+ onFocusChange?: DebounceWithFlushFunction<ElementNode[]>;
142
+ refresh: (depth?: number) => void;
143
+ }
144
+
145
+ export const Announcer: Announcer = {
146
+ debug: false,
147
+ enabled: true,
148
+ lang: 'en-US',
149
+ aria: false,
150
+ cancel: function () {
151
+ currentlySpeaking && currentlySpeaking.cancel();
152
+ },
153
+ clearPrevFocus: function (depth = 0) {
154
+ prevFocusPath = prevFocusPath.slice(0, depth);
155
+ resetFocusPathTimer();
156
+ },
157
+ speak: function (text, { append = false, notification = false } = {}) {
158
+ if (Announcer.onFocusChange && Announcer.enabled) {
159
+ if (append && currentlySpeaking && currentlySpeaking.active) {
160
+ currentlySpeaking.append(text);
161
+ } else {
162
+ Announcer.cancel();
163
+ textToSpeech(text, Announcer.aria, Announcer.lang, Announcer.voice);
164
+ }
165
+
166
+ if (notification) {
167
+ voiceOutDisabled = true;
168
+ currentlySpeaking?.series
169
+ .finally(() => {
170
+ voiceOutDisabled = false;
171
+ Announcer.refresh();
172
+ })
173
+ .catch(console.error);
174
+ }
175
+ }
176
+
177
+ return currentlySpeaking as SeriesResult;
178
+ },
179
+ refresh: function (depth = 0) {
180
+ Announcer.clearPrevFocus(depth);
181
+ Announcer.onFocusChange &&
182
+ Announcer.onFocusChange(untrack(() => focusPath()));
183
+ },
184
+ setupTimers: function ({
185
+ focusDebounce = 400,
186
+ focusChangeTimeout = fiveMinutes,
187
+ } = {}) {
188
+ Announcer.onFocusChange = debounceWithFlush(
189
+ onFocusChangeCore,
190
+ focusDebounce,
191
+ );
192
+
193
+ resetFocusPathTimer = debounceWithFlush(() => {
194
+ // Reset focus path for full announce
195
+ prevFocusPath = [];
196
+ }, focusChangeTimeout);
197
+ },
198
+ };
@@ -0,0 +1,20 @@
1
+ import { createEffect, on } from 'solid-js';
2
+ import { Announcer } from './announcer.js';
3
+ import { focusPath } from '../useFocusManager.js';
4
+
5
+ let doOnce = false;
6
+ export const useAnnouncer = (options?: {
7
+ focusDebounce?: number;
8
+ focusChangeTimeout?: number;
9
+ }) => {
10
+ if (doOnce) {
11
+ return Announcer;
12
+ }
13
+ doOnce = true;
14
+ Announcer.setupTimers(options);
15
+ createEffect(on(focusPath, Announcer.onFocusChange!, { defer: true }));
16
+
17
+ return Announcer;
18
+ };
19
+
20
+ export { Announcer };