react-random-loading-indicator 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.
- package/LICENSE +21 -0
- package/README.md +103 -0
- package/dist/index.cjs +403 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +76 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +376 -0
- package/dist/index.js.map +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# react-random-loading-indicator
|
|
2
|
+
|
|
3
|
+
A React loading component that randomly displays indicators from your resource list.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install react-random-loading-indicator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { RandomLoadingIndicator } from 'react-random-loading-indicator';
|
|
15
|
+
|
|
16
|
+
export function Demo({ loading }: { loading: boolean }) {
|
|
17
|
+
return (
|
|
18
|
+
<RandomLoadingIndicator
|
|
19
|
+
loading={loading}
|
|
20
|
+
sources={[
|
|
21
|
+
'/loading/1.gif',
|
|
22
|
+
'/loading/2.webp',
|
|
23
|
+
'/loading/3.svg'
|
|
24
|
+
]}
|
|
25
|
+
mode="per-load"
|
|
26
|
+
/>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Random Modes
|
|
32
|
+
|
|
33
|
+
- `per-load`: pick one random indicator every time loading starts
|
|
34
|
+
- `continuous`: keep changing indicators while loading is true
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
<RandomLoadingIndicator
|
|
38
|
+
loading={loading}
|
|
39
|
+
sources={['/loading/a.gif', '/loading/b.gif', '/loading/c.gif']}
|
|
40
|
+
mode="continuous"
|
|
41
|
+
intervalMs={800}
|
|
42
|
+
/>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Supported `sources` Formats
|
|
46
|
+
|
|
47
|
+
1. `string` URL (image)
|
|
48
|
+
2. `image` object
|
|
49
|
+
3. `video` object
|
|
50
|
+
4. `custom` renderer (for Lottie/Canvas/WebGL/etc.)
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
<RandomLoadingIndicator
|
|
54
|
+
loading={loading}
|
|
55
|
+
strategy="weighted"
|
|
56
|
+
sources={[
|
|
57
|
+
'/loading/a.gif',
|
|
58
|
+
{ type: 'video', src: '/loading/wave.webm', weight: 2 },
|
|
59
|
+
{ type: 'custom', weight: 3, render: () => <div>Custom Loader</div> }
|
|
60
|
+
]}
|
|
61
|
+
/>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Key Props
|
|
65
|
+
|
|
66
|
+
| Prop | Type | Default | Description |
|
|
67
|
+
| --- | --- | --- | --- |
|
|
68
|
+
| `loading` | `boolean` | required | Whether loading is active |
|
|
69
|
+
| `sources` | `IndicatorSource[]` | required | Candidate resource list |
|
|
70
|
+
| `mode` | `'per-load' \| 'continuous'` | `'per-load'` | Random switching mode |
|
|
71
|
+
| `intervalMs` | `number` | `1200` | Switch interval in `continuous` mode |
|
|
72
|
+
| `strategy` | `'uniform' \| 'weighted' \| 'shuffle'` | `'uniform'` | Random strategy |
|
|
73
|
+
| `avoidImmediateRepeat` | `boolean` | `true` | Avoid immediate repeats |
|
|
74
|
+
| `seed` | `string \| number` | `undefined` | Deterministic random sequence |
|
|
75
|
+
| `preload` | `boolean` | `false` | Preload image resources |
|
|
76
|
+
| `idleBehavior` | `'hidden' \| 'last'` | `'hidden'` | Hide or keep last indicator after loading |
|
|
77
|
+
| `onIndicatorChange` | `(event) => void` | `undefined` | Callback on indicator change |
|
|
78
|
+
|
|
79
|
+
For full props, use the previewer.
|
|
80
|
+
|
|
81
|
+
## Previewer (Recommended)
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
npm run previewer
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
- Adjust all props visually
|
|
88
|
+
- Put local files in `previewer/public/assets`
|
|
89
|
+
- Use URLs like `/assets/1.svg` in the sources input
|
|
90
|
+
- Paste resource URLs and preview instantly
|
|
91
|
+
- Generate and copy ready-to-use code
|
|
92
|
+
|
|
93
|
+
## Local Development
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
npm install
|
|
97
|
+
npm run test
|
|
98
|
+
npm run build
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
RandomLoadingIndicator: () => RandomLoadingIndicator
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/RandomLoadingIndicator.tsx
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
|
|
30
|
+
// src/random.ts
|
|
31
|
+
function hashSeed(seed) {
|
|
32
|
+
if (typeof seed === "number" && Number.isFinite(seed)) {
|
|
33
|
+
return seed >>> 0;
|
|
34
|
+
}
|
|
35
|
+
const text = String(seed);
|
|
36
|
+
let hash = 2166136261;
|
|
37
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
38
|
+
hash ^= text.charCodeAt(i);
|
|
39
|
+
hash = Math.imul(hash, 16777619);
|
|
40
|
+
}
|
|
41
|
+
return hash >>> 0;
|
|
42
|
+
}
|
|
43
|
+
function createSeededRandom(seed) {
|
|
44
|
+
let state = hashSeed(seed);
|
|
45
|
+
if (state === 0) {
|
|
46
|
+
state = 1831565813;
|
|
47
|
+
}
|
|
48
|
+
return () => {
|
|
49
|
+
state += 1831565813;
|
|
50
|
+
let t = state;
|
|
51
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
52
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
53
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/selection.ts
|
|
58
|
+
function shuffledIndices(total, random) {
|
|
59
|
+
const result = Array.from({ length: total }, (_, index) => index);
|
|
60
|
+
for (let i = result.length - 1; i > 0; i -= 1) {
|
|
61
|
+
const j = Math.floor(random() * (i + 1));
|
|
62
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
function buildCandidates(total, previousIndex, avoidImmediateRepeat) {
|
|
67
|
+
const all = Array.from({ length: total }, (_, index) => index);
|
|
68
|
+
if (!avoidImmediateRepeat || previousIndex === null || total <= 1) {
|
|
69
|
+
return all;
|
|
70
|
+
}
|
|
71
|
+
return all.filter((index) => index !== previousIndex);
|
|
72
|
+
}
|
|
73
|
+
function pickByWeight(candidates, weights, random) {
|
|
74
|
+
const safeWeights = candidates.map((candidate) => Math.max(0, weights[candidate] ?? 1));
|
|
75
|
+
const totalWeight = safeWeights.reduce((sum, weight) => sum + weight, 0);
|
|
76
|
+
if (totalWeight <= 0) {
|
|
77
|
+
return candidates[Math.floor(random() * candidates.length)];
|
|
78
|
+
}
|
|
79
|
+
let cursor = random() * totalWeight;
|
|
80
|
+
for (let i = 0; i < candidates.length; i += 1) {
|
|
81
|
+
cursor -= safeWeights[i];
|
|
82
|
+
if (cursor <= 0) {
|
|
83
|
+
return candidates[i];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return candidates[candidates.length - 1];
|
|
87
|
+
}
|
|
88
|
+
function pickNextIndex({
|
|
89
|
+
total,
|
|
90
|
+
weights,
|
|
91
|
+
strategy,
|
|
92
|
+
previousIndex,
|
|
93
|
+
avoidImmediateRepeat,
|
|
94
|
+
random,
|
|
95
|
+
shuffleState,
|
|
96
|
+
signature
|
|
97
|
+
}) {
|
|
98
|
+
if (total <= 0) {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
if (total === 1) {
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
if (strategy === "shuffle") {
|
|
105
|
+
if (shuffleState.signature !== signature) {
|
|
106
|
+
shuffleState.signature = signature;
|
|
107
|
+
shuffleState.queue = [];
|
|
108
|
+
}
|
|
109
|
+
if (shuffleState.queue.length === 0) {
|
|
110
|
+
shuffleState.queue = shuffledIndices(total, random);
|
|
111
|
+
}
|
|
112
|
+
if (avoidImmediateRepeat && previousIndex !== null && shuffleState.queue.length > 1 && shuffleState.queue[0] === previousIndex) {
|
|
113
|
+
const swapIndex = 1 + Math.floor(random() * (shuffleState.queue.length - 1));
|
|
114
|
+
[shuffleState.queue[0], shuffleState.queue[swapIndex]] = [
|
|
115
|
+
shuffleState.queue[swapIndex],
|
|
116
|
+
shuffleState.queue[0]
|
|
117
|
+
];
|
|
118
|
+
}
|
|
119
|
+
if (avoidImmediateRepeat && previousIndex !== null && shuffleState.queue.length === 1 && shuffleState.queue[0] === previousIndex) {
|
|
120
|
+
shuffleState.queue = shuffledIndices(total, random);
|
|
121
|
+
if (shuffleState.queue[0] === previousIndex && shuffleState.queue.length > 1) {
|
|
122
|
+
[shuffleState.queue[0], shuffleState.queue[1]] = [
|
|
123
|
+
shuffleState.queue[1],
|
|
124
|
+
shuffleState.queue[0]
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const next = shuffleState.queue.shift();
|
|
129
|
+
return typeof next === "number" ? next : null;
|
|
130
|
+
}
|
|
131
|
+
const candidates = buildCandidates(total, previousIndex, avoidImmediateRepeat);
|
|
132
|
+
if (candidates.length === 0) {
|
|
133
|
+
return previousIndex;
|
|
134
|
+
}
|
|
135
|
+
if (strategy === "weighted") {
|
|
136
|
+
return pickByWeight(candidates, weights, random);
|
|
137
|
+
}
|
|
138
|
+
return candidates[Math.floor(random() * candidates.length)];
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// src/RandomLoadingIndicator.tsx
|
|
142
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
143
|
+
function joinClassNames(...values) {
|
|
144
|
+
const merged = values.filter(Boolean).join(" ").trim();
|
|
145
|
+
return merged.length > 0 ? merged : void 0;
|
|
146
|
+
}
|
|
147
|
+
function normalizeSource(source) {
|
|
148
|
+
if (typeof source === "string") {
|
|
149
|
+
return {
|
|
150
|
+
type: "image",
|
|
151
|
+
src: source,
|
|
152
|
+
weight: 1
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (source.type === "video") {
|
|
156
|
+
return {
|
|
157
|
+
...source,
|
|
158
|
+
type: "video",
|
|
159
|
+
weight: source.weight ?? 1
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (source.type === "custom") {
|
|
163
|
+
return {
|
|
164
|
+
...source,
|
|
165
|
+
type: "custom",
|
|
166
|
+
weight: source.weight ?? 1
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
...source,
|
|
171
|
+
type: "image",
|
|
172
|
+
weight: source.weight ?? 1
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function sourceSignature(sources) {
|
|
176
|
+
return sources.map((source, index) => {
|
|
177
|
+
if (source.type === "image") {
|
|
178
|
+
return `${index}:img:${source.src}:${source.weight}`;
|
|
179
|
+
}
|
|
180
|
+
if (source.type === "video") {
|
|
181
|
+
return `${index}:video:${source.src}:${source.weight}`;
|
|
182
|
+
}
|
|
183
|
+
return `${index}:custom:${source.id ?? "no-id"}:${source.weight}`;
|
|
184
|
+
}).join("|");
|
|
185
|
+
}
|
|
186
|
+
function renderSourceNode(source, loading, index, ariaLabel, sourceClassName, sourceStyle) {
|
|
187
|
+
const mergedClassName = joinClassNames(sourceClassName, source.className);
|
|
188
|
+
const mergedStyle = {
|
|
189
|
+
...sourceStyle,
|
|
190
|
+
...source.style
|
|
191
|
+
};
|
|
192
|
+
if (source.type === "image") {
|
|
193
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
194
|
+
"img",
|
|
195
|
+
{
|
|
196
|
+
src: source.src,
|
|
197
|
+
alt: source.alt ?? ariaLabel,
|
|
198
|
+
loading: source.loading,
|
|
199
|
+
decoding: source.decoding,
|
|
200
|
+
referrerPolicy: source.referrerPolicy,
|
|
201
|
+
crossOrigin: source.crossOrigin,
|
|
202
|
+
width: source.width,
|
|
203
|
+
height: source.height,
|
|
204
|
+
className: mergedClassName,
|
|
205
|
+
style: {
|
|
206
|
+
...mergedStyle,
|
|
207
|
+
objectFit: source.objectFit ?? mergedStyle.objectFit
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
if (source.type === "video") {
|
|
213
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
214
|
+
"video",
|
|
215
|
+
{
|
|
216
|
+
src: source.src,
|
|
217
|
+
poster: source.poster,
|
|
218
|
+
autoPlay: source.autoPlay ?? true,
|
|
219
|
+
loop: source.loop ?? true,
|
|
220
|
+
muted: source.muted ?? true,
|
|
221
|
+
playsInline: source.playsInline ?? true,
|
|
222
|
+
controls: source.controls ?? false,
|
|
223
|
+
width: source.width,
|
|
224
|
+
height: source.height,
|
|
225
|
+
className: mergedClassName,
|
|
226
|
+
style: {
|
|
227
|
+
...mergedStyle,
|
|
228
|
+
objectFit: source.objectFit ?? mergedStyle.objectFit
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: mergedClassName, style: mergedStyle, children: source.render({
|
|
234
|
+
isActive: true,
|
|
235
|
+
index,
|
|
236
|
+
loading
|
|
237
|
+
}) });
|
|
238
|
+
}
|
|
239
|
+
function RandomLoadingIndicator({
|
|
240
|
+
loading,
|
|
241
|
+
sources,
|
|
242
|
+
mode = "per-load",
|
|
243
|
+
intervalMs = 1200,
|
|
244
|
+
strategy = "uniform",
|
|
245
|
+
seed,
|
|
246
|
+
avoidImmediateRepeat = true,
|
|
247
|
+
preload = false,
|
|
248
|
+
pauseWhenDocumentHidden = true,
|
|
249
|
+
idleBehavior = "hidden",
|
|
250
|
+
fallback = null,
|
|
251
|
+
className,
|
|
252
|
+
style,
|
|
253
|
+
sourceClassName,
|
|
254
|
+
sourceStyle,
|
|
255
|
+
ariaLabel = "Loading indicator",
|
|
256
|
+
onIndicatorChange
|
|
257
|
+
}) {
|
|
258
|
+
const normalizedSources = (0, import_react.useMemo)(
|
|
259
|
+
() => sources.map((source) => normalizeSource(source)),
|
|
260
|
+
[sources]
|
|
261
|
+
);
|
|
262
|
+
const weights = (0, import_react.useMemo)(
|
|
263
|
+
() => normalizedSources.map((source) => Math.max(0, source.weight)),
|
|
264
|
+
[normalizedSources]
|
|
265
|
+
);
|
|
266
|
+
const sourceKey = (0, import_react.useMemo)(() => sourceSignature(normalizedSources), [normalizedSources]);
|
|
267
|
+
const selectionKey = `${sourceKey}:${strategy}:${seed === void 0 ? "native" : String(seed)}:${avoidImmediateRepeat}`;
|
|
268
|
+
const random = (0, import_react.useMemo)(
|
|
269
|
+
() => seed === void 0 ? Math.random : createSeededRandom(seed),
|
|
270
|
+
[seed]
|
|
271
|
+
);
|
|
272
|
+
const [activeIndex, setActiveIndex] = (0, import_react.useState)(null);
|
|
273
|
+
const activeIndexRef = (0, import_react.useRef)(null);
|
|
274
|
+
const previousRef = (0, import_react.useRef)({
|
|
275
|
+
loading: false,
|
|
276
|
+
selectionKey: ""
|
|
277
|
+
});
|
|
278
|
+
const shuffleStateRef = (0, import_react.useRef)({
|
|
279
|
+
queue: [],
|
|
280
|
+
signature: ""
|
|
281
|
+
});
|
|
282
|
+
const selectNext = (0, import_react.useCallback)(
|
|
283
|
+
(reason) => {
|
|
284
|
+
if (normalizedSources.length === 0) {
|
|
285
|
+
activeIndexRef.current = null;
|
|
286
|
+
setActiveIndex(null);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
const nextIndex = pickNextIndex({
|
|
290
|
+
total: normalizedSources.length,
|
|
291
|
+
weights,
|
|
292
|
+
strategy,
|
|
293
|
+
previousIndex: activeIndexRef.current,
|
|
294
|
+
avoidImmediateRepeat,
|
|
295
|
+
random,
|
|
296
|
+
shuffleState: shuffleStateRef.current,
|
|
297
|
+
signature: selectionKey
|
|
298
|
+
});
|
|
299
|
+
if (nextIndex === null || nextIndex === activeIndexRef.current) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
activeIndexRef.current = nextIndex;
|
|
303
|
+
setActiveIndex(nextIndex);
|
|
304
|
+
onIndicatorChange?.({
|
|
305
|
+
index: nextIndex,
|
|
306
|
+
source: sources[nextIndex],
|
|
307
|
+
reason
|
|
308
|
+
});
|
|
309
|
+
},
|
|
310
|
+
[avoidImmediateRepeat, normalizedSources.length, onIndicatorChange, random, selectionKey, sources, strategy, weights]
|
|
311
|
+
);
|
|
312
|
+
(0, import_react.useEffect)(() => {
|
|
313
|
+
const previous = previousRef.current;
|
|
314
|
+
const selectionChanged = previous.selectionKey !== selectionKey;
|
|
315
|
+
if (loading) {
|
|
316
|
+
if (!previous.loading) {
|
|
317
|
+
selectNext("load-start");
|
|
318
|
+
} else if (selectionChanged || activeIndexRef.current === null || activeIndexRef.current >= normalizedSources.length) {
|
|
319
|
+
selectNext("source-update");
|
|
320
|
+
}
|
|
321
|
+
} else if (previous.loading && idleBehavior === "hidden") {
|
|
322
|
+
activeIndexRef.current = null;
|
|
323
|
+
setActiveIndex(null);
|
|
324
|
+
}
|
|
325
|
+
previousRef.current = {
|
|
326
|
+
loading,
|
|
327
|
+
selectionKey
|
|
328
|
+
};
|
|
329
|
+
}, [idleBehavior, loading, normalizedSources.length, selectNext, selectionKey]);
|
|
330
|
+
(0, import_react.useEffect)(() => {
|
|
331
|
+
if (normalizedSources.length > 0) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
activeIndexRef.current = null;
|
|
335
|
+
setActiveIndex(null);
|
|
336
|
+
}, [normalizedSources.length]);
|
|
337
|
+
(0, import_react.useEffect)(() => {
|
|
338
|
+
if (!loading || mode !== "continuous" || normalizedSources.length < 2) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
const delay = Math.max(100, intervalMs);
|
|
342
|
+
const timerId = window.setInterval(() => {
|
|
343
|
+
if (pauseWhenDocumentHidden && typeof document !== "undefined" && document.visibilityState === "hidden") {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
selectNext("interval");
|
|
347
|
+
}, delay);
|
|
348
|
+
return () => {
|
|
349
|
+
window.clearInterval(timerId);
|
|
350
|
+
};
|
|
351
|
+
}, [intervalMs, loading, mode, normalizedSources.length, pauseWhenDocumentHidden, selectNext]);
|
|
352
|
+
(0, import_react.useEffect)(() => {
|
|
353
|
+
if (!preload || typeof window === "undefined") {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const preloaders = [];
|
|
357
|
+
normalizedSources.forEach((source) => {
|
|
358
|
+
if (source.type === "image") {
|
|
359
|
+
const image = new window.Image();
|
|
360
|
+
if (source.crossOrigin) {
|
|
361
|
+
image.crossOrigin = source.crossOrigin;
|
|
362
|
+
}
|
|
363
|
+
image.decoding = source.decoding ?? "async";
|
|
364
|
+
image.src = source.src;
|
|
365
|
+
preloaders.push(image);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
return () => {
|
|
369
|
+
preloaders.forEach((image) => {
|
|
370
|
+
image.src = "";
|
|
371
|
+
});
|
|
372
|
+
};
|
|
373
|
+
}, [normalizedSources, preload]);
|
|
374
|
+
if (!loading && idleBehavior === "hidden") {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
if (activeIndex === null) {
|
|
378
|
+
return loading ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback }) : null;
|
|
379
|
+
}
|
|
380
|
+
const activeSource = normalizedSources[activeIndex];
|
|
381
|
+
if (!activeSource) {
|
|
382
|
+
return loading ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback }) : null;
|
|
383
|
+
}
|
|
384
|
+
const currentIndex = activeIndex;
|
|
385
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
386
|
+
"span",
|
|
387
|
+
{
|
|
388
|
+
role: "status",
|
|
389
|
+
"aria-live": "polite",
|
|
390
|
+
"aria-busy": loading,
|
|
391
|
+
"aria-label": ariaLabel,
|
|
392
|
+
className,
|
|
393
|
+
style,
|
|
394
|
+
"data-rli-index": currentIndex,
|
|
395
|
+
children: renderSourceNode(activeSource, loading, currentIndex, ariaLabel, sourceClassName, sourceStyle)
|
|
396
|
+
}
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
400
|
+
0 && (module.exports = {
|
|
401
|
+
RandomLoadingIndicator
|
|
402
|
+
});
|
|
403
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/RandomLoadingIndicator.tsx","../src/random.ts","../src/selection.ts"],"sourcesContent":["export { RandomLoadingIndicator } from './RandomLoadingIndicator';\nexport type {\n BaseIndicatorSource,\n CustomIndicatorSource,\n IdleBehavior,\n ImageIndicatorSource,\n IndicatorChangeEvent,\n IndicatorChangeReason,\n IndicatorRenderContext,\n IndicatorSource,\n RandomLoadingIndicatorProps,\n RandomMode,\n RandomStrategy,\n VideoIndicatorSource\n} from './types';\n","import { useCallback, useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from 'react';\nimport { createSeededRandom } from './random';\nimport { pickNextIndex, type ShuffleState } from './selection';\nimport type {\n CustomIndicatorSource,\n ImageIndicatorSource,\n IndicatorChangeReason,\n IndicatorSource,\n RandomLoadingIndicatorProps,\n VideoIndicatorSource\n} from './types';\n\ntype NormalizedImageSource = Omit<ImageIndicatorSource, 'type' | 'weight'> & {\n type: 'image';\n weight: number;\n};\n\ntype NormalizedVideoSource = Omit<VideoIndicatorSource, 'weight'> & {\n weight: number;\n};\n\ntype NormalizedCustomSource = Omit<CustomIndicatorSource, 'weight'> & {\n weight: number;\n};\n\ntype NormalizedSource = NormalizedImageSource | NormalizedVideoSource | NormalizedCustomSource;\n\nfunction joinClassNames(...values: Array<string | undefined>): string | undefined {\n const merged = values.filter(Boolean).join(' ').trim();\n return merged.length > 0 ? merged : undefined;\n}\n\nfunction normalizeSource(source: IndicatorSource): NormalizedSource {\n if (typeof source === 'string') {\n return {\n type: 'image',\n src: source,\n weight: 1\n };\n }\n\n if (source.type === 'video') {\n return {\n ...source,\n type: 'video',\n weight: source.weight ?? 1\n };\n }\n\n if (source.type === 'custom') {\n return {\n ...source,\n type: 'custom',\n weight: source.weight ?? 1\n };\n }\n\n return {\n ...source,\n type: 'image',\n weight: source.weight ?? 1\n };\n}\n\nfunction sourceSignature(sources: NormalizedSource[]): string {\n return sources\n .map((source, index) => {\n if (source.type === 'image') {\n return `${index}:img:${source.src}:${source.weight}`;\n }\n if (source.type === 'video') {\n return `${index}:video:${source.src}:${source.weight}`;\n }\n return `${index}:custom:${source.id ?? 'no-id'}:${source.weight}`;\n })\n .join('|');\n}\n\nfunction renderSourceNode(\n source: NormalizedSource,\n loading: boolean,\n index: number,\n ariaLabel: string,\n sourceClassName?: string,\n sourceStyle?: CSSProperties\n): ReactNode {\n const mergedClassName = joinClassNames(sourceClassName, source.className);\n const mergedStyle = {\n ...sourceStyle,\n ...source.style\n };\n\n if (source.type === 'image') {\n return (\n <img\n src={source.src}\n alt={source.alt ?? ariaLabel}\n loading={source.loading}\n decoding={source.decoding}\n referrerPolicy={source.referrerPolicy}\n crossOrigin={source.crossOrigin}\n width={source.width}\n height={source.height}\n className={mergedClassName}\n style={{\n ...mergedStyle,\n objectFit: source.objectFit ?? mergedStyle.objectFit\n }}\n />\n );\n }\n\n if (source.type === 'video') {\n return (\n <video\n src={source.src}\n poster={source.poster}\n autoPlay={source.autoPlay ?? true}\n loop={source.loop ?? true}\n muted={source.muted ?? true}\n playsInline={source.playsInline ?? true}\n controls={source.controls ?? false}\n width={source.width}\n height={source.height}\n className={mergedClassName}\n style={{\n ...mergedStyle,\n objectFit: source.objectFit ?? mergedStyle.objectFit\n }}\n />\n );\n }\n\n return (\n <span className={mergedClassName} style={mergedStyle}>\n {source.render({\n isActive: true,\n index,\n loading\n })}\n </span>\n );\n}\n\nexport function RandomLoadingIndicator({\n loading,\n sources,\n mode = 'per-load',\n intervalMs = 1200,\n strategy = 'uniform',\n seed,\n avoidImmediateRepeat = true,\n preload = false,\n pauseWhenDocumentHidden = true,\n idleBehavior = 'hidden',\n fallback = null,\n className,\n style,\n sourceClassName,\n sourceStyle,\n ariaLabel = 'Loading indicator',\n onIndicatorChange\n}: RandomLoadingIndicatorProps) {\n const normalizedSources = useMemo(\n () => sources.map((source) => normalizeSource(source)),\n [sources]\n );\n const weights = useMemo(\n () => normalizedSources.map((source) => Math.max(0, source.weight)),\n [normalizedSources]\n );\n const sourceKey = useMemo(() => sourceSignature(normalizedSources), [normalizedSources]);\n const selectionKey = `${sourceKey}:${strategy}:${seed === undefined ? 'native' : String(seed)}:${avoidImmediateRepeat}`;\n\n const random = useMemo(\n () => (seed === undefined ? Math.random : createSeededRandom(seed)),\n [seed]\n );\n\n const [activeIndex, setActiveIndex] = useState<number | null>(null);\n const activeIndexRef = useRef<number | null>(null);\n const previousRef = useRef({\n loading: false,\n selectionKey: ''\n });\n const shuffleStateRef = useRef<ShuffleState>({\n queue: [],\n signature: ''\n });\n\n const selectNext = useCallback(\n (reason: IndicatorChangeReason) => {\n if (normalizedSources.length === 0) {\n activeIndexRef.current = null;\n setActiveIndex(null);\n return;\n }\n\n const nextIndex = pickNextIndex({\n total: normalizedSources.length,\n weights,\n strategy,\n previousIndex: activeIndexRef.current,\n avoidImmediateRepeat,\n random,\n shuffleState: shuffleStateRef.current,\n signature: selectionKey\n });\n\n if (nextIndex === null || nextIndex === activeIndexRef.current) {\n return;\n }\n\n activeIndexRef.current = nextIndex;\n setActiveIndex(nextIndex);\n onIndicatorChange?.({\n index: nextIndex,\n source: sources[nextIndex],\n reason\n });\n },\n [avoidImmediateRepeat, normalizedSources.length, onIndicatorChange, random, selectionKey, sources, strategy, weights]\n );\n\n useEffect(() => {\n const previous = previousRef.current;\n const selectionChanged = previous.selectionKey !== selectionKey;\n\n if (loading) {\n if (!previous.loading) {\n selectNext('load-start');\n } else if (selectionChanged || activeIndexRef.current === null || activeIndexRef.current >= normalizedSources.length) {\n selectNext('source-update');\n }\n } else if (previous.loading && idleBehavior === 'hidden') {\n activeIndexRef.current = null;\n setActiveIndex(null);\n }\n\n previousRef.current = {\n loading,\n selectionKey\n };\n }, [idleBehavior, loading, normalizedSources.length, selectNext, selectionKey]);\n\n useEffect(() => {\n if (normalizedSources.length > 0) {\n return;\n }\n activeIndexRef.current = null;\n setActiveIndex(null);\n }, [normalizedSources.length]);\n\n useEffect(() => {\n if (!loading || mode !== 'continuous' || normalizedSources.length < 2) {\n return;\n }\n\n const delay = Math.max(100, intervalMs);\n const timerId = window.setInterval(() => {\n if (pauseWhenDocumentHidden && typeof document !== 'undefined' && document.visibilityState === 'hidden') {\n return;\n }\n selectNext('interval');\n }, delay);\n\n return () => {\n window.clearInterval(timerId);\n };\n }, [intervalMs, loading, mode, normalizedSources.length, pauseWhenDocumentHidden, selectNext]);\n\n useEffect(() => {\n if (!preload || typeof window === 'undefined') {\n return;\n }\n\n const preloaders: HTMLImageElement[] = [];\n normalizedSources.forEach((source) => {\n if (source.type === 'image') {\n const image = new window.Image();\n if (source.crossOrigin) {\n image.crossOrigin = source.crossOrigin;\n }\n image.decoding = source.decoding ?? 'async';\n image.src = source.src;\n preloaders.push(image);\n }\n });\n\n return () => {\n preloaders.forEach((image) => {\n image.src = '';\n });\n };\n }, [normalizedSources, preload]);\n\n if (!loading && idleBehavior === 'hidden') {\n return null;\n }\n\n if (activeIndex === null) {\n return loading ? <>{fallback}</> : null;\n }\n\n const activeSource = normalizedSources[activeIndex];\n if (!activeSource) {\n return loading ? <>{fallback}</> : null;\n }\n\n const currentIndex = activeIndex;\n\n return (\n <span\n role=\"status\"\n aria-live=\"polite\"\n aria-busy={loading}\n aria-label={ariaLabel}\n className={className}\n style={style}\n data-rli-index={currentIndex}\n >\n {renderSourceNode(activeSource, loading, currentIndex, ariaLabel, sourceClassName, sourceStyle)}\n </span>\n );\n}\n","function hashSeed(seed: string | number): number {\n if (typeof seed === 'number' && Number.isFinite(seed)) {\n return seed >>> 0;\n }\n\n const text = String(seed);\n let hash = 2166136261;\n for (let i = 0; i < text.length; i += 1) {\n hash ^= text.charCodeAt(i);\n hash = Math.imul(hash, 16777619);\n }\n return hash >>> 0;\n}\n\nexport function createSeededRandom(seed: string | number): () => number {\n let state = hashSeed(seed);\n if (state === 0) {\n state = 0x6d2b79f5;\n }\n\n return () => {\n state += 0x6d2b79f5;\n let t = state;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n };\n}\n","import type { RandomStrategy } from './types';\n\nexport interface ShuffleState {\n queue: number[];\n signature: string;\n}\n\nexport interface PickNextIndexParams {\n total: number;\n weights: number[];\n strategy: RandomStrategy;\n previousIndex: number | null;\n avoidImmediateRepeat: boolean;\n random: () => number;\n shuffleState: ShuffleState;\n signature: string;\n}\n\nfunction shuffledIndices(total: number, random: () => number): number[] {\n const result = Array.from({ length: total }, (_, index) => index);\n for (let i = result.length - 1; i > 0; i -= 1) {\n const j = Math.floor(random() * (i + 1));\n [result[i], result[j]] = [result[j], result[i]];\n }\n return result;\n}\n\nfunction buildCandidates(\n total: number,\n previousIndex: number | null,\n avoidImmediateRepeat: boolean\n): number[] {\n const all = Array.from({ length: total }, (_, index) => index);\n if (!avoidImmediateRepeat || previousIndex === null || total <= 1) {\n return all;\n }\n return all.filter((index) => index !== previousIndex);\n}\n\nfunction pickByWeight(\n candidates: number[],\n weights: number[],\n random: () => number\n): number {\n const safeWeights = candidates.map((candidate) => Math.max(0, weights[candidate] ?? 1));\n const totalWeight = safeWeights.reduce((sum, weight) => sum + weight, 0);\n\n if (totalWeight <= 0) {\n return candidates[Math.floor(random() * candidates.length)];\n }\n\n let cursor = random() * totalWeight;\n for (let i = 0; i < candidates.length; i += 1) {\n cursor -= safeWeights[i];\n if (cursor <= 0) {\n return candidates[i];\n }\n }\n\n return candidates[candidates.length - 1];\n}\n\nexport function pickNextIndex({\n total,\n weights,\n strategy,\n previousIndex,\n avoidImmediateRepeat,\n random,\n shuffleState,\n signature\n}: PickNextIndexParams): number | null {\n if (total <= 0) {\n return null;\n }\n\n if (total === 1) {\n return 0;\n }\n\n if (strategy === 'shuffle') {\n if (shuffleState.signature !== signature) {\n shuffleState.signature = signature;\n shuffleState.queue = [];\n }\n\n if (shuffleState.queue.length === 0) {\n shuffleState.queue = shuffledIndices(total, random);\n }\n\n if (\n avoidImmediateRepeat &&\n previousIndex !== null &&\n shuffleState.queue.length > 1 &&\n shuffleState.queue[0] === previousIndex\n ) {\n const swapIndex = 1 + Math.floor(random() * (shuffleState.queue.length - 1));\n [shuffleState.queue[0], shuffleState.queue[swapIndex]] = [\n shuffleState.queue[swapIndex],\n shuffleState.queue[0]\n ];\n }\n\n if (\n avoidImmediateRepeat &&\n previousIndex !== null &&\n shuffleState.queue.length === 1 &&\n shuffleState.queue[0] === previousIndex\n ) {\n shuffleState.queue = shuffledIndices(total, random);\n if (shuffleState.queue[0] === previousIndex && shuffleState.queue.length > 1) {\n [shuffleState.queue[0], shuffleState.queue[1]] = [\n shuffleState.queue[1],\n shuffleState.queue[0]\n ];\n }\n }\n\n const next = shuffleState.queue.shift();\n return typeof next === 'number' ? next : null;\n }\n\n const candidates = buildCandidates(total, previousIndex, avoidImmediateRepeat);\n if (candidates.length === 0) {\n return previousIndex;\n }\n\n if (strategy === 'weighted') {\n return pickByWeight(candidates, weights, random);\n }\n\n return candidates[Math.floor(random() * candidates.length)];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAsG;;;ACAtG,SAAS,SAAS,MAA+B;AAC/C,MAAI,OAAO,SAAS,YAAY,OAAO,SAAS,IAAI,GAAG;AACrD,WAAO,SAAS;AAAA,EAClB;AAEA,QAAM,OAAO,OAAO,IAAI;AACxB,MAAI,OAAO;AACX,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,YAAQ,KAAK,WAAW,CAAC;AACzB,WAAO,KAAK,KAAK,MAAM,QAAQ;AAAA,EACjC;AACA,SAAO,SAAS;AAClB;AAEO,SAAS,mBAAmB,MAAqC;AACtE,MAAI,QAAQ,SAAS,IAAI;AACzB,MAAI,UAAU,GAAG;AACf,YAAQ;AAAA,EACV;AAEA,SAAO,MAAM;AACX,aAAS;AACT,QAAI,IAAI;AACR,QAAI,KAAK,KAAK,IAAK,MAAM,IAAK,IAAI,CAAC;AACnC,SAAK,IAAI,KAAK,KAAK,IAAK,MAAM,GAAI,IAAI,EAAE;AACxC,aAAS,IAAK,MAAM,QAAS,KAAK;AAAA,EACpC;AACF;;;ACTA,SAAS,gBAAgB,OAAe,QAAgC;AACtE,QAAM,SAAS,MAAM,KAAK,EAAE,QAAQ,MAAM,GAAG,CAAC,GAAG,UAAU,KAAK;AAChE,WAAS,IAAI,OAAO,SAAS,GAAG,IAA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O,SAAS,SAAS;AAC3B,cAAM,QAAQ,IAAI,OAAO,MAAM;AAC/B,YAAI,OAAO,aAAa;AACtB,gBAAM,cAAc,OAAO;AAAA,QAC7B;AACA,cAAM,WAAW,OAAO,YAAY;AACpC,cAAM,MAAM,OAAO;AACnB,mBAAW,KAAK,KAAK;AAAA,MACvB;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,iBAAW,QAAQ,CAAC,UAAU;AAC5B,cAAM,MAAM;AAAA,MACd,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,mBAAmB,OAAO,CAAC;AAE/B,MAAI,CAAC,WAAW,iBAAiB,UAAU;AACzC,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,MAAM;AACxB,WAAO,UAAU,2EAAG,oBAAS,IAAM;AAAA,EACrC;AAEA,QAAM,eAAe,kBAAkB,WAAW;AAClD,MAAI,CAAC,cAAc;AACjB,WAAO,UAAU,2EAAG,oBAAS,IAAM;AAAA,EACrC;AAEA,QAAM,eAAe;AAErB,SACE;AAAA,IAAC;AAAA;AAAA,MACC,MAAK;AAAA,MACL,aAAU;AAAA,MACV,aAAW;AAAA,MACX,cAAY;AAAA,MACZ;AAAA,MACA;AAAA,MACA,kBAAgB;AAAA,MAEf,2BAAiB,cAAc,SAAS,cAAc,WAAW,iBAAiB,WAAW;AAAA;AAAA,EAChG;AAEJ;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { CSSProperties, ReactNode, ImgHTMLAttributes, VideoHTMLAttributes } from 'react';
|
|
3
|
+
|
|
4
|
+
type RandomMode = 'per-load' | 'continuous';
|
|
5
|
+
type RandomStrategy = 'uniform' | 'weighted' | 'shuffle';
|
|
6
|
+
type IdleBehavior = 'hidden' | 'last';
|
|
7
|
+
interface IndicatorRenderContext {
|
|
8
|
+
isActive: boolean;
|
|
9
|
+
index: number;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface BaseIndicatorSource {
|
|
13
|
+
id?: string;
|
|
14
|
+
weight?: number;
|
|
15
|
+
className?: string;
|
|
16
|
+
style?: CSSProperties;
|
|
17
|
+
}
|
|
18
|
+
interface ImageIndicatorSource extends BaseIndicatorSource {
|
|
19
|
+
type?: 'image';
|
|
20
|
+
src: string;
|
|
21
|
+
alt?: string;
|
|
22
|
+
decoding?: ImgHTMLAttributes<HTMLImageElement>['decoding'];
|
|
23
|
+
loading?: ImgHTMLAttributes<HTMLImageElement>['loading'];
|
|
24
|
+
referrerPolicy?: ImgHTMLAttributes<HTMLImageElement>['referrerPolicy'];
|
|
25
|
+
crossOrigin?: ImgHTMLAttributes<HTMLImageElement>['crossOrigin'];
|
|
26
|
+
objectFit?: CSSProperties['objectFit'];
|
|
27
|
+
width?: number | string;
|
|
28
|
+
height?: number | string;
|
|
29
|
+
}
|
|
30
|
+
interface VideoIndicatorSource extends BaseIndicatorSource {
|
|
31
|
+
type: 'video';
|
|
32
|
+
src: string;
|
|
33
|
+
poster?: string;
|
|
34
|
+
autoPlay?: VideoHTMLAttributes<HTMLVideoElement>['autoPlay'];
|
|
35
|
+
loop?: VideoHTMLAttributes<HTMLVideoElement>['loop'];
|
|
36
|
+
muted?: VideoHTMLAttributes<HTMLVideoElement>['muted'];
|
|
37
|
+
playsInline?: VideoHTMLAttributes<HTMLVideoElement>['playsInline'];
|
|
38
|
+
controls?: VideoHTMLAttributes<HTMLVideoElement>['controls'];
|
|
39
|
+
objectFit?: CSSProperties['objectFit'];
|
|
40
|
+
width?: number | string;
|
|
41
|
+
height?: number | string;
|
|
42
|
+
}
|
|
43
|
+
interface CustomIndicatorSource extends BaseIndicatorSource {
|
|
44
|
+
type: 'custom';
|
|
45
|
+
render: (context: IndicatorRenderContext) => ReactNode;
|
|
46
|
+
}
|
|
47
|
+
type IndicatorSource = string | ImageIndicatorSource | VideoIndicatorSource | CustomIndicatorSource;
|
|
48
|
+
type IndicatorChangeReason = 'load-start' | 'interval' | 'source-update';
|
|
49
|
+
interface IndicatorChangeEvent {
|
|
50
|
+
index: number;
|
|
51
|
+
source: IndicatorSource;
|
|
52
|
+
reason: IndicatorChangeReason;
|
|
53
|
+
}
|
|
54
|
+
interface RandomLoadingIndicatorProps {
|
|
55
|
+
loading: boolean;
|
|
56
|
+
sources: IndicatorSource[];
|
|
57
|
+
mode?: RandomMode;
|
|
58
|
+
intervalMs?: number;
|
|
59
|
+
strategy?: RandomStrategy;
|
|
60
|
+
seed?: string | number;
|
|
61
|
+
avoidImmediateRepeat?: boolean;
|
|
62
|
+
preload?: boolean;
|
|
63
|
+
pauseWhenDocumentHidden?: boolean;
|
|
64
|
+
idleBehavior?: IdleBehavior;
|
|
65
|
+
fallback?: ReactNode;
|
|
66
|
+
className?: string;
|
|
67
|
+
style?: CSSProperties;
|
|
68
|
+
sourceClassName?: string;
|
|
69
|
+
sourceStyle?: CSSProperties;
|
|
70
|
+
ariaLabel?: string;
|
|
71
|
+
onIndicatorChange?: (event: IndicatorChangeEvent) => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
declare function RandomLoadingIndicator({ loading, sources, mode, intervalMs, strategy, seed, avoidImmediateRepeat, preload, pauseWhenDocumentHidden, idleBehavior, fallback, className, style, sourceClassName, sourceStyle, ariaLabel, onIndicatorChange }: RandomLoadingIndicatorProps): react_jsx_runtime.JSX.Element | null;
|
|
75
|
+
|
|
76
|
+
export { type BaseIndicatorSource, type CustomIndicatorSource, type IdleBehavior, type ImageIndicatorSource, type IndicatorChangeEvent, type IndicatorChangeReason, type IndicatorRenderContext, type IndicatorSource, RandomLoadingIndicator, type RandomLoadingIndicatorProps, type RandomMode, type RandomStrategy, type VideoIndicatorSource };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { CSSProperties, ReactNode, ImgHTMLAttributes, VideoHTMLAttributes } from 'react';
|
|
3
|
+
|
|
4
|
+
type RandomMode = 'per-load' | 'continuous';
|
|
5
|
+
type RandomStrategy = 'uniform' | 'weighted' | 'shuffle';
|
|
6
|
+
type IdleBehavior = 'hidden' | 'last';
|
|
7
|
+
interface IndicatorRenderContext {
|
|
8
|
+
isActive: boolean;
|
|
9
|
+
index: number;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface BaseIndicatorSource {
|
|
13
|
+
id?: string;
|
|
14
|
+
weight?: number;
|
|
15
|
+
className?: string;
|
|
16
|
+
style?: CSSProperties;
|
|
17
|
+
}
|
|
18
|
+
interface ImageIndicatorSource extends BaseIndicatorSource {
|
|
19
|
+
type?: 'image';
|
|
20
|
+
src: string;
|
|
21
|
+
alt?: string;
|
|
22
|
+
decoding?: ImgHTMLAttributes<HTMLImageElement>['decoding'];
|
|
23
|
+
loading?: ImgHTMLAttributes<HTMLImageElement>['loading'];
|
|
24
|
+
referrerPolicy?: ImgHTMLAttributes<HTMLImageElement>['referrerPolicy'];
|
|
25
|
+
crossOrigin?: ImgHTMLAttributes<HTMLImageElement>['crossOrigin'];
|
|
26
|
+
objectFit?: CSSProperties['objectFit'];
|
|
27
|
+
width?: number | string;
|
|
28
|
+
height?: number | string;
|
|
29
|
+
}
|
|
30
|
+
interface VideoIndicatorSource extends BaseIndicatorSource {
|
|
31
|
+
type: 'video';
|
|
32
|
+
src: string;
|
|
33
|
+
poster?: string;
|
|
34
|
+
autoPlay?: VideoHTMLAttributes<HTMLVideoElement>['autoPlay'];
|
|
35
|
+
loop?: VideoHTMLAttributes<HTMLVideoElement>['loop'];
|
|
36
|
+
muted?: VideoHTMLAttributes<HTMLVideoElement>['muted'];
|
|
37
|
+
playsInline?: VideoHTMLAttributes<HTMLVideoElement>['playsInline'];
|
|
38
|
+
controls?: VideoHTMLAttributes<HTMLVideoElement>['controls'];
|
|
39
|
+
objectFit?: CSSProperties['objectFit'];
|
|
40
|
+
width?: number | string;
|
|
41
|
+
height?: number | string;
|
|
42
|
+
}
|
|
43
|
+
interface CustomIndicatorSource extends BaseIndicatorSource {
|
|
44
|
+
type: 'custom';
|
|
45
|
+
render: (context: IndicatorRenderContext) => ReactNode;
|
|
46
|
+
}
|
|
47
|
+
type IndicatorSource = string | ImageIndicatorSource | VideoIndicatorSource | CustomIndicatorSource;
|
|
48
|
+
type IndicatorChangeReason = 'load-start' | 'interval' | 'source-update';
|
|
49
|
+
interface IndicatorChangeEvent {
|
|
50
|
+
index: number;
|
|
51
|
+
source: IndicatorSource;
|
|
52
|
+
reason: IndicatorChangeReason;
|
|
53
|
+
}
|
|
54
|
+
interface RandomLoadingIndicatorProps {
|
|
55
|
+
loading: boolean;
|
|
56
|
+
sources: IndicatorSource[];
|
|
57
|
+
mode?: RandomMode;
|
|
58
|
+
intervalMs?: number;
|
|
59
|
+
strategy?: RandomStrategy;
|
|
60
|
+
seed?: string | number;
|
|
61
|
+
avoidImmediateRepeat?: boolean;
|
|
62
|
+
preload?: boolean;
|
|
63
|
+
pauseWhenDocumentHidden?: boolean;
|
|
64
|
+
idleBehavior?: IdleBehavior;
|
|
65
|
+
fallback?: ReactNode;
|
|
66
|
+
className?: string;
|
|
67
|
+
style?: CSSProperties;
|
|
68
|
+
sourceClassName?: string;
|
|
69
|
+
sourceStyle?: CSSProperties;
|
|
70
|
+
ariaLabel?: string;
|
|
71
|
+
onIndicatorChange?: (event: IndicatorChangeEvent) => void;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
declare function RandomLoadingIndicator({ loading, sources, mode, intervalMs, strategy, seed, avoidImmediateRepeat, preload, pauseWhenDocumentHidden, idleBehavior, fallback, className, style, sourceClassName, sourceStyle, ariaLabel, onIndicatorChange }: RandomLoadingIndicatorProps): react_jsx_runtime.JSX.Element | null;
|
|
75
|
+
|
|
76
|
+
export { type BaseIndicatorSource, type CustomIndicatorSource, type IdleBehavior, type ImageIndicatorSource, type IndicatorChangeEvent, type IndicatorChangeReason, type IndicatorRenderContext, type IndicatorSource, RandomLoadingIndicator, type RandomLoadingIndicatorProps, type RandomMode, type RandomStrategy, type VideoIndicatorSource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
// src/RandomLoadingIndicator.tsx
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
// src/random.ts
|
|
5
|
+
function hashSeed(seed) {
|
|
6
|
+
if (typeof seed === "number" && Number.isFinite(seed)) {
|
|
7
|
+
return seed >>> 0;
|
|
8
|
+
}
|
|
9
|
+
const text = String(seed);
|
|
10
|
+
let hash = 2166136261;
|
|
11
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
12
|
+
hash ^= text.charCodeAt(i);
|
|
13
|
+
hash = Math.imul(hash, 16777619);
|
|
14
|
+
}
|
|
15
|
+
return hash >>> 0;
|
|
16
|
+
}
|
|
17
|
+
function createSeededRandom(seed) {
|
|
18
|
+
let state = hashSeed(seed);
|
|
19
|
+
if (state === 0) {
|
|
20
|
+
state = 1831565813;
|
|
21
|
+
}
|
|
22
|
+
return () => {
|
|
23
|
+
state += 1831565813;
|
|
24
|
+
let t = state;
|
|
25
|
+
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
26
|
+
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
27
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/selection.ts
|
|
32
|
+
function shuffledIndices(total, random) {
|
|
33
|
+
const result = Array.from({ length: total }, (_, index) => index);
|
|
34
|
+
for (let i = result.length - 1; i > 0; i -= 1) {
|
|
35
|
+
const j = Math.floor(random() * (i + 1));
|
|
36
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
function buildCandidates(total, previousIndex, avoidImmediateRepeat) {
|
|
41
|
+
const all = Array.from({ length: total }, (_, index) => index);
|
|
42
|
+
if (!avoidImmediateRepeat || previousIndex === null || total <= 1) {
|
|
43
|
+
return all;
|
|
44
|
+
}
|
|
45
|
+
return all.filter((index) => index !== previousIndex);
|
|
46
|
+
}
|
|
47
|
+
function pickByWeight(candidates, weights, random) {
|
|
48
|
+
const safeWeights = candidates.map((candidate) => Math.max(0, weights[candidate] ?? 1));
|
|
49
|
+
const totalWeight = safeWeights.reduce((sum, weight) => sum + weight, 0);
|
|
50
|
+
if (totalWeight <= 0) {
|
|
51
|
+
return candidates[Math.floor(random() * candidates.length)];
|
|
52
|
+
}
|
|
53
|
+
let cursor = random() * totalWeight;
|
|
54
|
+
for (let i = 0; i < candidates.length; i += 1) {
|
|
55
|
+
cursor -= safeWeights[i];
|
|
56
|
+
if (cursor <= 0) {
|
|
57
|
+
return candidates[i];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return candidates[candidates.length - 1];
|
|
61
|
+
}
|
|
62
|
+
function pickNextIndex({
|
|
63
|
+
total,
|
|
64
|
+
weights,
|
|
65
|
+
strategy,
|
|
66
|
+
previousIndex,
|
|
67
|
+
avoidImmediateRepeat,
|
|
68
|
+
random,
|
|
69
|
+
shuffleState,
|
|
70
|
+
signature
|
|
71
|
+
}) {
|
|
72
|
+
if (total <= 0) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
if (total === 1) {
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (strategy === "shuffle") {
|
|
79
|
+
if (shuffleState.signature !== signature) {
|
|
80
|
+
shuffleState.signature = signature;
|
|
81
|
+
shuffleState.queue = [];
|
|
82
|
+
}
|
|
83
|
+
if (shuffleState.queue.length === 0) {
|
|
84
|
+
shuffleState.queue = shuffledIndices(total, random);
|
|
85
|
+
}
|
|
86
|
+
if (avoidImmediateRepeat && previousIndex !== null && shuffleState.queue.length > 1 && shuffleState.queue[0] === previousIndex) {
|
|
87
|
+
const swapIndex = 1 + Math.floor(random() * (shuffleState.queue.length - 1));
|
|
88
|
+
[shuffleState.queue[0], shuffleState.queue[swapIndex]] = [
|
|
89
|
+
shuffleState.queue[swapIndex],
|
|
90
|
+
shuffleState.queue[0]
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
if (avoidImmediateRepeat && previousIndex !== null && shuffleState.queue.length === 1 && shuffleState.queue[0] === previousIndex) {
|
|
94
|
+
shuffleState.queue = shuffledIndices(total, random);
|
|
95
|
+
if (shuffleState.queue[0] === previousIndex && shuffleState.queue.length > 1) {
|
|
96
|
+
[shuffleState.queue[0], shuffleState.queue[1]] = [
|
|
97
|
+
shuffleState.queue[1],
|
|
98
|
+
shuffleState.queue[0]
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const next = shuffleState.queue.shift();
|
|
103
|
+
return typeof next === "number" ? next : null;
|
|
104
|
+
}
|
|
105
|
+
const candidates = buildCandidates(total, previousIndex, avoidImmediateRepeat);
|
|
106
|
+
if (candidates.length === 0) {
|
|
107
|
+
return previousIndex;
|
|
108
|
+
}
|
|
109
|
+
if (strategy === "weighted") {
|
|
110
|
+
return pickByWeight(candidates, weights, random);
|
|
111
|
+
}
|
|
112
|
+
return candidates[Math.floor(random() * candidates.length)];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// src/RandomLoadingIndicator.tsx
|
|
116
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
117
|
+
function joinClassNames(...values) {
|
|
118
|
+
const merged = values.filter(Boolean).join(" ").trim();
|
|
119
|
+
return merged.length > 0 ? merged : void 0;
|
|
120
|
+
}
|
|
121
|
+
function normalizeSource(source) {
|
|
122
|
+
if (typeof source === "string") {
|
|
123
|
+
return {
|
|
124
|
+
type: "image",
|
|
125
|
+
src: source,
|
|
126
|
+
weight: 1
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (source.type === "video") {
|
|
130
|
+
return {
|
|
131
|
+
...source,
|
|
132
|
+
type: "video",
|
|
133
|
+
weight: source.weight ?? 1
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (source.type === "custom") {
|
|
137
|
+
return {
|
|
138
|
+
...source,
|
|
139
|
+
type: "custom",
|
|
140
|
+
weight: source.weight ?? 1
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
...source,
|
|
145
|
+
type: "image",
|
|
146
|
+
weight: source.weight ?? 1
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function sourceSignature(sources) {
|
|
150
|
+
return sources.map((source, index) => {
|
|
151
|
+
if (source.type === "image") {
|
|
152
|
+
return `${index}:img:${source.src}:${source.weight}`;
|
|
153
|
+
}
|
|
154
|
+
if (source.type === "video") {
|
|
155
|
+
return `${index}:video:${source.src}:${source.weight}`;
|
|
156
|
+
}
|
|
157
|
+
return `${index}:custom:${source.id ?? "no-id"}:${source.weight}`;
|
|
158
|
+
}).join("|");
|
|
159
|
+
}
|
|
160
|
+
function renderSourceNode(source, loading, index, ariaLabel, sourceClassName, sourceStyle) {
|
|
161
|
+
const mergedClassName = joinClassNames(sourceClassName, source.className);
|
|
162
|
+
const mergedStyle = {
|
|
163
|
+
...sourceStyle,
|
|
164
|
+
...source.style
|
|
165
|
+
};
|
|
166
|
+
if (source.type === "image") {
|
|
167
|
+
return /* @__PURE__ */ jsx(
|
|
168
|
+
"img",
|
|
169
|
+
{
|
|
170
|
+
src: source.src,
|
|
171
|
+
alt: source.alt ?? ariaLabel,
|
|
172
|
+
loading: source.loading,
|
|
173
|
+
decoding: source.decoding,
|
|
174
|
+
referrerPolicy: source.referrerPolicy,
|
|
175
|
+
crossOrigin: source.crossOrigin,
|
|
176
|
+
width: source.width,
|
|
177
|
+
height: source.height,
|
|
178
|
+
className: mergedClassName,
|
|
179
|
+
style: {
|
|
180
|
+
...mergedStyle,
|
|
181
|
+
objectFit: source.objectFit ?? mergedStyle.objectFit
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (source.type === "video") {
|
|
187
|
+
return /* @__PURE__ */ jsx(
|
|
188
|
+
"video",
|
|
189
|
+
{
|
|
190
|
+
src: source.src,
|
|
191
|
+
poster: source.poster,
|
|
192
|
+
autoPlay: source.autoPlay ?? true,
|
|
193
|
+
loop: source.loop ?? true,
|
|
194
|
+
muted: source.muted ?? true,
|
|
195
|
+
playsInline: source.playsInline ?? true,
|
|
196
|
+
controls: source.controls ?? false,
|
|
197
|
+
width: source.width,
|
|
198
|
+
height: source.height,
|
|
199
|
+
className: mergedClassName,
|
|
200
|
+
style: {
|
|
201
|
+
...mergedStyle,
|
|
202
|
+
objectFit: source.objectFit ?? mergedStyle.objectFit
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return /* @__PURE__ */ jsx("span", { className: mergedClassName, style: mergedStyle, children: source.render({
|
|
208
|
+
isActive: true,
|
|
209
|
+
index,
|
|
210
|
+
loading
|
|
211
|
+
}) });
|
|
212
|
+
}
|
|
213
|
+
function RandomLoadingIndicator({
|
|
214
|
+
loading,
|
|
215
|
+
sources,
|
|
216
|
+
mode = "per-load",
|
|
217
|
+
intervalMs = 1200,
|
|
218
|
+
strategy = "uniform",
|
|
219
|
+
seed,
|
|
220
|
+
avoidImmediateRepeat = true,
|
|
221
|
+
preload = false,
|
|
222
|
+
pauseWhenDocumentHidden = true,
|
|
223
|
+
idleBehavior = "hidden",
|
|
224
|
+
fallback = null,
|
|
225
|
+
className,
|
|
226
|
+
style,
|
|
227
|
+
sourceClassName,
|
|
228
|
+
sourceStyle,
|
|
229
|
+
ariaLabel = "Loading indicator",
|
|
230
|
+
onIndicatorChange
|
|
231
|
+
}) {
|
|
232
|
+
const normalizedSources = useMemo(
|
|
233
|
+
() => sources.map((source) => normalizeSource(source)),
|
|
234
|
+
[sources]
|
|
235
|
+
);
|
|
236
|
+
const weights = useMemo(
|
|
237
|
+
() => normalizedSources.map((source) => Math.max(0, source.weight)),
|
|
238
|
+
[normalizedSources]
|
|
239
|
+
);
|
|
240
|
+
const sourceKey = useMemo(() => sourceSignature(normalizedSources), [normalizedSources]);
|
|
241
|
+
const selectionKey = `${sourceKey}:${strategy}:${seed === void 0 ? "native" : String(seed)}:${avoidImmediateRepeat}`;
|
|
242
|
+
const random = useMemo(
|
|
243
|
+
() => seed === void 0 ? Math.random : createSeededRandom(seed),
|
|
244
|
+
[seed]
|
|
245
|
+
);
|
|
246
|
+
const [activeIndex, setActiveIndex] = useState(null);
|
|
247
|
+
const activeIndexRef = useRef(null);
|
|
248
|
+
const previousRef = useRef({
|
|
249
|
+
loading: false,
|
|
250
|
+
selectionKey: ""
|
|
251
|
+
});
|
|
252
|
+
const shuffleStateRef = useRef({
|
|
253
|
+
queue: [],
|
|
254
|
+
signature: ""
|
|
255
|
+
});
|
|
256
|
+
const selectNext = useCallback(
|
|
257
|
+
(reason) => {
|
|
258
|
+
if (normalizedSources.length === 0) {
|
|
259
|
+
activeIndexRef.current = null;
|
|
260
|
+
setActiveIndex(null);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const nextIndex = pickNextIndex({
|
|
264
|
+
total: normalizedSources.length,
|
|
265
|
+
weights,
|
|
266
|
+
strategy,
|
|
267
|
+
previousIndex: activeIndexRef.current,
|
|
268
|
+
avoidImmediateRepeat,
|
|
269
|
+
random,
|
|
270
|
+
shuffleState: shuffleStateRef.current,
|
|
271
|
+
signature: selectionKey
|
|
272
|
+
});
|
|
273
|
+
if (nextIndex === null || nextIndex === activeIndexRef.current) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
activeIndexRef.current = nextIndex;
|
|
277
|
+
setActiveIndex(nextIndex);
|
|
278
|
+
onIndicatorChange?.({
|
|
279
|
+
index: nextIndex,
|
|
280
|
+
source: sources[nextIndex],
|
|
281
|
+
reason
|
|
282
|
+
});
|
|
283
|
+
},
|
|
284
|
+
[avoidImmediateRepeat, normalizedSources.length, onIndicatorChange, random, selectionKey, sources, strategy, weights]
|
|
285
|
+
);
|
|
286
|
+
useEffect(() => {
|
|
287
|
+
const previous = previousRef.current;
|
|
288
|
+
const selectionChanged = previous.selectionKey !== selectionKey;
|
|
289
|
+
if (loading) {
|
|
290
|
+
if (!previous.loading) {
|
|
291
|
+
selectNext("load-start");
|
|
292
|
+
} else if (selectionChanged || activeIndexRef.current === null || activeIndexRef.current >= normalizedSources.length) {
|
|
293
|
+
selectNext("source-update");
|
|
294
|
+
}
|
|
295
|
+
} else if (previous.loading && idleBehavior === "hidden") {
|
|
296
|
+
activeIndexRef.current = null;
|
|
297
|
+
setActiveIndex(null);
|
|
298
|
+
}
|
|
299
|
+
previousRef.current = {
|
|
300
|
+
loading,
|
|
301
|
+
selectionKey
|
|
302
|
+
};
|
|
303
|
+
}, [idleBehavior, loading, normalizedSources.length, selectNext, selectionKey]);
|
|
304
|
+
useEffect(() => {
|
|
305
|
+
if (normalizedSources.length > 0) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
activeIndexRef.current = null;
|
|
309
|
+
setActiveIndex(null);
|
|
310
|
+
}, [normalizedSources.length]);
|
|
311
|
+
useEffect(() => {
|
|
312
|
+
if (!loading || mode !== "continuous" || normalizedSources.length < 2) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const delay = Math.max(100, intervalMs);
|
|
316
|
+
const timerId = window.setInterval(() => {
|
|
317
|
+
if (pauseWhenDocumentHidden && typeof document !== "undefined" && document.visibilityState === "hidden") {
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
selectNext("interval");
|
|
321
|
+
}, delay);
|
|
322
|
+
return () => {
|
|
323
|
+
window.clearInterval(timerId);
|
|
324
|
+
};
|
|
325
|
+
}, [intervalMs, loading, mode, normalizedSources.length, pauseWhenDocumentHidden, selectNext]);
|
|
326
|
+
useEffect(() => {
|
|
327
|
+
if (!preload || typeof window === "undefined") {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
const preloaders = [];
|
|
331
|
+
normalizedSources.forEach((source) => {
|
|
332
|
+
if (source.type === "image") {
|
|
333
|
+
const image = new window.Image();
|
|
334
|
+
if (source.crossOrigin) {
|
|
335
|
+
image.crossOrigin = source.crossOrigin;
|
|
336
|
+
}
|
|
337
|
+
image.decoding = source.decoding ?? "async";
|
|
338
|
+
image.src = source.src;
|
|
339
|
+
preloaders.push(image);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
return () => {
|
|
343
|
+
preloaders.forEach((image) => {
|
|
344
|
+
image.src = "";
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
}, [normalizedSources, preload]);
|
|
348
|
+
if (!loading && idleBehavior === "hidden") {
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
if (activeIndex === null) {
|
|
352
|
+
return loading ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
|
|
353
|
+
}
|
|
354
|
+
const activeSource = normalizedSources[activeIndex];
|
|
355
|
+
if (!activeSource) {
|
|
356
|
+
return loading ? /* @__PURE__ */ jsx(Fragment, { children: fallback }) : null;
|
|
357
|
+
}
|
|
358
|
+
const currentIndex = activeIndex;
|
|
359
|
+
return /* @__PURE__ */ jsx(
|
|
360
|
+
"span",
|
|
361
|
+
{
|
|
362
|
+
role: "status",
|
|
363
|
+
"aria-live": "polite",
|
|
364
|
+
"aria-busy": loading,
|
|
365
|
+
"aria-label": ariaLabel,
|
|
366
|
+
className,
|
|
367
|
+
style,
|
|
368
|
+
"data-rli-index": currentIndex,
|
|
369
|
+
children: renderSourceNode(activeSource, loading, currentIndex, ariaLabel, sourceClassName, sourceStyle)
|
|
370
|
+
}
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
export {
|
|
374
|
+
RandomLoadingIndicator
|
|
375
|
+
};
|
|
376
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/RandomLoadingIndicator.tsx","../src/random.ts","../src/selection.ts"],"sourcesContent":["import { useCallback, useEffect, useMemo, useRef, useState, type CSSProperties, type ReactNode } from 'react';\nimport { createSeededRandom } from './random';\nimport { pickNextIndex, type ShuffleState } from './selection';\nimport type {\n CustomIndicatorSource,\n ImageIndicatorSource,\n IndicatorChangeReason,\n IndicatorSource,\n RandomLoadingIndicatorProps,\n VideoIndicatorSource\n} from './types';\n\ntype NormalizedImageSource = Omit<ImageIndicatorSource, 'type' | 'weight'> & {\n type: 'image';\n weight: number;\n};\n\ntype NormalizedVideoSource = Omit<VideoIndicatorSource, 'weight'> & {\n weight: number;\n};\n\ntype NormalizedCustomSource = Omit<CustomIndicatorSource, 'weight'> & {\n weight: number;\n};\n\ntype NormalizedSource = NormalizedImageSource | NormalizedVideoSource | NormalizedCustomSource;\n\nfunction joinClassNames(...values: Array<string | undefined>): string | undefined {\n const merged = values.filter(Boolean).join(' ').trim();\n return merged.length > 0 ? merged : undefined;\n}\n\nfunction normalizeSource(source: IndicatorSource): NormalizedSource {\n if (typeof source === 'string') {\n return {\n type: 'image',\n src: source,\n weight: 1\n };\n }\n\n if (source.type === 'video') {\n return {\n ...source,\n type: 'video',\n weight: source.weight ?? 1\n };\n }\n\n if (source.type === 'custom') {\n return {\n ...source,\n type: 'custom',\n weight: source.weight ?? 1\n };\n }\n\n return {\n ...source,\n type: 'image',\n weight: source.weight ?? 1\n };\n}\n\nfunction sourceSignature(sources: NormalizedSource[]): string {\n return sources\n .map((source, index) => {\n if (source.type === 'image') {\n return `${index}:img:${source.src}:${source.weight}`;\n }\n if (source.type === 'video') {\n return `${index}:video:${source.src}:${source.weight}`;\n }\n return `${index}:custom:${source.id ?? 'no-id'}:${source.weight}`;\n })\n .join('|');\n}\n\nfunction renderSourceNode(\n source: NormalizedSource,\n loading: boolean,\n index: number,\n ariaLabel: string,\n sourceClassName?: string,\n sourceStyle?: CSSProperties\n): ReactNode {\n const mergedClassName = joinClassNames(sourceClassName, source.className);\n const mergedStyle = {\n ...sourceStyle,\n ...source.style\n };\n\n if (source.type === 'image') {\n return (\n <img\n src={source.src}\n alt={source.alt ?? ariaLabel}\n loading={source.loading}\n decoding={source.decoding}\n referrerPolicy={source.referrerPolicy}\n crossOrigin={source.crossOrigin}\n width={source.width}\n height={source.height}\n className={mergedClassName}\n style={{\n ...mergedStyle,\n objectFit: source.objectFit ?? mergedStyle.objectFit\n }}\n />\n );\n }\n\n if (source.type === 'video') {\n return (\n <video\n src={source.src}\n poster={source.poster}\n autoPlay={source.autoPlay ?? true}\n loop={source.loop ?? true}\n muted={source.muted ?? true}\n playsInline={source.playsInline ?? true}\n controls={source.controls ?? false}\n width={source.width}\n height={source.height}\n className={mergedClassName}\n style={{\n ...mergedStyle,\n objectFit: source.objectFit ?? mergedStyle.objectFit\n }}\n />\n );\n }\n\n return (\n <span className={mergedClassName} style={mergedStyle}>\n {source.render({\n isActive: true,\n index,\n loading\n })}\n </span>\n );\n}\n\nexport function RandomLoadingIndicator({\n loading,\n sources,\n mode = 'per-load',\n intervalMs = 1200,\n strategy = 'uniform',\n seed,\n avoidImmediateRepeat = true,\n preload = false,\n pauseWhenDocumentHidden = true,\n idleBehavior = 'hidden',\n fallback = null,\n className,\n style,\n sourceClassName,\n sourceStyle,\n ariaLabel = 'Loading indicator',\n onIndicatorChange\n}: RandomLoadingIndicatorProps) {\n const normalizedSources = useMemo(\n () => sources.map((source) => normalizeSource(source)),\n [sources]\n );\n const weights = useMemo(\n () => normalizedSources.map((source) => Math.max(0, source.weight)),\n [normalizedSources]\n );\n const sourceKey = useMemo(() => sourceSignature(normalizedSources), [normalizedSources]);\n const selectionKey = `${sourceKey}:${strategy}:${seed === undefined ? 'native' : String(seed)}:${avoidImmediateRepeat}`;\n\n const random = useMemo(\n () => (seed === undefined ? Math.random : createSeededRandom(seed)),\n [seed]\n );\n\n const [activeIndex, setActiveIndex] = useState<number | null>(null);\n const activeIndexRef = useRef<number | null>(null);\n const previousRef = useRef({\n loading: false,\n selectionKey: ''\n });\n const shuffleStateRef = useRef<ShuffleState>({\n queue: [],\n signature: ''\n });\n\n const selectNext = useCallback(\n (reason: IndicatorChangeReason) => {\n if (normalizedSources.length === 0) {\n activeIndexRef.current = null;\n setActiveIndex(null);\n return;\n }\n\n const nextIndex = pickNextIndex({\n total: normalizedSources.length,\n weights,\n strategy,\n previousIndex: activeIndexRef.current,\n avoidImmediateRepeat,\n random,\n shuffleState: shuffleStateRef.current,\n signature: selectionKey\n });\n\n if (nextIndex === null || nextIndex === activeIndexRef.current) {\n return;\n }\n\n activeIndexRef.current = nextIndex;\n setActiveIndex(nextIndex);\n onIndicatorChange?.({\n index: nextIndex,\n source: sources[nextIndex],\n reason\n });\n },\n [avoidImmediateRepeat, normalizedSources.length, onIndicatorChange, random, selectionKey, sources, strategy, weights]\n );\n\n useEffect(() => {\n const previous = previousRef.current;\n const selectionChanged = previous.selectionKey !== selectionKey;\n\n if (loading) {\n if (!previous.loading) {\n selectNext('load-start');\n } else if (selectionChanged || activeIndexRef.current === null || activeIndexRef.current >= normalizedSources.length) {\n selectNext('source-update');\n }\n } else if (previous.loading && idleBehavior === 'hidden') {\n activeIndexRef.current = null;\n setActiveIndex(null);\n }\n\n previousRef.current = {\n loading,\n selectionKey\n };\n }, [idleBehavior, loading, normalizedSources.length, selectNext, selectionKey]);\n\n useEffect(() => {\n if (normalizedSources.length > 0) {\n return;\n }\n activeIndexRef.current = null;\n setActiveIndex(null);\n }, [normalizedSources.length]);\n\n useEffect(() => {\n if (!loading || mode !== 'continuous' || normalizedSources.length < 2) {\n return;\n }\n\n const delay = Math.max(100, intervalMs);\n const timerId = window.setInterval(() => {\n if (pauseWhenDocumentHidden && typeof document !== 'undefined' && document.visibilityState === 'hidden') {\n return;\n }\n selectNext('interval');\n }, delay);\n\n return () => {\n window.clearInterval(timerId);\n };\n }, [intervalMs, loading, mode, normalizedSources.length, pauseWhenDocumentHidden, selectNext]);\n\n useEffect(() => {\n if (!preload || typeof window === 'undefined') {\n return;\n }\n\n const preloaders: HTMLImageElement[] = [];\n normalizedSources.forEach((source) => {\n if (source.type === 'image') {\n const image = new window.Image();\n if (source.crossOrigin) {\n image.crossOrigin = source.crossOrigin;\n }\n image.decoding = source.decoding ?? 'async';\n image.src = source.src;\n preloaders.push(image);\n }\n });\n\n return () => {\n preloaders.forEach((image) => {\n image.src = '';\n });\n };\n }, [normalizedSources, preload]);\n\n if (!loading && idleBehavior === 'hidden') {\n return null;\n }\n\n if (activeIndex === null) {\n return loading ? <>{fallback}</> : null;\n }\n\n const activeSource = normalizedSources[activeIndex];\n if (!activeSource) {\n return loading ? <>{fallback}</> : null;\n }\n\n const currentIndex = activeIndex;\n\n return (\n <span\n role=\"status\"\n aria-live=\"polite\"\n aria-busy={loading}\n aria-label={ariaLabel}\n className={className}\n style={style}\n data-rli-index={currentIndex}\n >\n {renderSourceNode(activeSource, loading, currentIndex, ariaLabel, sourceClassName, sourceStyle)}\n </span>\n );\n}\n","function hashSeed(seed: string | number): number {\n if (typeof seed === 'number' && Number.isFinite(seed)) {\n return seed >>> 0;\n }\n\n const text = String(seed);\n let hash = 2166136261;\n for (let i = 0; i < text.length; i += 1) {\n hash ^= text.charCodeAt(i);\n hash = Math.imul(hash, 16777619);\n }\n return hash >>> 0;\n}\n\nexport function createSeededRandom(seed: string | number): () => number {\n let state = hashSeed(seed);\n if (state === 0) {\n state = 0x6d2b79f5;\n }\n\n return () => {\n state += 0x6d2b79f5;\n let t = state;\n t = Math.imul(t ^ (t >>> 15), t | 1);\n t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n return ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n };\n}\n","import type { RandomStrategy } from './types';\n\nexport interface ShuffleState {\n queue: number[];\n signature: string;\n}\n\nexport interface PickNextIndexParams {\n total: number;\n weights: number[];\n strategy: RandomStrategy;\n previousIndex: number | null;\n avoidImmediateRepeat: boolean;\n random: () => number;\n shuffleState: ShuffleState;\n signature: string;\n}\n\nfunction shuffledIndices(total: number, random: () => number): number[] {\n const result = Array.from({ length: total }, (_, index) => index);\n for (let i = result.length - 1; i > 0; i -= 1) {\n const j = Math.floor(random() * (i + 1));\n [result[i], result[j]] = [result[j], result[i]];\n }\n return result;\n}\n\nfunction buildCandidates(\n total: number,\n previousIndex: number | null,\n avoidImmediateRepeat: boolean\n): number[] {\n const all = Array.from({ length: total }, (_, index) => index);\n if (!avoidImmediateRepeat || previousIndex === null || total <= 1) {\n return all;\n }\n return all.filter((index) => index !== previousIndex);\n}\n\nfunction pickByWeight(\n candidates: number[],\n weights: number[],\n random: () => number\n): number {\n const safeWeights = candidates.map((candidate) => Math.max(0, weights[candidate] ?? 1));\n const totalWeight = safeWeights.reduce((sum, weight) => sum + weight, 0);\n\n if (totalWeight <= 0) {\n return candidates[Math.floor(random() * candidates.length)];\n }\n\n let cursor = random() * totalWeight;\n for (let i = 0; i < candidates.length; i += 1) {\n cursor -= safeWeights[i];\n if (cursor <= 0) {\n return candidates[i];\n }\n }\n\n return candidates[candidates.length - 1];\n}\n\nexport function pickNextIndex({\n total,\n weights,\n strategy,\n previousIndex,\n avoidImmediateRepeat,\n random,\n shuffleState,\n signature\n}: PickNextIndexParams): number | null {\n if (total <= 0) {\n return null;\n }\n\n if (total === 1) {\n return 0;\n }\n\n if (strategy === 'shuffle') {\n if (shuffleState.signature !== signature) {\n shuffleState.signature = signature;\n shuffleState.queue = [];\n }\n\n if (shuffleState.queue.length === 0) {\n shuffleState.queue = shuffledIndices(total, random);\n }\n\n if (\n avoidImmediateRepeat &&\n previousIndex !== null &&\n shuffleState.queue.length > 1 &&\n shuffleState.queue[0] === previousIndex\n ) {\n const swapIndex = 1 + Math.floor(random() * (shuffleState.queue.length - 1));\n [shuffleState.queue[0], shuffleState.queue[swapIndex]] = [\n shuffleState.queue[swapIndex],\n shuffleState.queue[0]\n ];\n }\n\n if (\n avoidImmediateRepeat &&\n previousIndex !== null &&\n shuffleState.queue.length === 1 &&\n shuffleState.queue[0] === previousIndex\n ) {\n shuffleState.queue = shuffledIndices(total, random);\n if (shuffleState.queue[0] === previousIndex && shuffleState.queue.length > 1) {\n [shuffleState.queue[0], shuffleState.queue[1]] = [\n shuffleState.queue[1],\n shuffleState.queue[0]\n ];\n }\n }\n\n const next = shuffleState.queue.shift();\n return typeof next === 'number' ? next : null;\n }\n\n const candidates = buildCandidates(total, previousIndex, avoidImmediateRepeat);\n if (candidates.length === 0) {\n return previousIndex;\n }\n\n if (strategy === 'weighted') {\n return pickByWeight(candidates, weights, random);\n }\n\n return candidates[Math.floor(random() * candidates.length)];\n}\n"],"mappings":";AAAA,SAAS,aAAa,WAAW,SAAS,QAAQ,gBAAoD;;;ACAtG,SAAS,SAAS,MAA+B;AAC/C,MAAI,OAAO,SAAS,YAAY,OAAO,SAAS,IAAI,GAAG;AACrD,WAAO,SAAS;AAAA,EAClB;AAEA,QAAM,OAAO,OAAO,IAAI;AACxB,MAAI,OAAO;AACX,WAAS,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK,GAAG;AACvC,YAAQ,KAAK,WAAW,CAAC;AACzB,WAAO,KAAK,KAAK,MAAM,QAAQ;AAAA,EACjC;AACA,SAAO,SAAS;AAClB;AAEO,SAAS,mBAAmB,MAAqC;AACtE,MAAI,QAAQ,SAAS,IAAI;AACzB,MAAI,UAAU,GAAG;AACf,YAAQ;AAAA,EACV;AAEA,SAAO,MAAM;AACX,aAAS;AACT,QAAI,IAAI;AACR,QAAI,KAAK,KAAK,IAAK,MAAM,IAAK,IAAI,CAAC;AACnC,SAAK,IAAI,KAAK,KAAK,IAAK,MAAM,GAAI,IAAI,EAAE;AACxC,aAAS,IAAK,MAAM,QAAS,KAAK;AAAA,EACpC;AACF;;;ACTA,SAAS,gBAAgB,OAAe,QAAgC;AACtE,QAAM,SAAS,MAAM,KAAK,EAAE,QAAQ,MAAM,GAAG,CAAC,GAAG,UAAU,KAAK;AAChE,WAAS,IAAI,OAAO,SAAS,GAAG,IAAI,GAAG,KAAK,GAAG;AAC7C,UAAM,IAAI,KAAK,MAAM,OAAO,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/B,YAAI,OAAO,aAAa;AACtB,gBAAM,cAAc,OAAO;AAAA,QAC7B;AACA,cAAM,WAAW,OAAO,YAAY;AACpC,cAAM,MAAM,OAAO;AACnB,mBAAW,KAAK,KAAK;AAAA,MACvB;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,iBAAW,QAAQ,CAAC,UAAU;AAC5B,cAAM,MAAM;AAAA,MACd,CAAC;AAAA,IACH;AAAA,EACF,GAAG,CAAC,mBAAmB,OAAO,CAAC;AAE/B,MAAI,CAAC,WAAW,iBAAiB,UAAU;AACzC,WAAO;AAAA,EACT;AAEA,MAAI,gBAAgB,MAAM;AACxB,WAAO,UAAU,gCAAG,oBAAS,IAAM;AAAA,EACrC;AAEA,QAAM,eAAe,kBAAkB,WAAW;AAClD,MAAI,CAAC,cAAc;AACjB,WAAO,UAAU,gCAAG,oBAAS,IAAM;AAAA,EACrC;AAEA,QAAM,eAAe;AAErB,SACE;AAAA,IAAC;AAAA;AAAA,MACC,MAAK;AAAA,MACL,aAAU;AAAA,MACV,aAAW;AAAA,MACX,cAAY;AAAA,MACZ;AAAA,MACA;AAAA,MACA,kBAAgB;AAAA,MAEf,2BAAiB,cAAc,SAAS,cAAc,WAAW,iBAAiB,WAAW;AAAA;AAAA,EAChG;AAEJ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-random-loading-indicator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A React component for random loading indicators.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"loading",
|
|
8
|
+
"indicator",
|
|
9
|
+
"spinner",
|
|
10
|
+
"random",
|
|
11
|
+
"component"
|
|
12
|
+
],
|
|
13
|
+
"author": "Qi Xi <me@imxiqi.com> (https://imxiqi.com)",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/xiqi/react-random-loading-indicator.git"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/xiqi/react-random-loading-indicator",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/xiqi/react-random-loading-indicator/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "dist/index.cjs",
|
|
25
|
+
"module": "dist/index.js",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"require": "./dist/index.cjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"README.md",
|
|
37
|
+
"LICENSE"
|
|
38
|
+
],
|
|
39
|
+
"sideEffects": false,
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"dev": "tsup --watch",
|
|
43
|
+
"previewer": "vite previewer --config previewer/vite.config.ts",
|
|
44
|
+
"previewer:build": "vite build previewer --config previewer/vite.config.ts",
|
|
45
|
+
"test": "vitest run",
|
|
46
|
+
"test:watch": "vitest",
|
|
47
|
+
"typecheck": "tsc --noEmit",
|
|
48
|
+
"prepublishOnly": "npm run build"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"react": ">=18",
|
|
52
|
+
"react-dom": ">=18"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@testing-library/react": "^16.3.2",
|
|
56
|
+
"@types/react": "^19.2.14",
|
|
57
|
+
"@types/react-dom": "^19.2.3",
|
|
58
|
+
"@vitejs/plugin-react": "^5.1.4",
|
|
59
|
+
"jsdom": "^28.1.0",
|
|
60
|
+
"react": "^19.2.4",
|
|
61
|
+
"react-dom": "^19.2.4",
|
|
62
|
+
"tsup": "^8.5.1",
|
|
63
|
+
"typescript": "^5.9.3",
|
|
64
|
+
"vite": "^7.3.1",
|
|
65
|
+
"vitest": "^4.0.18"
|
|
66
|
+
}
|
|
67
|
+
}
|