@oxyhq/bloom 0.34.3 → 0.35.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/lib/commonjs/avatar-group/AvatarGroup.js +3 -2
  2. package/lib/commonjs/avatar-group/AvatarGroup.js.map +1 -1
  3. package/lib/commonjs/avatar-group/AvatarGroup.web.js +5 -5
  4. package/lib/commonjs/avatar-group/AvatarGroupBase.js +217 -48
  5. package/lib/commonjs/avatar-group/AvatarGroupBase.js.map +1 -1
  6. package/lib/commonjs/avatar-group/cluster-layout.js +252 -0
  7. package/lib/commonjs/avatar-group/cluster-layout.js.map +1 -0
  8. package/lib/module/avatar-group/AvatarGroup.js +3 -2
  9. package/lib/module/avatar-group/AvatarGroup.js.map +1 -1
  10. package/lib/module/avatar-group/AvatarGroup.web.js +5 -5
  11. package/lib/module/avatar-group/AvatarGroupBase.js +217 -48
  12. package/lib/module/avatar-group/AvatarGroupBase.js.map +1 -1
  13. package/lib/module/avatar-group/cluster-layout.js +248 -0
  14. package/lib/module/avatar-group/cluster-layout.js.map +1 -0
  15. package/lib/typescript/commonjs/avatar-group/AvatarGroup.d.ts.map +1 -1
  16. package/lib/typescript/commonjs/avatar-group/AvatarGroupBase.d.ts.map +1 -1
  17. package/lib/typescript/commonjs/avatar-group/cluster-layout.d.ts +55 -0
  18. package/lib/typescript/commonjs/avatar-group/cluster-layout.d.ts.map +1 -0
  19. package/lib/typescript/commonjs/avatar-group/types.d.ts +23 -5
  20. package/lib/typescript/commonjs/avatar-group/types.d.ts.map +1 -1
  21. package/lib/typescript/module/avatar-group/AvatarGroup.d.ts.map +1 -1
  22. package/lib/typescript/module/avatar-group/AvatarGroupBase.d.ts.map +1 -1
  23. package/lib/typescript/module/avatar-group/cluster-layout.d.ts +55 -0
  24. package/lib/typescript/module/avatar-group/cluster-layout.d.ts.map +1 -0
  25. package/lib/typescript/module/avatar-group/types.d.ts +23 -5
  26. package/lib/typescript/module/avatar-group/types.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/__tests__/AvatarGroupCluster.test.tsx +168 -0
  29. package/src/avatar-group/AvatarGroup.stories.tsx +69 -0
  30. package/src/avatar-group/AvatarGroup.tsx +3 -2
  31. package/src/avatar-group/AvatarGroup.web.tsx +5 -5
  32. package/src/avatar-group/AvatarGroupBase.tsx +298 -89
  33. package/src/avatar-group/cluster-layout.ts +240 -0
  34. package/src/avatar-group/types.ts +23 -5
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Deterministic organic circle-packing for the {@link AvatarGroup} `cluster`
3
+ * layout — the iMessage-style "magnetic bubble cluster" where several avatars of
4
+ * NEAR-EQUAL size nestle tightly together and fill a round bounding box (a
5
+ * modestly larger primary with the rest only slightly smaller, packed against
6
+ * each other with a small uniform gap — not a big primary ringed by tiny dots).
7
+ *
8
+ * For 4+ members the layout is produced by a small, FULLY DETERMINISTIC
9
+ * force-directed relaxation (no `Math.random`): the primary is pinned at the
10
+ * centre and the remaining members are seeded on a golden-angle spiral, then a
11
+ * fixed number of relaxation passes (a) pull every non-primary circle toward the
12
+ * centre and (b) push any two circles apart until they clear a uniform gap. A
13
+ * fixed iteration count + deterministic seed means the same `count` always
14
+ * yields byte-identical positions, so native and web render the cluster
15
+ * identically with no `onLayout`/DOM measurement. The very small counts (1, 2,
16
+ * 3) — where a relaxation degenerates into a line or a lone pair — use explicit
17
+ * iMessage-style arrangements instead.
18
+ *
19
+ * Output is resolution-independent: each bubble is expressed as a fraction of
20
+ * the group's bounding box (`cx`/`cy` centre, `d` diameter, all 0..1), so the
21
+ * consumer just multiplies by the pixel `size`. The relaxed blob is recentred on
22
+ * its own centre and scaled so the outermost bubble edge lands on the box edge,
23
+ * so it fills the round box densely with minimal empty margin while every bubble
24
+ * is guaranteed to sit fully inside the `[0, 1]` box — the cluster drops in
25
+ * exactly where a single round Avatar would.
26
+ */
27
+
28
+ /** A single packed bubble, expressed as fractions of the bounding box (0..1). */
29
+ export interface ClusterBubble {
30
+ /** Centre X as a fraction of the box width. */
31
+ cx: number;
32
+ /** Centre Y as a fraction of the box height. */
33
+ cy: number;
34
+ /** Diameter as a fraction of the box size. */
35
+ d: number;
36
+ }
37
+
38
+ // Golden angle (~137.5°) — spreads the seed points evenly with no directional
39
+ // bias, which is what gives the relaxed result its organic, non-grid feel.
40
+ const GOLDEN_ANGLE = Math.PI * (3 - Math.sqrt(5));
41
+ // Fixed relaxation passes + a final separation-only cleanup so the last thing
42
+ // that happens is overlap resolution (gaps end uniform and non-negative).
43
+ const ITERATIONS = 500;
44
+ const CLEANUP_ITERATIONS = 80;
45
+ // How hard each pass pulls every non-primary circle toward the centre. This is
46
+ // the only compacting force; separation only ever pushes apart, so any positive
47
+ // value packs the cluster fully — this just controls convergence speed.
48
+ const CENTERING = 0.05;
49
+ // Fraction of an overlap resolved per pass. 1 fully separates each pass; with
50
+ // the pinned primary and many passes this stays stable and converges.
51
+ const SEPARATION_STRENGTH = 1;
52
+ // Uniform gap kept between every touching pair, in primary-radius units
53
+ // (primary radius = 1). Small, so near-equal bubbles nestle tightly like
54
+ // magnets rather than floating apart with visible margins.
55
+ const LAYOUT_GAP = 0.12;
56
+ // Initial spiral spacing. Only affects convergence (the result is re-fitted to
57
+ // the box afterwards), not the final scale.
58
+ const SEED_SPACING = 1.7;
59
+ // Relative radii: the primary is only MODESTLY the largest; every other member
60
+ // tapers gently from SECONDARY_MAX (nearest the primary) down to SECONDARY_MIN
61
+ // (outermost). The spread is deliberately narrow so the cluster reads as a pack
62
+ // of near-equal magnetic bubbles — a slightly larger primary, not a big primary
63
+ // ringed by tiny dots.
64
+ const PRIMARY_RADIUS = 1;
65
+ const SECONDARY_MAX = 0.9;
66
+ const SECONDARY_MIN = 0.8;
67
+ const EPSILON = 1e-6;
68
+
69
+ /** Relative radius for member `index` of a `count`-member cluster. */
70
+ function relativeRadius(index: number, count: number): number {
71
+ if (index === 0) return PRIMARY_RADIUS;
72
+ if (count <= 2) return SECONDARY_MAX;
73
+ const t = (index - 1) / (count - 2);
74
+ return SECONDARY_MAX + (SECONDARY_MIN - SECONDARY_MAX) * t;
75
+ }
76
+
77
+ /**
78
+ * One separation pass: push any pair closer than `(r_i + r_j + gap)` apart. The
79
+ * primary (index 0) is pinned — when a pair involves it, only the other circle
80
+ * moves — which keeps the largest avatar dead-centre and in front.
81
+ */
82
+ function separate(xs: number[], ys: number[], radii: number[], count: number): void {
83
+ for (let i = 0; i < count; i++) {
84
+ for (let j = i + 1; j < count; j++) {
85
+ let dx = (xs[j] ?? 0) - (xs[i] ?? 0);
86
+ let dy = (ys[j] ?? 0) - (ys[i] ?? 0);
87
+ let dist = Math.hypot(dx, dy);
88
+ const minDist = (radii[i] ?? 0) + (radii[j] ?? 0) + LAYOUT_GAP;
89
+ if (dist >= minDist) continue;
90
+ if (dist < EPSILON) {
91
+ // Coincident points: pick a deterministic direction from the indices so
92
+ // the split is stable (never random).
93
+ const a = (i + 1) * GOLDEN_ANGLE + j;
94
+ dx = Math.cos(a);
95
+ dy = Math.sin(a);
96
+ dist = 1;
97
+ }
98
+ const overlap = (minDist - dist) * SEPARATION_STRENGTH;
99
+ const nx = dx / dist;
100
+ const ny = dy / dist;
101
+ if (i === 0) {
102
+ // Primary pinned: move only the other circle by the full overlap.
103
+ xs[j] = (xs[j] ?? 0) + nx * overlap;
104
+ ys[j] = (ys[j] ?? 0) + ny * overlap;
105
+ } else {
106
+ xs[i] = (xs[i] ?? 0) - (nx * overlap) / 2;
107
+ ys[i] = (ys[i] ?? 0) - (ny * overlap) / 2;
108
+ xs[j] = (xs[j] ?? 0) + (nx * overlap) / 2;
109
+ ys[j] = (ys[j] ?? 0) + (ny * overlap) / 2;
110
+ }
111
+ }
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Deterministic force-directed pack of `count` circles (primary pinned at the
117
+ * centre), returned as box-fraction bubbles. Used for every cluster with 4+
118
+ * members; 1–3 are handled as explicit arrangements in
119
+ * {@link computeClusterLayout}.
120
+ */
121
+ function packCluster(count: number): ClusterBubble[] {
122
+ const radii = new Array<number>(count);
123
+ const xs = new Array<number>(count);
124
+ const ys = new Array<number>(count);
125
+
126
+ for (let i = 0; i < count; i++) {
127
+ radii[i] = relativeRadius(i, count);
128
+ if (i === 0) {
129
+ xs[i] = 0;
130
+ ys[i] = 0;
131
+ } else {
132
+ // Golden-angle spiral seed around the pinned primary.
133
+ const seedR = SEED_SPACING * Math.sqrt(i);
134
+ const angle = i * GOLDEN_ANGLE;
135
+ xs[i] = seedR * Math.cos(angle);
136
+ ys[i] = seedR * Math.sin(angle);
137
+ }
138
+ }
139
+
140
+ for (let iter = 0; iter < ITERATIONS; iter++) {
141
+ // (a) Centering: pull every non-primary circle toward the centre.
142
+ for (let i = 1; i < count; i++) {
143
+ xs[i] = (xs[i] ?? 0) * (1 - CENTERING);
144
+ ys[i] = (ys[i] ?? 0) * (1 - CENTERING);
145
+ }
146
+ // (b) Separation resolves any resulting overlaps (primary stays pinned).
147
+ separate(xs, ys, radii, count);
148
+ xs[0] = 0;
149
+ ys[0] = 0;
150
+ }
151
+ // Final separation-only passes so the cluster ends with clean uniform gaps.
152
+ for (let iter = 0; iter < CLEANUP_ITERATIONS; iter++) {
153
+ separate(xs, ys, radii, count);
154
+ xs[0] = 0;
155
+ ys[0] = 0;
156
+ }
157
+
158
+ // Fit to the unit box so the pack FILLS the round box edge-to-edge with
159
+ // minimal empty margin. The primary was pinned at the origin during
160
+ // relaxation, but the relaxed blob is not centred on it, so we recentre on the
161
+ // cluster's OWN centre (its bounding-box midpoint) rather than the primary:
162
+ // enclose every bubble in a circle around that centre, then scale so the
163
+ // outermost bubble edge lands exactly on the box radius (0.5). Centring the
164
+ // enclosing circle at the box centre keeps every bubble inside the [0, 1] box
165
+ // (the enclosing circle is inscribed in the square) while packing the blob
166
+ // tightly against the edge instead of floating with a gap.
167
+ let minX = Infinity;
168
+ let maxX = -Infinity;
169
+ let minY = Infinity;
170
+ let maxY = -Infinity;
171
+ for (let i = 0; i < count; i++) {
172
+ const x = xs[i] ?? 0;
173
+ const y = ys[i] ?? 0;
174
+ const r = radii[i] ?? 0;
175
+ if (x - r < minX) minX = x - r;
176
+ if (x + r > maxX) maxX = x + r;
177
+ if (y - r < minY) minY = y - r;
178
+ if (y + r > maxY) maxY = y + r;
179
+ }
180
+ const centerX = (minX + maxX) / 2;
181
+ const centerY = (minY + maxY) / 2;
182
+ let bound = 0;
183
+ for (let i = 0; i < count; i++) {
184
+ const reach =
185
+ Math.hypot((xs[i] ?? 0) - centerX, (ys[i] ?? 0) - centerY) + (radii[i] ?? 0);
186
+ if (reach > bound) bound = reach;
187
+ }
188
+ const scale = bound > EPSILON ? 0.5 / bound : 0.5;
189
+
190
+ const bubbles = new Array<ClusterBubble>(count);
191
+ for (let i = 0; i < count; i++) {
192
+ bubbles[i] = {
193
+ cx: 0.5 + ((xs[i] ?? 0) - centerX) * scale,
194
+ cy: 0.5 + ((ys[i] ?? 0) - centerY) * scale,
195
+ d: 2 * (radii[i] ?? 0) * scale,
196
+ };
197
+ }
198
+ return bubbles;
199
+ }
200
+
201
+ /**
202
+ * Deterministic cluster layout for `count` bubbles, as box-fraction bubbles
203
+ * ordered primary-first. `count` includes the `+N` overflow bubble when present
204
+ * (it is simply the last, smallest member of the pack).
205
+ *
206
+ * - `<= 0` → empty.
207
+ * - `1` → a single bubble filling the box.
208
+ * - `2` → the iMessage "one in front, one behind" pair: two EQUAL-diameter
209
+ * bubbles offset on a diagonal — the front set low-left, the other tucked
210
+ * behind it to the upper-right. Same radius, so neither member reads as
211
+ * secondary; the overlap + separator ring alone convey the stacking.
212
+ * - `3` → an iMessage-style triangle: the larger primary along the bottom with
213
+ * two smaller members above it.
214
+ * - `4+` → the deterministic force-directed pack of near-equal bubbles, recentred
215
+ * and scaled to fill the round box edge-to-edge (a modestly larger primary near
216
+ * the centre, the rest packed magnetically around it, denser as the count
217
+ * grows).
218
+ */
219
+ export function computeClusterLayout(count: number): ClusterBubble[] {
220
+ if (count <= 0) return [];
221
+ if (count === 1) return [{ cx: 0.5, cy: 0.5, d: 1 }];
222
+ if (count === 2) {
223
+ // Two EQUAL-diameter bubbles, offset symmetrically on the diagonal: the
224
+ // front (index 0, highest zIndex) sits low-left, the other tucks behind it
225
+ // to the upper-right. They overlap (the intentional "front + behind" pair);
226
+ // the separator ring — not a size difference — reads the stacking.
227
+ return [
228
+ { cx: 0.35, cy: 0.65, d: 0.64 },
229
+ { cx: 0.65, cy: 0.35, d: 0.64 },
230
+ ];
231
+ }
232
+ if (count === 3) {
233
+ return [
234
+ { cx: 0.5, cy: 0.69, d: 0.54 },
235
+ { cx: 0.285, cy: 0.24, d: 0.4 },
236
+ { cx: 0.715, cy: 0.24, d: 0.4 },
237
+ ];
238
+ }
239
+ return packCluster(count);
240
+ }
@@ -30,14 +30,27 @@ export interface AvatarGroupProps {
30
30
  items: AvatarGroupItem[];
31
31
  /**
32
32
  * How the avatars are arranged.
33
- * - `'stack'` (default): overlapping facepile with a thin separator ring, a
34
- * trailing `+N` overflow chip, and the first item on top.
33
+ * - `'stack'` (default): overlapping horizontal facepile with a thin separator
34
+ * ring, a trailing `+N` overflow chip, and the first item on top.
35
35
  * - `'row'`: avatars placed adjacent with a positive `spacing` gap and NO
36
36
  * separator ring — an "adjacent row" of icons (e.g. a top-tokens strip).
37
37
  * Still collapses into the `+N` overflow chip at `max`.
38
+ * - `'cluster'`: a compact 2D "magnetic bubble cluster" (iMessage-style) —
39
+ * avatars of VARYING sizes deterministically packed into a round bounding
40
+ * box with a uniform gap: the first item (primary) is the largest and sits
41
+ * centred/in front, the rest nestle around it. Scales from 2 (a "front +
42
+ * behind" pair) up through a dense pack (~20), collapsing into a trailing
43
+ * `+N` bubble past `max`. Unlike the horizontal layouts, `size` is the
44
+ * overall box diameter (see `size`) and `max` defaults to 20 (see `max`), so
45
+ * it drops in where a single round Avatar would.
46
+ */
47
+ layout?: 'stack' | 'row' | 'cluster';
48
+ /**
49
+ * Diameter in pixels. For `'stack'`/`'row'` this is the diameter of EACH
50
+ * avatar. For `'cluster'` this is the diameter of the whole bounding box (the
51
+ * packed bubbles are sized as fractions of it), so the cluster occupies the
52
+ * same footprint as a single `size`-px Avatar.
38
53
  */
39
- layout?: 'stack' | 'row';
40
- /** Diameter of each avatar in pixels. */
41
54
  size?: number;
42
55
  /**
43
56
  * Rendition variant forwarded to each {@link Avatar}'s `variant` prop, which
@@ -46,7 +59,12 @@ export interface AvatarGroupProps {
46
59
  * `'thumb'`; pass `undefined` to request full-size renditions.
47
60
  */
48
61
  variant?: string;
49
- /** Maximum number of avatars to render before collapsing into the overflow chip. */
62
+ /**
63
+ * Maximum number of avatars to render before collapsing into the overflow
64
+ * chip. Defaults to 5 for `'stack'`/`'row'`, and to 20 for `'cluster'` (which
65
+ * packs densely). In `'cluster'` the cap is inclusive of the `+N` bubble: past
66
+ * the cap the last slot becomes the overflow bubble.
67
+ */
50
68
  max?: number;
51
69
  /**
52
70
  * Real total count of members. When provided and larger than the number of