muvra 0.1.0-alpha.0 → 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.
Files changed (46) hide show
  1. package/MIGRATION.md +4 -0
  2. package/README.md +242 -2
  3. package/dist/animation/effect.d.ts +4 -3
  4. package/dist/animation/effect.d.ts.map +1 -1
  5. package/dist/animation/effect.js +11 -1
  6. package/dist/animation/effect.js.map +1 -1
  7. package/dist/animation/scope.d.ts +7 -2
  8. package/dist/animation/scope.d.ts.map +1 -1
  9. package/dist/animation/scope.js +78 -22
  10. package/dist/animation/scope.js.map +1 -1
  11. package/dist/animation/types.d.ts +8 -0
  12. package/dist/animation/types.d.ts.map +1 -1
  13. package/dist/engine.d.ts.map +1 -1
  14. package/dist/engine.js +67 -8
  15. package/dist/engine.js.map +1 -1
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/interactive-back.d.ts +10 -0
  20. package/dist/interactive-back.d.ts.map +1 -0
  21. package/dist/interactive-back.js +143 -0
  22. package/dist/interactive-back.js.map +1 -0
  23. package/dist/internal.d.ts +18 -2
  24. package/dist/internal.d.ts.map +1 -1
  25. package/dist/internal.js.map +1 -1
  26. package/dist/types.d.ts +26 -2
  27. package/dist/types.d.ts.map +1 -1
  28. package/dist/vue-back-preview.d.ts +16 -0
  29. package/dist/vue-back-preview.d.ts.map +1 -0
  30. package/dist/vue-back-preview.js +185 -0
  31. package/dist/vue-back-preview.js.map +1 -0
  32. package/dist/vue.d.ts +6 -5
  33. package/dist/vue.d.ts.map +1 -1
  34. package/dist/vue.js +100 -44
  35. package/dist/vue.js.map +1 -1
  36. package/package.json +1 -1
  37. package/src/animation/effect.ts +17 -5
  38. package/src/animation/scope.ts +92 -21
  39. package/src/animation/types.ts +11 -0
  40. package/src/engine.ts +86 -11
  41. package/src/index.ts +11 -1
  42. package/src/interactive-back.ts +166 -0
  43. package/src/internal.ts +16 -0
  44. package/src/types.ts +29 -2
  45. package/src/vue-back-preview.ts +183 -0
  46. package/src/vue.ts +117 -51
package/src/vue.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  shallowRef,
16
16
  Suspense,
17
17
  watch,
18
+ watchPostEffect,
18
19
  } from "vue";
19
20
  import type { ComputedRef, PropType, Ref, VNodeChild } from "vue";
20
21
  import { isFocusable, tabbable } from "tabbable";
@@ -27,20 +28,27 @@ import { LayerStackView } from "./layers/vue.js";
27
28
  import { createExtensionHost } from "./layers/extensions.js";
28
29
  import type { ExtensionHost } from "./layers/extensions.js";
29
30
  import type { LayerHandle } from "./layers/types.js";
30
- import { isTransitionEffect, selectTransition, validTransition } from "./animation/effect.js";
31
- import { runTransition } from "./animation/scope.js";
32
- import type { TransitionEffect } from "./animation/types.js";
31
+ import {
32
+ isProgressTransition,
33
+ isTransitionEffect,
34
+ selectTransition,
35
+ validTransition,
36
+ } from "./animation/effect.js";
37
+ import { createProgressPlayback, runTransition } from "./animation/scope.js";
38
+ import type { TransitionEnvironment } from "./animation/types.js";
33
39
  import type { RenderRequest } from "./internal.js";
34
40
  import { notify } from "./notifications.js";
41
+ import { createBackPreviewRenderer } from "./vue-back-preview.js";
35
42
  import type {
43
+ BackPreviewProvider,
36
44
  PageCatalog,
37
45
  PageContext,
38
46
  PageNames,
47
+ PageOptions,
39
48
  PageStack,
40
49
  ParamsOf,
41
50
  ResolvedRoute,
42
51
  StackSnapshot,
43
- TransitionDriver,
44
52
  } from "./types.js";
45
53
 
46
54
  /** 在组件或 effectScope 内订阅状态,作用域结束后自动退订。普通服务使用 stack.subscribe。 */
@@ -332,9 +340,10 @@ const viewProps = {
332
340
  },
333
341
  // true 允许声明的动画播放;框架不隐式添加视觉效果。
