Motion
Motion is organized in five kinds (ADR 0019): presence (Presence, Enter, PopLayer), morph (no component uses it now), swap (ContentSwap), move (SPRING, springTo) and feedback (TRANSITION, PRESS). Components take parameters from semantic recipes (motionRecipe, MOTION), never their own durations or curves; with Reduce Motion they skip or only fade.
Status: experimental · Components: ContentSwap, Enter, Presence, PopLayer, Swap, Chevron
Import
Section titled “Import”import { ContentSwap, Enter, Presence, PopLayer, Swap, Chevron } from 'nacre'Plays a spec once on mount (MOTION.popIn here); remount with a new key to replay it.
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> )}Examples
Section titled “Examples”Presence
Section titled “Presence”Like If, but when when turns empty the child plays its exit before unmounting: it gets leave, plays when leaving() turns true, and calls 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> )}PopLayer
Section titled “PopLayer”A small layer inside Presence (back-to-bottom pill, new-message hint): fades in over 150ms and out over 100ms.
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> )}ContentSwap
Section titled “ContentSwap”Changes what sits in one place (levels, segment content): the old fades out in 80ms, the new fades in over 150ms, without scaling or moving; no animation on first mount or for data updates of the same key. Use it instead of Swap in new code.
import { Text, View, signal } from 'nsh'import { ContentSwap, Segmented, Swap } 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' ? 'Waiting' : l === 'mine' ? 'My chats' : 'Ended') return ( <View class="gap-3"> <Segmented testID="demo-swap-level" value={level()} onChange={(v) => level.set(v)} accessibilityLabel="Group" 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`}>Swap: the old fades out in 80ms, the new fades in over 150ms, nothing moves</Text> </View> )} </ContentSwap> </View> )}Swap with blur
Section titled “Swap with blur”effect=“blur” asks for a 4pt content blur (only for small content on solid surfaces; where the platform cannot draw it, on glass and with Reduce Motion it falls back to a plain fade — NSH on macOS does not draw content blur yet, FINDINGS 74, so this is a fade here). Rapid presses play only to the latest page, without queuing.
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 = () => ['Profile', 'History', 'Tickets'] return ( <View class="flex-row flex-wrap items-center gap-3"> <Button testID="demo-swap-blur-next" onPress={() => page.update((p) => (p + 1) % 3)}> Next </Button> {/* 快速连点:A→B→C 只演到最新的一页,不排队 */} <Button testID="demo-swap-blur-burst" variant="outline" onPress={() => page.update((p) => (p + 2) % 3)}> Skip two </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> )}Legacy Swap
Section titled “Legacy Swap”Kept for compatibility (no callers left in the library or the app): fades the content in from 6pt below when value changes, without keeping the old content. Use ContentSwap in new code.
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> )}Chevron
Section titled “Chevron”A disclosure’s arrow: right when collapsed, springing 90° clockwise to point down when expanded.
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> )}Accessibility
Section titled “Accessibility”- With Reduce Motion: Presence unmounts at once, animateLayout does nothing, springs land directly, ContentSwap swaps at once.
- While ContentSwap’s old content leaves it takes no pointer, and reads as busy.
- Motion does not change the accessibility tree: the trigger around a Chevron sets accessibilityState.expanded itself.
Do / Don’t
Section titled “Do / Don’t”Do
- Pick the kind first (presence, morph, swap, move, feedback), then the recipe
- Color transitions use TRANSITION.color; press scaling (TRANSITION.press with PRESS) is only for standalone command buttons
- Exits are faster than entrances
- Keyboard highlight and focus are instant
Don’t
- Write duration-* / transitionDuration / new springs in a component (CI’s ui-motion check stops it)
- Blur content or stagger items on glass popups
- Move or scale content whose place does not change
- Stretch high-frequency actions (opening a dropdown) with springs
- Scale fields, rows, cards, tags or popup triggers on press (change their fill only)
Property descriptions follow the Chinese source comments.
ContentSwap
Section titled “ContentSwap”| Prop | Type | Default | Description |
|---|---|---|---|
value * |
T |
— | |
keyOf |
(value: T) => unknown |
— | 内容身份;不给时用 value 本身 |
effect |
'fade' | 'blur' |
— | |
surface |
MotionSurface |
— | |
class |
string |
— | |
testID |
string |
— | |
children * |
(shown: Accessor<T>) => JSX.Element |
— |
* required
| Prop | Type | Default | Description |
|---|---|---|---|
spec * |
MotionSpec |
— | |
class |
string |
— | |
testID |
string |
— | |
children |
JSX.Element |
— |
* required
Presence
Section titled “Presence”| Prop | Type | Default | Description |
|---|---|---|---|
when * |
T | null | undefined | false |
— | |
children * |
(value: Accessor<NonNullable<T>>, leave: Leave) => JSX.Element |
— |
* required
PopLayer
Section titled “PopLayer”| Prop | Type | Default | Description |
|---|---|---|---|
leave * |
Leave |
— | |
class |
string |
— | |
testID |
string |
— | |
children |
JSX.Element |
— |
* required
| Prop | Type | Default | Description |
|---|---|---|---|
value * |
unknown |
— | |
motion |
SwapMotion | (() => SwapMotion) |
'up' |
|
class |
string |
— | |
testID |
string |
— | |
children |
JSX.Element |
— |
* required
Chevron
Section titled “Chevron”| Prop | Type | Default | Description |
|---|---|---|---|
open * |
boolean |
— | |
size |
number |
12 |
|
color |
ColorToken |
'muted-foreground' |
* required