magic-pill 9.4.0-next.1 → 9.4.0-next.3

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/Pill/goo.js CHANGED
@@ -29,6 +29,14 @@ function splitEasing(token) {
29
29
  if (value === "smooth") return SMOOTH;
30
30
  return value;
31
31
  }
32
+ /**
33
+ * A droplet's curve (its position and its size together): drawn out slowly, then growing (`DROPLET_GROW`),
34
+ * or the same backwards. `--mp-ease-split`, when an app sets it, picks the curve out instead.
35
+ */
36
+ function dropletEasing(token, kind) {
37
+ if (kind === "in") return DROPLET_SHRINK;
38
+ return token.trim() ? splitEasing(token) : DROPLET_GROW;
39
+ }
32
40
  /** How much goo the motion needs at progress `k` (0 to 1): none at the ends, all of it while the neck forms. */
33
41
  function gooAmount(k) {
34
42
  const step = (from, to, x) => {
@@ -55,7 +63,7 @@ function tuckedOffset(anchor, moving, start = 1) {
55
63
  let scale = start;
56
64
  if (!fits(moving.width, anchor.width)) scale = Math.min(scale, anchor.width / moving.width);
57
65
  if (!fits(moving.height, anchor.height)) scale = Math.min(scale, anchor.height / moving.height);
58
- scale = Math.max(.2, scale);
66
+ scale = Math.max(.1, scale);
59
67
  if (Math.abs(dx) / (anchor.width / 2 + moving.width / 2) >= Math.abs(dy) / (anchor.height / 2 + moving.height / 2)) {
60
68
  const sign = Math.sign(dx) || 1;
61
69
  return {
@@ -109,7 +117,7 @@ function createOverlay(zIndex, color, shadow) {
109
117
  const root = document.createElement("div");
110
118
  root.setAttribute("data-mp", "goo");
111
119
  root.setAttribute("aria-hidden", "true");
112
- root.style.cssText = `position:fixed;inset:0;pointer-events:none;z-index:${zIndex};`;
120
+ root.style.cssText = `position:fixed;left:0;top:0;width:0;height:0;pointer-events:none;z-index:${zIndex};`;
113
121
  const svg = document.createElementNS(SVG, "svg");
114
122
  svg.setAttribute("width", "0");
115
123
  svg.setAttribute("height", "0");
@@ -161,15 +169,29 @@ function createOverlay(zIndex, color, shadow) {
161
169
  moving
162
170
  };
163
171
  }
164
- /** Puts a shape where a pill is on screen, with its rounded corners at its current scale. */
165
- function place(shape, element) {
166
- const rect = element.getBoundingClientRect();
167
- const scale = element.offsetWidth > 0 ? rect.width / element.offsetWidth : 1;
168
- const radius = Number.parseFloat(getComputedStyle(element).borderTopLeftRadius) || 0;
169
- shape.style.width = `${rect.width}px`;
170
- shape.style.height = `${rect.height}px`;
171
- shape.style.transform = `translate(${rect.left}px, ${rect.top}px)`;
172
- shape.style.borderRadius = `${Math.min(radius * scale, rect.width / 2, rect.height / 2)}px`;
172
+ /**
173
+ * Puts the overlay around the two pills (with `margin` for the blur and the
174
+ * shadow), and each shape where its pill is, with its rounded corners at its
175
+ * current scale (`radius`: read once, at its full size).
176
+ */
177
+ function placeAll(overlay, pills, radii, margin) {
178
+ const a = pills.anchor.getBoundingClientRect();
179
+ const m = pills.moving.getBoundingClientRect();
180
+ const left = Math.min(a.left, m.left) - margin;
181
+ const top = Math.min(a.top, m.top) - margin;
182
+ const root = overlay.root.style;
183
+ root.transform = `translate(${left}px, ${top}px)`;
184
+ root.width = `${Math.max(a.right, m.right) + margin - left}px`;
185
+ root.height = `${Math.max(a.bottom, m.bottom) + margin - top}px`;
186
+ const put = (shape, rect, element, radius) => {
187
+ const scale = element.offsetWidth > 0 ? rect.width / element.offsetWidth : 1;
188
+ shape.style.width = `${rect.width}px`;
189
+ shape.style.height = `${rect.height}px`;
190
+ shape.style.transform = `translate(${rect.left - left}px, ${rect.top - top}px)`;
191
+ shape.style.borderRadius = `${Math.min(radius * scale, rect.width / 2, rect.height / 2)}px`;
192
+ };
193
+ put(overlay.anchor, a, pills.anchor, radii.anchor);
194
+ put(overlay.moving, m, pills.moving, radii.moving);
173
195
  }
174
196
  const OWN_LOOK = {
175
197
  transition: "none",
@@ -186,8 +208,9 @@ const readNumber = (style, name, fallback) => {
186
208
  };
187
209
  /**
188
210
  * Plays a split (the moving pill comes out of the anchor's edge, from where
189
- * it's tucked in to where it sits) or a join (the other way, after `delay`).
190
- * Reads `--mp-goo`, `--mp-electron-start` and `--mp-ease-split` on the
211
+ * it's tucked in to where it sits) or a join (the other way, after `delay`),
212
+ * as a droplet: drawn out small, growing to its size, its content showing once
213
+ * it's out. Reads `--mp-goo`, `--mp-electron-start` and `--mp-ease-split` on the
191
214
  * moving pill. Does nothing with reduced motion. Returns a function that
192
215
  * stops it at once.
193
216
  */
@@ -197,10 +220,21 @@ function playSplit(anchor, moving, kind, timing) {
197
220
  if (timing.duration <= 0 || require_timing.prefersReducedMotion() || typeof moving.animate !== "function") return () => {};
198
221
  const style = getComputedStyle(moving);
199
222
  const goo = readNumber(style, "--mp-goo", DEFAULT_GOO);
200
- const start = readNumber(style, "--mp-electron-start", 1);
223
+ const start = readNumber(style, "--mp-electron-start", DROPLET);
201
224
  const fade = require_timing.parseDuration(style.getPropertyValue("--_mp-fade"), 150);
202
- const easing = kind === "split" ? splitEasing(style.getPropertyValue("--mp-ease-split")) : SMOOTH;
225
+ const easing = dropletEasing(style.getPropertyValue("--mp-ease-split"), kind === "split" ? "out" : "in");
203
226
  let overlay = null;
227
+ const radii = {
228
+ anchor: 0,
229
+ moving: 0
230
+ };
231
+ let margin = 0;
232
+ const placeShapes = () => {
233
+ if (overlay) placeAll(overlay, {
234
+ anchor,
235
+ moving
236
+ }, radii, margin);
237
+ };
204
238
  let glass = kind === "join";
205
239
  const saved = /* @__PURE__ */ new Map();
206
240
  let frame = 0;
@@ -288,11 +322,14 @@ function playSplit(anchor, moving, kind, timing) {
288
322
  id: SPLIT,
289
323
  duration: total
290
324
  });
291
- else child.animate([{ opacity: 1 }, { opacity: 0 }], {
292
- id: SPLIT,
293
- duration: Math.max(1, fade),
294
- fill: "forwards"
295
- });
325
+ else {
326
+ const now = Number.parseFloat(getComputedStyle(child).opacity);
327
+ child.animate([{ opacity: Number.isFinite(now) ? now : 1 }, { opacity: 0 }], {
328
+ id: SPLIT,
329
+ duration: Math.max(1, fade),
330
+ fill: "forwards"
331
+ });
332
+ }
296
333
  };
297
334
  const begin = () => {
298
335
  begun = true;
@@ -302,9 +339,12 @@ function playSplit(anchor, moving, kind, timing) {
302
339
  saved.set("transition", [moving.style.getPropertyValue("transition"), moving.style.getPropertyPriority("transition")]);
303
340
  moving.style.setProperty("transition", "none", "important");
304
341
  const own = getComputedStyle(moving);
305
- overlay = createOverlay(zIndex, own.backgroundColor, dropShadow(own.boxShadow));
306
- place(overlay.anchor, anchor);
307
- place(overlay.moving, moving);
342
+ const shadow = dropShadow(own.boxShadow);
343
+ overlay = createOverlay(zIndex, own.backgroundColor, shadow);
344
+ radii.anchor = Number.parseFloat(getComputedStyle(anchor).borderTopLeftRadius) || 0;
345
+ radii.moving = Number.parseFloat(own.borderTopLeftRadius) || 0;
346
+ margin = goo * 3 + (shadow ? shadow.blur + Math.max(Math.abs(shadow.x), Math.abs(shadow.y)) : 0) + 8;
347
+ placeShapes();
308
348
  hideOwnLook();
309
349
  if (kind === "split") for (const name of BLUR) {
310
350
  saved.set(name, [moving.style.getPropertyValue(name), moving.style.getPropertyPriority(name)]);
@@ -328,8 +368,7 @@ function playSplit(anchor, moving, kind, timing) {
328
368
  if (begun && overlay) {
329
369
  const k = Math.min(1, Math.max(0, (elapsed - timing.delay) / timing.duration));
330
370
  overlay.blur.setAttribute("stdDeviation", (goo * gooAmount(k)).toFixed(2));
331
- place(overlay.anchor, anchor);
332
- place(overlay.moving, moving);
371
+ placeShapes();
333
372
  setGlass(!overlaps(moving.getBoundingClientRect(), anchor.getBoundingClientRect()));
334
373
  }
335
374
  if (elapsed >= timing.delay + timing.duration) {
@@ -343,10 +382,237 @@ function playSplit(anchor, moving, kind, timing) {
343
382
  running.set(moving, stop);
344
383
  return stop;
345
384
  }
385
+ /** The size an electron and the page's actions start at as they come out (a droplet), unless `--mp-electron-start` says */
386
+ const DROPLET = .15;
387
+ /** A droplet's size on its way out: drawn out small, then growing to its size; going back, the same backwards */
388
+ const DROPLET_GROW = "cubic-bezier(0.6, 0, 0.35, 1)";
389
+ const DROPLET_SHRINK = "cubic-bezier(0.65, 0, 0.4, 1)";
390
+ /**
391
+ * Inside the anchor at its nearest edge, as a flag goes into its sidebar, at `--mp-electron-start` of
392
+ * its size (`start` by default). `at`: where the anchor is going, if it's moving.
393
+ */
394
+ const atEdgeOf = (anchor, at, start = 1) => (rect, element) => tuckedOffset(at?.() ?? anchor.getBoundingClientRect(), rect, readNumber(getComputedStyle(element), "--mp-electron-start", start));
395
+ /** Inside the anchor at a point of the screen (where a pill closing to a circle ends), small. */
396
+ const atPoint = (x, y, scale = .3) => (rect) => ({
397
+ x: x - (rect.left + rect.width / 2),
398
+ y: y - (rect.top + rect.height / 2),
399
+ scale
400
+ });
401
+ const budding = /* @__PURE__ */ new WeakMap();
402
+ /**
403
+ * Pills budding out of another (or going back into it), one after the other,
404
+ * as the concept drew an electron's orbitals: from inside it, small and
405
+ * see-through, to their spot. The pills draw themselves the whole time (their
406
+ * glass, rim and shadow stay, no colour switch at the end); one overlay under
407
+ * them draws only the necks between them and the anchor: the goo of the
408
+ * moment, every pill's inside cut out of it. An electron's orbitals, an
409
+ * electron out of its host or out of the one next to it, the page's actions.
410
+ */
411
+ function playOrbit(electron, items, kind, timing) {
412
+ for (const { element } of items) {
413
+ budding.get(element)?.();
414
+ for (const animation of element.getAnimations?.({ subtree: true }) ?? []) if (animation.id === "mp-split") animation.cancel();
415
+ }
416
+ if (timing.duration <= 0 || require_timing.prefersReducedMotion() || typeof electron.animate !== "function" || items.length === 0) return () => {};
417
+ const style = getComputedStyle(electron);
418
+ const easing = kind === "open" ? splitEasing(style.getPropertyValue("--mp-ease-split")) : SMOOTH;
419
+ const centre = electron.getBoundingClientRect();
420
+ const cx = centre.left + centre.width / 2;
421
+ const cy = centre.top + centre.height / 2;
422
+ const longest = Math.max(...items.map(({ delay }) => delay)) + timing.duration;
423
+ for (const { element, delay, shown = 1, from = atPoint(cx, cy), droplet = false, opaque = droplet } of items) {
424
+ if (shown <= 0) continue;
425
+ const start = from(element.getBoundingClientRect(), element);
426
+ const end = getComputedStyle(element).scale;
427
+ const inside = {
428
+ translate: `${start.x}px ${start.y}px`,
429
+ scale: String(start.scale),
430
+ opacity: opaque ? shown : 0
431
+ };
432
+ const out = {
433
+ translate: "0px 0px",
434
+ scale: end === "none" ? "1" : end,
435
+ opacity: shown
436
+ };
437
+ const frames = kind === "open" ? [
438
+ inside,
439
+ {
440
+ ...out,
441
+ offset: .35
442
+ },
443
+ out
444
+ ] : [
445
+ out,
446
+ {
447
+ ...out,
448
+ offset: .65
449
+ },
450
+ inside
451
+ ];
452
+ const options = {
453
+ id: SPLIT,
454
+ duration: timing.duration,
455
+ delay,
456
+ fill: kind === "open" ? "backwards" : "forwards"
457
+ };
458
+ const ends = (kind === "open" ? [inside, out] : [out, inside]).map(({ translate, scale }) => ({
459
+ translate,
460
+ scale
461
+ }));
462
+ const curve = droplet ? dropletEasing(style.getPropertyValue("--mp-ease-split"), kind === "open" ? "out" : "in") : easing;
463
+ try {
464
+ element.animate(ends, {
465
+ ...options,
466
+ easing: curve
467
+ });
468
+ } catch {
469
+ element.animate(ends, {
470
+ ...options,
471
+ easing: POP
472
+ });
473
+ }
474
+ if (!opaque) element.animate(frames.map(({ opacity, offset }) => offset === void 0 ? { opacity } : {
475
+ opacity,
476
+ offset
477
+ }), {
478
+ ...options,
479
+ easing: "linear"
480
+ });
481
+ else if (kind === "close") element.animate([
482
+ { opacity: shown },
483
+ {
484
+ opacity: shown,
485
+ offset: .85
486
+ },
487
+ { opacity: 0 }
488
+ ], {
489
+ ...options,
490
+ easing: "linear"
491
+ });
492
+ if (droplet) {
493
+ const content = kind === "open" ? [
494
+ { opacity: 0 },
495
+ {
496
+ opacity: 0,
497
+ offset: .7
498
+ },
499
+ { opacity: 1 }
500
+ ] : [
501
+ { opacity: 1 },
502
+ {
503
+ opacity: 0,
504
+ offset: .3
505
+ },
506
+ { opacity: 0 }
507
+ ];
508
+ for (const child of element.children) child.animate(content, {
509
+ ...options,
510
+ easing: "linear"
511
+ });
512
+ }
513
+ }
514
+ const stop = playNecks([electron, ...items.filter(({ shown = 1 }) => shown > 0).map(({ element }) => element)], longest);
515
+ const stopAll = () => {
516
+ stop();
517
+ for (const { element } of items) if (budding.get(element) === stopAll) budding.delete(element);
518
+ };
519
+ for (const { element } of items) budding.set(element, stopAll);
520
+ return stopAll;
521
+ }
522
+ /**
523
+ * The necks between pills, for `duration` ms: the goo of the moment under
524
+ * them, every pill's inside cut out of it, in the first one's colour. The
525
+ * pills draw themselves. Pills sliding past each other (or out of one
526
+ * another) seem to melt together as they meet and part.
527
+ */
528
+ function playNecks(pills, duration) {
529
+ if (pills.length < 2 || duration <= 0 || require_timing.prefersReducedMotion() || typeof document === "undefined") return () => {};
530
+ const style = getComputedStyle(pills[0]);
531
+ const goo = readNumber(style, "--mp-goo", DEFAULT_GOO);
532
+ if (goo <= 0) return () => {};
533
+ const longest = duration;
534
+ const id = `mp-goo-${++overlays}`;
535
+ const root = document.createElement("div");
536
+ root.setAttribute("data-mp", "goo");
537
+ root.setAttribute("aria-hidden", "true");
538
+ root.style.cssText = `position:fixed;left:0;top:0;width:0;height:0;pointer-events:none;z-index:${(Number.parseInt(style.zIndex, 10) || 50) - 1};`;
539
+ const svg = document.createElementNS(SVG, "svg");
540
+ svg.setAttribute("width", "0");
541
+ svg.setAttribute("height", "0");
542
+ svg.style.position = "absolute";
543
+ const filter = document.createElementNS(SVG, "filter");
544
+ filter.id = id;
545
+ for (const [name, value] of Object.entries({
546
+ x: "-50%",
547
+ y: "-50%",
548
+ width: "200%",
549
+ height: "200%",
550
+ "color-interpolation-filters": "sRGB"
551
+ })) filter.setAttribute(name, value);
552
+ filter.innerHTML = `
553
+ <feGaussianBlur in="SourceAlpha" stdDeviation="0" result="blur"/>
554
+ <feComponentTransfer in="blur" result="goo"><feFuncA type="linear" slope="24" intercept="-12"/></feComponentTransfer>
555
+ <feMorphology in="SourceAlpha" operator="erode" radius="1" result="inside"/>
556
+ <feComposite in="goo" in2="inside" operator="out" result="necks"/>
557
+ <feFlood result="color"/>
558
+ <feComposite in="color" in2="necks" operator="in"/>`;
559
+ filter.querySelector("feFlood").style.setProperty("flood-color", style.backgroundColor);
560
+ svg.append(filter);
561
+ const layer = document.createElement("div");
562
+ layer.style.cssText = `position:absolute;inset:0;filter:url(#${id});`;
563
+ const radii = pills.map((pill) => Number.parseFloat(getComputedStyle(pill).borderTopLeftRadius) || 0);
564
+ const shapes = pills.map(() => {
565
+ const shape = document.createElement("div");
566
+ shape.style.cssText = "position:absolute;left:0;top:0;background:#000;";
567
+ layer.append(shape);
568
+ return shape;
569
+ });
570
+ root.append(svg, layer);
571
+ document.body.append(root);
572
+ const blur = filter.querySelector("feGaussianBlur");
573
+ const margin = goo * 3 + 8;
574
+ let frame = 0;
575
+ let stopped = false;
576
+ const stop = () => {
577
+ if (stopped) return;
578
+ stopped = true;
579
+ cancelAnimationFrame(frame);
580
+ root.remove();
581
+ };
582
+ const startedAt = performance.now();
583
+ const draw = (now) => {
584
+ if (stopped) return;
585
+ const elapsed = now - startedAt;
586
+ const rects = pills.map((pill) => pill.getBoundingClientRect());
587
+ const left = Math.min(...rects.map((rect) => rect.left)) - margin;
588
+ const top = Math.min(...rects.map((rect) => rect.top)) - margin;
589
+ root.style.transform = `translate(${left}px, ${top}px)`;
590
+ root.style.width = `${Math.max(...rects.map((rect) => rect.right)) + margin - left}px`;
591
+ root.style.height = `${Math.max(...rects.map((rect) => rect.bottom)) + margin - top}px`;
592
+ rects.forEach((rect, index) => {
593
+ const scale = pills[index].offsetWidth > 0 ? rect.width / pills[index].offsetWidth : 1;
594
+ const shape = shapes[index].style;
595
+ shape.width = `${rect.width}px`;
596
+ shape.height = `${rect.height}px`;
597
+ shape.transform = `translate(${rect.left - left}px, ${rect.top - top}px)`;
598
+ shape.borderRadius = `${Math.min(radii[index] * scale, rect.width / 2, rect.height / 2)}px`;
599
+ });
600
+ blur.setAttribute("stdDeviation", (goo * gooAmount(Math.min(1, elapsed / longest))).toFixed(2));
601
+ if (elapsed >= longest) return stop();
602
+ frame = requestAnimationFrame(draw);
603
+ };
604
+ frame = requestAnimationFrame(draw);
605
+ return stop;
606
+ }
346
607
 
608
+ exports.DROPLET = DROPLET;
347
609
  exports.SPLIT = SPLIT;
610
+ exports.atEdgeOf = atEdgeOf;
611
+ exports.atPoint = atPoint;
348
612
  exports.dropShadow = dropShadow;
349
613
  exports.gooAmount = gooAmount;
614
+ exports.playNecks = playNecks;
615
+ exports.playOrbit = playOrbit;
350
616
  exports.playSplit = playSplit;
351
617
  exports.splitEasing = splitEasing;
352
618
  exports.springEasing = springEasing;