跳转到内容
Nacre
组件配色
EN

动效

experimentalContentSwap · Enter · Presence · PopLayer · Swap · Chevron在画廊运行
ContentSwap
在浏览器里运行真实组件。

动效按五类语法组织(ADR 0019):出现 / 消失(Presence、Enter、PopLayer)、变形(暂无组件使用)、换内容(ContentSwap)、移动(SPRING、springTo)、反馈(TRANSITION、PRESS)。组件从语义配方取参数(motionRecipe、MOTION),不自己写时长与曲线;「减少动态效果」时都不播或只淡化。

状态:experimental · 组件:ContentSwap、Enter、Presence、PopLayer、Swap、Chevron

import { ContentSwap, Enter, Presence, PopLayer, Swap, Chevron } from 'nacre'

挂载时播一次 spec(这里是 MOTION.popIn);要重播就换 key 让它重新挂载。

import { Text, View, signal } from 'nsh'
import { Button, Enter, MOTION } from 'nacre'
import { TEXT_SIZE } from '../../src/textSize'
export function MotionEnter() {
const round = signal(1)
return (
<View class="flex-row flex-wrap items-center gap-3">
<Button testID="demo-enter-replay" class="w-[96px]" onPress={() => round.update((n) => n + 1)}>
重播
</Button>
{/* 换 key 重新挂载,Enter 就再播一次 */}
<Each of={[round()]} key={(n) => n}>
{(n) => (
<Enter spec={MOTION.popIn} testID="demo-enter" class="px-4 py-2 rounded-lg bg-plane">
<Text class={`${TEXT_SIZE.small} text-foreground`}>{`进场 ${n()}`}</Text>
</Enter>
)}
</Each>
</View>
)
}

像 If,但 when 变空时先让子组件播完退场再卸载:子组件拿到 leave,leaving() 变真时播退场,播完调 done()。

import { Text, View, createMotion, effect, signal } from 'nsh'
import { Button, MOTION, Presence } from 'nacre'
import type { Leave } from 'nacre'
import { TEXT_SIZE } from '../../src/textSize'
function NoticeCard(props: { leave: Leave }) {
const motion = createMotion({ enter: MOTION.popIn })
effect(() => {
if (!props.leave.leaving()) return
motion.play(MOTION.popOut).then(props.leave.done, props.leave.done)
})
return (
<View class="px-4 py-2 rounded-lg bg-plane" style={motion.style}>
<Text class={`${TEXT_SIZE.small} text-foreground`}>弹入 / 弹出</Text>
</View>
)
}
export function MotionPresence() {
const shown = signal(true)
return (
<View class="flex-row flex-wrap items-center gap-3">
<Button class="w-[96px]" onPress={() => shown.update((v) => !v)}>
{shown() ? '收起' : '出现'}
</Button>
<View class="w-[120px] h-10 justify-center">
<Presence when={shown()}>{(_, leave) => <NoticeCard leave={leave} />}</Presence>
</View>
</View>
)
}

放在 Presence 里的小浮层(回到底部胶囊、新消息提示):进 150ms、退 100ms 只淡入淡出,自己处理 leave。

import { Text, View, signal } from 'nsh'
import { Button, PopLayer, Presence } from 'nacre'
import { TEXT_SIZE } from '../../src/textSize'
export function MotionPopLayer() {
const open = signal(false)
return (
<View class="flex-row flex-wrap items-center gap-3">
{/* 按钮两态等宽、浮层放进固定占位:开合时同一行不被挤动 */}
<Button testID="demo-poplayer-toggle" class="w-[96px]" pressed={open()} onPress={() => open.update((v) => !v)}>
{open() ? '关掉浮层' : '打开浮层'}
</Button>
<View class="w-[120px] h-10 justify-center">
<Presence when={open()}>
{(_, leave) => (
<PopLayer leave={leave} testID="demo-poplayer" class="self-start px-4 py-2 rounded-lg bg-popover border border-border">
<Text class={`${TEXT_SIZE.small} text-popover-foreground`}>3 条新消息</Text>
</PopLayer>
)}
</Presence>
</View>
</View>
)
}

同一位置换内容身份(层级切换、分段内容):旧内容 80ms 淡出、新内容 150ms 淡入,不缩放不位移;首次挂载与同 key 数据更新不播。新代码用它,不用 Swap。

import { Text, View, signal } from 'nsh'
import { ContentSwap, Segmented } from 'nacre'
import { TEXT_SIZE } from '../../src/textSize'
const LEVELS = ['inbox', 'mine', 'done'] as const
type Level = (typeof LEVELS)[number]
export function MotionContentSwap() {
const level = signal<Level>('inbox')
const label = (l: Level) => (l === 'inbox' ? '待接入' : l === 'mine' ? '我的会话' : '已结束')
return (
<View class="gap-3">
<Segmented testID="demo-swap-level" value={level()} onChange={(v) => level.set(v)} accessibilityLabel="分组" options={LEVELS.map((l) => ({ value: l, label: label(l) }))} />
<ContentSwap value={level()} testID="demo-swap" class="w-[260px] max-w-full h-[88px] p-3 rounded-lg border border-border">
{(shown) => (
<View class="gap-1.5">
<Text testID="demo-swap-title" class={`${TEXT_SIZE.body} font-medium text-foreground`}>{label(shown())}</Text>
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>换内容:旧的 80ms 淡出,新的 150ms 淡入,不位移</Text>
</View>
)}
</ContentSwap>
</View>
)
}