334
342
  transition: {
335
- type: [Function, Boolean, Object] as PropType<TransitionDriver | TransitionEffect | boolean>,
343
+ type: [Function, Boolean, Object] as PropType<NonNullable<PageOptions["transition"]> | true>,
336
344
  default: true,
337
345
  },
346
+ backPreview: Function as PropType<BackPreviewProvider>,
338
347
  keyboard: { type: Boolean, default: true },
339
348
  restoreFocus: { type: Boolean, default: true },
340
349
  } as const;
@@ -360,6 +369,7 @@ const BoundPageStackView = defineComponent({
360
369
  const layers = createLayerStack({ onError: reportLayer });
361
370
  const pageGroup = Symbol("navigation-pages");
362
371
  const bindings = new Map<number, { layer: LayerHandle; extensions: ExtensionHost }>();
372
+ let revealed: { id: number } | undefined;
363
373
  function reconcile(value: StackSnapshot) {
364
374
  layers.batch(() => {
365
375
  const alive = new Set(value.rendered.map((entry) => entry.route.id));
@@ -399,7 +409,10 @@ const BoundPageStackView = defineComponent({
399
409
  extensions,
400
410
  pageProps: props.pageProps,
401
411
  onReady: () => {
402
- void nextTick().then(maybeFocus);
412
+ void nextTick().then(() => {
413
+ maybeFocus();
414
+ controller.refreshBack();
415
+ });
403
416
  },
404
417
  },
405
418
  {
@@ -410,7 +423,7 @@ const BoundPageStackView = defineComponent({
410
423
  });
411
424
  }
412
425
  binding.layer.update({
413
- visible: entry.visible,
426
+ visible: entry.visible || revealed?.id === entry.route.id,
414
427
  interactive: entry.focused && value.phase !== "transition",
415
428
  attrs: {
416
429
  "data-page-id": entry.route.id,
@@ -502,7 +515,87 @@ const BoundPageStackView = defineComponent({
502
515
  fallback,
503
516
  );
504
517
  };
518
+ const reducedMotion = ref(false);
519
+ let stopMotion: (() => void) | undefined;
520
+ function transitionEnvironment(
521
+ route: ResolvedRoute | undefined,
522
+ navigationId: number | undefined,
523
+ ): TransitionEnvironment {
524
+ return {
525
+ document: container.value?.ownerDocument,
526
+ createLayer(component, componentProps, fail) {
527
+ const foreground = layers
528
+ .getSnapshot()
529
+ .layers.filter((entry) => entry.visible)
530
+ .at(-1)?.anchor;
531
+ const layer = layers.create({
532
+ ...(foreground ? { placement: { above: foreground } } : {}),
533
+ attrs: { "data-page-stack-effect-layer": "", "aria-hidden": "true", inert: true },
534
+ ...(component ? { content: () => h(component, componentProps) } : {}),
535
+ onError: fail,
536
+ });
537
+ // 延迟申请挂载 Promise,避免申请期间同步取消后出现未被消费的拒绝。
538
+ return {
539
+ get element() {
540
+ return layer.element!;
541
+ },
542
+ get ready() {
543
+ return layer.whenMounted().then(() => {});
544
+ },
545
+ dispose: layer.dispose,
546
+ };
547
+ },
548
+ onCleanupError: (cause) =>
549
+ controller.report({
550
+ error: new PageStackError("TRANSITION_ERROR", "Transition cleanup failed.", cause),
551
+ navigationId,
552
+ route,
553
+ severity: "warning",
554
+ }),
555
+ };
556
+ }
557
+ const backPreview = createBackPreviewRenderer({
558
+ layers,
559
+ layerFor: (id) => bindings.get(id)?.layer,
560
+ reveal(id) {
561
+ const lease = { id };
562
+ revealed = lease;
563
+ bindings.get(id)?.layer.update({ visible: true });
564
+ return () => {
565
+ if (revealed !== lease) return;
566
+ revealed = undefined;
567
+ const entry = state.value.rendered.find((page) => page.route.id === id);
568
+ if (entry) bindings.get(id)?.layer.update({ visible: entry.visible });
569
+ };
570
+ },
571
+ transition: transitionFor,
572
+ provider: () => props.backPreview,
573
+ environment: (route) => transitionEnvironment(route, undefined),
574
+ reducedMotion: () => reducedMotion.value,
575
+ report: (cause, route) =>
576
+ controller.report({
577
+ error: new PageStackError("TRANSITION_ERROR", "Back preview failed.", cause),
578
+ navigationId: undefined,
579
+ route,
580
+ severity: "warning",
581
+ }),
582
+ });
583
+ watchPostEffect(() => {
584
+ // 模板更新后 DOM 才就绪。只在能力变化时发布快照,进度留在预览渲染器。
585
+ if (!state.value.busy && !state.value.disposed) controller.refreshBack();
586
+ });
505
587
  onMounted(() => {
588
+ const motion = container.value?.ownerDocument?.defaultView?.matchMedia?.(
589
+ "(prefers-reduced-motion: reduce)",
590
+ );
591
+ if (motion) {
592
+ const update = () => {
593
+ reducedMotion.value = motion.matches;
594
+ };
595
+ update();
596
+ motion.addEventListener?.("change", update);
597
+ stopMotion = () => motion.removeEventListener?.("change", update);
598
+ }
506
599
  detach = controller.attachRenderer(
507
600
  async (request) => {
508
601
  // 异步清理可能晚于下一次导航,上报时必须保留本次播放的导航标识。
@@ -514,10 +607,7 @@ const BoundPageStackView = defineComponent({
514
607
  const from = elementFor(request.from?.id);
515
608
  const to = elementFor(request.to?.id);
516
609
  if (!from && !to) return false;
517
- const reduced =
518
- typeof matchMedia !== "undefined" &&
519
- matchMedia("(prefers-reduced-motion: reduce)").matches;
520
- if (reduced) return false;
610
+ if (reducedMotion.value) return false;
521
611
  const context = {
522
612
  operation: request.operation,
523
613
  from,
@@ -527,54 +617,23 @@ const BoundPageStackView = defineComponent({
527
617
  durationMs: request.durationMs,
528
618
  signal: request.signal,
529
619
  };
530
- if (isTransitionEffect(driver)) {
531
- await runTransition(driver, context, {
532
- document: container.value?.ownerDocument,
533
- createLayer(component, componentProps, fail) {
534
- const foreground = layers
535
- .getSnapshot()
536
- .layers.filter((entry) => entry.visible)
537
- .at(-1)?.anchor;
538
- const layer = layers.create({
539
- ...(foreground ? { placement: { above: foreground } } : {}),
540
- attrs: { "data-page-stack-effect-layer": "", "aria-hidden": "true", inert: true },
541
- ...(component ? { content: () => h(component, componentProps) } : {}),
542
- onError: fail,
543
- });
544
- // 延迟申请挂载 Promise,避免申请期间同步取消后出现未被消费的拒绝。
545
- return {
546
- get element() {
547
- return layer.element!;
548
- },
549
- get ready() {
550
- return layer.whenMounted().then(() => {});
551
- },
552
- dispose: layer.dispose,
553
- };
554
- },
555
- onCleanupError: (cause) =>
556
- controller.report({
557
- error: new PageStackError(
558
- "TRANSITION_ERROR",
559
- "Transition cleanup failed.",
560
- cause,
561
- ),
562
- navigationId,
563
- route: request.to,
564
- severity: "warning",
565
- }),
566
- });
567
- } else await driver(context);
620
+ const environment = transitionEnvironment(request.to, navigationId);
621
+ if (isProgressTransition(driver))
622
+ await createProgressPlayback(driver, context, environment).settle(1);
623
+ else if (isTransitionEffect(driver)) await runTransition(driver, context, environment);
624
+ else await driver(context);
568
625
  return true;
569
626
  },
570
627
  (request) => {
571
628
  const driver = transitionFor(request);
572
- return !driver ? 0 : isTransitionEffect(driver) ? driver.duration : request.durationMs;
629
+ return !driver ? 0 : typeof driver === "object" ? driver.duration : request.durationMs;
573
630
  },
631
+ backPreview,
574
632
  );
575
633
  maybeFocus();
576
634
  });
577
635
  onBeforeUnmount(() => {
636
+ stopMotion?.();
578
637
  detach?.();
579
638
  lastFocus.clear();
580
639
  });
@@ -653,6 +712,13 @@ export const PageStackView = defineComponent({
653
712
  },
654
713
  { flush: "sync" },
655
714
  );
656
- return () => h(BoundPageStackView, { ...attrs, ...props, key: binding.value }, slots);
715
+ return () => {
716
+ const { backPreview, ...rest } = props;
717
+ return h(
718
+ BoundPageStackView,
719
+ { ...attrs, ...rest, ...(backPreview ? { backPreview } : {}), key: binding.value },
720
+ slots,
721
+ );
722
+ };
657
723
  },
658
724
  });