@aphrody/m3-motion

中文文档
1.4.7发布于 2026/10/23Apache-2.0TypeScriptES Module近 30 天下载 2172

Material 3 animation plugin powered by Motion (formerly Framer Motion)

原始使用文档
文档目录11 节
  1. 1. Container Transform ( M3ContainerTransform )
  2. Key Features
  3. Sub-Components
  4. Code Examples
  5. 2. Shared Axis ( M3SharedAxis )
  6. Code Example
  7. 3. Fade Through ( M3FadeThrough )
  8. Code Example
  9. 4. Duration and Easing Presets
  10. Motion Presets Summary
  11. License

@aphrody/m3-motion

React components and hooks for the Material 3 motion patterns (container transform, shared axis, fade through, collapse, fade) on Motion, with the M3 spring, easing and duration tokens.

bun add @aphrody/m3-motion motion react
import { M3Fade, M3FadeThrough } from "@aphrody/m3-motion";

<M3Fade in={open}>Details</M3Fade>;
<M3FadeThrough stateKey={tab}>{panel}</M3FadeThrough>;

License: Apache-2.0 (LICENSE). Install guide: docs/material/m3/INSTALL.md.


1. Container Transform (M3ContainerTransform)

The container transform pattern transitions one UI element to another (e.g., morphing a small Card component into a full-page Detail view).

Key Features

  • Aspect Ratio Preservation: Automatically prevents layout distortion (skewing/stretching) of children during container morphs using layout-position correction.
  • Spec-Compliant Cross-Fade: Fades out the starting content early (0% to 30% of transition) and delays the fade-in of target content (20% to 100% of transition) to avoid messy intermediate states.
  • Pointer Event Management: Prevents inactive, hidden content from intercepting pointer interactions.
  • Flexibility: Supports both the Single-Container Toggle approach (re-rendering within the same container) and the Two-Container Shared Layout approach (different source/target elements linked via layoutId).

Sub-Components

  • M3ContainerTransform: Parent morphing container.
  • M3ContainerTransformStart: Wrapper for starting content (e.g., collapsed card details).
  • M3ContainerTransformEnd: Wrapper for ending content (e.g., expanded page details).

Code Examples

Single-Container Toggle Approach

Best when morphing a single component instance in place using state.

import React, { useState } from "react";
import {
  M3ContainerTransform,
  M3ContainerTransformStart,
  M3ContainerTransformEnd,
} from "@aphrody/m3-motion";

export const MorphingCard = () => {
  const [isExpanded, setIsExpanded] = useState(false);

  return (
    <M3ContainerTransform
      isExpanded={isExpanded}
      speed="default"
      style={{
        width: isExpanded ? 500 : 250,
        height: isExpanded ? 400 : 150,
        borderRadius: isExpanded ? 28 : 16,
        backgroundColor: "var(--md-sys-color-surface-container)",
        cursor: "pointer",
      }}
      onClick={() => setIsExpanded(!isExpanded)}
    >
      {/* Start state (collapsed content) */}
      <M3ContainerTransformStart>
        <div style={{ padding: 16 }}>
          <h3>Collapsed Card</h3>
          <p>Click me to expand...</p>
        </div>
      </M3ContainerTransformStart>

      {/* End state (expanded detail content) */}
      <M3ContainerTransformEnd>
        <div style={{ padding: 24 }}>
          <h2>Detailed View</h2>
          <p>This is the expanded content showing the full detail surface.</p>
          <button
            onClick={e => {
              e.stopPropagation();
              setIsExpanded(false);
            }}
          >
            Close
          </button>
        </div>
      </M3ContainerTransformEnd>
    </M3ContainerTransform>
  );
};

Two-Container Shared Layout Approach (linked via layoutId)

Best for page/route transitions or separate DOM elements. Wrap the conditional render in <AnimatePresence> to enable the exit fade-out transition.

import React, { useState } from "react";
import { AnimatePresence } from "motion/react";
import {
  M3ContainerTransform,
  M3ContainerTransformStart,
  M3ContainerTransformEnd,
} from "@aphrody/m3-motion";

export const ListView = () => {
  const [selectedId, setSelectedId] = useState<string | null>(null);

  return (
    <div>
      {/* List of items */}
      {!selectedId && (
        <div className="grid">
          <M3ContainerTransform
            layoutId="card-item-1"
            onClick={() => setSelectedId("1")}
            style={{
              borderRadius: 16,
              background: "#eee",
              width: 200,
              height: 120,
            }}
          >
            <M3ContainerTransformStart>
              <div style={{ padding: 12 }}>Card Content 1</div>
            </M3ContainerTransformStart>
          </M3ContainerTransform>
        </div>
      )}

      {/* Expanded detail page */}
      <AnimatePresence>
        {selectedId === "1" && (
          <div className="backdrop">
            <M3ContainerTransform
              layoutId="card-item-1"
              style={{
                borderRadius: 24,
                background: "#fff",
                width: 600,
                height: 450,
              }}
            >
              <M3ContainerTransformEnd>
                <div style={{ padding: 24 }}>
                  <h2>Expanded Title 1</h2>
                  <p>Long detail text...</p>
                  <button onClick={() => setSelectedId(null)}>
                    Back to List
                  </button>
                </div>
              </M3ContainerTransformEnd>
            </M3ContainerTransform>
          </div>
        )}
      </AnimatePresence>
    </div>
  );
};