effect=“blur” 申请 4pt 内容模糊(实底上的小内容层才给;平台不支持、玻璃面上、减少动态效果时自动退成纯淡化。macOS 的 NSH 目前不画内容模糊,FINDINGS 74,这里实际是淡化)。快速连点只演到最新一页,不排队。

import { Text, View, effect, signal } from 'nsh'
import { Button, ContentSwap } from 'nacre'
import { TEXT_SIZE } from '../../src/textSize'
export function MotionContentSwapBlur() {
const page = signal(0)
const pages = () => ['客户资料', '历史会话', '工单']
return (
<View class="flex-row flex-wrap items-center gap-3">
<Button testID="demo-swap-blur-next" onPress={() => page.update((p) => (p + 1) % 3)}>
下一页
</Button>
{/* 快速连点:A→B→C 只演到最新的一页,不排队 */}
<Button testID="demo-swap-blur-burst" variant="outline" onPress={() => page.update((p) => (p + 2) % 3)}>
连跳两页
</Button>
<ContentSwap value={page()} effect="blur" testID="demo-swap-blur" class="w-[200px] max-w-full h-12 px-3 justify-center rounded-lg bg-muted">
{(shown) => <Text testID="demo-swap-blur-title" class={`${TEXT_SIZE.body} text-foreground`}>{pages()[shown()]}</Text>}
</ContentSwap>
</View>
)
}

兼容保留(库与应用里已无调用点):value 变了让内容从下 6pt 淡入一次,不冻结旧内容。新代码用 ContentSwap。

import { Text, View, signal } from 'nsh'
import { Segmented, Swap } from 'nacre'
import { TEXT_SIZE } from '../../src/textSize'
export function MotionSwap() {
const tab = signal<'a' | 'b'>('a')
return (
<View class="flex-row flex-wrap items-center gap-3">
<Segmented
value={tab()}
onChange={(v) => tab.set(v)}
accessibilityLabel="内容"
options={[
{ value: 'a', label: '第一页' },
{ value: 'b', label: '第二页' },
]}
/>
<Swap value={tab()} class="w-[200px] max-w-full">
<Text class={`${TEXT_SIZE.small} text-foreground`}>{tab() === 'a' ? '第一页的内容' : '第二页的内容'}</Text>
</Swap>
</View>
)
}

折叠区的箭头:收起朝右,展开用弹簧顺时针转 90° 朝下。

import { Text, View, signal } from 'nsh'
import { Chevron, FOCUS_RING } from 'nacre'
import { TEXT_SIZE, TOUCH } from '../../src/textSize'
export function MotionChevron() {
const open = signal(false)
return (
<View
testID="demo-chevron-toggle"
role="button"
focusable
accessibilityLabel="质检结果"
accessibilityState={{ expanded: open() }}
onPress={() => open.update((v) => !v)}
class={`${TOUCH ? 'min-h-11' : ''} flex-row items-center gap-1.5 rounded-sm ${FOCUS_RING}`}
>
<Chevron open={open()} />
<Text class={`${TEXT_SIZE.small} text-foreground`}>{open() ? '已展开' : '已收起'}</Text>
</View>
)
}
  • 系统「减少动态效果」打开时:Presence 直接卸载,animateLayout 不做,弹簧直接落位,ContentSwap 直接换。
  • ContentSwap 离场期间旧内容不接指针,读屏上报 busy。
  • 动效不改变读屏树:Chevron 所在的触发器自己写 accessibilityState.expanded。

该用

  • 先选语法(出现、变形、换内容、移动、反馈),再取配方
  • 颜色过渡用 TRANSITION.color;按下缩放(TRANSITION.press + PRESS)只给独立的命令按钮
  • 离场比进场快
  • 键盘高亮、焦点瞬时,不加过渡

不该用

  • 在组件里写 duration-* / transitionDuration / 新弹簧(CI 的 ui-motion 拦)
  • 玻璃浮层上做内容模糊或逐项错峰
  • 位置不变的换内容加位移或缩放
  • 高频操作(打开下拉)用弹簧拖长时间
  • 字段、行、卡片、标签、弹浮层的触发器按下缩放(只变底色)
属性 类型 默认 说明
value * T —
keyOf (value: T) => unknown — 内容身份;不给时用 value 本身
effect 'fade' | 'blur' —
surface MotionSurface —
class string —
testID string —
children * (shown: Accessor<T>) => JSX.Element —

* 必填

属性 类型 默认 说明
spec * MotionSpec —
class string —
testID string —
children JSX.Element —

* 必填

属性 类型 默认 说明
when * T | null | undefined | false —
children * (value: Accessor<NonNullable<T>>, leave: Leave) => JSX.Element —

* 必填

属性 类型 默认 说明
leave * Leave —
class string —
testID string —
children JSX.Element —

* 必填

属性 类型 默认 说明
value * unknown —
motion SwapMotion | (() => SwapMotion) 'up'
class string —
testID string —
children JSX.Element —

* 必填

属性 类型 默认 说明
open * boolean —
size number 12
color ColorToken 'muted-foreground'

* 必填