@andamento/react 0.0.0-stage → 0.1.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.
@@ -0,0 +1,296 @@
1
+ import * as react from 'react';
2
+ import { HTMLAttributes, ReactNode, CSSProperties, SVGProps } from 'react';
3
+ import { LayoutOptions, FitOptions, Layout, Cell, ContentCells, ContentPlacement, Focus, Rect, ImageFillOptions, CellFill } from '@andamento/core';
4
+ export { Cell, ContentCells, ContentPlacement, Focus, HitOptions, Layout, LayoutOptions, MatchOptions, SafeAreaOptions } from '@andamento/core';
5
+ import { SafeAreaProp, FitProp, CellState, PerCell } from '@andamento/dom';
6
+ export { CellState, FitProp, PerCell, PointerLike, cellFromPointer } from '@andamento/dom';
7
+ import { FillSource, ReliefOptions, ImageSource } from '@andamento/svg';
8
+ export { FillSource, ReliefOptions, defaultPalette, neutralPalette } from '@andamento/svg';
9
+
10
+ /**
11
+ * Compute a layout, recomputing only when the (serializable) options change.
12
+ * Inline object literals are fine: options are compared by value.
13
+ */
14
+ declare function useTileLayout(options: LayoutOptions): Layout;
15
+ /**
16
+ * Compute a layout whose tiles are sized to give about `count` usable cells (see
17
+ * `fitCount` in @andamento/core). Options are compared by value; a `cells` function by
18
+ * identity.
19
+ */
20
+ declare function useFitCount(options: LayoutOptions, count: number, fit?: FitOptions): Layout;
21
+
22
+ interface TileLayoutProps extends Omit<HTMLAttributes<HTMLDivElement>, "children"> {
23
+ /** Layout options; or pass a precomputed `layout` instead. */
24
+ options?: LayoutOptions;
25
+ layout?: Layout;
26
+ /**
27
+ * Any React content. Each top-level child fills one tile, in reading order
28
+ * (top-to-bottom, left-to-right), and is clipped to the tile's shape.
29
+ * Extra children beyond the number of tiles are not rendered.
30
+ */
31
+ children?: ReactNode;
32
+ /**
33
+ * Alternative to `children`: compute each tile's content from its cell. Takes precedence.
34
+ * `slot` numbers the cells allowed by `contentCells` in reading order (-1 for the rest).
35
+ */
36
+ renderCell?: (cell: Cell, state: {
37
+ slot: number;
38
+ }) => ReactNode;
39
+ /**
40
+ * Which tiles may hold children: "all" (default), "whole" (only tiles not cut by the
41
+ * region outline), or a test per cell. The others are left empty.
42
+ */
43
+ contentCells?: ContentCells;
44
+ /**
45
+ * Where children go when there are fewer than tiles: "reading" (default; the first
46
+ * tiles in reading order), "center" (the middle of the region) or "spread" (evenly
47
+ * over it). Children always run in reading order across the tiles chosen.
48
+ */
49
+ placement?: ContentPlacement;
50
+ /**
51
+ * Width / height of each child, in order. When given, each child goes in a tile of
52
+ * similar shape, so `object-fit: cover` crops as little as possible (landscape photos in
53
+ * wide tiles, portraits in tall ones); see `matchSlots`. Featured tiles still take the
54
+ * first children. Missing entries count as square. Ignored with `renderCell`.
55
+ */
56
+ aspects?: readonly number[];
57
+ /**
58
+ * Keep content out of the tiles' clipped corners: each tile's content goes in a box at
59
+ * the tile's safe area (see `safeArea`), the largest rectangle that fits inside its shape,
60
+ * instead of filling the tile's bounding box. Useful for text in triangles or hexagons.
61
+ * With `aspects`, each child's box has the child's shape, so it shows whole and uncropped.
62
+ * The box has `data-safe-area`; style its contents to fill it. Tiles show `fill` around
63
+ * their content. For tiles an `edges: "window"` layout cuts, `true` or "tile" fits the
64
+ * whole tile (content near the edge is partly hidden, like the tile) and "visible" the
65
+ * part that shows.
66
+ */
67
+ safeArea?: SafeAreaProp;
68
+ /** With `aspects`: "shape" also picks which tiles hold children by their shape, ignoring `placement`. */
69
+ choose?: "placement" | "shape";
70
+ /**
71
+ * Focal point of each child (fractions of its width and height; see `estimateFocus`): what
72
+ * a photo keeps in view as its tile crops it. Each tile sets `--andamento-object-position`,
73
+ * which `coverStyle` uses as `object-position`; exact when the photo's shape is known (`aspects`, or the focus's `aspect`).
74
+ */
75
+ focalPoints?: readonly (Focus | undefined)[];
76
+ /**
77
+ * How many tiles each child spans (1 for an ordinary child; rounded): heavier childs get a
78
+ * featured tile of that many merged tiles, where they fall in reading order (see
79
+ * `weightedLayout`). With `fit`, the tiles are sized to fit everything. Needs `options`.
80
+ */
81
+ weights?: readonly number[];
82
+ /**
83
+ * Color of empty tiles: a color, a palette (neighboring tiles get different entries),
84
+ * or a function per cell. Defaults to `neutralPalette`; use "transparent" for none.
85
+ */
86
+ fill?: FillSource;
87
+ /**
88
+ * Size the tiles to the children: scales the pattern so there are about as many tiles
89
+ * (counted by `contentCells`) as children. `true`/"atLeast" makes sure every child gets
90
+ * a tile; "nearest" gets as close as possible. Ignored with `layout` or `renderCell`.
91
+ */
92
+ fit?: FitProp;
93
+ /**
94
+ * One picture across all the tiles: rendered in every tile, sized and positioned to the
95
+ * whole layout, so each tile shows its own piece. Use an element that fills its box,
96
+ * e.g. `<img src=… style={coverStyle} />`. It sits behind the tile's content, and empty
97
+ * tiles show it instead of `fill`. (Each tile gets its own copy, so a video would play
98
+ * out of sync; images are loaded once.)
99
+ */
100
+ backdrop?: ReactNode;
101
+ /**
102
+ * Raised tiles: a bevel round each tile, lit from one side and drawn over its content,
103
+ * and optionally a soft shadow each tile casts into the grout (set `shadow`). `true` for
104
+ * the defaults. With `gap` as the grout width and `background` as its color or texture,
105
+ * it looks like a tiled surface. See `ReliefOptions`.
106
+ */
107
+ relief?: boolean | ReliefOptions;
108
+ /**
109
+ * Keyboard navigation: tiles with content become focusable as one tab stop (roving
110
+ * tabindex). Arrow keys move to the nearest tile in that direction, whatever the tiling;
111
+ * Home and End go to the first and last. The focused tile shows `focusRing`.
112
+ */
113
+ navigable?: boolean;
114
+ /**
115
+ * Called when a tile with content is activated: clicked, or Enter or Space while it has
116
+ * focus (with `navigable`).
117
+ */
118
+ onCellActivate?: (cell: Cell, state: CellState) => void;
119
+ /** Accessible name for each tile (its `aria-label`), e.g. a photo's description. */
120
+ cellLabel?: PerCell<string | undefined>;
121
+ /**
122
+ * Color of the focus outline drawn inside the focused tile's shape (a CSS outline would be
123
+ * clipped away), with a white inner line so it shows on any content. `false` for none,
124
+ * if you style `[data-cell-id]:focus-visible` yourself. Defaults to "#1a56db".
125
+ */
126
+ focusRing?: string | false;
127
+ /**
128
+ * Animate changes: when the layout changes (a resize, another tiling, a new gap) or the
129
+ * children do, tiles glide and morph into their new places and shapes, a child keeping
130
+ * its tile as it moves; new tiles fade in and removed ones fade out. Pure CSS
131
+ * transitions, skipped when the user prefers reduced motion. `true` for 400 ms, or
132
+ * `{ duration, easing }`.
133
+ */
134
+ animate?: boolean | {
135
+ duration?: number;
136
+ easing?: string;
137
+ };
138
+ /**
139
+ * Mount only the tiles in view (plus `overscan` pixels round it, default 300), for
140
+ * layouts with thousands of tiles. Tiles mount and unmount as the page or any scrolling
141
+ * container scrolls, or the layout resizes. Keyboard navigation still reaches every
142
+ * tile (the focused one stays mounted and is scrolled to), and list items carry
143
+ * `aria-setsize` and `aria-posinset`. With `renderCell`, every cell `contentCells`
144
+ * allows counts as having content. On the server no tiles are rendered.
145
+ */
146
+ virtualize?: boolean | {
147
+ overscan?: number;
148
+ };
149
+ /** Content for empty tiles. */
150
+ fallback?: PerCell<ReactNode>;
151
+ /** Class name for each tile wrapper (fixed or per cell). */
152
+ cellClassName?: PerCell<string>;
153
+ /** Extra style for each tile wrapper (fixed or per cell), e.g. a background color. */
154
+ cellStyle?: PerCell<CSSProperties>;
155
+ /** Color painted across the whole region, visible through the gaps. */
156
+ background?: string;
157
+ }
158
+ /**
159
+ * Renders a tiling as HTML: one absolutely positioned, `clip-path`-ed element per tile,
160
+ * holding whatever you put in it (images, video, text, components). Everything is sized
161
+ * in percentages, so the layout scales with the container's width.
162
+ *
163
+ * Each tile wrapper has `data-cell-id` and `data-index`, plus `data-empty` (no content)
164
+ * and `data-boundary` (cut by the region outline) for styling.
165
+ *
166
+ * Clipping also applies to pointer events, so hover and clicks follow the tile shapes.
167
+ */
168
+ declare function TileLayout(props: TileLayoutProps): react.JSX.Element;
169
+ /** Style that makes an <img> or <video> cover its tile, like `object-fit: cover`. */
170
+ declare const coverStyle: CSSProperties;
171
+
172
+ interface SvgCellRenderContext {
173
+ /** `d` attribute of the cell outline. */
174
+ path: string;
175
+ /** Id of a <clipPath> with the cell's shape, already in the SVG. */
176
+ clipPathId: string;
177
+ /** Ready-made `clipPath` prop value: `url(#…)`. */
178
+ clipPath: string;
179
+ /** The fill/image the default renderer would have used. */
180
+ fill: string;
181
+ image?: string;
182
+ /** Which item of `images` the cell shows (-1 if none or if `images` is a function). */
183
+ slot: number;
184
+ }
185
+ interface SvgTileLayoutProps extends Omit<SVGProps<SVGSVGElement>, "children" | "fill" | "stroke" | "strokeWidth"> {
186
+ /** Layout options; or pass a precomputed `layout` instead. */
187
+ options?: LayoutOptions;
188
+ layout?: Layout;
189
+ images?: ImageSource;
190
+ repeatImages?: boolean;
191
+ /** Which cells may hold an image from the `images` array: "all" (default), "whole" or a test. */
192
+ contentCells?: ContentCells;
193
+ /** Where images go when there are fewer than cells: "reading" (default), "center" or "spread". */
194
+ placement?: ContentPlacement;
195
+ /** Width / height of each image, to put images in cells of similar shape (see `matchSlots`). */
196
+ imageAspects?: readonly number[];
197
+ /** With `imageAspects`: "shape" also picks which cells get images by their shape. */
198
+ choose?: "placement" | "shape";
199
+ /**
200
+ * "cover" (default) fills each cell's bounding box, cropped to its shape; "contain" shows
201
+ * each image whole inside the cell's safe area (see `safeArea`), at its `imageAspects` shape.
202
+ */
203
+ imageFit?: "cover" | "contain";
204
+ /**
205
+ * With `imageFit: "contain"`, for cells an `edges: "window"` layout cuts: fit the image in
206
+ * the whole tile ("tile", the default) or in the part that shows ("visible").
207
+ */
208
+ containWithin?: "tile" | "visible";
209
+ /**
210
+ * Focal point of each image (by position; fractions of its width and height, see
211
+ * `estimateFocus`), kept in view when "cover" crops it (see `coverRect`). Needs each image's
212
+ * shape, from `imageAspects` or the focus's `aspect`.
213
+ */
214
+ imageFocus?: readonly (Focus | undefined)[];
215
+ /**
216
+ * How many tiles each image in `images` spans (1 for most): heavier images get featured
217
+ * tiles where they fall in reading order (see `weightedLayout`). Needs `options`.
218
+ */
219
+ imageWeights?: readonly number[];
220
+ /**
221
+ * Size the tiles to the `images` array (counted by `contentCells`): `true`/"atLeast" or
222
+ * "nearest". Ignored with `layout`, `repeatImages` or an `images` function.
223
+ */
224
+ fit?: FitProp;
225
+ /** Cell color under (or instead of) images. Defaults to `defaultPalette`. */
226
+ fill?: FillSource;
227
+ stroke?: string;
228
+ strokeWidth?: number;
229
+ /** Color painted across the whole region, behind the cells. */
230
+ background?: string;
231
+ /**
232
+ * One image across all the cells (a URL), cropped to cover the layout and clipped to the
233
+ * cells. The default cell rendering then skips `fill`; per-cell `images` go on top.
234
+ */
235
+ backdrop?: string;
236
+ /**
237
+ * Raised tiles: a lit bevel over each cell, and optionally a shadow cast into the grout.
238
+ * `true` for the defaults. See `ReliefOptions`. Drawn over `renderCell` content too.
239
+ */
240
+ relief?: boolean | ReliefOptions;
241
+ /** Replace the default cell rendering with SVG content. Rendered inside a <g> per cell. */
242
+ renderCell?: (cell: Cell, ctx: SvgCellRenderContext) => ReactNode;
243
+ /** Accessible title. */
244
+ title?: string;
245
+ /** Text alternative per image in `images`: those cells become labeled images; the rest decoration. */
246
+ imageAlts?: readonly string[];
247
+ }
248
+ /**
249
+ * Renders a tiling as a single <svg>: colored cells, optionally with images clipped
250
+ * to each cell. Use this when you need pure SVG (export, print, embedding in other SVG);
251
+ * use <TileLayout> to put arbitrary HTML/React content in the tiles.
252
+ */
253
+ declare function SvgTileLayout(props: SvgTileLayoutProps): react.JSX.Element;
254
+ interface SvgCellImageProps extends Omit<SVGProps<SVGImageElement>, "href" | "x" | "y" | "width" | "height"> {
255
+ cell: Cell;
256
+ href: string;
257
+ /** Pass `ctx.clipPath` from `renderCell`. */
258
+ clipPath: string;
259
+ /**
260
+ * Draw the image whole inside this box (e.g. `safeArea(cell, layout)`) instead of
261
+ * covering the cell.
262
+ */
263
+ box?: Rect;
264
+ /** Cover this rectangle instead of the cell's box (e.g. `coverRect(cell, layout, { aspect, focus })`). */
265
+ cover?: Rect;
266
+ }
267
+ /**
268
+ * An SVG <image> that covers the cell (like CSS `object-fit: cover`), clipped to its shape;
269
+ * or, with `box`, shown whole inside the box (like `object-fit: contain`).
270
+ */
271
+ declare function SvgCellImage({ cell, href, clipPath, box, cover, ...rest }: SvgCellImageProps): react.JSX.Element;
272
+
273
+ /**
274
+ * Load an image and color tiles from it (see `imageFill`): returns a fill to pass as
275
+ * `fill`, or `undefined` while the image loads (or if it fails), so the default fill shows
276
+ * meanwhile. `maxSize` caps the size the image is read at (default 256 px on the long side).
277
+ */
278
+ declare function useImageFill(src: string | undefined, options?: ImageFillOptions & {
279
+ maxSize?: number;
280
+ }): CellFill | undefined;
281
+
282
+ /**
283
+ * Measure images' aspect ratios (width / height) by loading them, for the `aspects` and
284
+ * `imageAspects` props. Returns `undefined` until every image has loaded or failed (failed
285
+ * ones count as square), so content isn't reshuffled image by image as they arrive.
286
+ */
287
+ declare function useImageAspects(srcs: readonly string[] | undefined): number[] | undefined;
288
+
289
+ /**
290
+ * Guess each image's focal point (see `estimateFocus`) by loading it, for the `focus` and
291
+ * `imageFocus` props. Returns `undefined` until every image has been looked at; images that
292
+ * fail to load (or that the browser won't let a canvas read) get the middle.
293
+ */
294
+ declare function useImageFocus(srcs: readonly string[] | undefined): Focus[] | undefined;
295
+
296
+ export { SvgCellImage, type SvgCellImageProps, type SvgCellRenderContext, SvgTileLayout, type SvgTileLayoutProps, TileLayout, type TileLayoutProps, coverStyle, useFitCount, useImageAspects, useImageFill, useImageFocus, useTileLayout };