2. Shared Axis (M3SharedAxis)

Used for transitions between UI elements that have a spatial or navigational relationship (e.g., page stepper, tab switching, or dialog overlays). Supports x, y, and z axes.

  • Default enter duration: 300ms (medium2) with emphasizedDecelerate ease.
  • Default exit duration: 200ms (short4) with emphasizedAccelerate ease.

Code Example

import React, { useState } from "react";
import { M3SharedAxis } from "@aphrody/m3-motion";

export const StepWizard = () => {
  const [step, setStep] = useState(0);

  return (
    <div>
      <M3SharedAxis
        stateKey={step}
        axis="x"
        forward={step > 0}
        style={{ padding: 20 }}
      >
        {step === 0 && <div>Step 1: Enter details</div>}
        {step === 1 && <div>Step 2: Review options</div>}
        {step === 2 && <div>Step 3: Confirm purchase</div>}
      </M3SharedAxis>

      <button onClick={() => setStep(prev => Math.max(0, prev - 1))}>
        Back
      </button>
      <button onClick={() => setStep(prev => Math.min(2, prev + 1))}>
        Next
      </button>
    </div>
  );
};

3. Fade Through (M3FadeThrough)

Used for transitions between primary views that do not have a strong spatial relationship (e.g., bottom navigation switching, settings screens).

  • Default exit duration (fade-out): 90ms with emphasizedAccelerate ease.
  • Default enter duration (fade-in & scale-up from 92%): 210ms with emphasizedDecelerate ease.

Code Example

import React, { useState } from "react";
import { M3FadeThrough } from "@aphrody/m3-motion";

export const AppNavigation = () => {
  const [currentTab, setCurrentTab] = useState("home");

  return (
    <div>
      <M3FadeThrough stateKey={currentTab} style={{ height: 400 }}>
        {currentTab === "home" && <div>Home View Content</div>}
        {currentTab === "search" && <div>Search Page Content</div>}
        {currentTab === "profile" && <div>User Profile Settings</div>}
      </M3FadeThrough>

      <nav>
        <button onClick={() => setCurrentTab("home")}>Home</button>
        <button onClick={() => setCurrentTab("search")}>Search</button>
        <button onClick={() => setCurrentTab("profile")}>Profile</button>
      </nav>
    </div>
  );
};

4. Duration and Easing Presets

Developers have direct access to official Material Design 3 timings and curves:

import { m3Easings, m3Durations, m3Springs } from "@aphrody/m3-motion";

// Custom animation example using M3 tokens
const myCustomTransition = {
  ease: m3Easings.emphasizedDecelerate,
  duration: m3Durations.medium4, // 400ms
};

Motion Presets Summary

  • m3Easings: Bezier curves for emphasized (approx), emphasizedDecelerate (enter), emphasizedAccelerate (exit), standard, standardDecelerate, and standardAccelerate.
  • m3Durations: Timing scales from short1 (50ms) to extraLong4 (1000ms).
  • m3Springs: Hand-tuned spring constants (fast, default, slow) split into spatial (bouncy) and effects (critically damped).

License

Apache-2.0

版本与发布记录

@aphrody/m3-motion 共发布 17 个版本,首次发布于 2026/06/04;过去 12 个月发布了 17 个版本。

版本发布日期距今
1.4.7latest2026/10/2315 天前
1.4.42026/10/1820 天前
3.3.112026/10/1028 天前
3.3.92026/10/1029 天前
3.3.82026/10/0929 天前
3.3.72026/10/071 个月前

关于 @aphrody/m3-motion 的常见问题

@aphrody/m3-motion 是什么?

Material 3 animation plugin powered by Motion (formerly Framer Motion)

如何安装 @aphrody/m3-motion?

在项目目录运行 npm install @aphrody/m3-motion,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18.0.0)、motion(>=12.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aphrody/m3-motion 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aphrody/m3-motion 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aphrody/m3-motion 可以商用吗?

@aphrody/m3-motion 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aphrody/m3-motion 的下载量有多少?

根据 npm 官方下载统计,@aphrody/m3-motion 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,172 次,日均约 72 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aphrody/m3-motion 还在维护吗?

最新版本 1.4.7 发布于 2026/10/23(15 天前);至今共发布 17 个版本,其中过去 12 个月发布了 17 个;首次发布于 2026/06/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。