@andamento/react 0.0.0-stage → 0.2.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/index.js ADDED
@@ -0,0 +1,572 @@
1
+ // src/TileLayout.tsx
2
+ import {
3
+ Children,
4
+ cloneElement,
5
+ isValidElement,
6
+ useEffect,
7
+ useId,
8
+ useLayoutEffect,
9
+ useRef as useRef2,
10
+ useState
11
+ } from "react";
12
+ import { cellsInRect } from "@andamento/core";
13
+ import {
14
+ coverStyle as sharedCoverStyle
15
+ } from "@andamento/dom";
16
+ import {
17
+ backdropStyle,
18
+ backgroundStyle,
19
+ containerStyle,
20
+ frameBuilder,
21
+ keyAction,
22
+ overlayStyle,
23
+ resolvePerCell as resolve,
24
+ ringStrokes,
25
+ safeBoxStyle,
26
+ sameRect,
27
+ shadowShape,
28
+ tileSheet,
29
+ tileStyle,
30
+ tileViewBox,
31
+ viewRect,
32
+ watchView
33
+ } from "@andamento/dom/internal";
34
+
35
+ // src/useTileLayout.ts
36
+ import { useMemo, useRef } from "react";
37
+ import { createLayout, fitCount } from "@andamento/core";
38
+ import { layoutResolver, optionsKey } from "@andamento/dom/internal";
39
+ function useTileLayout(options) {
40
+ const key = optionsKey(options);
41
+ return useMemo(() => createLayout(options), [key]);
42
+ }
43
+ function useFitCount(options, count, fit = {}) {
44
+ const key = optionsKey(options);
45
+ const { cells, mode } = fit;
46
+ return useMemo(() => fitCount(options, count, { cells, mode }).layout, [key, count, cells, mode]);
47
+ }
48
+ function useResolvedLayout(component, options, layout, fit) {
49
+ const resolver = useRef();
50
+ resolver.current ?? (resolver.current = layoutResolver(`<${component}>`));
51
+ return resolver.current(options, layout, fit);
52
+ }
53
+
54
+ // src/TileLayout.tsx
55
+ import { jsx, jsxs } from "react/jsx-runtime";
56
+ var isEmpty = (n) => n == null || n === false || n === true || n === "";
57
+ var css = (s) => s;
58
+ function TileLayout(props) {
59
+ const {
60
+ options,
61
+ layout: given,
62
+ children,
63
+ renderCell,
64
+ contentCells,
65
+ placement,
66
+ aspects,
67
+ choose,
68
+ focalPoints,
69
+ weights,
70
+ safeArea: safeArea2,
71
+ fill,
72
+ fit,
73
+ backdrop,
74
+ relief,
75
+ navigable,
76
+ onCellActivate,
77
+ cellLabel,
78
+ focusRing = "#1a56db",
79
+ animate,
80
+ virtualize,
81
+ fallback,
82
+ cellClassName,
83
+ cellStyle,
84
+ background,
85
+ style,
86
+ role,
87
+ onKeyDown,
88
+ ...rest
89
+ } = props;
90
+ const uid = "andamento" + useId().replace(/:/g, "");
91
+ const items = renderCell ? [] : Children.toArray(children);
92
+ const layout = useResolvedLayout("TileLayout", options, given, {
93
+ fit: !renderCell && fit,
94
+ count: items.length,
95
+ cells: contentCells,
96
+ weights: renderCell ? void 0 : weights,
97
+ placement
98
+ });
99
+ const { bounds } = layout;
100
+ const buildFrame = useRef2();
101
+ buildFrame.current ?? (buildFrame.current = frameBuilder());
102
+ const frame = buildFrame.current(layout, {
103
+ count: renderCell ? Infinity : items.length,
104
+ aspects: renderCell ? void 0 : aspects,
105
+ contentCells,
106
+ placement,
107
+ choose,
108
+ fill,
109
+ relief,
110
+ animate
111
+ });
112
+ const { slots, anim } = frame;
113
+ const keyOf = (i) => {
114
+ const slot = slots[i];
115
+ if (!renderCell && slot >= 0) {
116
+ const child = items[slot];
117
+ return isValidElement(child) && child.key != null ? `child:${child.key}` : `child:${slot}`;
118
+ }
119
+ return `cell:${layout.cells[i].id}`;
120
+ };
121
+ const makeEntry = (i) => {
122
+ const cell = layout.cells[i];
123
+ const slot = slots[i];
124
+ let content = null;
125
+ if (renderCell) content = renderCell(cell, { slot });
126
+ else if (slot >= 0) content = items[slot];
127
+ const empty = isEmpty(content);
128
+ const state = { empty, slot: empty && !renderCell ? -1 : slot };
129
+ if (empty) content = resolve(fallback, cell, state);
130
+ return { i, cell, content, key: keyOf(i), empty, state };
131
+ };
132
+ const virtual = !!virtualize;
133
+ const overscan = Math.max(0, (typeof virtualize === "object" ? virtualize.overscan : void 0) ?? 300);
134
+ const container = useRef2(null);
135
+ const [view, setView] = useState(null);
136
+ const allEntries = virtual ? void 0 : layout.cells.map((_, i) => makeEntry(i));
137
+ const hasContent = (i) => allEntries ? !allEntries[i].empty : renderCell ? slots[i] >= 0 : slots[i] >= 0 && !isEmpty(items[slots[i]]);
138
+ const mounted = useRef2(false);
139
+ const lastKeys = useRef2(/* @__PURE__ */ new Set());
140
+ const entering = useRef2(/* @__PURE__ */ new Set());
141
+ const lastTiles = useRef2(/* @__PURE__ */ new Map());
142
+ const [exiting, setExiting] = useState(() => /* @__PURE__ */ new Map());
143
+ const timers = useRef2(/* @__PURE__ */ new Set());
144
+ const focusable = new Set(navigable ? layout.cells.filter((c) => hasContent(c.index)).map((c) => c.id) : []);
145
+ const [activeId, setActiveId] = useState();
146
+ const firstFocusable = layout.cells.find((c) => focusable.has(c.id))?.id;
147
+ const active = activeId !== void 0 && focusable.has(activeId) ? activeId : firstFocusable;
148
+ const tiles = useRef2(/* @__PURE__ */ new Map());
149
+ const pendingFocus = useRef2();
150
+ const list = (role ?? "list") === "list";
151
+ let entries;
152
+ if (allEntries) entries = allEntries;
153
+ else {
154
+ const shown = new Set(view && view.width >= 0 ? cellsInRect(layout, view).map((c) => c.index) : []);
155
+ const activeCell = active !== void 0 ? layout.cells.find((c) => c.id === active) : void 0;
156
+ if (activeCell) shown.add(activeCell.index);
157
+ entries = [...shown].sort((a, b) => a - b).map(makeEntry);
158
+ }
159
+ let positions;
160
+ if (virtual) {
161
+ positions = /* @__PURE__ */ new Map();
162
+ for (const c of layout.cells) if (hasContent(c.index)) positions.set(c.index, positions.size + 1);
163
+ }
164
+ const allKeys = anim ? new Set(layout.cells.map((_, i) => keyOf(i))) : void 0;
165
+ for (const e of entries) if (anim && mounted.current && !lastKeys.current.has(e.key)) entering.current.add(e.key);
166
+ const handleKeyDown = (e) => {
167
+ onKeyDown?.(e);
168
+ if (e.defaultPrevented || !navigable || e.altKey || e.ctrlKey || e.metaKey) return;
169
+ const entry = entries.find((x) => tiles.current.get(x.cell.id) === e.target);
170
+ if (!entry || !focusable.has(entry.cell.id)) return;
171
+ const action = keyAction(layout, entry.cell, e.key, (c) => focusable.has(c.id), !!onCellActivate);
172
+ if (!action) return;
173
+ e.preventDefault();
174
+ if ("activate" in action) onCellActivate(entry.cell, entry.state);
175
+ else if ("move" in action) {
176
+ setActiveId(action.move.id);
177
+ const el = tiles.current.get(action.move.id);
178
+ if (el) el.focus();
179
+ else pendingFocus.current = action.move.id;
180
+ }
181
+ };
182
+ const rendered = entries.map(({ i, cell, content, key, empty, state }) => {
183
+ const slot = slots[i];
184
+ const bevel = frame.bevel(i);
185
+ const tile = tileStyle(frame, cell, {
186
+ empty,
187
+ safeArea: safeArea2,
188
+ backdrop: backdrop != null,
189
+ clickable: !!onCellActivate && !empty,
190
+ focus: !renderCell && slot >= 0 ? focalPoints?.[slot] : void 0,
191
+ aspect: !renderCell && slot >= 0 ? aspects?.[slot] : void 0
192
+ });
193
+ const silent = empty && isEmpty(content);
194
+ const canFocus = focusable.has(cell.id);
195
+ const viewBox = tileViewBox(cell);
196
+ return /* @__PURE__ */ jsxs(
197
+ "div",
198
+ {
199
+ ref: (el) => {
200
+ if (el) tiles.current.set(cell.id, el);
201
+ else tiles.current.delete(cell.id);
202
+ },
203
+ role: silent ? void 0 : list ? "listitem" : void 0,
204
+ "aria-hidden": silent ? true : void 0,
205
+ "aria-label": silent ? void 0 : resolve(cellLabel, cell, state),
206
+ "aria-setsize": positions && list && !silent ? positions.size : void 0,
207
+ "aria-posinset": positions && list && !silent ? positions.get(i) : void 0,
208
+ tabIndex: canFocus ? cell.id === active ? 0 : -1 : void 0,
209
+ onFocus: canFocus ? (e) => e.target === e.currentTarget && setActiveId(cell.id) : void 0,
210
+ onClick: onCellActivate && !empty ? () => onCellActivate(cell, state) : void 0,
211
+ className: resolve(cellClassName, cell, state),
212
+ "data-cell-id": cell.id,
213
+ "data-index": cell.index,
214
+ "data-empty": empty ? "" : void 0,
215
+ "data-boundary": cell.boundary ? "" : void 0,
216
+ "data-entering": entering.current.has(key) ? "" : void 0,
217
+ style: { ...css(tile), ...resolve(cellStyle, cell, state) },
218
+ children: [
219
+ backdrop != null && // z-index -1 puts it behind the content but inside the tile: the tile's
220
+ // clip-path makes it a stacking context.
221
+ /* @__PURE__ */ jsx("div", { "aria-hidden": true, style: css(backdropStyle(frame, cell)), children: backdrop }),
222
+ safeArea2 && content != null && !isEmpty(content) ? /* @__PURE__ */ jsx("div", { "data-safe-area": "", style: css(safeBoxStyle(frame, cell, !renderCell && !empty && aspects ? aspects[slot] ?? 1 : void 0, safeArea2)), children: content }) : content,
223
+ bevel && bevel.length > 0 && // Over the content; the tile's box is its bounding box, so map that to the viewBox.
224
+ // When animating, the bevel is for the new shape, so it fades in as the tile arrives.
225
+ /* @__PURE__ */ jsx(
226
+ "svg",
227
+ {
228
+ "aria-hidden": true,
229
+ "data-bevel": "",
230
+ "data-late": anim ? "" : void 0,
231
+ viewBox,
232
+ preserveAspectRatio: "none",
233
+ style: css(overlayStyle),
234
+ children: bevel.map((p, k) => /* @__PURE__ */ jsx("path", { d: p.d, fill: p.fill, fillOpacity: p.opacity }, k))
235
+ },
236
+ anim ? String(tile.clipPath) : void 0
237
+ ),
238
+ canFocus && focusRing !== false && // The tile's outline, stroked across its edge; the clip keeps the inner half.
239
+ /* @__PURE__ */ jsx("svg", { "aria-hidden": true, "data-focus-ring": "", viewBox, preserveAspectRatio: "none", style: css(overlayStyle), children: ringStrokes(frame, cell, focusRing).map((p, k) => /* @__PURE__ */ jsx("path", { d: p.d, fill: "none", stroke: p.stroke, strokeWidth: p.width, vectorEffect: "non-scaling-stroke" }, k)) })
240
+ ]
241
+ },
242
+ key
243
+ );
244
+ });
245
+ useIsomorphicLayoutEffect(() => {
246
+ const current = new Map(rendered.map((el) => [el.key, el]));
247
+ const present = allKeys ?? current;
248
+ if (anim && mounted.current) {
249
+ const gone = [...lastTiles.current].filter(([k]) => !present.has(k));
250
+ if (gone.length) {
251
+ setExiting((prev) => new Map([...prev, ...gone]));
252
+ const timer = setTimeout(() => {
253
+ timers.current.delete(timer);
254
+ setExiting((prev) => {
255
+ const next = new Map(prev);
256
+ for (const [k, el] of gone) if (next.get(k) === el) next.delete(k);
257
+ return next;
258
+ });
259
+ }, anim.duration);
260
+ timers.current.add(timer);
261
+ }
262
+ }
263
+ for (const k of entering.current) if (!present.has(k)) entering.current.delete(k);
264
+ lastKeys.current = new Set(present.keys());
265
+ lastTiles.current = current;
266
+ mounted.current = true;
267
+ if (pendingFocus.current) {
268
+ const el = tiles.current.get(pendingFocus.current);
269
+ if (el) {
270
+ pendingFocus.current = void 0;
271
+ el.focus();
272
+ }
273
+ }
274
+ });
275
+ useIsomorphicLayoutEffect(() => {
276
+ if (!virtual) return;
277
+ const el = container.current;
278
+ if (!el) return;
279
+ return watchView(el, () => {
280
+ const next = viewRect(el, layout, overscan);
281
+ setView((v) => sameRect(v ?? void 0, next) ? v : next);
282
+ });
283
+ }, [virtual, layout, overscan]);
284
+ useEffect(() => {
285
+ const set = timers.current;
286
+ return () => set.forEach(clearTimeout);
287
+ }, []);
288
+ const leaving = anim ? [...exiting].filter(([k]) => !lastKeys.current.has(k) && !rendered.some((el) => el.key === k)) : [];
289
+ const sheet = tileSheet({ focusRing: !!navigable && focusRing !== false, anim });
290
+ const shadow = shadowShape(frame, entries.map((e) => e.cell));
291
+ return /* @__PURE__ */ jsxs(
292
+ "div",
293
+ {
294
+ "data-andamento": "",
295
+ ref: container,
296
+ role: role ?? "list",
297
+ ...rest,
298
+ onKeyDown: handleKeyDown,
299
+ style: { ...css(containerStyle(layout)), ...style },
300
+ children: [
301
+ sheet && /* @__PURE__ */ jsx("style", { children: sheet }),
302
+ background && /* @__PURE__ */ jsx("div", { "aria-hidden": true, style: css(backgroundStyle(frame, background)) }),
303
+ shadow && /* @__PURE__ */ jsxs("svg", { "aria-hidden": true, "data-andamento-shadow": "", viewBox: shadow.viewBox, preserveAspectRatio: "none", style: css(shadow.style), children: [
304
+ /* @__PURE__ */ jsx("filter", { id: `${uid}-shadow`, x: "-10%", y: "-10%", width: "120%", height: "120%", children: /* @__PURE__ */ jsx("feGaussianBlur", { stdDeviation: shadow.blur }) }),
305
+ /* @__PURE__ */ jsx("path", { d: shadow.d, fillRule: "evenodd", transform: shadow.transform, fill: "#000", fillOpacity: shadow.opacity, filter: `url(#${uid}-shadow)` })
306
+ ] }),
307
+ rendered,
308
+ leaving.map(
309
+ ([k, el]) => (
310
+ // Fades out through the [data-exiting] rule in the style sheet.
311
+ cloneElement(el, {
312
+ key: k,
313
+ ref: null,
314
+ role: void 0,
315
+ tabIndex: void 0,
316
+ onClick: void 0,
317
+ onFocus: void 0,
318
+ "aria-hidden": true,
319
+ "data-entering": void 0,
320
+ "data-exiting": ""
321
+ })
322
+ )
323
+ )
324
+ ]
325
+ }
326
+ );
327
+ }
328
+ var useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
329
+ var coverStyle = css(sharedCoverStyle);
330
+
331
+ // src/SvgTileLayout.tsx
332
+ import { useId as useId2, useMemo as useMemo2 } from "react";
333
+ import { cellShape, coverRect, safeArea } from "@andamento/core";
334
+ import { cellPath, defaultBevel, regionPath, viewPath, reliefPaths, shadowOffset } from "@andamento/svg";
335
+ import { cellA11y, resolveCellStyles } from "@andamento/svg/internal";
336
+ import { Fragment, jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
337
+ function SvgTileLayout(props) {
338
+ const {
339
+ options,
340
+ layout: given,
341
+ images,
342
+ repeatImages,
343
+ contentCells,
344
+ placement,
345
+ imageAspects,
346
+ choose,
347
+ imageFit,
348
+ containWithin,
349
+ imageFocus,
350
+ imageWeights,
351
+ fit,
352
+ fill,
353
+ stroke,
354
+ strokeWidth = 1,
355
+ background,
356
+ backdrop,
357
+ relief: reliefProp,
358
+ renderCell,
359
+ title,
360
+ imageAlts,
361
+ ...svgProps
362
+ } = props;
363
+ const fitTo = Array.isArray(images) && !repeatImages ? images.length : 0;
364
+ const layout = useResolvedLayout("SvgTileLayout", options, given, {
365
+ fit,
366
+ count: fitTo,
367
+ cells: contentCells,
368
+ weights: Array.isArray(images) ? imageWeights : void 0,
369
+ placement
370
+ });
371
+ const prefix = "andamento" + useId2().replace(/:/g, "");
372
+ const styles = useMemo2(
373
+ () => resolveCellStyles(layout, { images, repeatImages, fill, contentCells, placement, imageAspects, choose }),
374
+ [layout, images, repeatImages, fill, contentCells, placement, imageAspects, choose]
375
+ );
376
+ const { x, y, width, height } = layout.bounds;
377
+ const relief = reliefProp === true ? {} : reliefProp || void 0;
378
+ const reliefKey = relief ? JSON.stringify(relief) : "";
379
+ const bevels = useMemo2(() => {
380
+ if (!relief) return void 0;
381
+ const bevel = relief.bevel ?? defaultBevel(layout);
382
+ return bevel > 0 ? layout.cells.map((c) => reliefPaths(layout, c, relief, bevel)) : void 0;
383
+ }, [layout, reliefKey]);
384
+ const cast = relief && shadowOffset(relief);
385
+ return /* @__PURE__ */ jsxs2("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: `${x} ${y} ${width} ${height}`, role: imageAlts?.length ? "group" : title ? "img" : void 0, ...svgProps, children: [
386
+ title && /* @__PURE__ */ jsx2("title", { children: title }),
387
+ background && /* @__PURE__ */ jsx2("path", { className: "andamento-region", d: regionPath(layout), fill: background, fillRule: "evenodd" }),
388
+ layout.view && /* @__PURE__ */ jsx2("clipPath", { id: `${prefix}-view`, children: /* @__PURE__ */ jsx2("path", { d: viewPath(layout), clipRule: "evenodd" }) }),
389
+ /* @__PURE__ */ jsxs2("g", { className: layout.view ? "andamento-view" : void 0, clipPath: layout.view ? `url(#${prefix}-view)` : void 0, children: [
390
+ backdrop && /* @__PURE__ */ jsxs2(Fragment, { children: [
391
+ /* @__PURE__ */ jsx2("clipPath", { id: `${prefix}-backdrop`, children: layout.cells.map((cell) => /* @__PURE__ */ jsx2("path", { d: cellPath(layout, cell), clipRule: "evenodd" }, cell.id)) }),
392
+ /* @__PURE__ */ jsx2(
393
+ "image",
394
+ {
395
+ className: "andamento-backdrop",
396
+ href: backdrop,
397
+ x,
398
+ y,
399
+ width,
400
+ height,
401
+ preserveAspectRatio: "xMidYMid slice",
402
+ clipPath: `url(#${prefix}-backdrop)`
403
+ }
404
+ )
405
+ ] }),
406
+ cast && /* @__PURE__ */ jsxs2("g", { className: "andamento-shadow", clipPath: `url(#${prefix}-region)`, children: [
407
+ /* @__PURE__ */ jsx2("clipPath", { id: `${prefix}-region`, children: /* @__PURE__ */ jsx2("path", { d: regionPath(layout), clipRule: "evenodd" }) }),
408
+ /* @__PURE__ */ jsx2("filter", { id: `${prefix}-shadow`, x: "-10%", y: "-10%", width: "120%", height: "120%", children: /* @__PURE__ */ jsx2("feGaussianBlur", { stdDeviation: cast.blur }) }),
409
+ /* @__PURE__ */ jsx2(
410
+ "path",
411
+ {
412
+ d: layout.cells.map((c) => cellPath(layout, c)).join(""),
413
+ fillRule: "evenodd",
414
+ transform: `translate(${cast.dx} ${cast.dy})`,
415
+ fill: "#000",
416
+ fillOpacity: cast.opacity,
417
+ filter: `url(#${prefix}-shadow)`
418
+ }
419
+ )
420
+ ] }),
421
+ layout.cells.map((cell, i) => {
422
+ const d = cellPath(layout, cell);
423
+ const clipPathId = `${prefix}-clip-${cell.index}`;
424
+ const style = styles[i];
425
+ const ctx = { path: d, clipPathId, clipPath: `url(#${clipPathId})`, ...style };
426
+ return /* @__PURE__ */ jsxs2(
427
+ "g",
428
+ {
429
+ className: "andamento-cell",
430
+ "data-cell-id": cell.id,
431
+ "data-index": cell.index,
432
+ "data-empty": style.image ? void 0 : "",
433
+ "data-boundary": cell.boundary ? "" : void 0,
434
+ ...cellA11y(style, imageAlts),
435
+ children: [
436
+ /* @__PURE__ */ jsx2("clipPath", { id: clipPathId, children: /* @__PURE__ */ jsx2("path", { d, clipRule: "evenodd" }) }),
437
+ renderCell ? renderCell(cell, ctx) : /* @__PURE__ */ jsx2(
438
+ DefaultCell,
439
+ {
440
+ cell,
441
+ ctx,
442
+ noFill: !!backdrop,
443
+ box: imageFit === "contain" && style.image ? safeArea(cell, layout, { aspect: imageAspects && style.slot >= 0 ? imageAspects[style.slot] : void 0, within: containWithin }) : void 0,
444
+ cover: (() => {
445
+ const focus = imageFit !== "contain" && style.image && style.slot >= 0 ? imageFocus?.[style.slot] : void 0;
446
+ const aspect = focus ? imageAspects?.[style.slot] ?? focus.aspect : void 0;
447
+ return focus && aspect ? coverRect(cell, layout, { aspect, focus }) : void 0;
448
+ })()
449
+ }
450
+ ),
451
+ bevels?.[i].map((p, k) => /* @__PURE__ */ jsx2("path", { className: "andamento-bevel", d: p.d, fill: p.fill, fillOpacity: p.opacity }, k)),
452
+ stroke && /* @__PURE__ */ jsx2("path", { d, fill: "none", stroke, strokeWidth })
453
+ ]
454
+ },
455
+ cell.id
456
+ );
457
+ })
458
+ ] })
459
+ ] });
460
+ }
461
+ function DefaultCell({ cell, ctx, noFill, box, cover }) {
462
+ return /* @__PURE__ */ jsxs2(Fragment, { children: [
463
+ !noFill && /* @__PURE__ */ jsx2("path", { d: ctx.path, fill: ctx.fill, fillRule: "evenodd" }),
464
+ ctx.image && /* @__PURE__ */ jsx2(SvgCellImage, { cell, href: ctx.image, clipPath: ctx.clipPath, box, cover })
465
+ ] });
466
+ }
467
+ function SvgCellImage({ cell, href, clipPath, box, cover, ...rest }) {
468
+ const b = box ?? cover ?? cellShape(cell).bbox;
469
+ return /* @__PURE__ */ jsx2(
470
+ "image",
471
+ {
472
+ href,
473
+ x: b.x,
474
+ y: b.y,
475
+ width: b.width,
476
+ height: b.height,
477
+ preserveAspectRatio: box ? "xMidYMid meet" : "xMidYMid slice",
478
+ clipPath,
479
+ ...rest
480
+ }
481
+ );
482
+ }
483
+
484
+ // src/useImageFill.ts
485
+ import { useEffect as useEffect2, useMemo as useMemo3, useState as useState2 } from "react";
486
+ import { imageFill, loadImagePixels } from "@andamento/core";
487
+ function useImageFill(src, options = {}) {
488
+ const { fit, palette, fallback, samples, maxSize = 256 } = options;
489
+ const [pixels, setPixels] = useState2();
490
+ useEffect2(() => {
491
+ if (!src) return;
492
+ let live = true;
493
+ loadImagePixels(src, maxSize).then(
494
+ (p) => live && setPixels({ src, pixels: p }),
495
+ (e) => console.warn(`useImageFill: ${e.message}`)
496
+ );
497
+ return () => {
498
+ live = false;
499
+ };
500
+ }, [src, maxSize]);
501
+ const paletteKey = palette?.join(",");
502
+ return useMemo3(
503
+ () => pixels && pixels.src === src ? imageFill(pixels.pixels, { fit, palette, fallback, samples }) : void 0,
504
+ // eslint-disable-next-line react-hooks/exhaustive-deps
505
+ [pixels, src, fit, paletteKey, fallback, samples]
506
+ );
507
+ }
508
+
509
+ // src/useImageAspects.ts
510
+ import { useEffect as useEffect3, useState as useState3 } from "react";
511
+ function useImageAspects(srcs) {
512
+ const key = srcs?.join("\n");
513
+ const [result, setResult] = useState3();
514
+ useEffect3(() => {
515
+ if (!srcs || key === void 0 || typeof Image === "undefined") return;
516
+ let live = true;
517
+ Promise.all(
518
+ srcs.map(
519
+ (src) => new Promise((resolve2) => {
520
+ const img = new Image();
521
+ img.onload = () => resolve2(img.naturalWidth && img.naturalHeight ? img.naturalWidth / img.naturalHeight : 1);
522
+ img.onerror = () => resolve2(1);
523
+ img.src = src;
524
+ })
525
+ )
526
+ ).then((aspects) => live && setResult({ key, aspects }));
527
+ return () => {
528
+ live = false;
529
+ };
530
+ }, [key]);
531
+ return result && result.key === key ? result.aspects : void 0;
532
+ }
533
+
534
+ // src/useImageFocus.ts
535
+ import { useEffect as useEffect4, useState as useState4 } from "react";
536
+ import { estimateFocus, loadImagePixels as loadImagePixels2 } from "@andamento/core";
537
+ function useImageFocus(srcs) {
538
+ const key = srcs?.join("\n");
539
+ const [result, setResult] = useState4();
540
+ useEffect4(() => {
541
+ if (!srcs || key === void 0 || typeof document === "undefined") return;
542
+ let live = true;
543
+ Promise.all(
544
+ srcs.map(
545
+ (src) => loadImagePixels2(src, 128).then(estimateFocus).catch(() => ({ x: 0.5, y: 0.5 }))
546
+ )
547
+ ).then((focus) => live && setResult({ key, focus }));
548
+ return () => {
549
+ live = false;
550
+ };
551
+ }, [key]);
552
+ return result && result.key === key ? result.focus : void 0;
553
+ }
554
+
555
+ // src/index.tsx
556
+ import { cellFromPointer } from "@andamento/dom";
557
+ import { defaultPalette, neutralPalette } from "@andamento/svg";
558
+ export {
559
+ SvgCellImage,
560
+ SvgTileLayout,
561
+ TileLayout,
562
+ cellFromPointer,
563
+ coverStyle,
564
+ defaultPalette,
565
+ neutralPalette,
566
+ useFitCount,
567
+ useImageAspects,
568
+ useImageFill,
569
+ useImageFocus,
570
+ useTileLayout
571
+ };
572
+ //# sourceMappingURL=index.js.map