@solidtv/solid 1.2.2 → 1.3.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.
- package/dist/src/core/dom-renderer/domRenderer.d.ts +4 -0
- package/dist/src/core/dom-renderer/domRenderer.js +24 -3
- package/dist/src/core/dom-renderer/domRenderer.js.map +1 -1
- package/dist/src/core/elementNode.js +4 -0
- package/dist/src/core/elementNode.js.map +1 -1
- package/dist/src/core/utils.d.ts +4 -2
- package/dist/src/core/utils.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/core/dom-renderer/domRenderer.ts +26 -3
- package/src/core/elementNode.ts +4 -0
- package/src/core/utils.ts +4 -2
- package/dist/src/primitives/Rail.d.ts +0 -27
- package/dist/src/primitives/Rail.jsx +0 -322
- package/dist/src/primitives/Rail.jsx.map +0 -1
- package/src/primitives/Rail.tsx +0 -401
package/src/primitives/Rail.tsx
DELETED
|
@@ -1,401 +0,0 @@
|
|
|
1
|
-
import * as s from 'solid-js';
|
|
2
|
-
import * as lng from '@solidtv/solid';
|
|
3
|
-
import * as lngp from '@solidtv/solid/primitives';
|
|
4
|
-
import * as utils from '../utils.js';
|
|
5
|
-
import {
|
|
6
|
-
defaultTransitionBack,
|
|
7
|
-
defaultTransitionForward,
|
|
8
|
-
defaultTransitionDown,
|
|
9
|
-
defaultTransitionUp,
|
|
10
|
-
} from './utils/handleNavigation.js';
|
|
11
|
-
|
|
12
|
-
export type RailProps<T> = lng.NewOmit<lngp.RowProps, 'children'> & {
|
|
13
|
-
each: readonly T[] | undefined | null | false;
|
|
14
|
-
/** Uniform child size along the axis (width for Row, height for Column).
|
|
15
|
-
* If omitted, measured from the first laid-out child. */
|
|
16
|
-
itemSize?: number;
|
|
17
|
-
/** Space between items along the axis. Default 30. */
|
|
18
|
-
gap?: number;
|
|
19
|
-
/** Container size along the axis. If omitted, falls back to props.width/height,
|
|
20
|
-
* then measured from the rail's own layout. */
|
|
21
|
-
viewportSize?: number;
|
|
22
|
-
/** Extra items rendered outside the visible region on each side. Default 1. */
|
|
23
|
-
bufferSize?: number;
|
|
24
|
-
/** Window position where the focused item is pinned. Default 0. */
|
|
25
|
-
scrollIndex?: number;
|
|
26
|
-
wrap?: boolean;
|
|
27
|
-
onEndReached?: () => void;
|
|
28
|
-
/** Items from the end at which onEndReached first fires. Default 0. */
|
|
29
|
-
onEndReachedThreshold?: number;
|
|
30
|
-
/** Account for the focus-state scale when measuring item size. */
|
|
31
|
-
factorScale?: boolean;
|
|
32
|
-
children: (item: s.Accessor<T>, index: s.Accessor<number>) => s.JSX.Element;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
function createRail<T>(
|
|
36
|
-
isRow: boolean,
|
|
37
|
-
props: RailProps<T>,
|
|
38
|
-
keyHandlers: Record<string, lng.KeyHandler>,
|
|
39
|
-
) {
|
|
40
|
-
const axis: 'x' | 'y' = isRow ? 'x' : 'y';
|
|
41
|
-
const sizeKey: 'width' | 'height' = isRow ? 'width' : 'height';
|
|
42
|
-
|
|
43
|
-
const items = s.createMemo(() => props.each || []);
|
|
44
|
-
const itemCount = s.createMemo(() => items().length);
|
|
45
|
-
const gap = s.createMemo(() => props.gap ?? 30);
|
|
46
|
-
const bufferSize = s.createMemo(() => Math.max(0, props.bufferSize ?? 1));
|
|
47
|
-
const scrollIndex = s.createMemo(() => Math.max(0, props.scrollIndex ?? 0));
|
|
48
|
-
|
|
49
|
-
const [cursor, setCursor] = s.createSignal(props.selected ?? 0);
|
|
50
|
-
const [measuredItem, setMeasuredItem] = s.createSignal(0);
|
|
51
|
-
const [measuredViewport, setMeasuredViewport] = s.createSignal(0);
|
|
52
|
-
|
|
53
|
-
const itemSize = s.createMemo(() => props.itemSize ?? measuredItem());
|
|
54
|
-
// stride is only meaningful once item size is known; gap alone is not
|
|
55
|
-
// enough to space items, so treat unmeasured itemSize as "no stride yet"
|
|
56
|
-
// and let the bootstrap window in `windowLength` render a tiny slice for
|
|
57
|
-
// the first child to measure against.
|
|
58
|
-
const stride = s.createMemo(() => {
|
|
59
|
-
const is = itemSize();
|
|
60
|
-
return is > 0 ? is + gap() : 0;
|
|
61
|
-
});
|
|
62
|
-
const viewportSize = s.createMemo(
|
|
63
|
-
() =>
|
|
64
|
-
props.viewportSize ??
|
|
65
|
-
(props[sizeKey] as number | undefined) ??
|
|
66
|
-
measuredViewport(),
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
// displaySize = how many items fit in the viewport (uniform sizes).
|
|
70
|
-
// Returns 0 until both stride and viewport size are known.
|
|
71
|
-
const displaySize = s.createMemo(() => {
|
|
72
|
-
const st = stride();
|
|
73
|
-
const vp = viewportSize();
|
|
74
|
-
if (!st || !vp) return 0;
|
|
75
|
-
return Math.max(1, Math.ceil(vp / st));
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
// While unmeasured, behave as if displaySize === 1 so the bootstrap slice
|
|
79
|
-
// is positioned sensibly around the cursor instead of walking past the end.
|
|
80
|
-
const safeDisplaySize = s.createMemo(() => Math.max(1, displaySize()));
|
|
81
|
-
|
|
82
|
-
// Absolute index of the leftmost visible item. Pinning formula.
|
|
83
|
-
const visibleStart = s.createMemo(() => {
|
|
84
|
-
const total = itemCount();
|
|
85
|
-
if (total === 0) return 0;
|
|
86
|
-
const ds = safeDisplaySize();
|
|
87
|
-
if (props.wrap) return utils.mod(cursor() - scrollIndex(), total);
|
|
88
|
-
if (total <= ds) return 0;
|
|
89
|
-
return utils.clamp(cursor() - scrollIndex(), 0, total - ds);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
// First rendered slot, includes leading buffer.
|
|
93
|
-
const windowStart = s.createMemo(() => {
|
|
94
|
-
const total = itemCount();
|
|
95
|
-
if (total === 0) return 0;
|
|
96
|
-
if (props.wrap) return utils.mod(visibleStart() - bufferSize(), total);
|
|
97
|
-
return Math.max(0, visibleStart() - bufferSize());
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// Pool size = visible + leading + trailing buffer. Capped by total.
|
|
101
|
-
const windowLength = s.createMemo(() => {
|
|
102
|
-
const total = itemCount();
|
|
103
|
-
if (total === 0) return 0;
|
|
104
|
-
const len = safeDisplaySize() + 2 * bufferSize();
|
|
105
|
-
if (props.wrap) return Math.min(len, total);
|
|
106
|
-
return Math.min(len, total - windowStart());
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
const windowItems = s.createMemo(() => {
|
|
110
|
-
const len = windowLength();
|
|
111
|
-
if (len === 0) return [] as T[];
|
|
112
|
-
const start = windowStart();
|
|
113
|
-
const all = items();
|
|
114
|
-
if (props.wrap) {
|
|
115
|
-
const total = all.length;
|
|
116
|
-
return Array.from(
|
|
117
|
-
{ length: len },
|
|
118
|
-
(_, i) => all[utils.mod(start + i, total)]!,
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
return all.slice(start, start + len);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// Slot of the focused item inside the rendered pool.
|
|
125
|
-
const windowSelected = s.createMemo(() => {
|
|
126
|
-
if (props.wrap) {
|
|
127
|
-
const total = itemCount();
|
|
128
|
-
if (total === 0) return 0;
|
|
129
|
-
return utils.mod(cursor() - windowStart(), total);
|
|
130
|
-
}
|
|
131
|
-
return cursor() - windowStart();
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// Camera offset: keep visibleStart aligned to the viewport's leading edge.
|
|
135
|
-
const targetOffset = s.createMemo(() => -visibleStart() * stride());
|
|
136
|
-
|
|
137
|
-
// Total extent along the axis covered by the currently-rendered pool. The
|
|
138
|
-
// renderer culls children whose global rect falls outside their parent's
|
|
139
|
-
// strictBound, so we grow the rail's own bound to enclose all items in the
|
|
140
|
-
// pool (including buffer slots). Combined with `clipping={false}` this
|
|
141
|
-
// keeps off-viewport buffer items renderable as the camera slides.
|
|
142
|
-
const contentExtent = s.createMemo(() => {
|
|
143
|
-
const st = stride();
|
|
144
|
-
if (!st) return 0;
|
|
145
|
-
return (windowStart() + windowLength()) * st;
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// Final width/height we hand to the underlying view along the axis.
|
|
149
|
-
// Always at least the user-supplied size so the rail occupies its slot in
|
|
150
|
-
// parent layout when the pool is short / unmeasured.
|
|
151
|
-
const railWidth = s.createMemo(() => {
|
|
152
|
-
if (!isRow) return props.width;
|
|
153
|
-
const userW = (props.width as number | undefined) ?? 0;
|
|
154
|
-
return Math.max(userW, contentExtent());
|
|
155
|
-
});
|
|
156
|
-
const railHeight = s.createMemo(() => {
|
|
157
|
-
if (isRow) return props.height;
|
|
158
|
-
const userH = (props.height as number | undefined) ?? 0;
|
|
159
|
-
return Math.max(userH, contentExtent());
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
let viewRef!: lngp.NavigableElement;
|
|
163
|
-
let cachedAnim: lng.IAnimationController | undefined;
|
|
164
|
-
let lastNavTime = 0;
|
|
165
|
-
let endFired = false;
|
|
166
|
-
let initialised = false;
|
|
167
|
-
|
|
168
|
-
function getAdaptiveDuration(duration: number = 250) {
|
|
169
|
-
const now = performance.now();
|
|
170
|
-
const delta = now - lastNavTime;
|
|
171
|
-
lastNavTime = now;
|
|
172
|
-
if (delta < duration) return delta;
|
|
173
|
-
return duration;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function applyOffset(target: number, animated: boolean) {
|
|
177
|
-
if (!viewRef) return;
|
|
178
|
-
if (cachedAnim && cachedAnim.state === 'running') cachedAnim.stop();
|
|
179
|
-
if (animated && lng.Config.animationsEnabled) {
|
|
180
|
-
cachedAnim = viewRef
|
|
181
|
-
.animate(
|
|
182
|
-
{ [axis]: target },
|
|
183
|
-
{
|
|
184
|
-
...viewRef.animationSettings,
|
|
185
|
-
duration: getAdaptiveDuration(viewRef.animationSettings?.duration),
|
|
186
|
-
},
|
|
187
|
-
)
|
|
188
|
-
.start();
|
|
189
|
-
} else {
|
|
190
|
-
viewRef.lng[axis] = target;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
function scrollToIndex(this: lng.ElementNode, index: number) {
|
|
195
|
-
s.untrack(() => {
|
|
196
|
-
if (itemCount() === 0) return;
|
|
197
|
-
setCursor(utils.clamp(index, 0, itemCount() - 1));
|
|
198
|
-
if (!lng.hasFocus(viewRef)) viewRef.setFocus();
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const onSelectedChanged: lngp.OnSelectedChanged = function (
|
|
203
|
-
idx,
|
|
204
|
-
elm,
|
|
205
|
-
active,
|
|
206
|
-
lastIdx,
|
|
207
|
-
) {
|
|
208
|
-
if (props.onSelectedChanged) {
|
|
209
|
-
props.onSelectedChanged.call(this, idx, this, active, lastIdx);
|
|
210
|
-
}
|
|
211
|
-
if (idx === lastIdx) return;
|
|
212
|
-
|
|
213
|
-
const total = itemCount();
|
|
214
|
-
if (total === 0) return;
|
|
215
|
-
|
|
216
|
-
const poolLen = elm.children.length;
|
|
217
|
-
const rawDelta = idx - (lastIdx ?? 0);
|
|
218
|
-
let delta = rawDelta;
|
|
219
|
-
if (props.wrap && poolLen > 0) {
|
|
220
|
-
const half = poolLen / 2;
|
|
221
|
-
if (delta > half) delta = delta - poolLen;
|
|
222
|
-
else if (delta < -half) delta = delta + poolLen;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const next = props.wrap
|
|
226
|
-
? utils.mod(cursor() + delta, total)
|
|
227
|
-
: utils.clamp(cursor() + delta, 0, total - 1);
|
|
228
|
-
setCursor(next);
|
|
229
|
-
|
|
230
|
-
if (props.onEndReached) {
|
|
231
|
-
const threshold = props.onEndReachedThreshold ?? 0;
|
|
232
|
-
const crossed = next >= total - 1 - threshold;
|
|
233
|
-
if (crossed && !endFired) {
|
|
234
|
-
endFired = true;
|
|
235
|
-
props.onEndReached();
|
|
236
|
-
} else if (!crossed && endFired) {
|
|
237
|
-
endFired = false;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
// Animate the camera when visibleStart shifts; snap on first frame.
|
|
243
|
-
s.createEffect(() => {
|
|
244
|
-
const target = targetOffset();
|
|
245
|
-
if (!viewRef) return;
|
|
246
|
-
applyOffset(target, initialised);
|
|
247
|
-
initialised = true;
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
// Drive `selected` (and focus) imperatively. A `createMemo` would skip
|
|
251
|
-
// emitting when the computed slot value is unchanged across a window shift
|
|
252
|
-
// (e.g. cursor 3→4 with scrollIndex=0, windowStart 0→1 keeps slot=3), which
|
|
253
|
-
// leaves `el.selected` stuck at whatever selectChild just wrote during
|
|
254
|
-
// navigation. Tracking the underlying signals here forces a write every
|
|
255
|
-
// time cursor / window changes, overriding selectChild and keeping
|
|
256
|
-
// focus on the right slot as the pool slides.
|
|
257
|
-
s.createEffect(() => {
|
|
258
|
-
if (!viewRef) return;
|
|
259
|
-
const len = windowLength();
|
|
260
|
-
const ws = windowStart();
|
|
261
|
-
const c = cursor();
|
|
262
|
-
const sel = len === 0 ? 0 : utils.clamp(c - ws, 0, len - 1);
|
|
263
|
-
viewRef.selected = sel;
|
|
264
|
-
if (lng.hasFocus(viewRef) && viewRef.children[sel]) {
|
|
265
|
-
viewRef.children[sel].setFocus();
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
// Clamp cursor if the data shrinks; reset latch.
|
|
270
|
-
s.createEffect(
|
|
271
|
-
s.on(items, () => {
|
|
272
|
-
const total = itemCount();
|
|
273
|
-
if (total === 0) return;
|
|
274
|
-
if (cursor() >= total) setCursor(Math.max(0, total - 1));
|
|
275
|
-
endFired = false;
|
|
276
|
-
}),
|
|
277
|
-
);
|
|
278
|
-
|
|
279
|
-
// React to external `selected` changes.
|
|
280
|
-
s.createEffect(
|
|
281
|
-
s.on(
|
|
282
|
-
() => props.selected,
|
|
283
|
-
(sel) => {
|
|
284
|
-
if (sel == null) return;
|
|
285
|
-
const total = itemCount();
|
|
286
|
-
if (total === 0) return;
|
|
287
|
-
const safe = utils.clamp(sel, 0, total - 1);
|
|
288
|
-
if (safe !== cursor()) setCursor(safe);
|
|
289
|
-
},
|
|
290
|
-
{ defer: true },
|
|
291
|
-
),
|
|
292
|
-
);
|
|
293
|
-
|
|
294
|
-
const onContainerLayout = function (
|
|
295
|
-
this: lng.ElementNode,
|
|
296
|
-
el: lng.ElementNode,
|
|
297
|
-
) {
|
|
298
|
-
if (props.viewportSize == null && props[sizeKey] == null) {
|
|
299
|
-
const v = (el[sizeKey] as number) || 0;
|
|
300
|
-
if (v && v !== measuredViewport()) setMeasuredViewport(v);
|
|
301
|
-
}
|
|
302
|
-
props.onLayout?.call(this, el);
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
// Position each user-rendered child directly (no wrapper view). We assign
|
|
306
|
-
// x/y on viewRef.children[i] reactively so the slots track the sliding
|
|
307
|
-
// window without an extra node per item.
|
|
308
|
-
s.createEffect(() => {
|
|
309
|
-
if (!viewRef) return;
|
|
310
|
-
const ws = windowStart();
|
|
311
|
-
const st = stride();
|
|
312
|
-
const children = viewRef.children;
|
|
313
|
-
for (let i = 0; i < children.length; i++) {
|
|
314
|
-
const c = children[i];
|
|
315
|
-
if (!c) continue;
|
|
316
|
-
const p = (ws + i) * st;
|
|
317
|
-
if (isRow) c.x = p;
|
|
318
|
-
else c.y = p;
|
|
319
|
-
}
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
// Measure item size from the first rendered child if not provided.
|
|
323
|
-
s.createEffect(() => {
|
|
324
|
-
if (props.itemSize != null) return;
|
|
325
|
-
// Track window changes so we re-measure if the first item swaps.
|
|
326
|
-
windowItems();
|
|
327
|
-
if (!viewRef) return;
|
|
328
|
-
const first = viewRef.children[0];
|
|
329
|
-
if (!first) return;
|
|
330
|
-
let raw = (first[sizeKey] as number) || 0;
|
|
331
|
-
if (props.factorScale) {
|
|
332
|
-
const focusStyle = first.style?.focus as lng.NodeStyles | undefined;
|
|
333
|
-
const scale = focusStyle?.scale ?? first.scale ?? 1;
|
|
334
|
-
raw = raw * scale;
|
|
335
|
-
}
|
|
336
|
-
if (raw && raw !== measuredItem()) setMeasuredItem(raw);
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
return (
|
|
340
|
-
<view
|
|
341
|
-
transitionLeft={isRow ? defaultTransitionBack : undefined}
|
|
342
|
-
transitionRight={isRow ? defaultTransitionForward : undefined}
|
|
343
|
-
transitionUp={!isRow ? defaultTransitionUp : undefined}
|
|
344
|
-
transitionDown={!isRow ? defaultTransitionDown : undefined}
|
|
345
|
-
{...props}
|
|
346
|
-
{...keyHandlers}
|
|
347
|
-
selected={0}
|
|
348
|
-
ref={lngp.chainRefs((el) => {
|
|
349
|
-
viewRef = el as lngp.NavigableElement;
|
|
350
|
-
}, props.ref)}
|
|
351
|
-
forwardFocus={/* @once */ lngp.navigableForwardFocus}
|
|
352
|
-
scrollToIndex={/* @once */ scrollToIndex}
|
|
353
|
-
onSelectedChanged={/* @once */ onSelectedChanged}
|
|
354
|
-
onLayout={onContainerLayout}
|
|
355
|
-
clipping={false}
|
|
356
|
-
width={railWidth()}
|
|
357
|
-
height={railHeight()}
|
|
358
|
-
>
|
|
359
|
-
<s.Index each={windowItems()}>
|
|
360
|
-
{(item, i) => {
|
|
361
|
-
const absIndex = s.createMemo(() => {
|
|
362
|
-
const total = itemCount();
|
|
363
|
-
if (props.wrap && total > 0) {
|
|
364
|
-
return utils.mod(windowStart() + i, total);
|
|
365
|
-
}
|
|
366
|
-
return windowStart() + i;
|
|
367
|
-
});
|
|
368
|
-
// No wrapper view: the user's element is positioned directly via
|
|
369
|
-
// an effect that writes x/y onto viewRef.children[i].
|
|
370
|
-
return props.children(item, absIndex);
|
|
371
|
-
}}
|
|
372
|
-
</s.Index>
|
|
373
|
-
</view>
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export function RailRow<T>(props: RailProps<T>) {
|
|
378
|
-
return createRail(true, props, {
|
|
379
|
-
onLeft: lngp.chainFunctions(
|
|
380
|
-
props.onLeft,
|
|
381
|
-
lngp.handleNavigation('left'),
|
|
382
|
-
) as lng.KeyHandler,
|
|
383
|
-
onRight: lngp.chainFunctions(
|
|
384
|
-
props.onRight,
|
|
385
|
-
lngp.handleNavigation('right'),
|
|
386
|
-
) as lng.KeyHandler,
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
export function RailColumn<T>(props: RailProps<T>) {
|
|
391
|
-
return createRail(false, props, {
|
|
392
|
-
onUp: lngp.chainFunctions(
|
|
393
|
-
props.onUp,
|
|
394
|
-
lngp.handleNavigation('up'),
|
|
395
|
-
) as lng.KeyHandler,
|
|
396
|
-
onDown: lngp.chainFunctions(
|
|
397
|
-
props.onDown,
|
|
398
|
-
lngp.handleNavigation('down'),
|
|
399
|
-
) as lng.KeyHandler,
|
|
400
|
-
});
|
|
401
|
-
}
|